/
base.py
93 lines (71 loc) · 2.22 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from datetime import datetime
def attr_to_str(value):
if not value:
return None
return value[0]
def attr_to_list(value):
return value or []
def attr_to_bool(value):
if not value:
return False
return value[0] in (True, "TRUE")
def attr_to_date(value):
if value is None:
return None
dt = value[0]["__datetime__"]
return datetime.strptime(dt, r"%Y%m%d%H%M%SZ")
CONVERTERS = {
"str": attr_to_str,
"list": attr_to_list,
"bool": attr_to_bool,
"date": attr_to_date,
}
class Representation:
attr_names = {}
attr_types = {}
attr_options = {}
pkey = None
def __init__(self, raw):
self.raw = raw
def __getattr__(self, key):
try:
ipa_key = self.attr_names[key]
except KeyError:
raise AttributeError(key)
attr_type = self.attr_types.get(key, "str")
converter = CONVERTERS[attr_type]
return converter(self.raw.get(ipa_key))
def __iter__(self):
yield from self.attr_names.keys()
def __eq__(self, other):
return isinstance(other, self.__class__) and self.raw == other.raw
def __hash__(self):
return hash(self.dn)
@property
def dn(self):
return self.raw.get("dn")
@classmethod
def get_ipa_pkey(cls):
if cls.pkey is None:
raise NotImplementedError
try:
return cls.attr_names[cls.pkey]
except KeyError:
raise NotImplementedError
def diff_fields(self, other):
"""
Compares two instances of the same class, and returns the properties
with values that are different
"""
if not isinstance(other, self.__class__):
raise ValueError(
f"Can't diff a {self.__class__.__name__} instance against a "
f"{other.__class__.__name__} instance"
)
return [key for key in self if getattr(self, key) != getattr(other, key)]
def as_dict(self):
return {attr: getattr(self, attr) for attr in self.attr_names}
def get_attr_option(self, attr):
attr_to_options = self.attr_names.copy()
attr_to_options.update(self.attr_options)
return attr_to_options[attr]