Skip to content

Commit

Permalink
Remove OrderedDict in favor of python3.7+ dict
Browse files Browse the repository at this point in the history
After we drop support for python3.6 we can relly that dictionaries
preserve the insertion order:
https://docs.python.org/3.7/whatsnew/3.7.html

This means we can replace the usage of OrderedDict with a standard
dictionaries.

Something we have to keep in mind is that even thought the insertion
order is preserved the equality comparison for normal dicts is
insensitive for normal dicts compared to OrderedDict

For example:
>>> OrderedDict([(1,1), (2,2)]) == OrderedDict([(2,2), (1,1)])
False
>>> dict([(1,1), (2,2)]) == dict([(2,2), (1,1)])
True

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
  • Loading branch information
MVrachev committed Jan 19, 2022
1 parent 0f59f4b commit e3b267e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 36 deletions.
34 changes: 14 additions & 20 deletions examples/repo_example/basic_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"""
import os
import tempfile
from collections import OrderedDict
from datetime import datetime, timedelta
from pathlib import Path
from typing import Any, Dict
Expand Down Expand Up @@ -103,7 +102,7 @@ def _in(days: float) -> datetime:
signed=Targets(
version=1, spec_version=SPEC_VERSION, expires=_in(7), targets={}
),
signatures=OrderedDict(),
signatures={},
)

# For the purpose of this example we use the top-level targets role to protect
Expand Down Expand Up @@ -134,7 +133,7 @@ def _in(days: float) -> datetime:
expires=_in(7),
meta={"targets.json": MetaFile(version=1)},
),
OrderedDict(),
{},
)

# Timestamp (freshness)
Expand All @@ -156,7 +155,7 @@ def _in(days: float) -> datetime:
expires=_in(1),
snapshot_meta=MetaFile(version=1),
),
OrderedDict(),
{},
)

# Root (root of trust)
Expand Down Expand Up @@ -195,7 +194,7 @@ def _in(days: float) -> datetime:
},
consistent_snapshot=True,
),
signatures=OrderedDict(),
signatures={},
)

# NOTE: We only need the public part to populate root, so it is possible to use
Expand Down Expand Up @@ -292,7 +291,7 @@ def _in(days: float) -> datetime:
expires=_in(7),
targets={target_path: target_file_info},
),
signatures=OrderedDict(),
signatures={},
)


Expand All @@ -313,20 +312,15 @@ def _in(days: float) -> datetime:
keys[delegatee_name]
)
},
roles=OrderedDict(
[
(
delegatee_name,
DelegatedRole(
name=delegatee_name,
keyids=[keys[delegatee_name]["keyid"]],
threshold=1,
terminating=True,
paths=["*.py"],
),
)
]
),
roles={
delegatee_name: DelegatedRole(
name=delegatee_name,
keyids=[keys[delegatee_name]["keyid"]],
threshold=1,
terminating=True,
paths=["*.py"],
),
},
)

# Remove target file info from top-level targets (delegatee is now responsible)
Expand Down
7 changes: 3 additions & 4 deletions examples/repo_example/hashed_bin_delegation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import hashlib
import os
import tempfile
from collections import OrderedDict
from datetime import datetime, timedelta
from pathlib import Path
from typing import Any, Dict, Iterator, List, Tuple
Expand Down Expand Up @@ -160,10 +159,10 @@ def find_hash_bin(path: str) -> str:
keys["bin-n"]
)
},
roles=OrderedDict(),
roles={},
),
),
signatures=OrderedDict(),
signatures={},
)

# The hash bin generator yields an ordered list of incremental hash bin names
Expand All @@ -190,7 +189,7 @@ def find_hash_bin(path: str) -> str:
signed=Targets(
version=1, spec_version=SPEC_VERSION, expires=_in(7), targets={}
),
signatures=OrderedDict(),
signatures={},
)

# Add target file
Expand Down
13 changes: 6 additions & 7 deletions tests/repository_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import logging
import os
import tempfile
from collections import OrderedDict
from dataclasses import dataclass, field
from datetime import datetime, timedelta
from typing import Dict, Iterator, List, Optional, Tuple
Expand Down Expand Up @@ -167,15 +166,15 @@ def _initialize(self) -> None:
"""Setup a minimal valid repository."""

targets = Targets(1, SPEC_VER, self.safe_expiry, {}, None)
self.md_targets = Metadata(targets, OrderedDict())
self.md_targets = Metadata(targets, {})

meta = {"targets.json": MetaFile(targets.version)}
snapshot = Snapshot(1, SPEC_VER, self.safe_expiry, meta)
self.md_snapshot = Metadata(snapshot, OrderedDict())
self.md_snapshot = Metadata(snapshot, {})

snapshot_meta = MetaFile(snapshot.version)
timestamp = Timestamp(1, SPEC_VER, self.safe_expiry, snapshot_meta)
self.md_timestamp = Metadata(timestamp, OrderedDict())
self.md_timestamp = Metadata(timestamp, {})

roles = {role_name: Role([], 1) for role_name in TOP_LEVEL_ROLE_NAMES}
root = Root(1, SPEC_VER, self.safe_expiry, {}, roles, True)
Expand All @@ -185,7 +184,7 @@ def _initialize(self) -> None:
root.add_key(role, key)
self.add_signer(role, signer)

self.md_root = Metadata(root, OrderedDict())
self.md_root = Metadata(root, {})
self.publish_root()

def publish_root(self) -> None:
Expand Down Expand Up @@ -357,7 +356,7 @@ def add_delegation(

# Create delegation
if delegator.delegations is None:
delegator.delegations = Delegations({}, OrderedDict())
delegator.delegations = Delegations({}, {})
# put delegation last by default
delegator.delegations.roles[role.name] = role

Expand All @@ -368,7 +367,7 @@ def add_delegation(

# Add metadata for the role
if role.name not in self.md_delegates:
self.md_delegates[role.name] = Metadata(targets, OrderedDict())
self.md_delegates[role.name] = Metadata(targets, {})

def write(self) -> None:
"""Dump current repository metadata to self.dump_dir
Expand Down
9 changes: 4 additions & 5 deletions tuf/api/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import io
import logging
import tempfile
from collections import OrderedDict
from datetime import datetime
from typing import (
IO,
Expand Down Expand Up @@ -113,7 +112,7 @@ class Metadata(Generic[T]):
signing the canonical serialized representation of 'signed'.
"""

def __init__(self, signed: T, signatures: "OrderedDict[str, Signature]"):
def __init__(self, signed: T, signatures: Dict[str, Signature]):
self.signed: T = signed
self.signatures = signatures

Expand Down Expand Up @@ -150,7 +149,7 @@ def from_dict(cls, metadata: Dict[str, Any]) -> "Metadata[T]":
raise ValueError(f'unrecognized metadata type "{_type}"')

# Make sure signatures are unique
signatures: "OrderedDict[str, Signature]" = OrderedDict()
signatures: Dict[str, Signature] = {}
for sig_dict in metadata.pop("signatures"):
sig = Signature.from_dict(sig_dict)
if sig.keyid in signatures:
Expand Down Expand Up @@ -1211,7 +1210,7 @@ class Delegations:
def __init__(
self,
keys: Dict[str, Key],
roles: "OrderedDict[str, DelegatedRole]",
roles: Dict[str, DelegatedRole],
unrecognized_fields: Optional[Mapping[str, Any]] = None,
):
self.keys = keys
Expand All @@ -1233,7 +1232,7 @@ def from_dict(cls, delegations_dict: Dict[str, Any]) -> "Delegations":
for keyid, key_dict in keys.items():
keys_res[keyid] = Key.from_dict(keyid, key_dict)
roles = delegations_dict.pop("roles")
roles_res: "OrderedDict[str, DelegatedRole]" = OrderedDict()
roles_res: Dict[str, DelegatedRole] = {}
for role_dict in roles:
new_role = DelegatedRole.from_dict(role_dict)
if new_role.name in roles_res:
Expand Down

0 comments on commit e3b267e

Please sign in to comment.