Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/tuf/api/serialization/__init__.py: 76%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

21 statements  

1# Copyright New York University and the TUF contributors 

2# SPDX-License-Identifier: MIT OR Apache-2.0 

3 

4"""``tuf.api.serialization`` module provides abstract base classes and concrete 

5implementations to serialize and deserialize TUF metadata. 

6 

7Any custom de/serialization implementations should inherit from the abstract 

8base classes defined in this module. The implementations can use the 

9``to_dict()``/``from_dict()`` implementations available in the Metadata 

10API objects. 

11 

12- Metadata de/serializers are used to convert to and from wireline formats. 

13- Signed serializers are used to canonicalize data for cryptographic signatures 

14 generation and verification. 

15""" 

16 

17import abc 

18from typing import TYPE_CHECKING 

19 

20from tuf.api.exceptions import RepositoryError 

21 

22if TYPE_CHECKING: 

23 from tuf.api.metadata import Metadata, Signed 

24 

25 

26class SerializationError(RepositoryError): 

27 """Error during serialization.""" 

28 

29 

30class DeserializationError(RepositoryError): 

31 """Error during deserialization.""" 

32 

33 

34class MetadataDeserializer(metaclass=abc.ABCMeta): 

35 """Abstract base class for deserialization of Metadata objects.""" 

36 

37 @abc.abstractmethod 

38 def deserialize(self, raw_data: bytes) -> "Metadata": 

39 """Deserialize bytes to Metadata object.""" 

40 raise NotImplementedError 

41 

42 

43class MetadataSerializer(metaclass=abc.ABCMeta): 

44 """Abstract base class for serialization of Metadata objects.""" 

45 

46 @abc.abstractmethod 

47 def serialize(self, metadata_obj: "Metadata") -> bytes: 

48 """Serialize Metadata object to bytes.""" 

49 raise NotImplementedError 

50 

51 

52class SignedSerializer(metaclass=abc.ABCMeta): 

53 """Abstract base class for serialization of Signed objects.""" 

54 

55 @abc.abstractmethod 

56 def serialize(self, signed_obj: "Signed") -> bytes: 

57 """Serialize Signed object to bytes.""" 

58 raise NotImplementedError