-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathimmutable.py
312 lines (231 loc) · 10.5 KB
/
immutable.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
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
302
303
304
305
306
307
308
309
310
311
312
# -*- coding: utf-8 -*-
"""Model Webex Teams JSON objects as native Python objects.
Classes:
ImmutableData: Models Webex Teams JSON objects as native Python objects.
The ImmutableData class models any JSON object passed to it as a string or
Python dictionary as a native Python object; providing attribute access using
native dot-syntax (`object.attribute`).
Copyright (c) 2016-2019 Cisco and/or its affiliates.
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.
"""
from __future__ import (
absolute_import, absolute_import, division, division,
print_function, print_function, unicode_literals, unicode_literals,
)
from builtins import *
import json
from collections import defaultdict
from webexteamssdk.utils import json_dict
from .mixins.access_token import AccessTokenBasicPropertiesMixin
from .mixins.admin_audit_event import (
AdminAuditEventBasicPropertiesMixin,
AdminAuditEventDataBasicPropertiesMixin,
)
from .mixins.attachment_action import AttachmentActionBasicPropertiesMixin
from .mixins.event import EventBasicPropertiesMixin
from .mixins.guest_issuer_token import GuestIssuerTokenBasicPropertiesMixin
from .mixins.license import LicenseBasicPropertiesMixin
from .mixins.membership import MembershipBasicPropertiesMixin
from .mixins.message import MessageBasicPropertiesMixin
from .mixins.organization import OrganizationBasicPropertiesMixin
from .mixins.person import PersonBasicPropertiesMixin
from .mixins.role import RoleBasicPropertiesMixin
from .mixins.room import RoomBasicPropertiesMixin
from .mixins.room_meeting_info import RoomMeetingInfoBasicPropertiesMixin
from .mixins.team import TeamBasicPropertiesMixin
from .mixins.team_membership import TeamMembershipBasicPropertiesMixin
from .mixins.webhook import WebhookBasicPropertiesMixin
from .mixins.webhook_event import WebhookEventBasicPropertiesMixin
class ImmutableData(object):
"""Model a Webex Teams JSON object as an immutable native Python object."""
def __init__(self, json_data):
"""Init a new ImmutableData object from a dictionary or JSON string.
Args:
json_data(dict, basestring): Input JSON string or dictionary.
Raises:
TypeError: If the input object is not a dictionary or string.
"""
super(ImmutableData, self).__init__()
self._json_data = json_dict(json_data)
def __getattr__(self, item):
"""Provide native attribute access to the JSON object attributes.
This method is called when attempting to access a object attribute that
hasn't been defined for the object. For example trying to access
object.attribute1 when attribute1 hasn't been defined.
ImmutableData.__getattr__() checks the original JSON object to see if
the attribute exists, and if it does, it returns the attribute's value
from the original JSON object. This provides native access to all of
the JSON object's attributes.
Args:
item(str): Name of the Attribute being accessed.
Raises:
AttributeError: If the JSON object does not contain the attribute
requested.
"""
if item in list(self._json_data.keys()):
item_data = self._json_data[item]
if isinstance(item_data, dict):
return ImmutableData(item_data)
else:
return item_data
else:
raise AttributeError(
"'{}' object has no attribute '{}'"
"".format(self.__class__.__name__, item)
)
def __str__(self):
"""A human-readable string representation of this object."""
class_str = self.__class__.__name__
json_str = json.dumps(self._json_data, indent=2)
return "Webex Teams {}:\n{}".format(class_str, json_str)
def __repr__(self):
"""A string representing this object as valid Python expression."""
class_str = self.__class__.__name__
json_str = json.dumps(self._json_data, ensure_ascii=False)
return "{}({})".format(class_str, repr(json_str))
@classmethod
def _serialize(cls, data):
"""Serialize data to an frozen tuple."""
if hasattr(data, "__hash__") and callable(data.__hash__):
# If the data is already hashable (should be immutable) return it
return data
elif isinstance(data, list):
# Freeze the elements of the list and return as a tuple
return tuple((cls._serialize(item) for item in data))
elif isinstance(data, dict):
# Freeze the elements of the dictionary, sort them, and return
# them as a list of tuples
key_value_tuples = [
(key, cls._serialize(value))
for key, value in data.items()
]
key_value_tuples.sort()
return tuple(key_value_tuples)
else:
raise TypeError(
"Unable to freeze {} data type.".format(type(data))
)
def _freeze(self):
"""Freeze this object's JSON data."""
return self._serialize(self._json_data)
def __eq__(self, other):
"""Determine if two objects are equal."""
return isinstance(other, self.__class__) \
and self._freeze() == other._freeze()
def __hash__(self):
"""Hash the data object."""
return hash(self._freeze())
@property
def json_data(self):
"""A copy of the data object's JSON data (OrderedDict)."""
# TODO: When we move to Python v3+ only; use MappingProxyType.
return self._json_data.copy()
def to_dict(self):
"""Convert the Webex Teams object data to a dictionary."""
return dict(self._json_data)
def to_json(self, **kwargs):
"""Convert the Webex Teams object data to JSON.
Any keyword arguments provided are passed through the Python JSON
encoder.
"""
return json.dumps(self._json_data, **kwargs)
class AccessToken(ImmutableData, AccessTokenBasicPropertiesMixin):
"""Webex Teams Access-Token data model."""
class AdminAuditEventData(ImmutableData,
AdminAuditEventDataBasicPropertiesMixin):
"""Webex Teams Admin Audit Event Data object data model."""
class AdminAuditEvent(ImmutableData, AdminAuditEventBasicPropertiesMixin):
"""Webex Teams Admin Audit Event data model."""
@property
def data(self):
"""The event resource data."""
return AdminAuditEventData(self._json_data.get('data'))
class AttachmentAction(ImmutableData, AttachmentActionBasicPropertiesMixin):
"""Webex Attachment Actions data model"""
class Event(ImmutableData, EventBasicPropertiesMixin):
"""Webex Teams Event data model."""
@property
def data(self):
"""The event’s data representation.
This object will contain the event's resource, such as memberships or
messages, at the time the event took place.
"""
return ImmutableData(self._json_data.get('data'))
class License(ImmutableData, LicenseBasicPropertiesMixin):
"""Webex Teams License data model."""
class Membership(ImmutableData, MembershipBasicPropertiesMixin):
"""Webex Teams Membership data model."""
class Message(ImmutableData, MessageBasicPropertiesMixin):
"""Webex Teams Message data model."""
class Organization(ImmutableData, OrganizationBasicPropertiesMixin):
"""Webex Teams Organization data model."""
class Person(ImmutableData, PersonBasicPropertiesMixin):
"""Webex Teams Person data model."""
class Role(ImmutableData, RoleBasicPropertiesMixin):
"""Webex Teams Role data model."""
class Room(ImmutableData, RoomBasicPropertiesMixin):
"""Webex Teams Room data model."""
class RoomMeetingInfo(ImmutableData, RoomMeetingInfoBasicPropertiesMixin):
"""Webex Teams Room Meeting Info data model."""
class Team(ImmutableData, TeamBasicPropertiesMixin):
"""Webex Teams Team data model."""
class TeamMembership(ImmutableData, TeamMembershipBasicPropertiesMixin):
"""Webex Teams Team-Membership data model."""
class Webhook(ImmutableData, WebhookBasicPropertiesMixin):
"""Webex Teams Webhook data model."""
class WebhookEvent(ImmutableData, WebhookEventBasicPropertiesMixin):
"""Webex Teams Webhook-Events data model."""
@property
def data(self):
"""The event resource data."""
return ImmutableData(self._json_data.get('data'))
class GuestIssuerToken(ImmutableData, GuestIssuerTokenBasicPropertiesMixin):
"""Webex Teams Guest Issuer Token data model"""
immutable_data_models = defaultdict(
lambda: ImmutableData,
access_token=AccessToken,
admin_audit_event=AdminAuditEvent,
attachment_action=AttachmentAction,
event=Event,
license=License,
membership=Membership,
message=Message,
organization=Organization,
person=Person,
role=Role,
room=Room,
room_meeting_info=RoomMeetingInfo,
team=Team,
team_membership=TeamMembership,
webhook=Webhook,
webhook_event=WebhookEvent,
guest_issuer_token=GuestIssuerToken,
)
def immutable_data_factory(model, json_data):
"""Factory function for creating ImmutableData objects.
Args:
model(basestring): The data model to use when creating the
ImmutableData object (message, room, membership, etc.).
json_data(basestring, dict): The JSON string or dictionary data with
which to initialize the object.
Returns:
ImmutableData: The created ImmutableData object.
Raises:
TypeError: If the json_data parameter is not a JSON string or
dictionary.
"""
return immutable_data_models[model](json_data)