-
Notifications
You must be signed in to change notification settings - Fork 275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move metadata class model de/serialization to sub-package #1279
Changes from 1 commit
3d8cade
4a22b4a
499f1c8
240fb54
e1be085
8e9afc9
2f57eb8
2b40857
aa8225c
d823c8f
aba6ba3
f8fc5e2
ace25e4
326d2af
ab92ba2
bd94f6d
a53d68b
ef91964
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
[MESSAGE_CONTROL] | ||
disable=fixme | ||
|
||
[BASIC] | ||
good-names=e | ||
|
||
[FORMAT] | ||
indent-string=" " | ||
max-line-length=79 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
""" | ||
import json | ||
import six | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we using six in code that was never intended to support Python2? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For |
||
|
||
from securesystemslib.formats import encode_canonical | ||
|
||
|
@@ -16,16 +17,22 @@ | |
from tuf.api.metadata import Metadata, Signed | ||
from tuf.api.serialization import (MetadataSerializer, | ||
MetadataDeserializer, | ||
SignedSerializer) | ||
SignedSerializer, | ||
SerializationError, | ||
DeserializationError) | ||
|
||
|
||
class JSONDeserializer(MetadataDeserializer): | ||
"""Provides JSON-to-Metadata deserialize method. """ | ||
|
||
def deserialize(self, raw_data: bytes) -> Metadata: | ||
"""Deserialize utf-8 encoded JSON bytes into Metadata object. """ | ||
_dict = json.loads(raw_data.decode("utf-8")) | ||
return Metadata.from_dict(_dict) | ||
try: | ||
_dict = json.loads(raw_data.decode("utf-8")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment here, with the code moved, can we rename _dict? Possible alternatives include json_object and json_dict. |
||
return Metadata.from_dict(_dict) | ||
|
||
except Exception as e: # pylint: disable=broad-except | ||
six.raise_from(DeserializationError, e) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Python3 syntax for this appears to be raise SerializationError from e |
||
|
||
|
||
class JSONSerializer(MetadataSerializer): | ||
|
@@ -41,18 +48,26 @@ def __init__(self, compact: bool = False) -> None: | |
|
||
def serialize(self, metadata_obj: Metadata) -> bytes: | ||
"""Serialize Metadata object into utf-8 encoded JSON bytes. """ | ||
indent = (None if self.compact else 1) | ||
separators=((',', ':') if self.compact else (',', ': ')) | ||
return json.dumps(metadata_obj.to_dict(), | ||
indent=indent, | ||
separators=separators, | ||
sort_keys=True).encode("utf-8") | ||
try: | ||
indent = (None if self.compact else 1) | ||
separators=((',', ':') if self.compact else (',', ': ')) | ||
return json.dumps(metadata_obj.to_dict(), | ||
indent=indent, | ||
separators=separators, | ||
sort_keys=True).encode("utf-8") | ||
|
||
except Exception as e: # pylint: disable=broad-except | ||
six.raise_from(SerializationError, e) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Python3 syntax for this appears to be raise SerializationError from e |
||
|
||
|
||
class CanonicalJSONSerializer(SignedSerializer): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think it is worth clarifying, via the class name, that this is the OLPC canonical JSON? Counter-argument, I do not like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with both arguments. :D There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another way to avoid ambiguity would be to not call it |
||
"""A Signed-to-Canonical JSON 'serialize' method. """ | ||
|
||
def serialize(self, signed_obj: Signed) -> bytes: | ||
"""Serialize Signed object into utf-8 encoded Canonical JSON bytes. """ | ||
signed_dict = signed_obj.to_dict() | ||
return encode_canonical(signed_dict).encode("utf-8") | ||
try: | ||
signed_dict = signed_obj.to_dict() | ||
return encode_canonical(signed_dict).encode("utf-8") | ||
|
||
except Exception as e: # pylint: disable=broad-except | ||
six.raise_from(SerializationError, e) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's not use six here raise SerializationError from e |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: now that we are only supporting Python3 we can switch to less confusing metaclass syntax, either
or
we can handle this in a future PR, though.