1
|
|
|
"""Shared internal classes and functions.""" |
2
|
|
|
|
3
|
1 |
|
import collections |
4
|
1 |
|
import logging |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
# CONSTANTS #################################################################### |
8
|
|
|
|
9
|
1 |
|
MAPPER = '__mapper__' |
10
|
1 |
|
ALL = 'all' |
11
|
1 |
|
UUID = 'UUID' |
12
|
|
|
|
13
|
1 |
|
PRINT_VERBOSITY = 0 # minimum verbosity to using `print` |
14
|
1 |
|
STR_VERBOSITY = 3 # minimum verbosity to use verbose `__str__` |
15
|
1 |
|
MAX_VERBOSITY = 4 # maximum verbosity level implemented |
16
|
|
|
|
17
|
1 |
|
OVERRIDE_MESSAGE = "Method must be implemented in subclasses" |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
# GLOBALS ###################################################################### |
21
|
|
|
|
22
|
|
|
|
23
|
1 |
|
verbosity = 0 # global verbosity setting for controlling string formatting |
24
|
|
|
|
25
|
1 |
|
attrs = collections.defaultdict(collections.OrderedDict) |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
# LOGGING ###################################################################### |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
def _trace(self, message, *args, **kwargs): # pragma: no cover (manual test) |
32
|
|
|
"""Handler for a new TRACE logging level.""" |
33
|
|
|
if self.isEnabledFor(logging.DEBUG - 1): |
34
|
|
|
self._log(logging.DEBUG - 1, message, args, **kwargs) # pylint: disable=protected-access |
35
|
|
|
|
36
|
|
|
|
37
|
1 |
|
logging.addLevelName(logging.DEBUG - 1, "TRACE") |
38
|
1 |
|
logging.Logger.trace = _trace |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
# DECORATORS ################################################################### |
42
|
|
|
|
43
|
|
|
|
44
|
1 |
|
class classproperty(object): |
45
|
|
|
"""Read-only class property decorator.""" |
46
|
|
|
|
47
|
1 |
|
def __init__(self, getter): |
48
|
1 |
|
self.getter = getter |
49
|
|
|
|
50
|
1 |
|
def __get__(self, instance, owner): |
51
|
1 |
|
return self.getter(owner) |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
# FUNCTIONS #################################################################### |
55
|
|
|
|
56
|
|
|
|
57
|
1 |
|
def get_mapper(obj, *, expected=None): |
58
|
|
|
"""Get the `Mapper` instance attached to an object.""" |
59
|
1 |
|
try: |
60
|
1 |
|
mapper = object.__getattribute__(obj, MAPPER) |
61
|
1 |
|
except AttributeError: |
62
|
1 |
|
mapper = None |
63
|
|
|
|
64
|
1 |
|
if mapper and expected is False: |
65
|
1 |
|
msg = "{!r} is already mapped".format(obj) |
66
|
1 |
|
raise TypeError(msg) |
67
|
|
|
|
68
|
1 |
|
if not mapper and expected is True: |
69
|
1 |
|
msg = "{!r} is not mapped".format(obj) |
70
|
1 |
|
raise TypeError(msg) |
71
|
|
|
|
72
|
1 |
|
return mapper |
73
|
|
|
|
74
|
|
|
|
75
|
1 |
|
def set_mapper(obj, mapper): |
76
|
|
|
"""Attach a `Mapper` instance to an object.""" |
77
|
1 |
|
setattr(obj, MAPPER, mapper) |
78
|
|
|
return mapper |
79
|
|
|
|