-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathmodel.py
More file actions
301 lines (225 loc) · 8.92 KB
/
model.py
File metadata and controls
301 lines (225 loc) · 8.92 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
from __future__ import absolute_import
import six
from six.moves import range
class OpenShiftPythonException(Exception):
def __init__(self, msg, result=None, **kwargs):
super(self.__class__, self).__init__(msg)
self.msg = msg
self.result = result
self.kwargs = kwargs
def attributes(self):
return dict(self.kwargs)
def get_result(self):
"""
:return: Returns the Result object associated with
this exception if any. Might be None.
"""
return self.result
def as_dict(self):
d = dict(self.kwargs)
d["msg"] = self.msg
if self.result is not None:
d["result"] = self.result
return d
def __str__(self):
if self.result is None:
return self.msg
return "[" + self.msg + "]\n" + repr(self.result)
class ModelError(Exception):
def __init__(self, msg, **kwargs):
super(self.__class__, self).__init__(msg)
self.msg = msg
self.kwargs = kwargs
class MissingModel(dict):
def __init__(self):
super(self.__class__, self).__init__()
pass
def __getattr__(self, attr):
return self
def __setattr__(self, key, value):
raise ModelError("Invalid attempt to set key(%s) in missing branch of model" % key)
def __delattr__(self, key):
raise ModelError("Invalid attempt to delete key(%s) in missing branch of model" % key)
def __getitem__(self, attr):
return self
def __setitem__(self, key, value):
raise ModelError("Invalid attempt to set key(%s) in missing branch of model" % key)
def __delitem__(self, key):
raise ModelError("Invalid attempt to delete key(%s) in missing branch of model" % key)
# Express false-y
def __bool__(self):
return False
# Express false-y
def __len__(self):
return 0
def __str__(self):
return "(MissingModelBranch)"
def __repr__(self):
return "(MissingModelBranch)"
def __div__(self, other):
return self
def __add__(self, other):
return self
def __sub__(self, other):
return self
def __mul__(self, other):
return self
def can_match(self, *vals):
return False
# Singleton which indicates if any model attribute was not defined
Missing = MissingModel()
def to_model_or_val(v, case_insensitive=False):
if isinstance(v, ListModel) or isinstance(v, Model):
return v
if isinstance(v, list):
return ListModel(v, case_insensitive=case_insensitive)
elif isinstance(v, dict):
return Model(v, case_insensitive=case_insensitive)
else:
return v
def _element_can_match( master, test, case_insensitive=False):
if master is Missing:
return False
if master is None or test is None:
return master is test
if isinstance(master, str):
master = six.text_type(master) # Turn str into unicode
if case_insensitive:
master = master.lower()
if isinstance(test, str):
test = six.text_type(test) # Turn str into unicode
if case_insensitive:
test = test.lower()
for prim in [bool, int, six.text_type, float]:
if isinstance(master, prim):
return master == test or str(master) == str(test)
if isinstance(master, dict):
if isinstance(test, dict):
return _dict_is_subset(master, test, case_insensitive=case_insensitive)
else:
return False
if isinstance(master, list):
if isinstance(test, list):
return _list_is_subset(master, test, case_insensitive=case_insensitive)
else:
return False
raise ModelError("Don't know how to compare %s and %s" % (str(type(master)), str(type(test))))
def _element_in_list(master, e, case_insensitive=False):
for m in master:
if _element_can_match(m, e, case_insensitive=case_insensitive):
return True
return False
def _list_is_subset(master, test, case_insensitive=False):
for e in test:
if not _element_in_list(master, e, case_insensitive=case_insensitive):
return False
return True
def _dict_is_subset(master, subset, case_insensitive=False):
for k, v in subset.items():
if case_insensitive:
k = k.lower()
m = master.get(k, Missing)
if not _element_can_match(m, v, case_insensitive=case_insensitive):
return False
return True
class ListModel(list):
def __init__(self, list_to_model, case_insensitive=False):
super(ListModel, self).__init__()
self.__case_insensitive = case_insensitive
if list_to_model is not None:
self.extend(list_to_model)
def __setitem__(self, key, value):
super(self.__class__, self).__setitem__(key, value)
def __delitem__(self, key):
super(self.__class__, self).__delitem__(key)
def __getitem__(self, index):
if super(self.__class__, self).__len__() > index:
v = super(self.__class__, self).__getitem__(index)
if isinstance(v, Model):
return v
v = to_model_or_val(v, case_insensitive=self.__case_insensitive)
self.__setitem__(index, v)
return v
# Otherwise, trigger out of bounds exception
return super(self.__class__, self).__getitem__(index)
def __iter__(self):
for i in range(0, super(self.__class__, self).__len__()):
yield self[i]
def _primitive(self):
"""
:return: Returns the ListModel as a python list
:rtype: list
"""
l = []
for e in self:
l.append(e)
return l
def can_match(self, list_or_entry):
"""
Answers whether this list is a subset of the specified list. If the argument is not a list,
it placed into one for comparison purposes.
Elements of the argument list can be primitives, lists, or dicts. In the case of non-primitives, the list or
dicts must ultimately be subsets of at least one element in the receiver list.
:param list_or_entry: The list to compare or a primitive/dict that must exist in the receiver's list.
:return: Returns true if all of the elements specify can match (i.e. are subsets of) elements of this list.
"""
if not isinstance(list_or_entry, (list, tuple, ListModel)):
# If we were not passed a list, turn it into one
list_or_entry = [list_or_entry]
return _list_is_subset(self, list_or_entry, case_insensitive=self.__case_insensitive)
class Model(dict):
def __init__(self, dict_to_model=None, case_insensitive=False):
super(Model, self).__init__()
self.__case_insensitive = case_insensitive
if dict_to_model is not None:
for k, v in dict_to_model.items():
if self.__case_insensitive:
k = k.lower()
self[k] = to_model_or_val(v, case_insensitive=case_insensitive)
def __getattr__(self, attr):
if isinstance(attr, six.string_types):
if attr.startswith('_Model__'): # e.g. _Model__case_insensitive
raise AttributeError
if self.__case_insensitive:
attr = attr.lower()
if super(Model, self).__contains__(attr):
v = super(self.__class__, self).get(attr)
if isinstance(v, Model) or isinstance(v, ListModel):
return v
v = to_model_or_val(v, self.__case_insensitive)
self.__setattr__(attr, v)
return v
else:
return Missing
def __setattr__(self, key, value):
if key.startswith('_Model__'): # e.g. _Model__case_insensitive
return super(Model, self).__setattr__(key, value)
if self.__case_insensitive:
key = key.lower()
self.__setitem__(key, value)
def __getitem__(self, key):
return self.__getattr__(key)
def __setitem__(self, key, value):
super(Model, self).__setitem__(key, to_model_or_val(value, case_insensitive=self.__case_insensitive))
def __delitem__(self, key):
if self.__is_case_sensitive__():
key = key.lower()
super(Model, self).__delitem__(key)
def _primitive(self):
"""
:return: Returns the Model as a python dict
:rtype: dict
"""
d = {}
for k, v in six.iteritems(self):
if isinstance(v, Model) or isinstance(v, ListModel):
v = v._primitive()
d[k] = v
return d
def can_match(self, val):
"""
Answers whether this Model matches all elements of the argument.
:param val: A dict or Model with elements set that must be found within this model.
:return: Returns true if all of the elements can match (i.e. are subsets of) elements of this list.
"""
return _dict_is_subset(self, val, case_insensitive=self.__case_insensitive)