Skip to content
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

Merged
merged 18 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ commands =

[testenv:lint]
commands =
pylint {toxinidir}/tuf --ignore={toxinidir}/tuf/api
# Use different pylint configs for legacy and new (tuf/api) code
# NOTE: Contrary to what the pylint docs suggest, ignoring full paths does
# work, unfortunately each subdirectory has to be ignored explicitly.
pylint {toxinidir}/tuf --ignore={toxinidir}/tuf/api,{toxinidir}/tuf/api/serialization
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to ignore all files under a certain folder?
That way you would be able to ignore everything under tuf/api.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to ignore all files under a certain folder?

Nope (see my comment). I even looked at the implementation of --ignore and --ignore-patterns.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC there's a corresponding ticket on the pylint issue tracker.

pylint {toxinidir}/tuf/api --rcfile={toxinidir}/tuf/api/pylintrc
bandit -r {toxinidir}/tuf
3 changes: 3 additions & 0 deletions tuf/api/pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ disable=fixme
[FORMAT]
indent-string=" "
max-line-length=79

[DESIGN]
min-public-methods=0
joshuagl marked this conversation as resolved.
Show resolved Hide resolved
43 changes: 43 additions & 0 deletions tuf/api/serialization/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""TUF role metadata de/serialization.
joshuagl marked this conversation as resolved.
Show resolved Hide resolved

This sub-package provides abstract base classes and concrete implementations to
serialize and deserialize TUF role metadata and metadata parts.

Any custom de/serialization implementations should inherit from the abstract
base classes defined in this __init__.py module.

- Metadata de/serializers are used to convert to and from wireline formats.
- Signed serializers are used to canonicalize data for cryptographic signatures
generation and verification.

"""
import abc

class MetadataDeserializer():
"""Abstract base class for deserialization of Metadata objects. """
__metaclass__ = abc.ABCMeta
Copy link
Member

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

import abc

class MetadataDeserializer(ABC):

or

import abc

class MetadataDeserializer(metaclass=ABCMeta):

we can handle this in a future PR, though.


@abc.abstractmethod
def deserialize(self, raw_data: bytes) -> "Metadata":
"""Deserialize passed bytes to Metadata object. """
raise NotImplementedError


class MetadataSerializer():
"""Abstract base class for serialization of Metadata objects. """
__metaclass__ = abc.ABCMeta

@abc.abstractmethod
def serialize(self, metadata_obj: "Metadata") -> bytes:
"""Serialize passed Metadata object to bytes. """
raise NotImplementedError


class SignedSerializer():
"""Abstract base class for serialization of Signed objects. """
__metaclass__ = abc.ABCMeta

@abc.abstractmethod
def serialize(self, signed_obj: "Signed") -> bytes:
"""Serialize passed Signed object to bytes. """
raise NotImplementedError