# Path: pubs/datacache.py
# class CacheEntrySet(object):
# 
#     def __init__(self, databroker, name):
#         self.databroker = databroker
#         self.name = name
#         if name == 'metacache':
#             self._pull_fun = databroker.pull_metadata
#             self._push_fun = databroker.push_metadata
#             self._mtime_fun = databroker.filebroker.mtime_metafile
#         elif name == 'bibcache':
#             self._pull_fun = databroker.pull_bibentry
#             self._push_fun = databroker.push_bibentry
#             self._mtime_fun = databroker.filebroker.mtime_bibfile
#         else:
#             raise ValueError
#         self._entries = None
#         self.modified = False
#         # does the filesystem supports subsecond stat time?
#         self.nsec_support = os.stat('.').st_mtime != int(os.stat('.').st_mtime)
# 
#     @property
#     def entries(self):
#         if self._entries is None:
#             self._entries = self._try_pull_cache()
#         return self._entries
# 
#     def flush(self, force=False):
#         if force or self.modified:
#             self.databroker.push_cache(self.name, self.entries)
#             self.modified = False
# 
#     def pull(self, citekey):
#         if self._is_outdated(citekey):
#             # if we get here, we must update the cache.
#             t = time.time()
#             data = self._pull_fun(citekey)
#             self.entries[citekey] = CacheEntry(data, t)
#             self.modified = True
#         return self.entries[citekey].data
# 
#     def push(self, citekey, data):
#         self._push_fun(citekey, data)
#         self.push_to_cache(citekey, data)
# 
#     def push_to_cache(self, citekey, data):
#         """Push to cash only."""
#         mtime = self._mtime_fun(citekey)
#         self.entries[citekey] = CacheEntry(data, mtime)
#         self.modified = True
# 
#     def remove_from_cache(self, citekey):
#         """Removes from cache only."""
#         if citekey in self.entries:
#             self.entries.pop(citekey)
#             self.modified = True
# 
#     def _try_pull_cache(self):
#         try:
#             return self.databroker.pull_cache(self.name)
#         except Exception:  # take no prisonners; if something is wrong, no cache.
#             return {}
# 
#     def _is_outdated(self, citekey):
#         if citekey in self.entries:
#             mtime = self._mtime_fun(citekey)
#             boundary = mtime if self.nsec_support else mtime + 1
#             return self.entries[citekey].timestamp < boundary
#         else:
#             return True


import unittest
import time
import dotdot
from pubs.datacache import CacheEntrySet
        self.meta = meta

    def push_cache(name, entries):
        if name != 'metacache':
            raise AttributeError


class FakeDataBrokerBib(object):

    bib = None

    def pull_bibentry(self, key):
        return self.bib

    def push_bibentry(self, key, bib):
        self.bib = bib

    def push_cache(name, entries):
        if name != 'bibcache':
            raise AttributeError


class TestCacheEntrySet(unittest.TestCase):

    def setUp(self):
        self.databroker_meta = FakeDataBrokerMeta()
        self.databroker_meta.filebroker = FakeFileBrokerMeta()
        self.databroker_bib = FakeDataBrokerBib()
        self.databroker_bib.filebroker = FakeFileBrokerBib()
        self.metacache = CacheEntrySet(self.databroker_meta, 'metacache')

# Path: pubs/datacache.py
# class CacheEntrySet(object):
# 
#     def __init__(self, databroker, name):
#         self.databroker = databroker
#         self.name = name
#         if name == 'metacache':
#             self._pull_fun = databroker.pull_metadata
#             self._push_fun = databroker.push_metadata
#             self._mtime_fun = databroker.filebroker.mtime_metafile
#         elif name == 'bibcache':
#             self._pull_fun = databroker.pull_bibentry
#             self._push_fun = databroker.push_bibentry
#             self._mtime_fun = databroker.filebroker.mtime_bibfile
#         else:
#             raise ValueError
#         self._entries = None
#         self.modified = False
#         # does the filesystem supports subsecond stat time?
#         self.nsec_support = os.stat('.').st_mtime != int(os.stat('.').st_mtime)
# 
#     @property
#     def entries(self):
#         if self._entries is None:
#             self._entries = self._try_pull_cache()
#         return self._entries
# 
#     def flush(self, force=False):
#         if force or self.modified:
#             self.databroker.push_cache(self.name, self.entries)
#             self.modified = False
# 
#     def pull(self, citekey):
#         if self._is_outdated(citekey):
#             # if we get here, we must update the cache.
#             t = time.time()
#             data = self._pull_fun(citekey)
#             self.entries[citekey] = CacheEntry(data, t)
#             self.modified = True
#         return self.entries[citekey].data
# 
#     def push(self, citekey, data):
#         self._push_fun(citekey, data)
#         self.push_to_cache(citekey, data)
# 
#     def push_to_cache(self, citekey, data):
#         """Push to cash only."""
#         mtime = self._mtime_fun(citekey)
#         self.entries[citekey] = CacheEntry(data, mtime)
#         self.modified = True
# 
#     def remove_from_cache(self, citekey):
#         """Removes from cache only."""
#         if citekey in self.entries:
#             self.entries.pop(citekey)
#             self.modified = True
# 
#     def _try_pull_cache(self):
#         try:
#             return self.databroker.pull_cache(self.name)
#         except Exception:  # take no prisonners; if something is wrong, no cache.
#             return {}
# 
#     def _is_outdated(self, citekey):
#         if citekey in self.entries:
#             mtime = self._mtime_fun(citekey)
#             boundary = mtime if self.nsec_support else mtime + 1
#             return self.entries[citekey].timestamp < boundary
#         else:
#             return True


# Path: tests/test_datacache.py
import unittest
import time
import dotdot
from pubs.datacache import CacheEntrySet

        self.meta = meta

    def push_cache(name, entries):
        if name != 'metacache':
            raise AttributeError


class FakeDataBrokerBib(object):

    bib = None

    def pull_bibentry(self, key):
        return self.bib

    def push_bibentry(self, key, bib):
        self.bib = bib

    def push_cache(name, entries):
        if name != 'bibcache':
            raise AttributeError


class TestCacheEntrySet(unittest.TestCase):

    def setUp(self):
        self.databroker_meta = FakeDataBrokerMeta()
        self.databroker_meta.filebroker = FakeFileBrokerMeta()
        self.databroker_bib = FakeDataBrokerBib()
        self.databroker_bib.filebroker = FakeFileBrokerBib()
        self.metacache = CacheEntrySet(self.databroker_meta, 'metacache')

        self.bibcache = CacheEntrySet(self.databroker_bib, 'bibcache')