Passed
Push — main ( 83a9fb...fa90c4 )
by Douglas
03:43
created

mandos.model.search_caches   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A SearchCache.kill() 0 4 1
A SearchCache.at() 0 3 1
A SearchCache.path() 0 3 1
A SearchCache.__init__() 0 11 2
A SearchCache._meta_path() 0 3 1
A SearchCache.save() 0 6 2
A SearchCache.next() 0 2 1
1
"""
2
Temporary caching of search results as they progress.
3
"""
4
from pathlib import Path
5
from typing import Iterator, Sequence
6
7
import orjson
0 ignored issues
show
introduced by
Unable to import 'orjson'
Loading history...
8
from suretime import Suretime
0 ignored issues
show
introduced by
Unable to import 'suretime'
Loading history...
9
10
from mandos import logger
11
12
13
class SearchCache:
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
14
    def __init__(self, path: Path, compounds: Sequence[str]):
15
        self._path = path
16
        if self._meta_path.exists():
17
            self._data = orjson.loads(self._meta_path.read_text(encoding="utf8"))
18
            logger.caution(f"Resuming {path} with {self.at} completed compounds")
19
        else:
20
            self._data = dict(
21
                start=Suretime.tagged.now_utc_sys().iso, last=None, path=self.path, done=set()
22
            )
23
            logger.debug(f"Starting fresh cache for {path}")
24
        self._queue: Iterator[str] = iter([c for c in compounds if c not in self._data["done"]])
25
26
    def next(self) -> str:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
27
        return next(self._queue)
28
29
    def save(self, *compounds: str) -> None:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
30
        for c in compounds:
0 ignored issues
show
Coding Style Naming introduced by
Variable name "c" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
31
            self._data["done"].add(c)
32
        data = orjson.dumps(self._data).decode(encoding="utf8")
33
        self._meta_path.write_text(data)
34
        logger.debug(f"Saved to {self._meta_path}")
35
36
    @property
37
    def path(self) -> Path:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
38
        return self._path
39
40
    @property
41
    def at(self) -> int:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
Coding Style Naming introduced by
Attribute name "at" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
42
        return len(self._data["done"])
43
44
    @property
45
    def _meta_path(self) -> Path:
46
        return self._path.parent / (self._path.name + ".meta.json.tmp")
47
48
    def kill(self) -> None:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
49
        self._data = None
50
        self._meta_path.unlink()
51
        logger.debug(f"Destroyed search cache {self._meta_path}")
52
53
54
__all__ = ["SearchCache"]
55