• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nbiotcloud / ucdp / 14226848616

02 Apr 2025 06:26PM UTC coverage: 96.779% (-0.02%) from 96.795%
14226848616

push

github

iccode17
improve cache hookup

8 of 9 new or added lines in 2 files covered. (88.89%)

4777 of 4936 relevant lines covered (96.78%)

7.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.74
/src/ucdp/cache.py
1
#
2
# MIT License
3
#
4
# Copyright (c) 2024-2025 nbiotcloud
5
#
6
# Permission is hereby granted, free of charge, to any person obtaining a copy
7
# of this software and associated documentation files (the "Software"), to deal
8
# in the Software without restriction, including without limitation the rights
9
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
# copies of the Software, and to permit persons to whom the Software is
11
# furnished to do so, subject to the following conditions:
12
#
13
# The above copyright notice and this permission notice shall be included in all
14
# copies or substantial portions of the Software.
15
#
16
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
# SOFTWARE.
23
#
24

25
"""
26
Cache.
27
"""
28

29
import os
8✔
30
from pathlib import Path
8✔
31
from shutil import rmtree
8✔
32

33
from anycache import AnyCache
8✔
34
from platformdirs import user_cache_dir
8✔
35
from pydantic import BaseModel
8✔
36

37

38
class Cache(BaseModel):
8✔
39
    """UCDP Caching System."""
40

41
    path: Path | None
8✔
42
    maxsize: int = 10 * 1024 * 1024
8✔
43

44
    @classmethod
8✔
45
    def init(cls) -> "Cache":
8✔
46
        """Initialize."""
47
        return Cache(path=get_cachepath())
8✔
48

49
    def disable(self):
8✔
50
        """Disable Caching."""
51
        self.path = None
×
52

53
    def clear(self):
8✔
54
        """Clear Cache."""
55
        if self.path:
8✔
56
            rmtree(self.path, ignore_errors=True)
8✔
57

58
    @property
8✔
59
    def templates_path(self) -> Path | None:
8✔
60
        """Path for Templates."""
61
        if not self.path:
8✔
62
            return None
8✔
63
        return self.path / "templates"
8✔
64

65
    @property
8✔
66
    def loader_cache(self) -> AnyCache:
8✔
67
        """Path for Loader."""
68
        return self._get_anycache("loader")
8✔
69

70
    def get_cache(self, name: str):
8✔
71
        """Create Cache Function Decorator."""
NEW
72
        return self._get_anycache(name).anycache
×
73

74
    def _get_anycache(self, name: str) -> AnyCache:
8✔
75
        if not self.path:
8✔
76
            return AnyCache(maxsize=0)
8✔
77
        return AnyCache(cachedir=self.path / name, maxsize=self.maxsize)
8✔
78

79

80
def get_cachepath() -> Path | None:
8✔
81
    """Get Cache Base Path."""
82
    try:
8✔
83
        envvar = os.environ["UCDP_CACHE"]
8✔
84
        if not envvar:
8✔
85
            return None
8✔
86
        path = Path(envvar)
8✔
87
    except KeyError:
8✔
88
        try:
8✔
89
            path = Path(user_cache_dir("ucdp", ensure_exists=True))
8✔
90
        except RuntimeError:  # pragma: no cover
91
            return None
92
    try:
8✔
93
        path.mkdir(parents=True, exist_ok=True)
8✔
94
        (path / ".initialized").touch()
8✔
95
    except Exception:
8✔
96
        return None
8✔
97
    return path
8✔
98

99

100
CACHE = Cache.init()
8✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc