Skip to content

Commit 2d793cf

Browse files
committed
fix: Update formats
1 parent 40f7573 commit 2d793cf

File tree

1 file changed

+85
-62
lines changed

1 file changed

+85
-62
lines changed

src/c2pa/c2pa.py

Lines changed: 85 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,7 @@ def __init__(self,
13581358
# we may have opened ourselves, and that we need to close later
13591359
self._backing_file = None
13601360

1361-
# Cache for manifest JSON string and parsed data to avoid multiple calls
1361+
# Caches for manifest JSON string and parsed data
13621362
self._manifest_json_str_cache = None
13631363
self._manifest_data_cache = None
13641364

@@ -1604,6 +1604,30 @@ def _cleanup_resources(self):
16041604
# Ensure we don't raise exceptions during cleanup
16051605
pass
16061606

1607+
def _get_cached_manifest_data(self) -> Optional[dict]:
1608+
"""Get the cached manifest data, fetching and parsing if not cached.
1609+
1610+
Returns:
1611+
A dictionary containing the parsed manifest data, or None if
1612+
JSON parsing fails
1613+
1614+
Raises:
1615+
C2paError: If there was an error getting the JSON
1616+
"""
1617+
if self._manifest_data_cache is None:
1618+
if self._manifest_json_str_cache is None:
1619+
self._manifest_json_str_cache = self.json()
1620+
1621+
try:
1622+
self._manifest_data_cache = json.loads(
1623+
self._manifest_json_str_cache
1624+
)
1625+
except json.JSONDecodeError:
1626+
# Failed to parse manifest JSON
1627+
return None
1628+
1629+
return self._manifest_data_cache
1630+
16071631
def close(self):
16081632
"""Release the reader resources.
16091633
@@ -1657,60 +1681,46 @@ def json(self) -> str:
16571681
self._manifest_json_str_cache = _convert_to_py_string(result)
16581682
return self._manifest_json_str_cache
16591683

1660-
def _get_cached_manifest_data(self) -> dict:
1661-
"""Get the cached manifest data, fetching and parsing if not cached.
1662-
1663-
Returns:
1664-
A dictionary containing the parsed manifest data
1665-
1666-
Raises:
1667-
C2paError: If there was an error getting or parsing the JSON
1668-
"""
1669-
if self._manifest_data_cache is None:
1670-
if self._manifest_json_str_cache is None:
1671-
self._manifest_json_str_cache = self.json()
1672-
1673-
try:
1674-
self._manifest_data_cache = json.loads(self._manifest_json_str_cache)
1675-
except json.JSONDecodeError as e:
1676-
raise C2paError(f"Failed to parse manifest JSON: {str(e)}") from e
1677-
1678-
return self._manifest_data_cache
1679-
1680-
def get_active_manifest(self) -> dict:
1684+
def get_active_manifest(self) -> Optional[dict]:
16811685
"""Get the active manifest from the manifest store.
16821686
16831687
This method retrieves the full manifest JSON and extracts the active
16841688
manifest based on the active_manifest key.
16851689
16861690
Returns:
16871691
A dictionary containing the active manifest data, including claims,
1688-
assertions, ingredients, and signature information.
1692+
assertions, ingredients, and signature information, or None if no
1693+
manifest is found or if there was an error parsing the JSON.
16891694
16901695
Raises:
1691-
C2paError: If there was an error getting the manifest JSON
16921696
KeyError: If the active_manifest key is missing from the JSON
16931697
"""
1694-
# Get cached manifest data
1695-
manifest_data = self._get_cached_manifest_data()
1698+
try:
1699+
# Get cached manifest data
1700+
manifest_data = self._get_cached_manifest_data()
1701+
if manifest_data is None:
1702+
# raise C2paError("Failed to parse manifest JSON")
1703+
return None
16961704

1697-
# Get the active manfiest id/label
1698-
if "active_manifest" not in manifest_data:
1699-
raise KeyError("No 'active_manifest' key found in manifest data")
1705+
# Get the active manfiest id/label
1706+
if "active_manifest" not in manifest_data:
1707+
raise KeyError("No 'active_manifest' key found")
17001708

1701-
active_manifest_id = manifest_data["active_manifest"]
1709+
active_manifest_id = manifest_data["active_manifest"]
17021710

1703-
# Retrieve the active manifest data using manifest id/label
1704-
if "manifests" not in manifest_data:
1705-
raise KeyError("No 'manifests' key found in manifest data")
1711+
# Retrieve the active manifest data using manifest id/label
1712+
if "manifests" not in manifest_data:
1713+
raise KeyError("No 'manifests' key found in manifest data")
17061714

1707-
manifests = manifest_data["manifests"]
1708-
if active_manifest_id not in manifests:
1709-
raise KeyError("Active manifest id not found in manifest store")
1715+
manifests = manifest_data["manifests"]
1716+
if active_manifest_id not in manifests:
1717+
raise KeyError("Active manifest not found in manifest store")
17101718

1711-
return manifests[active_manifest_id]
1719+
return manifests[active_manifest_id]
1720+
except C2paError.ManifestNotFound:
1721+
return None
17121722

1713-
def get_manifest_from_label(self, label: str) -> dict:
1723+
def get_manifest_from_label(self, label: str) -> Optional[dict]:
17141724
"""Get a specific manifest from the manifest store by its label.
17151725
17161726
This method retrieves the manifest JSON and extracts the manifest
@@ -1720,23 +1730,30 @@ def get_manifest_from_label(self, label: str) -> dict:
17201730
label: The manifest label/ID to look up in the manifest store
17211731
17221732
Returns:
1723-
A dictionary containing the manifest data for the specified label.
1733+
A dictionary containing the manifest data for the specified label,
1734+
or None if no manifest is found or if there was an error parsing
1735+
the JSON.
17241736
17251737
Raises:
1726-
C2paError: If there was an error getting the manifest JSON
17271738
KeyError: If the manifests key is missing from the JSON
17281739
"""
1729-
# Get cached manifest data
1730-
manifest_data = self._get_cached_manifest_data()
1740+
try:
1741+
# Get cached manifest data
1742+
manifest_data = self._get_cached_manifest_data()
1743+
if manifest_data is None:
1744+
# raise C2paError("Failed to parse manifest JSON")
1745+
return None
17311746

1732-
if "manifests" not in manifest_data:
1733-
raise KeyError("No 'manifests' key found in manifest data")
1747+
if "manifests" not in manifest_data:
1748+
raise KeyError("No 'manifests' key found in manifest data")
17341749

1735-
manifests = manifest_data["manifests"]
1736-
if label not in manifests:
1737-
raise KeyError("Manifest not found in manifest store")
1750+
manifests = manifest_data["manifests"]
1751+
if label not in manifests:
1752+
raise KeyError(f"Manifest {label} not found in manifest store")
17381753

1739-
return manifests[label]
1754+
return manifests[label]
1755+
except C2paError.ManifestNotFound:
1756+
return None
17401757

17411758
def get_validation_state(self) -> Optional[str]:
17421759
"""Get the validation state of the manifest store.
@@ -1747,15 +1764,18 @@ def get_validation_state(self) -> Optional[str]:
17471764
17481765
Returns:
17491766
The validation state as a string,
1750-
or None if the validation_state field is not present.
1751-
1752-
Raises:
1753-
C2paError: If there was an error getting the manifest JSON
1767+
or None if the validation_state field is not present or if no
1768+
manifest is found or if there was an error parsing the JSON.
17541769
"""
1755-
# Get cached manifest data
1756-
manifest_data = self._get_cached_manifest_data()
1770+
try:
1771+
# Get cached manifest data
1772+
manifest_data = self._get_cached_manifest_data()
1773+
if manifest_data is None:
1774+
return None
17571775

1758-
return manifest_data.get("validation_state")
1776+
return manifest_data.get("validation_state")
1777+
except C2paError.ManifestNotFound:
1778+
return None
17591779

17601780
def get_validation_results(self) -> Optional[dict]:
17611781
"""Get the validation results of the manifest store.
@@ -1767,15 +1787,18 @@ def get_validation_results(self) -> Optional[dict]:
17671787
Returns:
17681788
The validation results as a dictionary containing
17691789
validation details, or None if the validation_results
1770-
field is not present.
1771-
1772-
Raises:
1773-
C2paError: If there was an error getting the manifest JSON
1790+
field is not present or if no manifest is found or if
1791+
there was an error parsing the JSON.
17741792
"""
1775-
# Get cached manifest data
1776-
manifest_data = self._get_cached_manifest_data()
1793+
try:
1794+
# Get cached manifest data
1795+
manifest_data = self._get_cached_manifest_data()
1796+
if manifest_data is None:
1797+
return None
17771798

1778-
return manifest_data.get("validation_results")
1799+
return manifest_data.get("validation_results")
1800+
except C2paError.ManifestNotFound:
1801+
return None
17791802

17801803
def resource_to_stream(self, uri: str, stream: Any) -> int:
17811804
"""Write a resource to a stream.

0 commit comments

Comments
 (0)