예제 #1
0
파일: context.py 프로젝트: mcbeet/beet
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()
예제 #2
0
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"
예제 #3
0
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"}
예제 #4
0
파일: context.py 프로젝트: mcbeet/beet
 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,
     )
예제 #5
0
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()
예제 #6
0
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()) == []
예제 #7
0
파일: helpers.py 프로젝트: misode/beet
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()
예제 #8
0
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
예제 #9
0
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
예제 #10
0
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()
예제 #11
0
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"}
예제 #12
0
 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