1
|
|
|
import enum |
|
|
|
|
2
|
|
|
import inspect |
3
|
|
|
import sys |
4
|
|
|
import typing |
5
|
|
|
from datetime import datetime |
6
|
|
|
from pathlib import Path |
7
|
|
|
from typing import Type, TypeVar, Optional, Mapping, Any, Union, Sequence |
8
|
|
|
|
9
|
|
|
from mandos import logger |
10
|
|
|
from suretime import Suretime |
|
|
|
|
11
|
|
|
from typeddfs import TypedDf |
|
|
|
|
12
|
|
|
|
13
|
|
|
from mandos.model.settings import MANDOS_SETTINGS |
|
|
|
|
14
|
|
|
|
15
|
|
|
T = TypeVar("T", covariant=True) |
|
|
|
|
16
|
|
|
|
17
|
|
|
|
18
|
|
|
class InjectionError(LookupError): |
|
|
|
|
19
|
|
|
""" """ |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
class ReflectionUtils: |
|
|
|
|
23
|
|
|
@classmethod |
24
|
|
|
def get_generic_arg(cls, clazz: Type[T], bound: Optional[Type[T]] = None) -> Type: |
25
|
|
|
""" |
26
|
|
|
Finds the generic argument (specific TypeVar) of a :py:class:`~typing.Generic` class. |
27
|
|
|
**Assumes that ``clazz`` only has one type parameter. Always returns the first.** |
28
|
|
|
|
29
|
|
|
Args: |
30
|
|
|
clazz: The Generic class |
31
|
|
|
bound: If non-None, requires the returned type to be a subclass of ``bound`` (or equal to it) |
|
|
|
|
32
|
|
|
|
33
|
|
|
Returns: |
34
|
|
|
The class |
35
|
|
|
|
36
|
|
|
Raises: |
37
|
|
|
AssertionError: For most errors |
38
|
|
|
""" |
39
|
|
|
bases = clazz.__orig_bases__ |
40
|
|
|
try: |
41
|
|
|
param = typing.get_args(bases[0])[0] |
|
|
|
|
42
|
|
|
except KeyError: |
43
|
|
|
raise AssertionError(f"Failed to get generic type on {cls}") |
44
|
|
|
if not issubclass(param, bound): |
45
|
|
|
raise AssertionError(f"{param} is not a {bound}") |
46
|
|
|
return param |
47
|
|
|
|
48
|
|
|
@classmethod |
49
|
|
|
def subclass_dict(cls, clazz: Type[T], concrete: bool = False) -> Mapping[str, Type[T]]: |
|
|
|
|
50
|
|
|
return {c.__name__: c for c in cls.subclasses(clazz, concrete=concrete)} |
51
|
|
|
|
52
|
|
|
@classmethod |
53
|
|
|
def subclasses(cls, clazz, concrete: bool = False): |
|
|
|
|
54
|
|
|
for subclass in clazz.__subclasses__(): |
55
|
|
|
yield from cls.subclasses(subclass, concrete=concrete) |
56
|
|
|
if ( |
57
|
|
|
not concrete |
|
|
|
|
58
|
|
|
or not inspect.isabstract(subclass) |
|
|
|
|
59
|
|
|
and not subclass.__name__.startswith("_") |
|
|
|
|
60
|
|
|
): |
61
|
|
|
yield subclass |
62
|
|
|
|
63
|
|
|
@classmethod |
64
|
|
|
def default_arg_values(cls, func) -> Mapping[str, Optional[Any]]: |
|
|
|
|
65
|
|
|
return {k: v.default for k, v in cls.optional_args(func).items()} |
66
|
|
|
|
67
|
|
|
@classmethod |
68
|
|
|
def required_args(cls, func): |
69
|
|
|
""" |
70
|
|
|
Finds parameters that lack default values. |
71
|
|
|
|
72
|
|
|
Args: |
73
|
|
|
func: A function or method |
74
|
|
|
|
75
|
|
|
Returns: |
76
|
|
|
A dict mapping parameter names to instances of ``MappingProxyType``, |
77
|
|
|
just as ``inspect.signature(func).parameters`` does. |
78
|
|
|
""" |
79
|
|
|
return cls._args(func, True) |
80
|
|
|
|
81
|
|
|
@classmethod |
82
|
|
|
def optional_args(cls, func): |
83
|
|
|
""" |
84
|
|
|
Finds parameters that have default values. |
85
|
|
|
|
86
|
|
|
Args: |
87
|
|
|
func: A function or method |
88
|
|
|
|
89
|
|
|
Returns: |
90
|
|
|
A dict mapping parameter names to instances of ``MappingProxyType``, |
91
|
|
|
just as ``inspect.signature(func).parameters`` does. |
92
|
|
|
""" |
93
|
|
|
return cls._args(func, False) |
94
|
|
|
|
95
|
|
|
@classmethod |
96
|
|
|
def _args(cls, func, req): |
97
|
|
|
signature = inspect.signature(func) |
98
|
|
|
return { |
99
|
|
|
k: v |
100
|
|
|
for k, v in signature.parameters.items() |
101
|
|
|
if req |
102
|
|
|
and v.default is inspect.Parameter.empty |
103
|
|
|
or not req |
104
|
|
|
and v.default is not inspect.Parameter.empty |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
@classmethod |
108
|
|
|
def injection(cls, fully_qualified: str, clazz: Type[T]) -> Type[T]: |
109
|
|
|
""" |
110
|
|
|
Gets a **class** by its fully-resolved class name. |
111
|
|
|
|
112
|
|
|
Args: |
113
|
|
|
fully_qualified: |
114
|
|
|
clazz: |
115
|
|
|
|
116
|
|
|
Returns: |
117
|
|
|
The Type |
118
|
|
|
|
119
|
|
|
Raises: |
120
|
|
|
InjectionError: If the class was not found |
121
|
|
|
""" |
122
|
|
|
s = fully_qualified |
|
|
|
|
123
|
|
|
mod = s[: s.rfind(".")] |
124
|
|
|
clz = s[s.rfind(".") :] |
125
|
|
|
try: |
126
|
|
|
return getattr(sys.modules[mod], clz) |
127
|
|
|
except AttributeError: |
128
|
|
|
raise InjectionError( |
129
|
|
|
f"Did not find {clazz} by fully-qualified class name {fully_qualified}" |
130
|
|
|
) from None |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
class MiscUtils: |
134
|
|
|
""" |
135
|
|
|
These are here to make sure I always use the same NTP server, etc. |
136
|
|
|
""" |
137
|
|
|
|
138
|
|
|
@classmethod |
139
|
|
|
def empty_df(cls, clazz: Type[T], reserved: bool = False) -> T: |
|
|
|
|
140
|
|
|
if issubclass(clazz, TypedDf): |
|
|
|
|
141
|
|
|
if reserved: |
142
|
|
|
req = clazz.known_names() |
143
|
|
|
else: |
144
|
|
|
req = [*clazz.required_index_names(), *clazz.required_columns] |
145
|
|
|
return clazz({r: [] for r in req}) |
146
|
|
|
else: |
147
|
|
|
return clazz({}) |
148
|
|
|
|
149
|
|
|
@classmethod |
150
|
|
|
def adjust_filename(cls, to: Optional[Path], default: Union[str, Path], replace: bool) -> Path: |
|
|
|
|
151
|
|
|
if to is None: |
152
|
|
|
path = Path(default) |
153
|
|
|
elif str(to).startswith("."): |
154
|
|
|
path = Path(default).with_suffix(str(to)) |
155
|
|
|
elif to.is_dir() or to.suffix == "": |
156
|
|
|
path = to / default |
157
|
|
|
else: |
158
|
|
|
raise AssertionError(str(to)) |
159
|
|
|
path = Path(path) |
160
|
|
|
if path.exists() and not replace: |
|
|
|
|
161
|
|
|
raise FileExistsError(f"File {path} already exists") |
162
|
|
|
elif replace: |
163
|
|
|
logger.info(f"Overwriting existing file {path}.") |
164
|
|
|
return path |
165
|
|
|
|
166
|
|
|
@classmethod |
167
|
|
|
def ntp_utc(cls) -> datetime: |
|
|
|
|
168
|
|
|
ntp = Suretime.tagged.now_utc_ntp( |
169
|
|
|
ntp_server=MANDOS_SETTINGS.ntp_continent, ntp_clock="client-sent" |
170
|
|
|
) |
171
|
|
|
return ntp.use_clock_as_dt.dt |
172
|
|
|
|
173
|
|
|
@classmethod |
174
|
|
|
def utc(cls) -> datetime: |
|
|
|
|
175
|
|
|
return Suretime.tagged.now_utc_sys().dt |
176
|
|
|
|
177
|
|
|
@classmethod |
178
|
|
|
def serialize_list(cls, lst: Sequence[str]) -> str: |
|
|
|
|
179
|
|
|
return " || ".join([str(x) for x in lst]) |
180
|
|
|
|
181
|
|
|
@classmethod |
182
|
|
|
def deserialize_list(cls, s: str) -> Sequence[str]: |
|
|
|
|
183
|
|
|
return s.split(" || ") |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
class TrueFalseUnknown(enum.Enum): |
|
|
|
|
187
|
|
|
true = enum.auto() |
188
|
|
|
false = enum.auto() |
189
|
|
|
unknown = enum.auto() |
190
|
|
|
|
191
|
|
|
@classmethod |
192
|
|
|
def parse(cls, s: str): |
|
|
|
|
193
|
|
|
tf_map = { |
194
|
|
|
"t": TrueFalseUnknown.true, |
195
|
|
|
"f": TrueFalseUnknown.false, |
196
|
|
|
"true": TrueFalseUnknown.true, |
197
|
|
|
"false": TrueFalseUnknown.false, |
198
|
|
|
} |
199
|
|
|
return tf_map.get(s.lower().strip(), TrueFalseUnknown.unknown) |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
class CleverEnum(enum.Enum): |
203
|
|
|
""" |
204
|
|
|
An enum with a ``.of`` method that finds values |
205
|
|
|
with limited string/value fixing. |
206
|
|
|
May support an "unmatched" type -- a fallback value when there is no match. |
207
|
|
|
This is similar to pocketutils' simpler ``SmartEnum``. |
208
|
|
|
It is mainly useful for enums corresponding to concepts in ChEMBL and PubChem, |
209
|
|
|
where it's acceptable for the user to input spaces (like the database concepts use) |
210
|
|
|
rather than the underscores that Python requires. |
211
|
|
|
""" |
212
|
|
|
|
213
|
|
|
@classmethod |
214
|
|
|
def _unmatched_type(cls) -> Optional[__qualname__]: |
215
|
|
|
return None |
216
|
|
|
|
217
|
|
|
@classmethod |
218
|
|
|
def of(cls, s: Union[int, str, __qualname__]) -> __qualname__: |
|
|
|
|
219
|
|
|
""" |
220
|
|
|
Turns a string or int into this type. |
221
|
|
|
Case-insensitive. Replaces `` ``, ``.``, and ``-`` with ``_``. |
222
|
|
|
""" |
223
|
|
|
if isinstance(s, cls): |
224
|
|
|
return s |
225
|
|
|
key = s.strip().replace(" ", "_").replace(".", "_").replace("-", "_").lower() |
226
|
|
|
try: |
227
|
|
|
if isinstance(s, str): |
|
|
|
|
228
|
|
|
return cls[key] |
229
|
|
|
elif isinstance(key, int): |
230
|
|
|
return cls(key) |
231
|
|
|
else: |
232
|
|
|
raise TypeError(f"Lookup type {type(s)} for value {s} not a str or int") |
233
|
|
|
except KeyError: |
234
|
|
|
unk = cls._unmatched_type() |
235
|
|
|
if unk is None: |
236
|
|
|
raise |
237
|
|
|
logger.error(f"Value {key} not found. Using {unk}") |
238
|
|
|
if not isinstance(unk, cls): |
239
|
|
|
raise AssertionError(f"Wrong type {type(unk)} (lookup: {s})") |
240
|
|
|
return unk |
241
|
|
|
|
242
|
|
|
|
243
|
|
|
__all__ = ["InjectionError", "ReflectionUtils", "MiscUtils", "TrueFalseUnknown", "CleverEnum"] |
244
|
|
|
|