-
Notifications
You must be signed in to change notification settings - Fork 29
/
storage.py
52 lines (39 loc) · 1.76 KB
/
storage.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
import json
import sys
from dateutil.parser import parse
def _update_attributes(obj, d, keys, default=None):
"""Updates the attributes of `obj` with keys from `d` in camelCase. """
for key in keys:
parts = key.split('_')
camel_case = parts[0] + ''.join(map(str.title, parts[1:]))
setattr(obj, key, d.get(camel_case, default))
class ACL:
__ATTRIBUTES = (
'kind', 'id', 'self_link', 'bucket', 'object', 'generation', 'entity',
'role', 'email', 'entity_id', 'domain', 'project_team', 'etag'
)
def __init__(self, raw_json):
_update_attributes(self, raw_json, self.__ATTRIBUTES)
class Object:
__ATTRIBUTES = (
'kind', 'id', 'self_link', 'bucket', 'name', 'generation',
'metageneration', 'content_type', 'time_created', 'updated',
'time_deleted', 'storage_class', 'time_storage_class_updated', 'size',
'md5_hash', 'media_link', 'content_encoding', 'content_disposition',
'content_language', 'cache_control', 'metadata', 'owner', 'crc32c',
'component_count', 'customer_encryption'
)
def __init__(self, raw_json):
_update_attributes(self, raw_json, self.__ATTRIBUTES)
if self.time_created is not None:
self.time_created = parse(self.time_created)
if self.updated is not None:
self.updated = parse(self.updated)
if self.time_deleted is not None:
self.time_deleted = parse(self.time_deleted)
if self.time_storage_class_updated is not None:
self.time_storage_class_updated = \
parse(self.time_storage_class_updated)
self.acl = list(map(ACL, raw_json.get('acl') or []))
def handle_bucket_event(handle_fn):
handle_fn(Object(json.loads(sys.stdin.read())))