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

nbiotcloud / ucdp / 15808718976

22 Jun 2025 04:42PM UTC coverage: 96.64% (-0.1%) from 96.781%
15808718976

Pull #119

github

web-flow
Merge 0bc72d467 into afd15f6d2
Pull Request #119: Feature/speed up ls

4919 of 5090 relevant lines covered (96.64%)

7.73 hits per line

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

95.83
/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_path
8✔
35
from pydantic import BaseModel
8✔
36

37
from .logging import LOGGER
8✔
38

39

40
class Cache(BaseModel):
8✔
41
    """UCDP Caching System."""
42

43
    path: Path | None
8✔
44
    maxsize: int = 10 * 1024 * 1024
8✔
45

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

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

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

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

67
    def get_cache(self, name: str):
8✔
68
        """Create Cache Function Decorator."""
69
        return self._get_anycache(name).anycache
8✔
70

71
    def _get_anycache(self, name: str) -> AnyCache:
8✔
72
        if not self.path:
8✔
73
            return AnyCache(maxsize=0)
×
74
        return AnyCache(cachedir=self.path / name, maxsize=self.maxsize)
8✔
75

76

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

100

101
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