@@ -500,15 +500,15 @@ def __eq__(self, other: "MetadataInfo") -> bool:
500
500
501
501
def to_dict (self ) -> Dict :
502
502
"""Returns the dictionary representation of self. """
503
- json_dict = {"version" : self .version }
503
+ res_dict = {"version" : self .version }
504
504
505
505
if self .length is not None :
506
- json_dict ["length" ] = self .length
506
+ res_dict ["length" ] = self .length
507
507
508
508
if self .hashes is not None :
509
- json_dict ["hashes" ] = self .hashes
509
+ res_dict ["hashes" ] = self .hashes
510
510
511
- return json_dict
511
+ return res_dict
512
512
513
513
514
514
class Timestamp (Signed ):
@@ -534,22 +534,41 @@ def __init__(
534
534
super ().__init__ (_type , version , spec_version , expires )
535
535
self .meta = meta
536
536
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
+
537
550
@classmethod
538
551
def from_dict (cls , timestamp_dict : Mapping [str , Any ]) -> "Timestamp" :
539
552
"""Creates Timestamp object from its dict representation. """
540
553
common_args = cls ._common_fields_from_dict (timestamp_dict )
554
+ snapshot_filename = cls ._get_snapshot_filename (timestamp_dict ["meta" ])
541
555
meta = {
542
- "snapshot.json" : MetadataInfo (
543
- ** timestamp_dict ["meta" ]["snapshot.json" ]
556
+ snapshot_filename : MetadataInfo (
557
+ ** timestamp_dict ["meta" ][snapshot_filename ]
544
558
)
545
559
}
546
560
return cls (* common_args , meta )
547
561
548
562
def to_dict (self ) -> Dict [str , Any ]:
549
563
"""Returns the dict representation of self. """
550
564
timestamp_dict = self ._common_fields_to_dict ()
565
+ snapshot_filename = self ._get_snapshot_filename (self .meta )
551
566
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
+ }
553
572
)
554
573
return timestamp_dict
555
574
@@ -563,7 +582,8 @@ def update(
563
582
"""Assigns passed info about snapshot metadata to meta dict. """
564
583
# TODO: Consider renaming this function:
565
584
# 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 )
567
587
568
588
569
589
class Snapshot (Signed ):
@@ -668,13 +688,13 @@ def __eq__(self, other: "TargetInfo") -> bool:
668
688
)
669
689
670
690
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 }
673
693
674
694
if self .custom is not None :
675
- json_dict ["custom" ] = self .custom
695
+ res_dict ["custom" ] = self .custom
676
696
677
- return json_dict
697
+ return res_dict
678
698
679
699
680
700
class Targets (Signed ):
0 commit comments