forked from leancloud/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
188 lines (146 loc) · 5.02 KB
/
Copy pathutils.py
File metadata and controls
188 lines (146 loc) · 5.02 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
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
# coding: utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import copy
import json
import gzip
import hashlib
import hmac
from datetime import datetime
import arrow
import iso8601
import six
from werkzeug import LocalProxy
from dateutil import tz
import leancloud
from leancloud import operation
__author__ = 'asaka <lan@leancloud.rocks>'
def get_dumpable_types():
return (
leancloud.ACL,
leancloud.File,
leancloud.GeoPoint,
leancloud.Relation,
operation.BaseOp,
)
def encode(value, disallow_objects=False):
if isinstance(value, LocalProxy):
value = value._get_current_object()
if isinstance(value, datetime):
tzinfo = value.tzinfo
if tzinfo is None:
tzinfo = tz.tzlocal()
return {
'__type': 'Date',
'iso': arrow.get(value, tzinfo).to('utc').format('YYYY-MM-DDTHH:mm:ss.SSS') + 'Z',
}
if isinstance(value, leancloud.Object):
if disallow_objects:
raise ValueError('leancloud.Object not allowed')
return value._to_pointer()
if isinstance(value, leancloud.File):
if not value.url and not value.id:
raise ValueError('Tried to save an object containing an unsaved file.')
return {
'__type': 'File',
'id': value.id,
'name': value.name,
'url': value.url,
}
if isinstance(value, get_dumpable_types()):
return value.dump()
if isinstance(value, (tuple, list)):
return [encode(x, disallow_objects) for x in value]
if isinstance(value, dict):
return dict([(k, encode(v, disallow_objects)) for k, v in value.iteritems()])
return value
def decode(key, value):
if isinstance(value, get_dumpable_types()):
return value
if isinstance(value, (tuple, list)):
return [decode(key, x) for x in value]
if not isinstance(value, dict):
return value
if '__type' not in value:
return dict([(k, decode(k, v)) for k, v in value.iteritems()])
_type = value['__type']
if _type == 'Pointer':
value = copy.deepcopy(value)
class_name = value['className']
pointer = leancloud.Object.create(class_name)
if 'createdAt' in value:
value.pop('__type')
value.pop('className')
pointer._finish_fetch(value, True)
else:
pointer._finish_fetch({'objectId': value['objectId']}, False)
return pointer
if _type == 'Object':
value = copy.deepcopy(value)
class_name = value['className']
value.pop('__type')
value.pop('class_name')
obj = leancloud.Object.create(class_name)
obj._finish_fetch(value, True)
return obj
if _type == 'Date':
return arrow.get(iso8601.parse_date(value['iso'])).to('local').datetime
if _type == 'GeoPoint':
return leancloud.GeoPoint(latitude=value['latitude'], longitude=value['longitude'])
if key == 'ACL':
if isinstance(value, leancloud.ACL):
return value
return leancloud.ACL(value)
if _type == 'Relation':
relation = leancloud.Relation(None, key)
relation.target_class_name = value['className']
return relation
if _type == 'File':
f = leancloud.File(value['name'])
meta_data = value.get('metaData')
if meta_data:
f._metadata = meta_data
f._url = value['url']
f.id = value['objectId']
return f
def traverse_object(obj, callback, seen=None):
seen = seen or set()
if isinstance(obj, leancloud.Object):
if obj in seen:
return
seen.add(obj)
traverse_object(obj._attributes, callback, seen)
return callback(obj)
if isinstance(obj, (leancloud.Relation, leancloud.File)):
return callback(obj)
if isinstance(obj, (list, tuple)):
for idx, child in enumerate(obj):
new_child = traverse_object(child, callback, seen)
if new_child:
obj[idx] = new_child
return callback(obj)
if isinstance(obj, dict):
for key, child in obj.iteritems():
new_child = traverse_object(child, callback, seen)
if new_child:
obj[key] = new_child
return callback(obj)
return callback(obj)
def response_to_json(response):
"""
hack for requests in python 2.6
"""
content = response.content
# hack for requests in python 2.6
if 'application/json' in response.headers['Content-Type']:
if content[:2] == '\x1f\x8b': # gzip file magic header
f = six.StringIO(content)
g = gzip.GzipFile(fileobj=f)
content = g.read()
g.close()
f.close()
return json.loads(content)
def sign_disable_hook(hook_name, master_key, timestamp):
sign = hmac.new(master_key, '{}:{}'.format(hook_name, timestamp), hashlib.sha1).hexdigest()
return '{},{}'.format(timestamp, sign)