forked from 3wnbr1/transit-python2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.py
463 lines (427 loc) · 18.9 KB
/
decoder.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
## Copyright 2014 Cognitect. All Rights Reserved.
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS-IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
X_FIX_ARRAY = 1
X_mapkeystr = 0 #treat map-keys separately from just Keyword .. no need if all Keywords are treated-same
_X_mapkeystr = 'mapkeystr' if X_mapkeystr else True
X_mapcompreh= 1 #as-of timing-probi.. dictcomp is a-bit-faster than {}+loop , listcomp is a-bit-faster than []+loop
#X_tuple_via_listcomp =1 #unclear gain.. into X_FIX_ARRAY
X_decode_map =0 #slower
#X_decode_as_subscr =0 #cannot, 3 args #a[b] is faster than a.method(b)
#X_decode_self_cache =0 ?? less args= less noise but same speed probably or slower
X_is_cache_key_as_subscr =0 #no much gain.. #a[b] is faster than a.method(b)
X_is_cache_key_eq_in_cache =1 #better this.. avoid is_cache_key() at all
X_parse_string = 1
X_escaped_first= 1
X_decoders_direct =1 # remove klass.from_rep , direct funcs instead
X_tag_in_decoders =1 #??
X_decode_tag = 1
X_decode_str_with_parse =1 #only done with RollingCache.X_rework, X_tag_in_decoders, no X_mapkeystr
X_decode_bytes_last = 1
X_decode_no_bytes = 0 #tests need bytes :/
from collections import OrderedDict
from transit import transit_types
from transit import read_handlers as rh
from transit.helpers import pairs
from transit.transit_types import true, false
from transit.constants import MAP_AS_ARR, ESC, SUB, RES
from transit.rolling_cache import RollingCache, is_cacheable, is_cache_key
if X_is_cache_key_as_subscr:
class _is_cache_key:
__getitem__ = staticmethod( is_cache_key)
is_cache_key_as_subscr = _is_cache_key() #singleton
class _is_cacheable:
@staticmethod
def __getitem__( args ):
return is_cacheable( *args)
is_cacheable_as_subscr = _is_cacheable() #singleton
assert getattr( rh, 'X_plain', 0) and X_decoders_direct
class Tag(object):
def __init__(self, tag):
self.tag = tag
default_options = {
"decoders": {
"_": rh.NoneHandler,
":": rh.KeywordHandler,
"$": rh.SymbolHandler,
"?": rh.BooleanHandler,
"i": rh.IntHandler,
"d": rh.FloatHandler,
"f": rh.BigDecimalHandler,
"u": rh.UuidHandler,
"r": rh.UriHandler,
"t": rh.DateHandler,
"m": rh.DateHandler,
"n": rh.BigIntegerHandler,
"z": rh.SpecialNumbersHandler,
"link": rh.LinkHandler,
"list": rh.ListHandler,
"set": rh.SetHandler,
"cmap": rh.CmapHandler,
"'": rh.IdentityHandler,
},
"default_decoder": rh.DefaultHandler,
}
ground_decoders = {
"_": rh.NoneHandler,
"?": rh.BooleanHandler,
"i": rh.IntHandler,
"'": rh.IdentityHandler,
}
if X_tag_in_decoders:
assert X_decoders_direct
ground_decoders[ "#" ] = Tag
_escaped = SUB+ESC+RES
class Decoder(object):
"""The Decoder is the lowest level entry point for parsing, decoding, and
fully converting Transit data into Python objects.
During the creation of a Decoder object, you can specify custom options
in a dictionary. One such option is 'decoders'. Note that while you
can specify your own decoders and override many of the built in decoders,
some decoders are silently enforced and cannot be overriden. These are
known as Ground Decoders, and are needed to maintain bottom-tier
compatibility.
"""
map_factory = transit_types.frozendict
def __init__(self, options={}):
self.options = default_options.copy()
self.options.update(options)
self.decoders = self.options["decoders"].copy()
# Always ensure we control the ground decoders
self.decoders.update(ground_decoders)
if X_decode_map:
self.make_decode_map()
if X_decoders_direct:
def from_repper(x): return getattr( x, 'from_rep', x)
self.decoders = { k:from_repper(v) for k,v in self.decoders.items() }
self.options["default_decoder"] = from_repper( self.options["default_decoder"] )
def decode(self, node, cache=None, as_map_key=False):
"""Given a node of data (any supported decodeable obj - string, dict,
list), return the decoded object. Optionally set the current decode
cache [None]. If None, a new RollingCache is instantiated and used.
You may also hit to the decoder that this node is to be treated as a
map key [False]. This is used internally.
"""
if not cache:
cache = RollingCache()
#self.cache = cache
return self._decode(node, cache, as_map_key)
def _decode(self, node, cache, as_map_key):
#tp = type(node)
tp = node.__class__
if tp is str:
return self.decode_string(node, cache, as_map_key)
elif tp is bytes:
return self.decode_string(node.decode("utf-8"), cache, as_map_key)
elif tp is dict or tp is OrderedDict:
return self.decode_hash(node, cache, as_map_key)
elif tp is list:
return self.decode_list(node, cache, as_map_key)
elif tp is bool:
return true if node else false
return node
if X_decode_bytes_last:
def _decode(self, node, cache, as_map_key, *nodes):
tp = node.__class__
if tp is str:
return self.decode_string(node, cache, as_map_key)
elif tp is dict or tp is OrderedDict:
return self.decode_hash(node, cache, as_map_key)
elif tp is list:
return self.decode_list(node, cache, as_map_key)
elif tp is bool:
return true if node else false
elif tp is bytes: #last.. is it needed?
return self.decode_string(node.decode("utf-8"), cache, as_map_key)
return node
if X_decode_no_bytes:
def _decode(self, node, cache, as_map_key, *nodes):
tp = node.__class__
if tp is str:
return self.decode_string(node, cache, as_map_key)
elif tp is dict or tp is OrderedDict:
return self.decode_hash(node, cache, as_map_key)
elif tp is list:
return self.decode_list(node, cache, as_map_key)
elif tp is bool:
return true if node else false
#elif tp is bytes:
# return self.decode_string(node.decode("utf-8"), cache, as_map_key)
return node
if X_decode_map:
def make_decode_map( self):
self._decode_map = {
str: self.decode_string,
bytes: lambda node, *a,**ka: self.decode_string( node.decode("utf-8"), *a,**ka),
dict: self.decode_hash,
OrderedDict: self.decode_hash,
list: self.decode_list,
bool: lambda node, *a,**ka: true if node else false,
}
def _decode(self, node, cache, as_map_key):
tp = node.__class__
self_decode_map = self._decode_map
if tp in self_decode_map:
return self_decode_map[ tp ]( node, cache, as_map_key)
return node
def decode_list(self, node, cache, as_map_key):
"""Special case decodes map-as-array into map_factory.
Otherwise lists are treated into tuples.
"""
self_decode = self._decode
if node:
if node[0] == MAP_AS_ARR:
# key must be decoded before value for caching to work.
if X_mapcompreh:
# ... doc/python3/html/reference/expressions.html#dictionary-displays - Starting with 3.8, the key is evaluated before the value
return self.map_factory( {
self_decode(k, cache, _X_mapkeystr) : self_decode(v, cache, as_map_key)
for k,v in pairs(node[1:])
})
returned_dict = {}
for k, v in pairs(node[1:]):
key = self_decode(k, cache, _X_mapkeystr)
val = self_decode(v, cache, as_map_key)
returned_dict[key] = val
return self.map_factory(returned_dict)
decoded = self_decode(node[0], cache, as_map_key)
if isinstance(decoded, Tag):
return self.decode_tag(decoded.tag, self_decode(node[1], cache, as_map_key))
#if X_FIX_ARRAY:
# XXX fallthrough??? hahah will repeate parseing node[0] ..broken cache
return (decoded, *[self_decode(x, cache, as_map_key) for x in node[1:]])
return () #tuple(self_decode(x, cache, as_map_key) for x in node)
def decode_string(self, string, cache, as_map_key):
"""Decode a string - arguments follow the same convention as the
top-level 'decode' function.
"""
if is_cache_key(string):
return self.parse_string(cache.decode(string, as_map_key), cache, as_map_key)
if is_cacheable(string, as_map_key):
cache.encode(string, as_map_key)
return self.parse_string(string, cache, as_map_key)
if getattr( RollingCache, 'X_rework', 0):
def decode_string(self, string, cache, as_map_key):
if is_cache_key(string): return cache[ string ]
pstring = self.parse_string(string, None, as_map_key) #java:ReadCache.cacheRead does this inside
if is_cacheable(string, as_map_key):
cache.encache(pstring, True)
return pstring
if X_is_cache_key_eq_in_cache:
def decode_string(self, string, cache, as_map_key):
#if is_cache_key(string):
if string in cache: return cache[ string ]
pstring = self.parse_string(string, None, as_map_key) #java:ReadCache.cacheRead does this inside
if is_cacheable(string, as_map_key):
cache.encache(pstring, True)
return pstring
if getattr( RollingCache, 'X_is_cacheable_inside_encache', 0):
def decode_string(self, string, cache, as_map_key):
if is_cache_key(string): return cache[ string ]
pstring = self.parse_string(string, None, as_map_key) #java:ReadCache.cacheRead does this inside
#is_cacheable( string.. is inside
cache.encache( pstring, True, as_map_key, string)
return pstring
if X_is_cache_key_eq_in_cache and getattr( RollingCache, 'X_is_cacheable_inside_encache', 0):
def decode_string(self, string, cache, as_map_key):
#if is_cache_key(string):
if string in cache: return cache[ string ]
pstring = self.parse_string(string, None, as_map_key) #java:ReadCache.cacheRead does this inside
#is_cacheable( string.. is inside
cache.encache( pstring, True, as_map_key, string)
return pstring
def decode_tag(self, tag, rep):
decoder = self.decoders.get(tag, None)
if decoder:
return decoder(rep)
else:
return self.options["default_decoder"](tag, rep)
if X_decode_tag:
def decode_tag(self, tag, rep):
self_decoders = self.decoders
if tag in self_decoders:
return self_decoders[ tag ](rep)
return self.options["default_decoder"](tag, rep)
def decode_hash(self, hash, cache, as_map_key):
self_decode = self._decode
if len(hash) != 1:
if X_mapcompreh:
# ... doc/python3/html/reference/expressions.html#dictionary-displays - Starting with 3.8, the key is evaluated before the value
return self.map_factory( {
self_decode(k, cache, _X_mapkeystr) : self_decode(v, cache, False)
for k,v in hash.items()
})
h = {}
for k, v in hash.items():
# crude/verbose implementation, but this is only version that
# plays nice w/cache for both msgpack and json thus far.
# -- e.g., we have to specify encode/decode order for key/val
# -- explicitly, all implicit ordering has broken in corner
# -- cases, thus these extraneous seeming assignments
key = self_decode(k, cache, _X_mapkeystr)
val = self_decode(v, cache, False)
h[key] = val
return self.map_factory(h)
else:
key = list(hash)[0]
value = hash[key]
key = self_decode(key, cache, True)
if isinstance(key, Tag):
return self.decode_tag(key.tag, self_decode(value, cache, as_map_key))
return self.map_factory({key: self_decode(value, cache, False)})
def parse_string(self, string, cache, as_map_key):
if string.startswith(ESC):
m = string[1]
if X_mapkeystr:
if m==':' and as_map_key==_X_mapkeystr and as_map_key in self.decoders: #not assumed
return self.decoders[ as_map_key ](string[2:])
if m in self.decoders:
return self.decoders[m](string[2:])
elif m == ESC or m == SUB or m == RES:
return string[1:]
elif m == "#":
return Tag(string[2:])
else:
return self.options["default_decoder"](string[1], string[2:])
return string
if X_parse_string and X_mapkeystr:
def parse_string(self, string, cache, as_map_key):
if string and string[0] == ESC:
m = string[1]
decoders = self.decoders
if m==':' and as_map_key==_X_mapkeystr and as_map_key in decoders: #not assumed
return decoders[ as_map_key ](string[2:])
if m in decoders:
return decoders[m](string[2:])
elif m in _escaped:
return string[1:]
elif m == "#":
return Tag(string[2:])
else:
return self.options["default_decoder"]( m, string[2:])
return string
if X_parse_string and not X_mapkeystr:
def parse_string(self, string, cache, as_map_key):
if string and string[0] == ESC:
m = string[1]
decoders = self.decoders
if m in decoders:
return decoders[m](string[2:])
elif m in _escaped:
return string[1:]
elif m == "#":
return Tag(string[2:])
else:
return self.options["default_decoder"]( m, string[2:])
return string
if X_parse_string and not X_mapkeystr and X_tag_in_decoders:
def parse_string(self, string, cache, as_map_key):
if string and string[0] == ESC:
m = string[1]
decoders = self.decoders
if m in decoders:
return decoders[m](string[2:])
elif m in _escaped:
return string[1:]
else:
return self.options["default_decoder"]( m, string[2:])
return string
if X_parse_string and not X_mapkeystr and X_tag_in_decoders and X_escaped_first:
def parse_string(self, string, cache, as_map_key):
if string and string[0] == ESC:
m = string[1]
if m in _escaped:
return string[1:]
decoders = self.decoders
if m in decoders:
return decoders[m](string[2:])
else:
return self.options["default_decoder"]( m, string[2:])
return string
# inline best parse_string variant into decode_string
if all([ X_decode_str_with_parse ,
X_is_cache_key_eq_in_cache , getattr( RollingCache, 'X_rework', 0) ,
not X_mapkeystr , X_tag_in_decoders ,
]):
def decode_string(self, string, cache, as_map_key):
#if is_cache_key(string):
if string in cache: return cache[ string ]
#pstring = self.parse_string(string.. #java:ReadCache.cacheRead does this inside
if string and string[0] == ESC:
m = string[1]
decoders = self.decoders
if m in decoders:
pstring = decoders[m](string[2:])
elif m in _escaped:
pstring = string[1:]
else:
pstring = self.options["default_decoder"]( m, string[2:])
else: pstring = string
if is_cacheable(string, as_map_key):
cache.encache(pstring, True)
return pstring
if all([ X_decode_str_with_parse ,
X_is_cache_key_eq_in_cache , getattr( RollingCache, 'X_rework', 0) ,
not X_mapkeystr , X_tag_in_decoders ,
getattr( RollingCache, 'X_is_cacheable_inside_encache', 0)
]):
def decode_string(self, string, cache, as_map_key):
#if is_cache_key(string):
if string in cache: return cache[ string ]
#pstring = self.parse_string(string.. #java:ReadCache.cacheRead does this inside
if string and string[0] == ESC:
m = string[1]
decoders = self.decoders
if m in decoders:
pstring = decoders[m](string[2:])
elif m in _escaped:
pstring = string[1:]
else:
pstring = self.options["default_decoder"]( m, string[2:])
else: pstring = string
#is_cacheable( string.. is inside
cache.encache( pstring, True, as_map_key, string)
return pstring
if all([ X_decode_str_with_parse ,
X_is_cache_key_eq_in_cache , getattr( RollingCache, 'X_rework', 0) ,
not X_mapkeystr , X_tag_in_decoders , X_escaped_first,
getattr( RollingCache, 'X_is_cacheable_inside_encache', 0)
]):
def decode_string(self, string, cache, as_map_key):
#if is_cache_key(string):
if string in cache: return cache[ string ]
#pstring = self.parse_string(string.. #java:ReadCache.cacheRead does this inside
if string and string[0] == ESC:
m = string[1]
decoders = self.decoders
if m in _escaped:
pstring = string[1:]
elif m in decoders:
pstring = decoders[m](string[2:])
else:
pstring = self.options["default_decoder"]( m, string[2:])
else: pstring = string
#is_cacheable( string.. is inside
cache.encache( pstring, True, as_map_key, string)
return pstring
def register(self, key_or_tag, obj):
"""Register a custom Transit tag and new parsing function with the
decoder. Also, you can optionally set the 'default_decoder' with
this function. Your new tag and parse/decode function will be added
to the interal dictionary of decoders for this Decoder object.
"""
if X_decoders_direct: obj = getattr( obj, 'from_rep', obj)
if key_or_tag == "default_decoder":
self.options["default_decoder"] = obj
else:
self.decoders[key_or_tag] = obj