Skip to content

Commit d211c0f

Browse files
authored
fix: Add an API (#186)
1 parent dd24813 commit d211c0f

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/c2pa/c2pa.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
'c2pa_reader_from_manifest_data_and_stream',
4242
'c2pa_reader_free',
4343
'c2pa_reader_json',
44+
'c2pa_reader_detailed_json',
4445
'c2pa_reader_resource_to_stream',
4546
'c2pa_builder_from_json',
4647
'c2pa_builder_from_archive',
@@ -379,6 +380,9 @@ def _setup_function(func, argtypes, restype=None):
379380
_setup_function(
380381
_lib.c2pa_reader_json, [
381382
ctypes.POINTER(C2paReader)], ctypes.c_void_p)
383+
_setup_function(
384+
_lib.c2pa_reader_detailed_json, [
385+
ctypes.POINTER(C2paReader)], ctypes.c_void_p)
382386
_setup_function(_lib.c2pa_reader_resource_to_stream, [ctypes.POINTER(
383387
C2paReader), ctypes.c_char_p, ctypes.POINTER(C2paStream)], ctypes.c_int64)
384388
_setup_function(
@@ -1737,6 +1741,34 @@ def json(self) -> str:
17371741
self._manifest_json_str_cache = _convert_to_py_string(result)
17381742
return self._manifest_json_str_cache
17391743

1744+
def detailed_json(self) -> str:
1745+
"""Get the detailed JSON representation of the C2PA manifest store.
1746+
1747+
This method returns a more detailed JSON string than Reader.json(),
1748+
providing additional information about the manifest structure.
1749+
Note that the returned JSON by this method has a slightly different
1750+
structure than the one returned by Reader.json().
1751+
1752+
Returns:
1753+
A JSON string containing the detailed manifest store data.
1754+
1755+
Raises:
1756+
C2paError: If there is an error reading the manifest data or if
1757+
the Reader has been closed.
1758+
"""
1759+
1760+
self._ensure_valid_state()
1761+
1762+
result = _lib.c2pa_reader_detailed_json(self._reader)
1763+
1764+
if result is None:
1765+
error = _parse_operation_result_for_error(_lib.c2pa_error())
1766+
if error:
1767+
raise C2paError(error)
1768+
raise C2paError("Error during detailed manifest parsing in Reader")
1769+
1770+
return _convert_to_py_string(result)
1771+
17401772
def get_active_manifest(self) -> Optional[dict]:
17411773
"""Get the active manifest from the manifest store.
17421774

tests/test_unit_tests.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ 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_detailed(self):
68+
with open(self.testPath, "rb") as file:
69+
reader = Reader("image/jpeg", file)
70+
json_data = reader.detailed_json()
71+
self.assertIn(DEFAULT_TEST_FILE_NAME, json_data)
72+
6773
def test_get_active_manifest(self):
6874
with open(self.testPath, "rb") as file:
6975
reader = Reader("image/jpeg", file)
@@ -142,6 +148,13 @@ def test_stream_read_and_parse(self):
142148
title = manifest_store["manifests"][manifest_store["active_manifest"]]["title"]
143149
self.assertEqual(title, DEFAULT_TEST_FILE_NAME)
144150

151+
def test_stream_read_detailed_and_parse(self):
152+
with open(self.testPath, "rb") as file:
153+
reader = Reader("image/jpeg", file)
154+
manifest_store = json.loads(reader.detailed_json())
155+
title = manifest_store["manifests"][manifest_store["active_manifest"]]["claim"]["dc:title"]
156+
self.assertEqual(title, DEFAULT_TEST_FILE_NAME)
157+
145158
def test_stream_read_string_stream(self):
146159
with Reader(self.testPath) as reader:
147160
json_data = reader.json()

0 commit comments

Comments
 (0)