def test_get_cached_file_by_src(self, cache_env): # we can get a cached file by source file and options cm = CacheManager(str(cache_env / "cache")) # without a cache key my_id = cm.register_doc(str(cache_env / "src1.txt"), str(cache_env / "result1.txt")) path, key = cm.get_cached_file_by_source(str(cache_env / "src1.txt")) assert open(path, "r").read() == (cache_env / "result1.txt").read() assert key == '737b337e605199de28b3b64c674f9422_1_1' assert my_id == key
def test_get_cached_file_by_src_w_key(self, cache_env): cm = CacheManager(str(cache_env / "cache")) src = cache_env / "src1.txt" result1 = cache_env / "result1.txt" result2 = cache_env / "result2.txt" my_id1 = cm.register_doc(str(src), str(result1), 'mykey') path1, key1 = cm.get_cached_file_by_source(str(src), 'mykey') assert filecmp.cmp(path1, str(result1), shallow=False) assert key1 == '737b337e605199de28b3b64c674f9422_1_1' assert key1 == my_id1 # yet not existent cache file path2, key2 = cm.get_cached_file_by_source(str(src), 'otherkey') assert path2 is None assert key2 is None # store and retrieve 2nd cache file my_id3 = cm.register_doc(str(src), str(result2), 'otherkey') path3, key3 = cm.get_cached_file_by_source(str(src), 'otherkey') assert filecmp.cmp(path3, str(result2), shallow=False) assert key3 == '737b337e605199de28b3b64c674f9422_1_2' assert key3 == my_id3 return
def test_get_cached_file_by_src_failed(self, cache_env): # uncached files result in `None` as result cm = CacheManager(str(cache_env)) result, key = cm.get_cached_file_by_source(str(cache_env / "src1.txt")) assert result is None assert key is None
class Client(object): """A client to trigger document conversions. """ def __init__(self, cache_dir=None): self.cache_dir = cache_dir self.cache_manager = None if self.cache_dir is not None: self.cache_manager = CacheManager(self.cache_dir) def convert(self, src_doc_path, options={}): """Convert `src_doc_path` according to `options`. Calls :func:`convert_doc` internally and returns the result given by this function. """ return convert_doc(src_doc_path, options, self.cache_dir) def get_cached(self, cache_key): """Get the document from cache stored under `cache_key`. Returns ``None`` if no such file can be found or no cache dir was set at all. .. warning:: The returned path (if any) is part of cache! Do not remove or change the file. Copy it to another location instead. .. versionadded:: 1.1 """ if self.cache_manager is not None: return self.cache_manager.get_cached_file(cache_key) return None def get_cached_by_source(self, src_doc_path, options={}): """Get the document from cache by source doc and options. Find a cached document, which was created from the given `src_doc_path` and `options`. Returns the path to the document and a cache key you are encouraged to use for future access. Please note that this method is much more expensive than :meth:`get_cached`. Use it only if the `cache_key` returned upon registering a doc is absolutely not available any more. Returns ``(None, None)`` if no such file can be found or no cache dir was set at all. .. warning:: The returned path (if any) is part of cache! Do not remove or change the file. Copy it to another location instead. .. versionadded:: 1.1 """ repr_key = get_marker(options) if self.cache_manager is not None: return self.cache_manager.get_cached_file_by_source( src_doc_path, repr_key) return None, None