Skip to content

Commit 9bf7001

Browse files
authored
fix: Update docs (#128)
* fix: Docs * fix: Clean up repeat * fix: Typo
1 parent 23be719 commit 9bf7001

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

docs/usage.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Import the objects needed from the API:
1212
from c2pa import Builder, Reader, Signer, C2paSigningAlg, C2paSignerInfo
1313
```
1414

15-
You can use both `Builder` and `Reader` classes with context managers by using a `with` statement.
15+
You can use both `Builder`, `Reader` and `Signer` classes with context managers by using a `with` statement.
1616
Doing this is recommended to ensure proper resource and memory cleanup.
1717

1818
## Define manifest JSON
@@ -25,13 +25,15 @@ manifest_json = json.dumps({
2525
"claim_generator": "python_test/0.1",
2626
"assertions": [
2727
{
28-
"label": "c2pa.training-mining",
28+
"label": "cawg.training-mining",
2929
"data": {
3030
"entries": {
31-
"c2pa.ai_generative_training": { "use": "notAllowed" },
32-
"c2pa.ai_inference": { "use": "notAllowed" },
33-
"c2pa.ai_training": { "use": "notAllowed" },
34-
"c2pa.data_mining": { "use": "notAllowed" }
31+
"cawg.ai_inference": {
32+
"use": "notAllowed"
33+
},
34+
"cawg.ai_generative_training": {
35+
"use": "notAllowed"
36+
}
3537
}
3638
}
3739
}
@@ -62,7 +64,7 @@ try:
6264
manifest = json.loads(reader.json())
6365
active_manifest = manifest["manifests"][manifest["active_manifest"]]
6466
if active_manifest:
65-
# get the uri to the manifest's thumbnail and write it to a file
67+
# Get the uri to the manifest's thumbnail and write it to a file
6668
uri = active_manifest["thumbnail"]["identifier"]
6769
with open("thumbnail_v2.jpg", "wb") as f:
6870
reader.resource_to_stream(uri, f)
@@ -106,7 +108,7 @@ try:
106108
with open("path/to/source.jpg", "rb") as source_file, open("path/to/output.jpg", "wb") as dest_file:
107109
manifest_bytes = builder.sign(signer, "image/jpeg", source_file, dest_file)
108110

109-
# Verify the signed file
111+
# Verify the signed file by reading data from the signed output file
110112
with Reader("path/to/output.jpg") as reader:
111113
manifest_store = json.loads(reader.json())
112114
active_manifest = manifest_store["manifests"][manifest_store["active_manifest"]]
@@ -126,13 +128,13 @@ Instead of working with files, you can read, validate, and add a signed manifest
126128
try:
127129
# Create a reader from a format and stream
128130
with open("path/to/media_file.jpg", "rb") as stream:
129-
# First parameter can be mimetype or extension of the file
131+
# First parameter should be the type of the file (here, we use the mimetype)
130132
# But in any case we need something to identify the file type
131133
with Reader("image/jpeg", stream) as reader:
132134
# Print manifest store as JSON, as extracted by the Reader
133135
print("manifest store:", reader.json())
134136

135-
# Get the active manifest.
137+
# Get the active manifest
136138
manifest = json.loads(reader.json())
137139
active_manifest = manifest["manifests"][manifest["active_manifest"]]
138140
if active_manifest:
@@ -158,22 +160,23 @@ try:
158160
cert_data = cert_file.read()
159161
key_data = key_file.read()
160162

161-
# Create signer info
163+
# Create signer info using the read certificate and key data
162164
signer_info = C2paSignerInfo(
163165
alg=C2paSigningAlg.PS256,
164166
cert=cert_data,
165167
key=key_data,
166168
timestamp_url="http://timestamp.digicert.com"
167169
)
168170

169-
# Create signer using the defined SignerInfo
171+
# Create a Signer using the SignerInfo defined previously
170172
signer = Signer.from_info(signer_info)
171173

172-
# Create builder with manifest and add ingredients
174+
# Create a Builder with manifest and add ingredients
173175
with Builder(manifest_json) as builder:
174-
# Add any ingredients if needed
176+
# Add any ingredients as needed
175177
with open("path/to/ingredient.jpg", "rb") as ingredient_file:
176178
ingredient_json = json.dumps({"title": "Ingredient Image"})
179+
# Here the ingredient is added using streams
177180
builder.add_ingredient(ingredient_json, "image/jpeg", ingredient_file)
178181

179182
# Sign using streams
@@ -182,6 +185,7 @@ try:
182185

183186
# Verify the signed file
184187
with open("path/to/output.jpg", "rb") as stream:
188+
# Create a Reader to read data
185189
with Reader("image/jpeg", stream) as reader:
186190
manifest_store = json.loads(reader.json())
187191
active_manifest = manifest_store["manifests"][manifest_store["active_manifest"]]

tests/test_unit_tests.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ def test_reader_double_close(self):
9595
with self.assertRaises(Error):
9696
reader.json()
9797

98+
def test_reader_streams_with_nested(self):
99+
with open(self.testPath, "rb") as file:
100+
with Reader("image/jpeg", file) as reader:
101+
manifest_store = json.loads(reader.json())
102+
title = manifest_store["manifests"][manifest_store["active_manifest"]]["title"]
103+
self.assertEqual(title, DEFAULT_TEST_FILE_NAME)
104+
98105
def test_reader_close_cleanup(self):
99106
"""Test that close properly cleans up all resources."""
100107
with open(self.testPath, "rb") as file:

0 commit comments

Comments
 (0)