Skip to content

Commit b8967e2

Browse files
author
Jussi Kukkonen
committed
MetadataBundle: Improve docstrings
Document arguments and exceptions. Signed-off-by: Jussi Kukkonen <[email protected]>
1 parent f2cff95 commit b8967e2

File tree

1 file changed

+73
-12
lines changed

1 file changed

+73
-12
lines changed

tuf/client_rework/metadata_bundle.py

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* Metadata must be loaded/updated in order:
2020
root -> timestamp -> snapshot -> targets -> (other delegated targets)
2121
22+
2223
Exceptions are raised if metadata fails to load in any way.
2324
2425
Example of loading root, timestamp and snapshot:
@@ -121,12 +122,23 @@ def verify_with_threshold(
121122
class MetadataBundle(abc.Mapping):
122123
"""Internal class to keep track of valid metadata in Updater
123124
124-
MetadataBundle ensures that metadata is valid. It provides easy ways to
125-
update the metadata with the caller making decisions on what is updated.
125+
MetadataBundle ensures that the collection of metadata in the bundle valid.
126+
It provides easy ways to update the metadata with the caller making
127+
decisions on what is updated.
126128
"""
127129

128130
def __init__(self, data: bytes):
129-
"""Initialize by loading trusted root metadata"""
131+
"""Initialize bundle by loading trusted root metadata
132+
133+
Args:
134+
data: Trusted root metadata as bytes. Note that this metadata will
135+
will only be verified by itself: it is the source of trust for
136+
all metadata in the bundle.
137+
138+
Raises:
139+
RepositoryError: Metadata failed to load or verify. The actual
140+
error type and content will contain more details.
141+
"""
130142
self._bundle = {} # type: Dict[str: Metadata]
131143
self.reference_time = datetime.utcnow()
132144
self._root_update_finished = False
@@ -136,39 +148,53 @@ def __init__(self, data: bytes):
136148
logger.debug("Updating initial trusted root")
137149
self.update_root(data)
138150

139-
# Implement Mapping
140-
def __getitem__(self, key: str) -> Metadata:
141-
return self._bundle[key]
151+
def __getitem__(self, role: str) -> Metadata:
152+
"""Returns current Metadata for 'role'"""
153+
return self._bundle[role]
142154

143155
def __len__(self) -> int:
156+
"""Returns number of Metadata objects in bundle"""
144157
return len(self._bundle)
145158

146159
def __iter__(self) -> Iterator[Metadata]:
160+
"""Returns iterator over all Metadata objects in bundle"""
147161
return iter(self._bundle)
148162

149163
# Helper properties for top level metadata
150164
@property
151165
def root(self) -> Optional[Metadata]:
166+
"""Current root Metadata or None"""
152167
return self._bundle.get("root")
153168

154169
@property
155170
def timestamp(self) -> Optional[Metadata]:
171+
"""Current timestamp Metadata or None"""
156172
return self._bundle.get("timestamp")
157173

158174
@property
159175
def snapshot(self) -> Optional[Metadata]:
176+
"""Current snapshot Metadata or None"""
160177
return self._bundle.get("snapshot")
161178

162179
@property
163180
def targets(self) -> Optional[Metadata]:
181+
"""Current targets Metadata or None"""
164182
return self._bundle.get("targets")
165183

166184
# Methods for updating metadata
167185
def update_root(self, data: bytes):
168186
"""Verifies and loads 'data' as new root metadata.
169187
170188
Note that an expired intermediate root is considered valid: expiry is
171-
only checked for the final root in root_update_finished()."""
189+
only checked for the final root in root_update_finished().
190+
191+
Args:
192+
data: unverified new root metadata as bytes
193+
194+
Raises:
195+
RepositoryError: Metadata failed to load or verify. The actual
196+
error type and content will contain more details.
197+
"""
172198
if self._root_update_finished:
173199
raise RuntimeError(
174200
"Cannot update root after root update is finished"
@@ -206,7 +232,11 @@ def update_root(self, data: bytes):
206232
logger.debug("Updated root")
207233

208234
def root_update_finished(self):
209-
"""Mark root metadata as final."""
235+
"""Marks root metadata as final and verifies it is not expired
236+
237+
Raises:
238+
ExpiredMetadataError: The final root metadata is expired.
239+
"""
210240
if self._root_update_finished:
211241
raise RuntimeError("Root update is already finished")
212242

@@ -220,7 +250,15 @@ def root_update_finished(self):
220250
logger.debug("Verified final root.json")
221251

222252
def update_timestamp(self, data: bytes):
223-
"""Verifies and loads 'data' as new timestamp metadata."""
253+
"""Verifies and loads 'data' as new timestamp metadata.
254+
255+
Args:
256+
data: unverified new timestamp metadata as bytes
257+
258+
Raises:
259+
RepositoryError: Metadata failed to load or verify. The actual
260+
error type and content will contain more details.
261+
"""
224262
if not self._root_update_finished:
225263
# root_update_finished() not called
226264
raise RuntimeError("Cannot update timestamp before root")
@@ -270,7 +308,15 @@ def update_timestamp(self, data: bytes):
270308

271309
# TODO: remove pylint disable once the hash verification is in metadata.py
272310
def update_snapshot(self, data: bytes): # pylint: disable=too-many-branches
273-
"""Verifies and loads 'data' as new snapshot metadata."""
311+
"""Verifies and loads 'data' as new snapshot metadata.
312+
313+
Args:
314+
data: unverified new snapshot metadata as bytes
315+
316+
Raises:
317+
RepositoryError: Metadata failed to load or verify. The actual
318+
error type and content will contain more details.
319+
"""
274320

275321
if self.timestamp is None:
276322
raise RuntimeError("Cannot update snapshot before timestamp")
@@ -339,15 +385,30 @@ def update_snapshot(self, data: bytes): # pylint: disable=too-many-branches
339385
logger.debug("Updated snapshot")
340386

341387
def update_targets(self, data: bytes):
342-
"""Verifies and loads 'data' as new top-level targets metadata."""
388+
"""Verifies and loads 'data' as new top-level targets metadata.
389+
390+
Args:
391+
data: unverified new targets metadata as bytes
392+
393+
Raises:
394+
RepositoryError: Metadata failed to load or verify. The actual
395+
error type and content will contain more details.
396+
"""
343397
self.update_delegated_targets(data, "targets", "root")
344398

345399
def update_delegated_targets(
346400
self, data: bytes, role_name: str, delegator_name: str
347401
):
348402
"""Verifies and loads 'data' as new metadata for target 'role_name'.
349403
350-
Raises if verification fails
404+
Args:
405+
data: unverified new metadata as bytes
406+
role_name: The role name of the new metadata
407+
delegator_name: The name of the role delegating the new metadata
408+
409+
Raises:
410+
RepositoryError: Metadata failed to load or verify. The actual
411+
error type and content will contain more details.
351412
"""
352413
if self.snapshot is None:
353414
raise RuntimeError("Cannot load targets before snapshot")

0 commit comments

Comments
 (0)