1
|
|
|
import typing |
|
|
|
|
2
|
|
|
from pathlib import Path |
3
|
|
|
from typing import Union, Optional, MutableMapping |
4
|
|
|
|
5
|
|
|
import orjson |
|
|
|
|
6
|
|
|
from pocketutils.core.dot_dict import NestedDotDict |
|
|
|
|
7
|
|
|
from pocketutils.core.hashers import Hasher |
|
|
|
|
8
|
|
|
from pocketutils.tools.common_tools import CommonTools |
|
|
|
|
9
|
|
|
|
10
|
|
|
from mandos.model.utils.misc_utils import MiscUtils |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class MandosResources: |
|
|
|
|
14
|
|
|
|
15
|
|
|
start_time = MiscUtils.utc() |
16
|
|
|
start_time_local = start_time.astimezone() |
17
|
|
|
start_timestamp = start_time.isoformat(timespec="milliseconds") |
18
|
|
|
start_timestamp_filesys = start_time_local.strftime("%Y-%m-%d_%H-%M-%S") |
19
|
|
|
hasher: Hasher = Hasher("sha256", buffer_size=16 * 1024) |
20
|
|
|
resource_dir = Path(__file__).parent.parent.parent |
21
|
|
|
|
22
|
|
|
@classmethod |
23
|
|
|
def contains(cls, *nodes: Union[Path, str], suffix: Optional[str] = None) -> bool: |
24
|
|
|
"""Returns whether a resource file (or dir) exists.""" |
25
|
|
|
return cls.path(*nodes, suffix=suffix).exists() |
26
|
|
|
|
27
|
|
|
@classmethod |
28
|
|
|
def path(cls, *nodes: Union[Path, str], suffix: Optional[str] = None) -> Path: |
29
|
|
|
"""Gets a path of a test resource file under ``resources/``.""" |
30
|
|
|
path = Path(cls.resource_dir, "resources", *nodes) |
31
|
|
|
return path.with_suffix(path.suffix if suffix is None else suffix) |
32
|
|
|
|
33
|
|
|
@classmethod |
34
|
|
|
def a_path(cls, *nodes: Union[Path, str], suffixes: Optional[typing.Set[str]] = None) -> Path: |
35
|
|
|
"""Gets a path of a test resource file under ``resources/``, ignoring suffix.""" |
36
|
|
|
path = Path(cls.resource_dir, "resources", *nodes) |
37
|
|
|
return CommonTools.only( |
38
|
|
|
[ |
39
|
|
|
p |
40
|
|
|
for p in path.parent.glob(path.stem + "*") |
41
|
|
|
if p.is_file() and (suffixes is None or p.suffix in suffixes) |
42
|
|
|
] |
43
|
|
|
) |
44
|
|
|
|
45
|
|
|
@classmethod |
46
|
|
|
def json(cls, *nodes: Union[Path, str], suffix: Optional[str] = None) -> NestedDotDict: |
47
|
|
|
"""Reads a JSON file under ``resources/``.""" |
48
|
|
|
path = cls.path(*nodes, suffix=suffix) |
49
|
|
|
data = orjson.loads(Path(path).read_text(encoding="utf8")) |
50
|
|
|
return NestedDotDict(data) |
51
|
|
|
|
52
|
|
|
@classmethod |
53
|
|
|
def json_dict(cls, *nodes: Union[Path, str], suffix: Optional[str] = None) -> MutableMapping: |
54
|
|
|
"""Reads a JSON file under ``resources/``.""" |
55
|
|
|
path = cls.path(*nodes, suffix=suffix) |
56
|
|
|
data = orjson.loads(Path(path).read_text(encoding="utf8")) |
57
|
|
|
return data |
58
|
|
|
|
59
|
|
|
strings = None |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
MandosResources.strings = { |
63
|
|
|
k.partition(":")[0]: v for k, v in MandosResources.json("strings.json").items() |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
__all__ = ["MandosResources"] |
68
|
|
|
|