Completed
Push — develop ( b4a21c...b1df34 )
by Jace
02:37
created

yorm.classproperty   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 8
Duplicated Lines 0 %

Test Coverage

Coverage 100%
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
wmc 2
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 1
logger = logging.getLogger
41 1
log = logger(__name__)
42
43
44
# DECORATORS ###################################################################
45
46
47 1
class classproperty(object):
48
    """Read-only class property decorator."""
49
50 1
    def __init__(self, getter):
51 1
        self.getter = getter
52
53 1
    def __get__(self, instance, owner):
54 1
        return self.getter(owner)
55
56
57
# FUNCTIONS ####################################################################
58
59
60 1
def get_mapper(obj):
61
    """Get the `Mapper` instance attached to an object."""
62 1
    try:
63 1
        return object.__getattribute__(obj, MAPPER)
64 1
    except AttributeError:
65 1
        return None
66
67
68 1
def set_mapper(obj, mapper):
69
    """Attach a `Mapper` instance to an object."""
70 1
    setattr(obj, MAPPER, mapper)
71
    return mapper
72