Skip to content
Snippets Groups Projects
Commit 7f631254 authored by KERDREUX Jerome's avatar KERDREUX Jerome
Browse files

Format

parent d126e837
No related branches found
No related tags found
1 merge request!1First try of type hints
...@@ -16,25 +16,30 @@ def tag_hook(decoder, tag, shareable_index=None): ...@@ -16,25 +16,30 @@ def tag_hook(decoder, tag, shareable_index=None):
return bindings.URL(tag.value) return bindings.URL(tag.value)
return tag return tag
def default_encoder(encoder, value): def default_encoder(encoder, value):
if isinstance(value,bindings.UUID): if isinstance(value, bindings.UUID):
encoder.encode(CBORTag(37, value.bytes)) encoder.encode(CBORTag(37, value.bytes))
if isinstance(value,bindings.URL): if isinstance(value, bindings.URL):
encoder.encode(CBORTag(32, value.bytes)) encoder.encode(CBORTag(32, value.bytes))
def dumps(obj, **kwargs): def dumps(obj, **kwargs):
return cbor2.dumps(obj,default=default_encoder,**kwargs) return cbor2.dumps(obj, default=default_encoder, **kwargs)
def loads(payload, **kwargs): def loads(payload, **kwargs):
#return cbor2.loads(payload,tag_hook=tag_hook,**kwargs) # return cbor2.loads(payload,tag_hook=tag_hook,**kwargs)
return _loads(payload,tag_hook=tag_hook,**kwargs) return _loads(payload, tag_hook=tag_hook, **kwargs)
def _loads(s, **kwargs): def _loads(s, **kwargs):
with BytesIO(s) as fp: with BytesIO(s) as fp:
return CBORDecoder(fp, **kwargs).decode() return CBORDecoder(fp, **kwargs).decode()
#class CustomDecoder(CBORDecoder):pass
# class CustomDecoder(CBORDecoder):pass
def cleanup(obj): def cleanup(obj):
...@@ -45,14 +50,14 @@ def cleanup(obj): ...@@ -45,14 +50,14 @@ def cleanup(obj):
Warning: This operate in-place changes. Warning: This operate in-place changes.
Warning: This won't work for tags in dict keys. Warning: This won't work for tags in dict keys.
""" """
if isinstance(obj,list): if isinstance(obj, list):
for i in range(0,len(obj)): for i in range(0, len(obj)):
obj[i] = cleanup(obj[i]) obj[i] = cleanup(obj[i])
return obj return obj
if isinstance(obj,dict): if isinstance(obj, dict):
for k in obj.keys(): for k in obj.keys():
obj.update({k:cleanup(obj[k])}) obj.update({k: cleanup(obj[k])})
return obj return obj
if type(obj) in bindings.classes: if type(obj) in bindings.classes:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment