Completed
Pull Request — develop (#125)
by Jace
09:08
created

Mappable.insert()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
crap 1
1
"""Base classes for mapping."""
2
3 1
import abc
4 1
import functools
5 1
import logging
6
7 1
from .. import common
8
9 1
log = logging.getLogger(__name__)
10
11 1
_LOAD_BEFORE_METHODS = set()
12
_STORE_AFTER_METHODS = set()
13
14 1
15 View Code Duplication
def load_before(method):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
16
    """Decorator for methods that should load before call."""
17 1
18 1
    if getattr(method, '_load_before', False):
19
        return method
20 1
21
    _LOAD_BEFORE_METHODS.add(method.__name__)
22
23 1
    @functools.wraps(method)
24 1
    def wrapped(self, *args, **kwargs):
25 1
        """Decorated method."""
26 1
        if not _private_call(method, args):
27 1
            mapper = common.get_mapper(self)
28 1
            if mapper and mapper.modified:
29 1
                log.debug("Loading before call: %s", method.__name__)
30 1
                mapper.load()
31
                if mapper.auto_save_after_load:
32 1
                    mapper.save()
33
                    mapper.modified = False
34 1
35
        return method(self, *args, **kwargs)
36 1
37
    setattr(wrapped, '_load_before', True)
38
39 1
    return wrapped
40
41
42 1 View Code Duplication
def save_after(method):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
43 1
    """Decorator for methods that should save after call."""
44
45 1
    if getattr(method, '_save_after', False):
46
        return method
47
48 1
    _STORE_AFTER_METHODS.add(method.__name__)
49
50 1
    @functools.wraps(method)
51 1
    def wrapped(self, *args, **kwargs):
52 1
        """Decorated method."""
53 1
        result = method(self, *args, **kwargs)
54 1
55
        if not _private_call(method, args):
56 1
            mapper = common.get_mapper(self)
57
            if mapper and mapper.auto_save:
58 1
                log.debug("Saving after call: %s", method.__name__)
59
                mapper.save()
60 1
61
        return result
62
63 1
    setattr(wrapped, '_save_after', True)
64
65 1
    return wrapped
66 1
67 1
68
def _private_call(method, args, prefix='_'):
69 1
    """Determine if a call's first argument is a private variable name."""
70
    if method.__name__ in ('__getattribute__', '__setattr__'):
71
        assert isinstance(args[0], str)
72
        return args[0].startswith(prefix)
73 1
    else:
74
        return False
75
76
77
class Mappable(metaclass=abc.ABCMeta):
78 1
    """Base class for objects with attributes mapped to file."""
79
80
    # pylint: disable=no-member
81 1
82
    @load_before
83 1
    def __getattribute__(self, name):
84
        return object.__getattribute__(self, name)
85
86 1
    @save_after
87
    def __setattr__(self, name, value):
88 1
        super().__setattr__(name, value)
89
90
    @load_before
91 1
    def __iter__(self):
92
        return super().__iter__()
93 1
94
    @load_before
95
    def __getitem__(self, key):
96 1
        return super().__getitem__(key)
97
98 1
    @save_after
99
    def __setitem__(self, key, value):
100
        super().__setitem__(key, value)
101 1
102
    @save_after
103 1
    def __delitem__(self, key):
104
        super().__delitem__(key)
105
106 1
    @load_before
107
    @save_after
108 1
    def append(self, *args, **kwargs):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
109
        super().append(*args, **kwargs)
110
111 1
    @load_before
112
    @save_after
113
    def extend(self, *args, **kwargs):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
114 1
        super().extend(*args, **kwargs)
115 1
116 1
    @load_before
117
    @save_after
118
    def insert(self, *args, **kwargs):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
119 1
        super().insert(*args, **kwargs)
120 1
121 1
    @load_before
122 1
    @save_after
123 1
    def remove(self, *args, **kwargs):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
124
        super().remove(*args, **kwargs)
125 1
126 1
    @load_before
127 1
    @save_after
128
    def pop(self, *args, **kwargs):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
129 1
        super().pop(*args, **kwargs)
130 1
131 1
    @save_after
132 1
    def clear(self, *args, **kwargs):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
133 1
        super().clear(*args, **kwargs)
134
135 1
    @load_before
136 1
    @save_after
137 1
    def sort(self, *args, **kwargs):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
138
        super().sort(*args, **kwargs)
139
140
    @load_before
141
    @save_after
142
    def reverse(self, *args, **kwargs):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
143
        super().reverse(*args, **kwargs)
144
145
    @load_before
146
    @save_after
147
    def popitem(self, *args, **kwargs):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
148
        super().popitem(*args, **kwargs)
149
150
    @load_before
151
    @save_after
152
    def update(self, *args, **kwargs):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
153
        super().update(*args, **kwargs)
154
155
156
def patch_methods(instance):
157
    log.debug("Patching methods on: %r", instance)
158
    cls = instance.__class__
159
160
    for name in _LOAD_BEFORE_METHODS:
161
        try:
162
            method = getattr(cls, name)
163
        except AttributeError:
164
            log.trace("No method: %s", name)
165
        else:
166
            modified_method = load_before(method)
167
            setattr(cls, name, modified_method)
168
            log.trace("Patched to load before call: %s", name)
169
170
    for name in _STORE_AFTER_METHODS:
171
        try:
172
            method = getattr(cls, name)
173
        except AttributeError:
174
            log.trace("No method: %s", name)
175
        else:
176
            modified_method = save_after(method)
177
            setattr(cls, name, modified_method)
178
            log.trace("Patched to save after call: %s", name)
179