Passed
Push — main ( a07aa0...748456 )
by Douglas
01:55
created

mandos.model.settings.Settings.hmdb_cache_path()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
from __future__ import annotations
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
3
import os
4
import logging
5
from dataclasses import dataclass
6
from pathlib import Path
7
8
from chembl_webresource_client.settings import Settings as ChemblSettings
0 ignored issues
show
introduced by
Unable to import 'chembl_webresource_client.settings'
Loading history...
9
from pocketutils.core.dot_dict import NestedDotDict
0 ignored issues
show
introduced by
Unable to import 'pocketutils.core.dot_dict'
Loading history...
10
11
12
logger = logging.getLogger("mandos")
13
14
15
class Globals:
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
16
    chembl_settings = ChemblSettings.Instance()
17
    is_in_ci = "IS_IN_CI" in os.environ
18
    if is_in_ci:
19
        mandos_path = Path(__file__).parent.parent.parent / "tests" / "resources" / ".mandos-cache"
20
    else:
21
        mandos_path = Path(
22
            {k.lower(): v for k, v in os.environ.items()}.get(
23
                "MANDOS_HOME", Path.home() / ".mandos"
24
            )
25
        )
26
    settings_path = mandos_path / "settings.toml"
27
    chembl_cache = mandos_path / "chembl"
28
    taxonomy_cache = mandos_path / "taxonomy"
29
30
31
@dataclass(frozen=True, repr=True)
0 ignored issues
show
Documentation introduced by
Empty class docstring
Loading history...
best-practice introduced by
Too many instance attributes (13/7)
Loading history...
32
class Settings:
33
    """"""
34
35
    is_testing: bool
36
    cache_path: Path
37
    cache_gzip: bool
38
    chembl_n_retries: int
39
    chembl_timeout_sec: int
40
    chembl_query_delay_min: float
41
    chembl_query_delay_max: float
42
    chembl_fast_save: bool
43
    pubchem_n_retries: int
44
    pubchem_timeout_sec: float
45
    pubchem_query_delay_min: float
46
    pubchem_query_delay_max: float
47
    pubchem_use_parent: bool
48
49
    @property
50
    def chembl_cache_path(self) -> Path:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
51
        return self.cache_path / "chembl"
52
53
    @property
54
    def pubchem_cache_path(self) -> Path:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
55
        return self.cache_path / "pubchem"
56
57
    @property
58
    def hmdb_cache_path(self) -> Path:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
59
        return self.cache_path / "hmdb"
60
61
    @property
62
    def taxonomy_cache_path(self) -> Path:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
63
        return self.cache_path / "taxonomy"
64
65
    @property
66
    def match_cache_path(self) -> Path:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
67
        return self.cache_path / "match"
68
69
    @classmethod
70
    def from_file(cls, path: Path) -> Settings:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
71
        return cls.load(NestedDotDict.read_toml(path))
72
73
    @classmethod
74
    def empty(cls) -> Settings:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
75
        return cls.load(NestedDotDict({}))
76
77
    @classmethod
78
    def load(cls, data: NestedDotDict) -> Settings:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
79
        #  117571
80
        return cls(
81
            is_testing=data.get_as("mandos.is_testing", bool, False),
82
            cache_path=data.get_as("mandos.cache.path", Path, Globals.mandos_path).expanduser(),
83
            cache_gzip=data.get_as("mandos.cache.gzip", bool),
84
            chembl_n_retries=data.get_as("mandos.query.chembl.n_retries", int, 1),
85
            chembl_fast_save=data.get_as("mandos.query.chembl.fast_save", bool, True),
86
            chembl_timeout_sec=data.get_as("mandos.query.chembl.timeout_sec", int, 1),
87
            chembl_query_delay_min=data.get_as("mandos.query.chembl.delay_sec", float, 0.25),
88
            chembl_query_delay_max=data.get_as("mandos.query.chembl.delay_sec", float, 0.25),
89
            pubchem_timeout_sec=data.get_as("mandos.query.pubchem.timeout_sec", int, 1),
90
            pubchem_query_delay_min=data.get_as("mandos.query.pubchem.delay_sec", float, 0.25),
91
            pubchem_query_delay_max=data.get_as("mandos.query.pubchem.delay_sec", float, 0.25),
92
            pubchem_n_retries=data.get_as("mandos.query.pubchem.n_retries", int, 1),
93
            pubchem_use_parent=data.get_as("mandos.query.pubchem.use_parent", bool, True),
94
        )
95
96
    def set(self):
97
        """
98
99
        Returns:
100
101
        """
102
        instance = Globals.chembl_settings
103
        instance.CACHING = True
104
        if not Globals.is_in_ci:  # not sure if this is needed
105
            instance.CACHE_NAME = str(self.chembl_cache_path)
106
            logger.info(f"Setting ChEMBL cache to {self.chembl_cache_path}")
0 ignored issues
show
introduced by
Use lazy % formatting in logging functions
Loading history...
107
        instance.TOTAL_RETRIES = self.chembl_n_retries
108
        instance.FAST_SAVE = self.chembl_fast_save
109
        instance.TIMEOUT = self.chembl_timeout_sec
110
        self.chembl_cache_path.mkdir(exist_ok=True, parents=True)
111
        self.pubchem_cache_path.mkdir(exist_ok=True, parents=True)
112
        self.hmdb_cache_path.mkdir(exist_ok=True, parents=True)
113
        self.taxonomy_cache_path.mkdir(exist_ok=True, parents=True)
114
        self.match_cache_path.mkdir(exist_ok=True, parents=True)
115
116
117
if Globals.settings_path.exists():
118
    MANDOS_SETTINGS = Settings.from_file(Globals.settings_path)
119
    logger.info(f"Read settings at {Globals.settings_path}")
0 ignored issues
show
introduced by
Use lazy % formatting in logging functions
Loading history...
120
else:
121
    MANDOS_SETTINGS = Settings.empty()
122
    logger.info(f"Using default settings (no file at {Globals.settings_path})")
0 ignored issues
show
introduced by
Use lazy % formatting in logging functions
Loading history...
123
MANDOS_SETTINGS.set()
124
125
126
__all__ = ["MANDOS_SETTINGS"]
127