@@ -361,6 +361,20 @@ def bump_version(self) -> None:
361
361
self .version += 1
362
362
363
363
364
+ def _get_unrecognized_fields (
365
+ all_fields : Mapping [str , Any ], recognized_fields : list
366
+ ) -> Mapping [str , Any ]:
367
+ """Utility function to get the additional unrecognized fields from a dict.
368
+ Read ADR 0008."""
369
+
370
+ unrecognized_fields = {}
371
+ for key , value in all_fields .items ():
372
+ if key not in recognized_fields :
373
+ unrecognized_fields [key ] = value
374
+
375
+ return unrecognized_fields
376
+
377
+
364
378
class Root (Signed ):
365
379
"""A container for the signed part of root metadata.
366
380
@@ -393,6 +407,9 @@ class Root(Signed):
393
407
},
394
408
...
395
409
}
410
+ unrecognized_fields: An optional dictionary used to store any
411
+ unrecognized fields. Needed for backward compatibility.
412
+ Read ADR 0008.
396
413
397
414
"""
398
415
@@ -409,12 +426,14 @@ def __init__(
409
426
consistent_snapshot : bool ,
410
427
keys : Mapping [str , Any ],
411
428
roles : Mapping [str , Any ],
429
+ unrecognized_fields : Optional [Mapping [str , Any ]] = None ,
412
430
) -> None :
413
431
super ().__init__ (_type , version , spec_version , expires )
414
432
# TODO: Add classes for keys and roles
415
433
self .consistent_snapshot = consistent_snapshot
416
434
self .keys = keys
417
435
self .roles = roles
436
+ self .unrecognized_fields = unrecognized_fields
418
437
419
438
@classmethod
420
439
def from_dict (cls , root_dict : Mapping [str , Any ]) -> "Root" :
@@ -423,7 +442,22 @@ def from_dict(cls, root_dict: Mapping[str, Any]) -> "Root":
423
442
consistent_snapshot = root_dict .pop ("consistent_snapshot" )
424
443
keys = root_dict .pop ("keys" )
425
444
roles = root_dict .pop ("roles" )
426
- return cls (* common_args , consistent_snapshot , keys , roles )
445
+ # Store unregonized fields. Read ADR 0008.
446
+ expected_fields = [
447
+ "_type" ,
448
+ "version" ,
449
+ "spec_version" ,
450
+ "expires" ,
451
+ "consistent_snapshot" ,
452
+ "keys" ,
453
+ "roles" ,
454
+ ]
455
+ unrecognized_fields = _get_unrecognized_fields (
456
+ root_dict , expected_fields
457
+ )
458
+ return cls (
459
+ * common_args , consistent_snapshot , keys , roles , unrecognized_fields
460
+ )
427
461
428
462
def to_dict (self ) -> Dict [str , Any ]:
429
463
"""Returns the dict representation of self. """
@@ -433,6 +467,7 @@ def to_dict(self) -> Dict[str, Any]:
433
467
"consistent_snapshot" : self .consistent_snapshot ,
434
468
"keys" : self .keys ,
435
469
"roles" : self .roles ,
470
+ ** self .unrecognized_fields ,
436
471
}
437
472
)
438
473
return root_dict
@@ -475,6 +510,9 @@ class Timestamp(Signed):
475
510
}
476
511
}
477
512
}
513
+ unrecognized_fields: An optional dictionary used to store any
514
+ unrecognized fields. Needed for backward compatibility.
515
+ Read ADR 0008.
478
516
479
517
"""
480
518
@@ -485,22 +523,35 @@ def __init__(
485
523
spec_version : str ,
486
524
expires : datetime ,
487
525
meta : Mapping [str , Any ],
526
+ unrecognized_fields : Optional [Mapping [str , Any ]] = None ,
488
527
) -> None :
489
528
super ().__init__ (_type , version , spec_version , expires )
490
529
# TODO: Add class for meta
491
530
self .meta = meta
531
+ self .unrecognized_fields = unrecognized_fields
492
532
493
533
@classmethod
494
534
def from_dict (cls , timestamp_dict : Mapping [str , Any ]) -> "Timestamp" :
495
535
"""Creates Timestamp object from its dict representation. """
496
536
common_args = cls ._common_fields_from_dict (timestamp_dict )
497
537
meta = timestamp_dict .pop ("meta" )
498
- return cls (* common_args , meta )
538
+ # Store unregonized fields. Read ADR 0008.
539
+ expected_fields = [
540
+ "_type" ,
541
+ "version" ,
542
+ "spec_version" ,
543
+ "expires" ,
544
+ "meta" ,
545
+ ]
546
+ unrecognized_fields = _get_unrecognized_fields (
547
+ timestamp_dict , expected_fields
548
+ )
549
+ return cls (* common_args , meta , unrecognized_fields )
499
550
500
551
def to_dict (self ) -> Dict [str , Any ]:
501
552
"""Returns the dict representation of self. """
502
553
timestamp_dict = self ._common_fields_to_dict ()
503
- timestamp_dict .update ({"meta" : self .meta })
554
+ timestamp_dict .update ({"meta" : self .meta , ** self . unrecognized_fields })
504
555
return timestamp_dict
505
556
506
557
# Modification.
@@ -539,6 +590,9 @@ class Snapshot(Signed):
539
590
},
540
591
...
541
592
}
593
+ unrecognized_fields: An optional dictionary used to store any
594
+ unrecognized fields. Needed for backward compatibility.
595
+ Read ADR 0008.
542
596
543
597
"""
544
598
@@ -549,22 +603,35 @@ def __init__(
549
603
spec_version : str ,
550
604
expires : datetime ,
551
605
meta : Mapping [str , Any ],
606
+ unrecognized_fields : Optional [Mapping [str , Any ]] = None ,
552
607
) -> None :
553
608
super ().__init__ (_type , version , spec_version , expires )
554
609
# TODO: Add class for meta
555
610
self .meta = meta
611
+ self .unrecognized_fields = unrecognized_fields
556
612
557
613
@classmethod
558
614
def from_dict (cls , snapshot_dict : Mapping [str , Any ]) -> "Snapshot" :
559
615
"""Creates Snapshot object from its dict representation. """
560
616
common_args = cls ._common_fields_from_dict (snapshot_dict )
561
617
meta = snapshot_dict .pop ("meta" )
562
- return cls (* common_args , meta )
618
+ # Store unregonized fields. Read ADR 0008.
619
+ expected_fields = [
620
+ "_type" ,
621
+ "version" ,
622
+ "spec_version" ,
623
+ "expires" ,
624
+ "meta" ,
625
+ ]
626
+ unrecognized_fields = _get_unrecognized_fields (
627
+ snapshot_dict , expected_fields
628
+ )
629
+ return cls (* common_args , meta , unrecognized_fields )
563
630
564
631
def to_dict (self ) -> Dict [str , Any ]:
565
632
"""Returns the dict representation of self. """
566
633
snapshot_dict = self ._common_fields_to_dict ()
567
- snapshot_dict .update ({"meta" : self .meta })
634
+ snapshot_dict .update ({"meta" : self .meta , ** self . unrecognized_fields })
568
635
return snapshot_dict
569
636
570
637
# Modification.
@@ -638,6 +705,10 @@ class Targets(Signed):
638
705
]
639
706
}
640
707
708
+ unrecognized_fields: An optional dictionary used to store any
709
+ unrecognized fields. Needed for backward compatibility.
710
+ Read ADR 0008.
711
+
641
712
"""
642
713
643
714
# TODO: determine an appropriate value for max-args and fix places where
@@ -652,19 +723,33 @@ def __init__(
652
723
expires : datetime ,
653
724
targets : Mapping [str , Any ],
654
725
delegations : Mapping [str , Any ],
726
+ unrecognized_fields : Optional [Mapping [str , Any ]] = None ,
655
727
) -> None :
656
728
super ().__init__ (_type , version , spec_version , expires )
657
729
# TODO: Add class for meta
658
730
self .targets = targets
659
731
self .delegations = delegations
732
+ self .unrecognized_fields = unrecognized_fields
660
733
661
734
@classmethod
662
735
def from_dict (cls , targets_dict : Mapping [str , Any ]) -> "Targets" :
663
736
"""Creates Targets object from its dict representation. """
664
737
common_args = cls ._common_fields_from_dict (targets_dict )
665
738
targets = targets_dict .pop ("targets" )
666
739
delegations = targets_dict .pop ("delegations" )
667
- return cls (* common_args , targets , delegations )
740
+ # Store unregonized fields. Read ADR 0008.
741
+ expected_fields = [
742
+ "_type" ,
743
+ "version" ,
744
+ "spec_version" ,
745
+ "expires" ,
746
+ "meta" ,
747
+ ["targets" , "delegations" ],
748
+ ]
749
+ unrecognized_fields = _get_unrecognized_fields (
750
+ targets_dict , expected_fields
751
+ )
752
+ return cls (* common_args , targets , delegations , unrecognized_fields )
668
753
669
754
def to_dict (self ) -> Dict [str , Any ]:
670
755
"""Returns the dict representation of self. """
@@ -673,6 +758,7 @@ def to_dict(self) -> Dict[str, Any]:
673
758
{
674
759
"targets" : self .targets ,
675
760
"delegations" : self .delegations ,
761
+ ** self .unrecognized_fields ,
676
762
}
677
763
)
678
764
return targets_dict
0 commit comments