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
|
|
|
path_formats = {} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
# LOGGING ##################################################################### |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
logging.addLevelName(logging.DEBUG - 1, 'TRACE') |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
def _trace(self, message, *args, **kwargs): |
36
|
|
|
if self.isEnabledFor(logging.DEBUG - 1): |
37
|
1 |
|
# pylint: disable=protected-access |
38
|
1 |
|
self._log(logging.DEBUG - 1, message, args, **kwargs) |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
logging.Logger.trace = _trace |
42
|
|
|
|
43
|
|
|
|
44
|
1 |
|
# DECORATORS ################################################################## |
45
|
|
|
|
46
|
|
|
|
47
|
1 |
|
class classproperty: |
48
|
1 |
|
"""Read-only class property decorator.""" |
49
|
|
|
|
50
|
1 |
|
def __init__(self, getter): |
51
|
1 |
|
self.getter = getter |
52
|
|
|
|
53
|
|
|
def __get__(self, instance, owner): |
54
|
|
|
return self.getter(owner) |
55
|
|
|
|
56
|
|
|
|
57
|
1 |
|
# FUNCTIONS ################################################################### |
58
|
|
|
|
59
|
1 |
|
|
60
|
1 |
|
def get_mapper(obj, *, expected=None): |
61
|
1 |
|
"""Get the `Mapper` instance attached to an object.""" |
62
|
1 |
|
try: |
63
|
|
|
mapper = object.__getattribute__(obj, MAPPER) |
64
|
1 |
|
except AttributeError: |
65
|
1 |
|
mapper = None |
66
|
1 |
|
|
67
|
|
|
if mapper and expected is False: |
68
|
1 |
|
msg = "{!r} is already mapped".format(obj) |
69
|
1 |
|
raise TypeError(msg) |
70
|
1 |
|
|
71
|
|
|
if not mapper and expected is True: |
72
|
1 |
|
msg = "{!r} is not mapped".format(obj) |
73
|
|
|
raise TypeError(msg) |
74
|
|
|
|
75
|
1 |
|
return mapper |
76
|
|
|
|
77
|
1 |
|
|
78
|
1 |
|
def set_mapper(obj, mapper): |
79
|
|
|
"""Attach a `Mapper` instance to an object.""" |
80
|
|
|
setattr(obj, MAPPER, mapper) |
81
|
|
|
return mapper |
82
|
|
|
|