-
Notifications
You must be signed in to change notification settings - Fork 4
/
meta.py
315 lines (262 loc) · 7.47 KB
/
meta.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
313
314
315
from datetime import datetime
from .file_meta import FileMeta
import uuid
class Meta():
def __init__(self, json):
"""
Initialize the Meta class.
If certain keys, such as record_id, created, last_modified, and version
are not missing (such as when a record is created, but these variables
have not been set by the server yet).
Parameters
----------
json : dict
JSON key-value store of Meta data configuration.
Returns
-------
None
"""
# required
self.__writer_id = uuid.UUID(json['writer_id'])
self.__user_id = uuid.UUID(json['user_id'])
self.__record_type = str(json['type'])
self.__plain = json['plain'] if 'plain' in json else {}
# optional, as some get set by the server
# set these to None if they are not included when init is called.
# self.record_id = json['record_id'] if 'record_id' in json else None
if 'record_id' in json:
if json['record_id'] is not None and json['record_id'] != str(None):
self.__record_id = uuid.UUID(json['record_id'])
else:
self.__record_id = json['record_id']
else:
self.__record_id = str(None)
# When "printing" the timestamp (or converting to str), the T and Z characters are removed
# Therefore, we need to parse both formats.
try:
self.__created = datetime.strptime(json['created'], '%Y-%m-%dT%H:%M:%S.%fZ') if 'created' in json else None
except ValueError:
try:
self.__created = datetime.strptime(json['created'], '%Y-%m-%d %H:%M:%S.%f') if 'created' in json else None
except Exception:
self.__created = datetime.strptime(json['created'], '%Y-%m-%dT%H:%M:%SZ') if 'created' in json else None
try:
self.__last_modified = datetime.strptime(json['last_modified'], '%Y-%m-%dT%H:%M:%S.%fZ') if 'last_modified' in json else None
except ValueError:
try:
self.__last_modified = datetime.strptime(json['last_modified'], '%Y-%m-%d %H:%M:%S.%f') if 'last_modified' in json else None
except Exception:
self.__last_modified = datetime.strptime(json['last_modified'], '%Y-%m-%dT%H:%M:%SZ') if 'last_modified' in json else None
self.__version = json['version'] if 'version' in json else None
self.__file_meta = None
if json.get('file_meta') is not None:
self.__file_meta = FileMeta(checksum=json['file_meta'].get('checksum'),
compression=json['file_meta'].get('compression'),
size=int(json['file_meta'].get('size', "-1")),
file_url=json['file_meta'].get('file_url'),
file_name=json['file_meta'].get('file_name')
)
# writer_id getters and setters
@property
def writer_id(self):
"""
Get writer_id
Parameters
----------
None
Returns
-------
uuid.UUID
writer_id
"""
return self.__writer_id
@writer_id.setter
def writer_id(self, value):
"""
Set writer_id
Parameters
----------
value : uuid.UUID
writer id
Returns
-------
None
"""
self.__writer_id = value
# user_id getters and setters
@property
def user_id(self):
"""
Get user_id
Parameters
----------
None
Returns
-------
uuid.UUID
user_id
"""
return self.__user_id
@user_id.setter
def user_id(self, value):
"""
Set user_id
Parameters
----------
value : uuid.UUID
user_id
Returns
-------
None
"""
self.__user_id = value
# record_type getters and setters
@property
def record_type(self):
"""
Get record_type
Parameters
----------
None
Returns
-------
str
record type
"""
return self.__record_type
@record_type.setter
def record_type(self, value):
"""
Set record_type
Parameters
----------
value : str
record_type
Returns
-------
str
record type
"""
self.__record_type = value
# plain getters and setters
@property
def plain(self):
"""
Get plaintext metadata
Parameters
----------
None
Returns
-------
dict
plaintext metadata
"""
return self.__plain
@plain.setter
def plain(self, value):
"""
Set plaintext metadata
Parameters
----------
value : dict
plaintext metadata
Returns
-------
str
record type
"""
self.__plain = value
# record_id getters
@property
def record_id(self):
"""
Get record_id
Parameters
----------
None
Returns
-------
uuid.UUID
record_id
"""
return self.__record_id
# created getters
@property
def created(self):
"""
Get created time of record.
Parameters
----------
None
Returns
-------
datetime
Time record was created.
"""
return self.__created
# last_modified getters
@property
def last_modified(self):
"""
Get last_modified time of record.
Parameters
----------
None
Returns
-------
datetime
Time record was last_modified.
"""
return self.__last_modified
# version getters
@property
def version(self):
"""
Get version of record.
Parameters
----------
None
Returns
-------
str
version of the record type
"""
return self.__version
# file_meta getter
@property
def file_meta(self):
"""
Get file_meta of record if it exists
Returns
-------
File
File meta information returned when a record was uploaded with Large Files or None
"""
return self.__file_meta
def to_json(self):
"""
Serialize the configuration as JSON-style object.
Parameters
----------
None
Returns
-------
dict
JSON-style document containing the Meta elements.
"""
to_serialize = {
'record_id': str(self.__record_id),
'writer_id': str(self.__writer_id),
'user_id': str(self.__user_id),
'type': str(self.__record_type),
'plain': self.__plain,
'created': str(self.__created),
'last_modified': str(self.__last_modified),
'version': str(self.__version),
'file_meta': None if self.__file_meta is None else self.__file_meta.to_json()
}
# remove None (JSON null) objects
for key, value in list(to_serialize.items()):
if value is None or value == 'None':
del to_serialize[key]
return to_serialize