Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
191 lines (150 loc) · 5.22 KB

File metadata and controls

191 lines (150 loc) · 5.22 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# coding: utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import copy
import functools
from datetime import datetime
from datetime import timedelta
import arrow
import six
import iso8601
from werkzeug.local import LocalProxy
import dateutil.tz as 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, dump_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')
if dump_objects:
return value._dump()
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, dump_objects) for x in value]
if isinstance(value, dict):
return dict([(k, encode(v, disallow_objects, dump_objects)) for k, v in six.iteritems(value)])
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 key == 'ACL':
if isinstance(value, leancloud.ACL):
return value
return leancloud.ACL(value)
if '__type' not in value:
return dict([(k, decode(k, v)) for k, v in six.iteritems(value)])
_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._update_data(value)
else:
pointer._update_data({'objectId': value['objectId']})
return pointer
if _type == 'Object':
value = copy.deepcopy(value)
class_name = value['className']
value.pop('__type')
value.pop('className')
obj = leancloud.Object.create(class_name)
obj._update_data(value)
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 _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 six.iteritems(obj):
new_child = traverse_object(child, callback, seen)
if new_child:
obj[key] = new_child
return callback(obj)
return callback(obj)
class throttle(object):
def __init__(self, seconds=0, minutes=0, hours=0):
self.throttle_period = timedelta(
seconds=seconds, minutes=minutes, hours=hours
)
self.time_of_last_call = datetime.min
def __call__(self, fn):
@functools.wraps(fn)
def wrapper(*args, **kwargs):
now = datetime.now()
time_since_last_call = now - self.time_of_last_call
if time_since_last_call > self.throttle_period:
self.time_of_last_call = now
return fn(*args, **kwargs)
return wrapper
class classproperty(object):
def __init__(self, f):
self.f = f
def __get__(self, obj, owner):
return self.f(owner)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.