class ProjectCache(MultiCache[Cache]): """The project cache. The `generated` attribute is a MultiCache instance that's meant to be tracked by version control, unlike the main project cache that usually lives in the ignored `.beet_cache` directory. """ generated: MultiCache[Cache] def __init__( self, directory: FileSystemPath, generated_directory: FileSystemPath, default_cache: str = "default", gitignore: bool = True, cache_type: Type[Cache] = Cache, ): super().__init__(directory, default_cache, gitignore, cache_type=Cache) self.generated = MultiCache( generated_directory, default_cache, gitignore=False, cache_type=cache_type, ) def flush(self): super().flush() self.generated.flush()
def test_multi_cache(tmp_path: Path): with MultiCache(tmp_path) as cache: cache["foo"].json["hello"] = "world" assert (tmp_path / "foo" / "index.json").is_file() with MultiCache(tmp_path) as cache: assert cache["foo"].json["hello"] == "world"
def test_preload(tmp_path: Path): with MultiCache(tmp_path) as cache: cache["foo"].json["bar"] = 42 with MultiCache(tmp_path) as cache: assert not cache cache.preload() assert cache.keys() == {"foo"}
def __init__( self, directory: FileSystemPath, generated_directory: FileSystemPath, default_cache: str = "default", gitignore: bool = True, cache_type: Type[Cache] = Cache, ): super().__init__(directory, default_cache, gitignore, cache_type=Cache) self.generated = MultiCache( generated_directory, default_cache, gitignore=False, cache_type=cache_type, )
def test_cache_expiration(tmp_path: Path): with MultiCache(tmp_path) as cache: hello = cache.directory / "hello.txt" hello.write_text("world") cache["default"].timeout(milliseconds=200) sleep(0.1) with MultiCache(tmp_path) as cache: assert (cache.directory / "hello.txt").read_text() == "world" sleep(0.1) with MultiCache(tmp_path) as cache: assert cache["default"].expire is None assert not (cache.directory / "hello.txt").is_file()
def test_cache_clear(tmp_path: Path): with MultiCache(tmp_path / "cache") as cache: cache["foo"].json["hello"] = "world" assert len(list(tmp_path.iterdir())) == 1 cache.clear() assert len(cache) == 0 assert list(tmp_path.iterdir()) == []
def run_beet( config: Optional[Union[JsonDict, FileSystemPath]] = None, directory: Optional[FileSystemPath] = None, cache: Union[bool, MultiCache] = False, ) -> Context: # type: ignore """Run the entire toolchain programmatically.""" if not directory: directory = Path.cwd() with ExitStack() as stack: project = Project() if isinstance(cache, MultiCache): project.resolved_cache = cache elif not cache: project.resolved_cache = MultiCache( stack.enter_context(TemporaryDirectory())) if isinstance(config, dict): with config_error_handler("<project>"): project.resolved_config = ProjectConfig( **config).resolve(directory) elif config: project.config_path = config else: project.config_directory = directory return ProjectBuilder(project).build()
def test_cache_length(tmp_path: Path): with MultiCache(tmp_path) as cache: assert cache["1"] assert cache["2"] assert len(cache) == 2 assert len(list(tmp_path.iterdir())) == 2 del cache["1"] assert len(cache) == 1 assert len(list(tmp_path.iterdir())) == 1
def test_cache_refresh(tmp_path: Path): with MultiCache(tmp_path) as cache: cache["foo"].timeout(milliseconds=200) assert cache["foo"].expire is not None sleep(0.1) with MultiCache(tmp_path) as cache: assert cache["foo"].expire is not None cache["foo"].restart_timeout() sleep(0.1) with MultiCache(tmp_path) as cache: assert cache["foo"].expire is not None sleep(0.1) with MultiCache(tmp_path) as cache: assert cache["foo"].expire is None
def test_default_multi_cache_directory(tmp_path: Path): with MultiCache(tmp_path) as cache: hello = cache.directory / "hello.txt" hello.write_text("world") assert (tmp_path / "default" / "hello.txt").is_file()
def test_match(tmp_path: Path): with MultiCache(tmp_path) as cache: cache["test-foo"] cache["test-bar"] cache["other-hello"] assert cache.match("test-*") == {"test-foo", "test-bar"}
def cache(self) -> MultiCache: if self.resolved_cache is not None: return self.resolved_cache self.resolved_cache = MultiCache(self.directory / self.cache_name) return self.resolved_cache