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

winter-telescope / winterdrp / 3699973870

pending completion
3699973870

push

github

GitHub
Use tempfile for cache (#242)

32 of 32 new or added lines in 4 files covered. (100.0%)

4592 of 6126 relevant lines covered (74.96%)

0.75 hits per line

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

86.96
/winterdrp/data/cache.py
1
"""
2
Central module for handling the cache, currently used only for storing image data.
3
"""
4

5
import logging
1✔
6
import os
1✔
7
from pathlib import Path
1✔
8
from tempfile import TemporaryDirectory
1✔
9

10
import numpy as np
1✔
11

12
logger = logging.getLogger(__name__)
1✔
13

14
USE_CACHE: bool = bool(os.getenv("USE_WINTER_CACHE", "true"))
1✔
15

16

17
class CacheError(Exception):
1✔
18
    """Error Relating to cache"""
19

20

21
class Cache:
1✔
22
    """
23
    A cache object for storing temporary data
24
    """
25

26
    cache_dir: Path | None = None
1✔
27

28
    def get_cache_dir(self) -> Path:
1✔
29
        """
30
        Returns the current cache dir
31

32
        :return: Cache dir
33
        """
34
        if np.logical_and(self.cache_dir is not None, USE_CACHE):
1✔
35
            return self.cache_dir
1✔
36

37
        if USE_CACHE:
×
38
            err = (
39
                "The code has been configured to not use a cache, "
40
                "but is now trying to access a cache."
41
            )
42
            logger.error(err)
43
            raise CacheError(err)
44

45
        err = (
46
            "No cache dir has been set. "
47
            "Please set that before trying to use the cache."
48
        )
49
        logger.error(err)
50
        raise CacheError(err)
51

52
    def set_cache_dir(self, cache_dir: TemporaryDirectory | Path | str):
1✔
53
        """
54
        Function to set the cache directory
55

56
        :param cache_dir: Cache dir to set
57
        :return: None
58
        """
59
        if isinstance(cache_dir, TemporaryDirectory):
1✔
60
            cache_dir = cache_dir.name
1✔
61
        else:
62
            logger.warning(
×
63
                f"Setting the cache to directory {cache_dir}, "
64
                f"rather than using a {TemporaryDirectory}. "
65
                f"Remember to clean this cache yourself after you are done!"
66
            )
67
        self.cache_dir = Path(cache_dir)
1✔
68
        self.cache_dir.mkdir(parents=True, exist_ok=True)
1✔
69

70
    def __str__(self):
1✔
71
        return f"A cache, with path {self.cache_dir}"
×
72

73

74
cache = Cache()
1✔
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