1
|
|
|
from __future__ import annotations |
|
|
|
|
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 |
|
|
|
|
9
|
|
|
from pocketutils.core.dot_dict import NestedDotDict |
|
|
|
|
10
|
|
|
|
11
|
|
|
|
12
|
|
|
logger = logging.getLogger("mandos") |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
class Globals: |
|
|
|
|
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) |
|
|
|
|
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: |
|
|
|
|
51
|
|
|
return self.cache_path / "chembl" |
52
|
|
|
|
53
|
|
|
@property |
54
|
|
|
def pubchem_cache_path(self) -> Path: |
|
|
|
|
55
|
|
|
return self.cache_path / "pubchem" |
56
|
|
|
|
57
|
|
|
@property |
58
|
|
|
def hmdb_cache_path(self) -> Path: |
|
|
|
|
59
|
|
|
return self.cache_path / "hmdb" |
60
|
|
|
|
61
|
|
|
@property |
62
|
|
|
def taxonomy_cache_path(self) -> Path: |
|
|
|
|
63
|
|
|
return self.cache_path / "taxonomy" |
64
|
|
|
|
65
|
|
|
@property |
66
|
|
|
def match_cache_path(self) -> Path: |
|
|
|
|
67
|
|
|
return self.cache_path / "match" |
68
|
|
|
|
69
|
|
|
@classmethod |
70
|
|
|
def from_file(cls, path: Path) -> Settings: |
|
|
|
|
71
|
|
|
return cls.load(NestedDotDict.read_toml(path)) |
72
|
|
|
|
73
|
|
|
@classmethod |
74
|
|
|
def empty(cls) -> Settings: |
|
|
|
|
75
|
|
|
return cls.load(NestedDotDict({})) |
76
|
|
|
|
77
|
|
|
@classmethod |
78
|
|
|
def load(cls, data: NestedDotDict) -> Settings: |
|
|
|
|
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}") |
|
|
|
|
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}") |
|
|
|
|
120
|
|
|
else: |
121
|
|
|
MANDOS_SETTINGS = Settings.empty() |
122
|
|
|
logger.info(f"Using default settings (no file at {Globals.settings_path})") |
|
|
|
|
123
|
|
|
MANDOS_SETTINGS.set() |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
__all__ = ["MANDOS_SETTINGS"] |
127
|
|
|
|