author | Gregory Szorc <gregory.szorc@gmail.com> |
Tue, 22 Jan 2019 18:16:53 -0800 | |
changeset 41321 | 724b4606528e |
parent 37126 | 4bd73a955ab0 |
permissions | -rw-r--r-- |
37126
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
1 |
import re |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
2 |
import struct |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
3 |
from datetime import datetime, timedelta |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
4 |
from io import BytesIO |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
5 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
6 |
from .compat import timezone, xrange, byte_as_integer, unpack_float16 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
7 |
from .types import CBORTag, undefined, break_marker, CBORSimpleValue |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
8 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
9 |
timestamp_re = re.compile(r'^(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)' |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
10 |
r'(?:\.(\d+))?(?:Z|([+-]\d\d):(\d\d))$') |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
11 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
12 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
13 |
class CBORDecodeError(Exception): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
14 |
"""Raised when an error occurs deserializing a CBOR datastream.""" |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
15 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
16 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
17 |
def decode_uint(decoder, subtype, shareable_index=None, allow_indefinite=False): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
18 |
# Major tag 0 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
19 |
if subtype < 24: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
20 |
return subtype |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
21 |
elif subtype == 24: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
22 |
return struct.unpack('>B', decoder.read(1))[0] |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
23 |
elif subtype == 25: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
24 |
return struct.unpack('>H', decoder.read(2))[0] |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
25 |
elif subtype == 26: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
26 |
return struct.unpack('>L', decoder.read(4))[0] |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
27 |
elif subtype == 27: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
28 |
return struct.unpack('>Q', decoder.read(8))[0] |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
29 |
elif subtype == 31 and allow_indefinite: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
30 |
return None |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
31 |
else: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
32 |
raise CBORDecodeError('unknown unsigned integer subtype 0x%x' % subtype) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
33 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
34 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
35 |
def decode_negint(decoder, subtype, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
36 |
# Major tag 1 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
37 |
uint = decode_uint(decoder, subtype) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
38 |
return -uint - 1 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
39 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
40 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
41 |
def decode_bytestring(decoder, subtype, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
42 |
# Major tag 2 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
43 |
length = decode_uint(decoder, subtype, allow_indefinite=True) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
44 |
if length is None: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
45 |
# Indefinite length |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
46 |
buf = bytearray() |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
47 |
while True: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
48 |
initial_byte = byte_as_integer(decoder.read(1)) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
49 |
if initial_byte == 255: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
50 |
return buf |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
51 |
else: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
52 |
length = decode_uint(decoder, initial_byte & 31) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
53 |
value = decoder.read(length) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
54 |
buf.extend(value) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
55 |
else: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
56 |
return decoder.read(length) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
57 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
58 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
59 |
def decode_string(decoder, subtype, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
60 |
# Major tag 3 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
61 |
return decode_bytestring(decoder, subtype).decode('utf-8') |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
62 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
63 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
64 |
def decode_array(decoder, subtype, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
65 |
# Major tag 4 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
66 |
items = [] |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
67 |
decoder.set_shareable(shareable_index, items) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
68 |
length = decode_uint(decoder, subtype, allow_indefinite=True) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
69 |
if length is None: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
70 |
# Indefinite length |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
71 |
while True: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
72 |
value = decoder.decode() |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
73 |
if value is break_marker: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
74 |
break |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
75 |
else: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
76 |
items.append(value) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
77 |
else: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
78 |
for _ in xrange(length): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
79 |
item = decoder.decode() |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
80 |
items.append(item) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
81 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
82 |
return items |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
83 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
84 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
85 |
def decode_map(decoder, subtype, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
86 |
# Major tag 5 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
87 |
dictionary = {} |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
88 |
decoder.set_shareable(shareable_index, dictionary) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
89 |
length = decode_uint(decoder, subtype, allow_indefinite=True) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
90 |
if length is None: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
91 |
# Indefinite length |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
92 |
while True: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
93 |
key = decoder.decode() |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
94 |
if key is break_marker: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
95 |
break |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
96 |
else: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
97 |
value = decoder.decode() |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
98 |
dictionary[key] = value |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
99 |
else: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
100 |
for _ in xrange(length): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
101 |
key = decoder.decode() |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
102 |
value = decoder.decode() |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
103 |
dictionary[key] = value |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
104 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
105 |
if decoder.object_hook: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
106 |
return decoder.object_hook(decoder, dictionary) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
107 |
else: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
108 |
return dictionary |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
109 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
110 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
111 |
def decode_semantic(decoder, subtype, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
112 |
# Major tag 6 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
113 |
tagnum = decode_uint(decoder, subtype) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
114 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
115 |
# Special handling for the "shareable" tag |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
116 |
if tagnum == 28: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
117 |
shareable_index = decoder._allocate_shareable() |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
118 |
return decoder.decode(shareable_index) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
119 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
120 |
value = decoder.decode() |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
121 |
semantic_decoder = semantic_decoders.get(tagnum) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
122 |
if semantic_decoder: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
123 |
return semantic_decoder(decoder, value, shareable_index) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
124 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
125 |
tag = CBORTag(tagnum, value) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
126 |
if decoder.tag_hook: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
127 |
return decoder.tag_hook(decoder, tag, shareable_index) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
128 |
else: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
129 |
return tag |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
130 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
131 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
132 |
def decode_special(decoder, subtype, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
133 |
# Simple value |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
134 |
if subtype < 20: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
135 |
return CBORSimpleValue(subtype) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
136 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
137 |
# Major tag 7 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
138 |
return special_decoders[subtype](decoder) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
139 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
140 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
141 |
# |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
142 |
# Semantic decoders (major tag 6) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
143 |
# |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
144 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
145 |
def decode_datetime_string(decoder, value, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
146 |
# Semantic tag 0 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
147 |
match = timestamp_re.match(value) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
148 |
if match: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
149 |
year, month, day, hour, minute, second, micro, offset_h, offset_m = match.groups() |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
150 |
if offset_h: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
151 |
tz = timezone(timedelta(hours=int(offset_h), minutes=int(offset_m))) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
152 |
else: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
153 |
tz = timezone.utc |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
154 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
155 |
return datetime(int(year), int(month), int(day), int(hour), int(minute), int(second), |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
156 |
int(micro or 0), tz) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
157 |
else: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
158 |
raise CBORDecodeError('invalid datetime string: {}'.format(value)) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
159 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
160 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
161 |
def decode_epoch_datetime(decoder, value, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
162 |
# Semantic tag 1 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
163 |
return datetime.fromtimestamp(value, timezone.utc) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
164 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
165 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
166 |
def decode_positive_bignum(decoder, value, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
167 |
# Semantic tag 2 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
168 |
from binascii import hexlify |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
169 |
return int(hexlify(value), 16) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
170 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
171 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
172 |
def decode_negative_bignum(decoder, value, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
173 |
# Semantic tag 3 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
174 |
return -decode_positive_bignum(decoder, value) - 1 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
175 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
176 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
177 |
def decode_fraction(decoder, value, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
178 |
# Semantic tag 4 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
179 |
from decimal import Decimal |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
180 |
exp = Decimal(value[0]) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
181 |
mantissa = Decimal(value[1]) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
182 |
return mantissa * (10 ** exp) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
183 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
184 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
185 |
def decode_bigfloat(decoder, value, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
186 |
# Semantic tag 5 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
187 |
from decimal import Decimal |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
188 |
exp = Decimal(value[0]) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
189 |
mantissa = Decimal(value[1]) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
190 |
return mantissa * (2 ** exp) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
191 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
192 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
193 |
def decode_sharedref(decoder, value, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
194 |
# Semantic tag 29 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
195 |
try: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
196 |
shared = decoder._shareables[value] |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
197 |
except IndexError: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
198 |
raise CBORDecodeError('shared reference %d not found' % value) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
199 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
200 |
if shared is None: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
201 |
raise CBORDecodeError('shared value %d has not been initialized' % value) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
202 |
else: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
203 |
return shared |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
204 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
205 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
206 |
def decode_rational(decoder, value, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
207 |
# Semantic tag 30 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
208 |
from fractions import Fraction |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
209 |
return Fraction(*value) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
210 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
211 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
212 |
def decode_regexp(decoder, value, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
213 |
# Semantic tag 35 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
214 |
return re.compile(value) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
215 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
216 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
217 |
def decode_mime(decoder, value, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
218 |
# Semantic tag 36 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
219 |
from email.parser import Parser |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
220 |
return Parser().parsestr(value) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
221 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
222 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
223 |
def decode_uuid(decoder, value, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
224 |
# Semantic tag 37 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
225 |
from uuid import UUID |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
226 |
return UUID(bytes=value) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
227 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
228 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
229 |
def decode_set(decoder, value, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
230 |
# Semantic tag 258 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
231 |
return set(value) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
232 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
233 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
234 |
# |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
235 |
# Special decoders (major tag 7) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
236 |
# |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
237 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
238 |
def decode_simple_value(decoder, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
239 |
return CBORSimpleValue(struct.unpack('>B', decoder.read(1))[0]) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
240 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
241 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
242 |
def decode_float16(decoder, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
243 |
payload = decoder.read(2) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
244 |
return unpack_float16(payload) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
245 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
246 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
247 |
def decode_float32(decoder, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
248 |
return struct.unpack('>f', decoder.read(4))[0] |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
249 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
250 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
251 |
def decode_float64(decoder, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
252 |
return struct.unpack('>d', decoder.read(8))[0] |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
253 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
254 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
255 |
major_decoders = { |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
256 |
0: decode_uint, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
257 |
1: decode_negint, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
258 |
2: decode_bytestring, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
259 |
3: decode_string, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
260 |
4: decode_array, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
261 |
5: decode_map, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
262 |
6: decode_semantic, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
263 |
7: decode_special |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
264 |
} |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
265 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
266 |
special_decoders = { |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
267 |
20: lambda self: False, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
268 |
21: lambda self: True, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
269 |
22: lambda self: None, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
270 |
23: lambda self: undefined, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
271 |
24: decode_simple_value, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
272 |
25: decode_float16, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
273 |
26: decode_float32, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
274 |
27: decode_float64, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
275 |
31: lambda self: break_marker |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
276 |
} |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
277 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
278 |
semantic_decoders = { |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
279 |
0: decode_datetime_string, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
280 |
1: decode_epoch_datetime, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
281 |
2: decode_positive_bignum, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
282 |
3: decode_negative_bignum, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
283 |
4: decode_fraction, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
284 |
5: decode_bigfloat, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
285 |
29: decode_sharedref, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
286 |
30: decode_rational, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
287 |
35: decode_regexp, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
288 |
36: decode_mime, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
289 |
37: decode_uuid, |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
290 |
258: decode_set |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
291 |
} |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
292 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
293 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
294 |
class CBORDecoder(object): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
295 |
""" |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
296 |
Deserializes a CBOR encoded byte stream. |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
297 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
298 |
:param tag_hook: Callable that takes 3 arguments: the decoder instance, the |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
299 |
:class:`~cbor2.types.CBORTag` and the shareable index for the resulting object, if any. |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
300 |
This callback is called for any tags for which there is no built-in decoder. |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
301 |
The return value is substituted for the CBORTag object in the deserialized output. |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
302 |
:param object_hook: Callable that takes 2 arguments: the decoder instance and the dictionary. |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
303 |
This callback is called for each deserialized :class:`dict` object. |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
304 |
The return value is substituted for the dict in the deserialized output. |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
305 |
""" |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
306 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
307 |
__slots__ = ('fp', 'tag_hook', 'object_hook', '_shareables') |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
308 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
309 |
def __init__(self, fp, tag_hook=None, object_hook=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
310 |
self.fp = fp |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
311 |
self.tag_hook = tag_hook |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
312 |
self.object_hook = object_hook |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
313 |
self._shareables = [] |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
314 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
315 |
def _allocate_shareable(self): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
316 |
self._shareables.append(None) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
317 |
return len(self._shareables) - 1 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
318 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
319 |
def set_shareable(self, index, value): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
320 |
""" |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
321 |
Set the shareable value for the last encountered shared value marker, if any. |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
322 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
323 |
If the given index is ``None``, nothing is done. |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
324 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
325 |
:param index: the value of the ``shared_index`` argument to the decoder |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
326 |
:param value: the shared value |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
327 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
328 |
""" |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
329 |
if index is not None: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
330 |
self._shareables[index] = value |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
331 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
332 |
def read(self, amount): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
333 |
""" |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
334 |
Read bytes from the data stream. |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
335 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
336 |
:param int amount: the number of bytes to read |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
337 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
338 |
""" |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
339 |
data = self.fp.read(amount) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
340 |
if len(data) < amount: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
341 |
raise CBORDecodeError('premature end of stream (expected to read {} bytes, got {} ' |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
342 |
'instead)'.format(amount, len(data))) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
343 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
344 |
return data |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
345 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
346 |
def decode(self, shareable_index=None): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
347 |
""" |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
348 |
Decode the next value from the stream. |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
349 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
350 |
:raises CBORDecodeError: if there is any problem decoding the stream |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
351 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
352 |
""" |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
353 |
try: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
354 |
initial_byte = byte_as_integer(self.fp.read(1)) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
355 |
major_type = initial_byte >> 5 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
356 |
subtype = initial_byte & 31 |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
357 |
except Exception as e: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
358 |
raise CBORDecodeError('error reading major type at index {}: {}' |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
359 |
.format(self.fp.tell(), e)) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
360 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
361 |
decoder = major_decoders[major_type] |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
362 |
try: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
363 |
return decoder(self, subtype, shareable_index) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
364 |
except CBORDecodeError: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
365 |
raise |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
366 |
except Exception as e: |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
367 |
raise CBORDecodeError('error decoding value at index {}: {}'.format(self.fp.tell(), e)) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
368 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
369 |
def decode_from_bytes(self, buf): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
370 |
""" |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
371 |
Wrap the given bytestring as a file and call :meth:`decode` with it as the argument. |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
372 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
373 |
This method was intended to be used from the ``tag_hook`` hook when an object needs to be |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
374 |
decoded separately from the rest but while still taking advantage of the shared value |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
375 |
registry. |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
376 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
377 |
""" |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
378 |
old_fp = self.fp |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
379 |
self.fp = BytesIO(buf) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
380 |
retval = self.decode() |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
381 |
self.fp = old_fp |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
382 |
return retval |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
383 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
384 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
385 |
def loads(payload, **kwargs): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
386 |
""" |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
387 |
Deserialize an object from a bytestring. |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
388 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
389 |
:param bytes payload: the bytestring to serialize |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
390 |
:param kwargs: keyword arguments passed to :class:`~.CBORDecoder` |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
391 |
:return: the deserialized object |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
392 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
393 |
""" |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
394 |
fp = BytesIO(payload) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
395 |
return CBORDecoder(fp, **kwargs).decode() |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
396 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
397 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
398 |
def load(fp, **kwargs): |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
399 |
""" |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
400 |
Deserialize an object from an open file. |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
401 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
402 |
:param fp: the input file (any file-like object) |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
403 |
:param kwargs: keyword arguments passed to :class:`~.CBORDecoder` |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
404 |
:return: the deserialized object |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
405 |
|
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
406 |
""" |
4bd73a955ab0
thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
407 |
return CBORDecoder(fp, **kwargs).decode() |