Total Complexity | 8 |
Total Lines | 36 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | import functools |
||
2 | |||
3 | from . import _attribute_constructor |
||
4 | |||
5 | |||
6 | class ConditionalMethod: |
||
7 | name = None |
||
8 | __objclass__ = None |
||
9 | |||
10 | def __init__(self, source, field_check): |
||
11 | self.source = source |
||
12 | self.field_check = field_check |
||
13 | |||
14 | def __set_name__(self, owner, name): |
||
15 | self.__objclass__ = owner |
||
16 | self.name = name |
||
17 | |||
18 | def __get__(self, instance, owner): |
||
19 | if getattr(owner, self.field_check): |
||
20 | return getattr(self.source, self.name).__get__(instance, owner) |
||
21 | target = owner if instance is None else instance |
||
22 | return getattr(super(self.__objclass__, target), self.name) |
||
23 | |||
24 | def __set__(self, instance, value): |
||
25 | # Don't care about this coverage |
||
26 | raise AttributeError # pragma: nocover |
||
27 | |||
28 | def __delete__(self, instance): |
||
29 | # Don't care about this coverage |
||
30 | raise AttributeError # pragma: nocover |
||
31 | |||
32 | |||
33 | def conditional_method(source): |
||
34 | return _attribute_constructor.AttributeConstructor( |
||
35 | functools.partial(ConditionalMethod, source) |
||
36 | ) |
||
37 |