-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathbase.py
More file actions
155 lines (121 loc) · 5.4 KB
/
base.py
File metadata and controls
155 lines (121 loc) · 5.4 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
# Copyright (c) 2015-2019 The Botogram Authors (see AUTHORS)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
# This is used to make a reference to the current class while defining the
# fields table, since it's impossible to reference the class while defining it
_itself = object()
class BaseObject:
"""A base class for all of the API types"""
required = {}
optional = {}
replace_keys = {}
_check_equality_ = None
def __init__(self, data, api=None):
# Prevent receiving strange types
if not isinstance(data, dict):
raise ValueError("A dict must be provided")
# Populate the namespace
for group, required in ((self.required, True), (self.optional, False)):
for key, field_type in group.items():
# A required key must be present
if key not in data and required:
raise ValueError("The key %s must be present" % key)
# If the field type is _itself, replace it with this class
if field_type is _itself:
field_type = self.__class__
# Replace the keys -- useful for reserved keywords
new_key = key
if key in self.replace_keys:
new_key = self.replace_keys[key]
# Don't resolve non-present keys
if key not in data:
setattr(self, new_key, None)
continue
# It's important to note that the value is validated passing
# it in the field_type. This allows also automatic resolution
# of types nesting
setattr(self, new_key, field_type(data[key]))
if api is not None:
self.set_api(api)
def __eq__(self, other):
to_check = self._check_equality_
if to_check is None:
return id(self) == id(other)
if not isinstance(other, self.__class__):
return False
return getattr(self, to_check) == getattr(other, to_check)
def set_api(self, api):
"""Change the API instance"""
self._api = api
# Recursively set the API
for key in list(self.required.keys()) + list(self.optional.keys()):
# Be sure to use the right key
if key in self.replace_keys:
key = self.replace_keys[key]
value = getattr(self, key)
if value is None:
continue
# Update the API, if it supports that
if hasattr(value, "set_api"):
value.set_api(api)
def serialize(self):
"""Serialize this object"""
result = {}
for group, required in ((self.required, True), (self.optional, False)):
for key, field_type in group.items():
# Replace the keys -- useful for reserved keywords
new_key = key
if key in self.replace_keys:
new_key = self.replace_keys[key]
# A required key must be present
if not hasattr(self, new_key) and required:
raise ValueError("The key %s must be present" % new_key)
# Optional keys not present will be ignored
if not hasattr(self, new_key):
continue
# Empty keys will be ignored
if getattr(self, new_key) is None:
continue
result[key] = self._serialize_one(getattr(self, new_key))
return result
def _serialize_one(self, item):
"""Serialize one item"""
if isinstance(item, BaseObject):
return item.serialize()
if isinstance(item, list):
result = []
for one in item:
result.append(self._serialize_one(one))
return result
return item
class _MultipleList(list):
"""Custom list which adds the set_api method"""
def set_api(self, api):
"""Set the API on a multiple() result"""
for item in self:
if hasattr(item, "set_api"):
item.set_api(api)
def multiple(field_type):
"""_Accept a list of objects"""
def __(objects):
if not isinstance(objects, list):
raise ValueError("multiple(%r) needs a list of objects"
% field_type)
return _MultipleList([field_type(item) for item in objects])
return __