Skip to content

Commit 826c55d

Browse files
committed
fix: Some more convenience methods
1 parent 05a8380 commit 826c55d

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

src/c2pa/c2pa.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,6 @@ def get_active_manifest(self) -> dict:
16601660
Raises:
16611661
C2paError: If there was an error getting the manifest JSON
16621662
KeyError: If the active_manifest key is missing from the JSON
1663-
ValueError: If the active manifest ID is not found in the manifests
16641663
"""
16651664
# Self-read to get the JSON
16661665
manifest_json_str = self.json()
@@ -1681,10 +1680,42 @@ def get_active_manifest(self) -> dict:
16811680

16821681
manifests = manifest_data["manifests"]
16831682
if active_manifest_id not in manifests:
1684-
raise ValueError("Active manifest idnot found in manifests")
1683+
raise KeyError("Active manifest id not found in manifest store")
16851684

16861685
return manifests[active_manifest_id]
16871686

1687+
def get_manifest_by_label(self, label: str) -> dict:
1688+
"""Get a specific manifest from the manifest store by its label.
1689+
1690+
This method retrieves the manifest JSON and extracts the manifest
1691+
that corresponds to the provided manifest label/ID.
1692+
1693+
Args:
1694+
label: The manifest label/ID to look up in the manifest store
1695+
1696+
Returns:
1697+
A dictionary containing the manifest data for the specified label.
1698+
1699+
Raises:
1700+
C2paError: If there was an error getting the manifest JSON
1701+
KeyError: If the manifests key is missing from the JSON
1702+
"""
1703+
# Self-read to get the JSON
1704+
manifest_json_str = self.json()
1705+
try:
1706+
manifest_data = json.loads(manifest_json_str)
1707+
except json.JSONDecodeError as e:
1708+
raise C2paError(f"Failed to parse manifest JSON: {str(e)}") from e
1709+
1710+
if "manifests" not in manifest_data:
1711+
raise KeyError("No 'manifests' key found in manifest data")
1712+
1713+
manifests = manifest_data["manifests"]
1714+
if label not in manifests:
1715+
raise KeyError("Manifest not found in manifest store")
1716+
1717+
return manifests[label]
1718+
16881719
def get_validation_state(self) -> Optional[str]:
16891720
"""Get the validation state of the manifest store.
16901721

tests/test_unit_tests.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def test_stream_read(self):
6464
json_data = reader.json()
6565
self.assertIn(DEFAULT_TEST_FILE_NAME, json_data)
6666

67-
def test_stream_read_get_active_manifest(self):
67+
def test_get_active_manifest(self):
6868
with open(self.testPath, "rb") as file:
6969
reader = Reader("image/jpeg", file)
7070
active_manifest = reader.get_active_manifest()
@@ -73,6 +73,47 @@ def test_stream_read_get_active_manifest(self):
7373
expected_label = "contentauth:urn:uuid:c85a2b90-f1a0-4aa4-b17f-f938b475804e"
7474
self.assertEqual(active_manifest["label"], expected_label)
7575

76+
def test_get_manifest_by_label(self):
77+
with open(self.testPath, "rb") as file:
78+
reader = Reader("image/jpeg", file)
79+
80+
# Test getting manifest by the specific label
81+
label = "contentauth:urn:uuid:c85a2b90-f1a0-4aa4-b17f-f938b475804e"
82+
manifest = reader.get_manifest_by_label(label)
83+
84+
# Check that we got the correct manifest
85+
self.assertEqual(manifest["label"], label)
86+
87+
# Verify it's the same as the active manifest (since there's only one)
88+
active_manifest = reader.get_active_manifest()
89+
self.assertEqual(manifest, active_manifest)
90+
91+
def test_stream_get_non_active_manifest_by_label(self):
92+
video_path = os.path.join(FIXTURES_DIR, "video1.mp4")
93+
with open(video_path, "rb") as file:
94+
reader = Reader("video/mp4", file)
95+
96+
non_active_label = "urn:uuid:54281c07-ad34-430e-bea5-112a18facf0b"
97+
non_active_manifest = reader.get_manifest_by_label(non_active_label)
98+
99+
# Check that we got the correct manifest
100+
self.assertEqual(non_active_manifest["label"], non_active_label)
101+
102+
# Verify it's not the active manifest
103+
active_manifest = reader.get_active_manifest()
104+
self.assertNotEqual(non_active_manifest, active_manifest)
105+
self.assertNotEqual(non_active_manifest["label"], active_manifest["label"])
106+
107+
def test_stream_get_non_active_manifest_by_label_not_found(self):
108+
video_path = os.path.join(FIXTURES_DIR, "video1.mp4")
109+
with open(video_path, "rb") as file:
110+
reader = Reader("video/mp4", file)
111+
112+
# Try to get a manifest with a label that clearly doesn't exist
113+
non_existing_label = "urn:uuid:clearly-not-existing"
114+
with self.assertRaises(KeyError):
115+
reader.get_manifest_by_label(non_existing_label)
116+
76117
def test_stream_read_get_validation_state(self):
77118
with open(self.testPath, "rb") as file:
78119
reader = Reader("image/jpeg", file)

0 commit comments

Comments
 (0)