-
Notifications
You must be signed in to change notification settings - Fork 292
api: Add Metadata.from_bytes() #1354
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -128,20 +128,40 @@ def from_file( | |
| A TUF Metadata object. | ||
|
|
||
| """ | ||
| if storage_backend is None: | ||
| storage_backend = FilesystemBackend() | ||
|
|
||
| with storage_backend.get(filename) as file_obj: | ||
| return cls.from_bytes(file_obj.read(), deserializer) | ||
|
|
||
| @staticmethod | ||
| def from_bytes( | ||
| data: bytes, | ||
| deserializer: Optional[MetadataDeserializer] = None, | ||
| ) -> "Metadata": | ||
| """Loads TUF metadata from raw data. | ||
|
|
||
| Arguments: | ||
| data: metadata content as bytes. | ||
| deserializer: Optional; A MetadataDeserializer instance that | ||
|
Member
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. is this notation standard? Would we be better off just relying on the typing annotations to indicate optional arguments?
Member
Author
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 saw that in a google style guide example -- could leave that out as well, it does duplicate the annotation...
Member
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 leave it for now. Could you create an issue to ensure we review docstrings and make sure they are consistent before we cut a release. |
||
| implements deserialization. Default is JSONDeserializer. | ||
|
|
||
| Raises: | ||
| tuf.api.serialization.DeserializationError: | ||
| The file cannot be deserialized. | ||
|
|
||
| Returns: | ||
| A TUF Metadata object. | ||
| """ | ||
|
|
||
| if deserializer is None: | ||
| # Use local scope import to avoid circular import errors | ||
| # pylint: disable=import-outside-toplevel | ||
| from tuf.api.serialization.json import JSONDeserializer | ||
|
|
||
| deserializer = JSONDeserializer() | ||
|
|
||
| if storage_backend is None: | ||
| storage_backend = FilesystemBackend() | ||
|
|
||
| with storage_backend.get(filename) as file_obj: | ||
| raw_data = file_obj.read() | ||
|
|
||
| return deserializer.deserialize(raw_data) | ||
| return deserializer.deserialize(data) | ||
|
|
||
| def to_dict(self) -> Dict[str, Any]: | ||
| """Returns the dict representation of self. """ | ||
|
|
||
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.
Is indentation off here, or is it just the GitHub app?