1
|
|
|
""" |
2
|
|
|
Code for decorateme. |
3
|
|
|
""" |
4
|
|
|
from __future__ import annotations |
5
|
|
|
|
6
|
|
|
import enum |
7
|
|
|
import html as _html |
8
|
|
|
from abc import abstractmethod |
9
|
|
|
from collections.abc import Callable, Collection |
10
|
|
|
from functools import total_ordering, wraps |
11
|
|
|
from typing import Literal, TypeVar, final |
|
|
|
|
12
|
|
|
from warnings import warn |
13
|
|
|
|
14
|
|
|
T = TypeVar("T", bound=type) |
|
|
|
|
15
|
|
|
|
16
|
|
|
|
17
|
|
|
def reserved(cls: T) -> T: |
18
|
|
|
""" |
19
|
|
|
Empty but is declared for future use. |
20
|
|
|
""" |
21
|
|
|
return cls |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
class CodeIncompleteError(NotImplementedError): |
25
|
|
|
"""The code is not finished.""" |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class CodeRemovedError(NotImplementedError): |
29
|
|
|
"""The code was removed.""" |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
class PreviewWarning(UserWarning): |
33
|
|
|
"""The code being called is a preview, unstable. or immature.""" |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
@enum.unique |
37
|
|
|
class CodeStatus(enum.Enum): |
38
|
|
|
""" |
39
|
|
|
An enum for the quality/maturity of code, |
40
|
|
|
ranging from incomplete to deprecated. |
41
|
|
|
""" |
42
|
|
|
|
43
|
|
|
INCOMPLETE = -2 |
44
|
|
|
PREVIEW = -1 |
45
|
|
|
STABLE = 0 |
46
|
|
|
PENDING_DEPRECATION = 1 |
47
|
|
|
DEPRECATED = 2 |
48
|
|
|
REMOVED = 3 |
49
|
|
|
|
50
|
|
|
@classmethod |
51
|
|
|
def of(cls, x: int | str | CodeStatus) -> CodeStatus: |
|
|
|
|
52
|
|
|
if isinstance(x, str): |
53
|
|
|
return cls[x.lower().strip()] |
54
|
|
|
if isinstance(x, CodeStatus): |
55
|
|
|
return x |
56
|
|
|
if isinstance(x, int): |
57
|
|
|
return cls(x) |
58
|
|
|
raise TypeError(f"Invalid type {type(x)} for {x}") |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
def status( |
|
|
|
|
62
|
|
|
level: int | str | CodeStatus, |
|
|
|
|
63
|
|
|
vr: str | None = "", |
|
|
|
|
64
|
|
|
msg: str | None = None, |
|
|
|
|
65
|
|
|
): |
66
|
|
|
""" |
67
|
|
|
Annotate code quality. Emits a warning if bad code is called. |
68
|
|
|
|
69
|
|
|
Args: |
70
|
|
|
level: The quality / maturity |
71
|
|
|
vr: First version the status / warning applies to |
72
|
|
|
msg: Explanation and/or when it will be removed or completed |
73
|
|
|
""" |
74
|
|
|
|
75
|
|
|
level = CodeStatus.of(level) |
76
|
|
|
|
77
|
|
|
@wraps(status) |
78
|
|
|
def dec(func): |
79
|
|
|
func.__status__ = level |
80
|
|
|
if level is CodeStatus.STABLE: |
|
|
|
|
81
|
|
|
return func |
82
|
|
|
elif level is CodeStatus.REMOVED: |
83
|
|
|
|
84
|
|
|
def my_fn(*_, **__): |
85
|
|
|
raise CodeRemovedError(f"{func.__name__} was removed (as of version: {vr}). {msg}") |
86
|
|
|
|
87
|
|
|
return wraps(func)(my_fn) |
88
|
|
|
|
89
|
|
|
elif level is CodeStatus.INCOMPLETE: |
90
|
|
|
|
91
|
|
|
def my_fn(*_, **__): |
92
|
|
|
raise CodeIncompleteError( |
93
|
|
|
f"{func.__name__} is incomplete (as of version: {vr}). {msg}" |
94
|
|
|
) |
95
|
|
|
|
96
|
|
|
return wraps(func)(my_fn) |
97
|
|
|
elif level is CodeStatus.PREVIEW: |
98
|
|
|
|
99
|
|
|
def my_fn(*args, **kwargs): |
100
|
|
|
warn( |
101
|
|
|
f"{func.__name__} is a preview or immature (as of version: {vr}). {msg}", |
102
|
|
|
PreviewWarning, |
103
|
|
|
) |
104
|
|
|
return func(*args, **kwargs) |
105
|
|
|
|
106
|
|
|
return wraps(func)(my_fn) |
107
|
|
|
elif level is CodeStatus.PENDING_DEPRECATION: |
108
|
|
|
|
109
|
|
|
def my_fn(*args, **kwargs): |
110
|
|
|
warn( |
111
|
|
|
f"{func.__name__} is pending deprecation (as of version: {vr}). {msg}", |
112
|
|
|
PendingDeprecationWarning, |
113
|
|
|
) |
114
|
|
|
return func(*args, **kwargs) |
115
|
|
|
|
116
|
|
|
return wraps(func)(my_fn) |
117
|
|
|
elif level is CodeStatus.DEPRECATED: |
118
|
|
|
|
119
|
|
|
def my_fn(*args, **kwargs): |
120
|
|
|
warn( |
121
|
|
|
f"{func.__name__} is deprecated (as of version: {vr}). {msg}", |
122
|
|
|
DeprecationWarning, |
123
|
|
|
) |
124
|
|
|
return func(*args, **kwargs) |
125
|
|
|
|
126
|
|
|
return wraps(func)(my_fn) |
127
|
|
|
raise AssertionError(f"What is {level}?") |
128
|
|
|
|
129
|
|
|
return dec |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
def incomplete( |
|
|
|
|
133
|
|
|
vr: str | None = "", |
|
|
|
|
134
|
|
|
msg: str | None = None, |
|
|
|
|
135
|
|
|
): |
136
|
|
|
return status(CodeStatus.INCOMPLETE, vr, msg) |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
def preview( |
|
|
|
|
140
|
|
|
vr: str | None = "", |
|
|
|
|
141
|
|
|
msg: str | None = None, |
|
|
|
|
142
|
|
|
): |
143
|
|
|
return status(CodeStatus.PREVIEW, vr, msg) |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
def pending_deprecation( |
|
|
|
|
147
|
|
|
vr: str | None = "", |
|
|
|
|
148
|
|
|
msg: str | None = None, |
|
|
|
|
149
|
|
|
): |
150
|
|
|
return status(CodeStatus.PENDING_DEPRECATION, vr, msg) |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
def deprecated( |
|
|
|
|
154
|
|
|
vr: str | None = "", |
|
|
|
|
155
|
|
|
msg: str | None = None, |
|
|
|
|
156
|
|
|
): |
157
|
|
|
return status(CodeStatus.DEPRECATED, vr, msg) |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
def removed( |
|
|
|
|
161
|
|
|
vr: str | None = "", |
|
|
|
|
162
|
|
|
msg: str | None = None, |
|
|
|
|
163
|
|
|
): |
164
|
|
|
return status(CodeStatus.REMOVED, vr, msg) |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
class _SpecialStr(str): |
168
|
|
|
""" |
169
|
|
|
A string that can be displayed with Jupyter with line breaks and tabs. |
170
|
|
|
""" |
171
|
|
|
|
172
|
|
|
def _repr_html_(self): |
173
|
|
|
return str(self.replace("\n", "<br />").replace("\t", "    ")) |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
class _Utils: |
177
|
|
|
@classmethod |
178
|
|
|
def exclude_fn(cls, *items) -> Callable[str, bool]: |
|
|
|
|
179
|
|
|
fns = [cls._exclude_fn(x) for x in items] |
180
|
|
|
|
181
|
|
|
def exclude(s: str): |
|
|
|
|
182
|
|
|
return any(fn(s) for fn in fns) |
183
|
|
|
|
184
|
|
|
return exclude |
185
|
|
|
|
186
|
|
|
@classmethod |
187
|
|
|
def _exclude_fn(cls, exclude) -> Callable[str, bool]: |
|
|
|
|
188
|
|
|
if exclude is None or exclude is False: |
189
|
|
|
return lambda s: False |
190
|
|
|
if isinstance(exclude, str): |
|
|
|
|
191
|
|
|
return lambda s: s == exclude |
192
|
|
|
elif isinstance(exclude, Collection): |
193
|
|
|
return lambda s: s in exclude |
194
|
|
|
elif callable(exclude): |
195
|
|
|
return exclude |
196
|
|
|
else: |
197
|
|
|
raise TypeError(str(exclude)) |
198
|
|
|
|
199
|
|
|
@classmethod |
200
|
|
|
def gen_str( |
|
|
|
|
201
|
|
|
cls, |
|
|
|
|
202
|
|
|
obj, |
|
|
|
|
203
|
|
|
fields: Collection[str] = None, |
|
|
|
|
204
|
|
|
*, |
|
|
|
|
205
|
|
|
exclude: Callable[[str], bool] = lambda _: False, |
|
|
|
|
206
|
|
|
address: bool = False, |
|
|
|
|
207
|
|
|
angular: bool = False, |
|
|
|
|
208
|
|
|
): |
209
|
|
|
_name = obj.__class__.__name__ |
210
|
|
|
_fields = ", ".join( |
211
|
|
|
k + "=" + ('"' + v + '"' if isinstance(v, str) else str(v)) |
212
|
|
|
for k, v in cls.gen_list(obj, fields, exclude=exclude, address=address) |
213
|
|
|
) |
214
|
|
|
if angular: |
|
|
|
|
215
|
|
|
return f"<{_name} {_fields}>" |
216
|
|
|
else: |
217
|
|
|
return f"{_name}({_fields})" |
218
|
|
|
|
219
|
|
|
@classmethod |
220
|
|
|
def gen_html( |
|
|
|
|
221
|
|
|
cls, |
|
|
|
|
222
|
|
|
obj, |
|
|
|
|
223
|
|
|
fields: Collection[str] = None, |
|
|
|
|
224
|
|
|
*, |
|
|
|
|
225
|
|
|
exclude: Callable[[str], bool] = lambda _: False, |
|
|
|
|
226
|
|
|
address: bool = True, |
|
|
|
|
227
|
|
|
angular: bool = True, |
|
|
|
|
228
|
|
|
): |
229
|
|
|
_name = obj.__class__.__name__ |
230
|
|
|
_fields = ", ".join( |
231
|
|
|
f"{k}={_html.escape(v)}" |
232
|
|
|
for k, v in cls.gen_list(obj, fields, exclude=exclude, address=address) |
233
|
|
|
) |
234
|
|
|
if angular: |
|
|
|
|
235
|
|
|
return f"<<strong>{_name}</strong> {_fields}>" |
236
|
|
|
else: |
237
|
|
|
return f"{_name}({_fields})" |
238
|
|
|
|
239
|
|
|
@classmethod |
240
|
|
|
def gen_list( |
|
|
|
|
241
|
|
|
cls, |
|
|
|
|
242
|
|
|
obj, |
|
|
|
|
243
|
|
|
fields: Collection[str] = None, |
|
|
|
|
244
|
|
|
*, |
|
|
|
|
245
|
|
|
exclude: Callable[[str], bool] = lambda _: False, |
|
|
|
|
246
|
|
|
address: bool = False, |
|
|
|
|
247
|
|
|
): |
248
|
|
|
yield from _Utils.var_items(obj, fields, exclude=exclude) |
249
|
|
|
if address: |
250
|
|
|
yield "@", str(hex(id(obj))) |
251
|
|
|
|
252
|
|
|
@classmethod |
253
|
|
|
def var_items(cls, obj, fields, exclude): |
|
|
|
|
254
|
|
|
yield from [ |
255
|
|
|
(key, value) |
256
|
|
|
for key, value in vars(obj).items() |
257
|
|
|
if (fields is None) or (key in fields) |
258
|
|
|
if not key.startswith(f"_{obj.__class__.__name__}__") # imperfect exclude mangled |
259
|
|
|
and not exclude(key) |
260
|
|
|
] |
261
|
|
|
|
262
|
|
|
|
263
|
|
|
def add_reprs( |
|
|
|
|
264
|
|
|
fields: Collection[str] | None = None, |
|
|
|
|
265
|
|
|
*, |
|
|
|
|
266
|
|
|
exclude: Collection[str] | Callable[[str], bool] | None | Literal[False] = None, |
|
|
|
|
267
|
|
|
exclude_from_str: Collection[str] | Callable[[str], bool] | None | Literal[False] = None, |
|
|
|
|
268
|
|
|
exclude_from_repr: Collection[str] | Callable[[str], bool] | None | Literal[False] = None, |
|
|
|
|
269
|
|
|
exclude_html: Collection[str] | Callable[[str], bool] | None | Literal[False] = None, |
|
|
|
|
270
|
|
|
exclude_rich: Collection[str] | Callable[[str], bool] | None | Literal[False] = None, |
|
|
|
|
271
|
|
|
address: bool = False, |
|
|
|
|
272
|
|
|
angular: bool = False, |
|
|
|
|
273
|
|
|
html: bool = True, |
|
|
|
|
274
|
|
|
rich: bool = True, |
|
|
|
|
275
|
|
|
): |
276
|
|
|
""" |
277
|
|
|
Auto-adds ``__repr__``, ``__str__``, ``_repr_html``, and ``__rich_repr__`` |
278
|
|
|
that use instances' attributes (``vars``). |
279
|
|
|
""" |
280
|
|
|
exclude_from_repr = _Utils.exclude_fn(exclude, exclude_from_repr) |
281
|
|
|
exclude_from_str = _Utils.exclude_fn(exclude, exclude_from_str) |
282
|
|
|
exclude_html = _Utils.exclude_fn(exclude, exclude_html) |
283
|
|
|
exclude_rich = _Utils.exclude_fn(exclude, exclude_rich) |
284
|
|
|
|
285
|
|
|
def __repr(self) -> str: |
286
|
|
|
return _Utils.gen_str(self, fields, exclude=exclude_from_repr, address=address) |
287
|
|
|
|
288
|
|
|
def __str(self) -> str: |
289
|
|
|
return _Utils.gen_str(self, fields, exclude=exclude_from_str) |
290
|
|
|
|
291
|
|
|
def __html(self) -> str: |
292
|
|
|
return _Utils.gen_html(self, fields, exclude=exclude_html) |
293
|
|
|
|
294
|
|
|
if rich: |
295
|
|
|
from rich.repr import RichReprResult |
|
|
|
|
296
|
|
|
|
297
|
|
|
def __rich(self) -> RichReprResult: |
298
|
|
|
yield from _Utils.gen_list(self, fields, exclude=exclude_rich) |
299
|
|
|
|
300
|
|
|
@wraps(add_reprs) |
301
|
|
|
def dec(cls): |
302
|
|
|
if cls.__str__ is object.__str__: |
303
|
|
|
cls.__str__ = __str |
304
|
|
|
if cls.__repr__ is object.__repr__: |
305
|
|
|
cls.__repr__ = __repr |
306
|
|
|
if not hasattr(cls, "_repr_html") and html: |
307
|
|
|
cls._repr_html_ = __html |
|
|
|
|
308
|
|
|
if not hasattr(cls, "__rich_repr__") and rich: |
309
|
|
|
cls.__rich_repr__ = __rich |
|
|
|
|
310
|
|
|
cls.__rich_repr__.angular = angular |
311
|
|
|
return cls |
312
|
|
|
|
313
|
|
|
return dec |
314
|
|
|
|
315
|
|
|
|
316
|
|
|
__all__ = [ |
317
|
|
|
"abstractmethod", |
318
|
|
|
"total_ordering", |
319
|
|
|
"final", |
320
|
|
|
"CodeStatus", |
321
|
|
|
"status", |
322
|
|
|
"CodeIncompleteError", |
323
|
|
|
"PreviewWarning", |
324
|
|
|
"CodeRemovedError", |
325
|
|
|
"add_reprs", |
326
|
|
|
"deprecated", |
327
|
|
|
"pending_deprecation", |
328
|
|
|
"incomplete", |
329
|
|
|
"preview", |
330
|
|
|
"removed", |
331
|
|
|
] |
332
|
|
|
|