|
1
|
|
|
#!/usr/bin/env python |
|
2
|
|
|
|
|
3
|
|
|
__all__ = [ |
|
4
|
|
|
'ResultSets' |
|
5
|
|
|
] |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
FIELD_LIST_MAP = { |
|
9
|
|
|
'Room': ['id', 'title', 'type', 'isLocked', |
|
10
|
|
|
'lastActivity', 'created', 'creatorId', |
|
11
|
|
|
'teamId'], |
|
12
|
|
|
'Team': ['id', 'name', 'created'], |
|
13
|
|
|
'Webhook': ['id', 'name', 'targetUrl', 'resource', |
|
14
|
|
|
'event', 'filter', 'secret', 'created', |
|
15
|
|
|
'data'], |
|
16
|
|
|
'Message': ['id', 'roomId', 'roomType', 'toPersonId', 'toPersonEmail', |
|
17
|
|
|
'text', 'markdown', 'files', 'personId', 'personEmail', |
|
18
|
|
|
'created', 'mentionedPeople'] |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
class ResultSets(object): |
|
23
|
|
|
|
|
24
|
|
|
def selector(self, output): |
|
25
|
|
|
try: |
|
26
|
|
|
field_list = FIELD_LIST_MAP.get(output.__name__) |
|
27
|
|
|
return self.parse(output, field_list) |
|
28
|
|
|
except KeyError: |
|
29
|
|
|
return output |
|
30
|
|
|
|
|
31
|
|
|
def formatter(self, output): |
|
32
|
|
|
formatted = [] |
|
33
|
|
|
if isinstance(output, list): |
|
34
|
|
|
for o in output: |
|
35
|
|
|
formatted.append(self.selector(o)) |
|
36
|
|
|
else: |
|
37
|
|
|
formatted = self.selector(output) |
|
38
|
|
|
return formatted |
|
39
|
|
|
|
|
40
|
|
|
def _getval(self, obj, field): |
|
41
|
|
|
return self.selector(getattr(obj, field)) |
|
42
|
|
|
|
|
43
|
|
|
def parse(self, output, field_list): |
|
44
|
|
|
instance_data = {field: self._getval(output, field) for field in field_list} |
|
45
|
|
|
return instance_data |
|
46
|
|
|
|