Skip to content

Commit 5617896

Browse files
author
Lukas Puehringer
committed
Consolidate tuf metadata interface function names
Rename: read_from_json -> from_json_file write_to_json -> to_json_file as_dict -> to_dict as_json -> to_json Signed-off-by: Lukas Puehringer <[email protected]>
1 parent 714f8a1 commit 5617896

File tree

2 files changed

+33
-34
lines changed

2 files changed

+33
-34
lines changed

tests/test_api.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def test_generic_read(self):
9090
('targets', Targets)]:
9191

9292
path = os.path.join(self.repo_dir, 'metadata', metadata + '.json')
93-
metadata_obj = Metadata.read_from_json(path)
93+
metadata_obj = Metadata.from_json_file(path)
9494

9595
# Assert that generic method instantiates the right inner class for
9696
# each metadata type
@@ -104,39 +104,39 @@ def test_generic_read(self):
104104
f.write(json.dumps(bad_metadata).encode('utf-8'))
105105

106106
with self.assertRaises(ValueError):
107-
Metadata.read_from_json(bad_metadata_path)
107+
Metadata.from_json_file(bad_metadata_path)
108108

109109
os.remove(bad_metadata_path)
110110

111111

112112
def test_compact_json(self):
113113
path = os.path.join(self.repo_dir, 'metadata', 'targets.json')
114-
metadata_obj = Metadata.read_from_json(path)
114+
metadata_obj = Metadata.from_json_file(path)
115115
self.assertTrue(
116-
len(metadata_obj.as_json(compact=True)) <
117-
len(metadata_obj.as_json()))
116+
len(metadata_obj.to_json(compact=True)) <
117+
len(metadata_obj.to_json()))
118118

119119

120120
def test_read_write_read_compare(self):
121121
for metadata in ['snapshot', 'timestamp', 'targets']:
122122
path = os.path.join(self.repo_dir, 'metadata', metadata + '.json')
123-
metadata_obj = Metadata.read_from_json(path)
123+
metadata_obj = Metadata.from_json_file(path)
124124

125125
path_2 = path + '.tmp'
126-
metadata_obj.write_to_json(path_2)
127-
metadata_obj_2 = Metadata.read_from_json(path_2)
126+
metadata_obj.to_json_file(path_2)
127+
metadata_obj_2 = Metadata.from_json_file(path_2)
128128

129129
self.assertDictEqual(
130-
metadata_obj.as_dict(),
131-
metadata_obj_2.as_dict())
130+
metadata_obj.to_dict(),
131+
metadata_obj_2.to_dict())
132132

133133
os.remove(path_2)
134134

135135

136136
def test_sign_verify(self):
137137
# Load sample metadata (targets) and assert ...
138138
path = os.path.join(self.repo_dir, 'metadata', 'targets.json')
139-
metadata_obj = Metadata.read_from_json(path)
139+
metadata_obj = Metadata.from_json_file(path)
140140

141141
# ... it has a single existing signature,
142142
self.assertTrue(len(metadata_obj.signatures) == 1)
@@ -181,7 +181,7 @@ def test_metadata_base(self):
181181
# with real data
182182
snapshot_path = os.path.join(
183183
self.repo_dir, 'metadata', 'snapshot.json')
184-
md = Metadata.read_from_json(snapshot_path)
184+
md = Metadata.from_json_file(snapshot_path)
185185

186186
self.assertEqual(md.signed.version, 1)
187187
md.signed.bump_version()
@@ -196,7 +196,7 @@ def test_metadata_base(self):
196196
def test_metadata_snapshot(self):
197197
snapshot_path = os.path.join(
198198
self.repo_dir, 'metadata', 'snapshot.json')
199-
snapshot = Metadata.read_from_json(snapshot_path)
199+
snapshot = Metadata.from_json_file(snapshot_path)
200200

201201
# Create a dict representing what we expect the updated data to be
202202
fileinfo = snapshot.signed.meta
@@ -212,7 +212,7 @@ def test_metadata_snapshot(self):
212212
def test_metadata_timestamp(self):
213213
timestamp_path = os.path.join(
214214
self.repo_dir, 'metadata', 'timestamp.json')
215-
timestamp = Metadata.read_from_json(timestamp_path)
215+
timestamp = Metadata.from_json_file(timestamp_path)
216216

217217
self.assertEqual(timestamp.signed.version, 1)
218218
timestamp.signed.bump_version()

tuf/api/metadata.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,22 @@ def __init__(
7575
self.signed = signed
7676
self.signatures = signatures
7777

78-
def as_dict(self) -> JsonDict:
79-
"""Returns the JSON-serializable dictionary representation of self. """
80-
return {
81-
'signatures': self.signatures,
82-
'signed': self.signed.as_dict()
83-
}
8478

85-
def as_json(self, compact: bool = False) -> None:
79+
def to_json(self, compact: bool = False) -> None:
8680
"""Returns the optionally compacted JSON representation of self. """
8781
return json.dumps(
88-
self.as_dict(),
82+
self.to_dict(),
8983
indent=(None if compact else 1),
9084
separators=((',', ':') if compact else (',', ': ')),
9185
sort_keys=True)
9286

87+
def to_dict(self) -> JsonDict:
88+
"""Returns the JSON-serializable dictionary representation of self. """
89+
return {
90+
'signatures': self.signatures,
91+
'signed': self.signed.to_dict()
92+
}
93+
9394
def sign(self, key: JsonDict, append: bool = False) -> JsonDict:
9495
"""Creates signature over 'signed' and assigns it to 'signatures'.
9596
@@ -151,9 +152,8 @@ def verify(self, key: JsonDict) -> bool:
151152

152153
return True
153154

154-
155155
@classmethod
156-
def read_from_json(
156+
def from_json_file(
157157
cls, filename: str,
158158
storage_backend: Optional[StorageBackendInterface] = None
159159
) -> 'Metadata':
@@ -196,7 +196,7 @@ def read_from_json(
196196
signed=inner_cls(**signable['signed']),
197197
signatures=signable['signatures'])
198198

199-
def write_to_json(
199+
def to_json_file(
200200
self, filename: str, compact: bool = False,
201201
storage_backend: StorageBackendInterface = None) -> None:
202202
"""Writes the JSON representation of self to file storage.
@@ -214,10 +214,9 @@ def write_to_json(
214214
215215
"""
216216
with tempfile.TemporaryFile() as f:
217-
f.write(self.as_json(compact).encode('utf-8'))
217+
f.write(self.to_json(compact).encode('utf-8'))
218218
persist_temp_file(f, filename, storage_backend)
219219

220-
221220
class Signed:
222221
"""A base class for the signed part of TUF metadata.
223222
@@ -278,7 +277,7 @@ def bump_version(self) -> None:
278277
"""Increments the metadata version number by 1."""
279278
self.version += 1
280279

281-
def as_dict(self) -> JsonDict:
280+
def to_dict(self) -> JsonDict:
282281
"""Returns the JSON-serializable dictionary representation of self. """
283282
return {
284283
'_type': self._type,
@@ -312,9 +311,9 @@ def __init__(self, meta: JsonDict = None, **kwargs) -> None:
312311
# TODO: Is there merit in creating classes for dict fields?
313312
self.meta = meta
314313

315-
def as_dict(self) -> JsonDict:
314+
def to_dict(self) -> JsonDict:
316315
"""Returns the JSON-serializable dictionary representation of self. """
317-
json_dict = super().as_dict()
316+
json_dict = super().to_dict()
318317
json_dict.update({
319318
'meta': self.meta
320319
})
@@ -361,9 +360,9 @@ def __init__(self, meta: JsonDict = None, **kwargs) -> None:
361360
super().__init__(**kwargs)
362361
self.meta = meta
363362

364-
def as_dict(self) -> JsonDict:
363+
def to_dict(self) -> JsonDict:
365364
"""Returns the JSON-serializable dictionary representation of self. """
366-
json_dict = super().as_dict()
365+
json_dict = super().to_dict()
367366
json_dict.update({
368367
'meta': self.meta
369368
})
@@ -446,9 +445,9 @@ def __init__(
446445
self.targets = targets
447446
self.delegations = delegations
448447

449-
def as_dict(self) -> JsonDict:
448+
def to_dict(self) -> JsonDict:
450449
"""Returns the JSON-serializable dictionary representation of self. """
451-
json_dict = super().as_dict()
450+
json_dict = super().to_dict()
452451
json_dict.update({
453452
'targets': self.targets,
454453
'delegations': self.delegations,

0 commit comments

Comments
 (0)