Skip to content

Commit 1572385

Browse files
committed
Remove hardcoded "json" strings
Additionally, add support for other file extenntions in the Timestamp class. Signed-off-by: Martin Vrachev <[email protected]>
1 parent 5380720 commit 1572385

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

tuf/api/metadata.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -500,15 +500,15 @@ def __eq__(self, other: "MetadataInfo") -> bool:
500500

501501
def to_dict(self) -> Dict:
502502
"""Returns the dictionary representation of self. """
503-
json_dict = {"version": self.version}
503+
res_dict = {"version": self.version}
504504

505505
if self.length is not None:
506-
json_dict["length"] = self.length
506+
res_dict["length"] = self.length
507507

508508
if self.hashes is not None:
509-
json_dict["hashes"] = self.hashes
509+
res_dict["hashes"] = self.hashes
510510

511-
return json_dict
511+
return res_dict
512512

513513

514514
class Timestamp(Signed):
@@ -534,22 +534,41 @@ def __init__(
534534
super().__init__(_type, version, spec_version, expires)
535535
self.meta = meta
536536

537+
@classmethod
538+
def _get_snapshot_filename(cls, meta_dict):
539+
"""Get the snapshot file name from the metadata dict.
540+
The snapshot file could be have a custom extension rather than JSON.
541+
See the tuf/api/serialization folder."""
542+
meta_files = list(meta_dict.keys())
543+
if len(meta_files) != 1:
544+
raise ValueError(
545+
f"the meta_dict should contain information about the"
546+
f"snapshot file only, but got {meta_dict}"
547+
)
548+
return meta_files[0]
549+
537550
@classmethod
538551
def from_dict(cls, timestamp_dict: Mapping[str, Any]) -> "Timestamp":
539552
"""Creates Timestamp object from its dict representation. """
540553
common_args = cls._common_fields_from_dict(timestamp_dict)
554+
snapshot_filename = cls._get_snapshot_filename(timestamp_dict["meta"])
541555
meta = {
542-
"snapshot.json": MetadataInfo(
543-
**timestamp_dict["meta"]["snapshot.json"]
556+
snapshot_filename: MetadataInfo(
557+
**timestamp_dict["meta"][snapshot_filename]
544558
)
545559
}
546560
return cls(*common_args, meta)
547561

548562
def to_dict(self) -> Dict[str, Any]:
549563
"""Returns the dict representation of self. """
550564
timestamp_dict = self._common_fields_to_dict()
565+
snapshot_filename = self._get_snapshot_filename(self.meta)
551566
timestamp_dict.update(
552-
{"meta": {"snapshot.json": self.meta["snapshot.json"].to_dict()}}
567+
{
568+
"meta": {
569+
snapshot_filename: self.meta[snapshot_filename].to_dict()
570+
}
571+
}
553572
)
554573
return timestamp_dict
555574

@@ -563,7 +582,8 @@ def update(
563582
"""Assigns passed info about snapshot metadata to meta dict. """
564583
# TODO: Consider renaming this function:
565584
# see: https://github.com/theupdateframework/tuf/issues/1230
566-
self.meta["snapshot.json"] = MetadataInfo(version, length, hashes)
585+
snapshot_filename = self._get_snapshot_filename(self.meta)
586+
self.meta[snapshot_filename] = MetadataInfo(version, length, hashes)
567587

568588

569589
class Snapshot(Signed):
@@ -668,13 +688,13 @@ def __eq__(self, other: "TargetInfo") -> bool:
668688
)
669689

670690
def to_dict(self) -> Dict:
671-
"""Returns the JSON-serializable dictionary representation of self. """
672-
json_dict = {"length": self.length, "hashes": self.hashes}
691+
"""Returns the dictionary representation of self. """
692+
res_dict = {"length": self.length, "hashes": self.hashes}
673693

674694
if self.custom is not None:
675-
json_dict["custom"] = self.custom
695+
res_dict["custom"] = self.custom
676696

677-
return json_dict
697+
return res_dict
678698

679699

680700
class Targets(Signed):

0 commit comments

Comments
 (0)