@@ -93,8 +93,10 @@ def getitem(d, key):
9393# V2 signing API example
9494try :
9595 # Read the private key and certificate files
96- key = open (keyFile ,"rb" ).read ()
97- certs = open (pemFile ,"rb" ).read ()
96+ with open (keyFile , "rb" ) as key_file :
97+ key = key_file .read ()
98+ with open (pemFile , "rb" ) as cert_file :
99+ certs = cert_file .read ()
98100
99101 # Create a signer using the new API
100102 signer_info = c2pa .C2paSignerInfo (
@@ -103,66 +105,56 @@ def getitem(d, key):
103105 private_key = key ,
104106 ta_url = b"http://timestamp.digicert.com"
105107 )
106- signer = c2pa .Signer .from_info (signer_info )
107108
108- # Create the builder
109- builder = c2pa .Builder (manifest_json )
109+ with c2pa .Signer .from_info (signer_info ) as signer :
110+ with c2pa .Builder (manifest_json ) as builder :
111+ # Add the thumbnail resource using a stream
112+ with open (fixtures_dir + "A_thumbnail.jpg" , "rb" ) as thumbnail_file :
113+ builder .add_resource ("thumbnail" , thumbnail_file )
110114
111- # Add the thumbnail resource using a stream
112- with open (fixtures_dir + "A_thumbnail.jpg" , "rb" ) as thumbnail_file :
113- builder .add_resource ( "thumbnail " , thumbnail_file )
115+ # Add the ingredient using the correct method
116+ with open (fixtures_dir + "A_thumbnail.jpg" , "rb" ) as ingredient_file :
117+ builder .add_ingredient ( json . dumps ( ingredient_json ), "image/jpeg " , ingredient_file )
114118
115- # Add the ingredient using the correct method
116- with open (fixtures_dir + "A_thumbnail.jpg" , "rb" ) as ingredient_file :
117- builder .add_ingredient (json .dumps (ingredient_json ), "image/jpeg" , ingredient_file )
119+ if os .path .exists (testOutputFile ):
120+ os .remove (testOutputFile )
118121
119- if os .path .exists (testOutputFile ):
120- os .remove (testOutputFile )
122+ # Sign the file using the stream-based sign method
123+ with open (testFile , "rb" ) as source_file :
124+ with open (testOutputFile , "w+b" ) as dest_file :
125+ result = builder .sign (signer , "image/jpeg" , source_file , dest_file )
121126
122- # Sign the file using the stream-based sign method
123- with open (testFile , "rb" ) as source_file :
124- with open (testOutputFile , "w+b" ) as dest_file :
125- result = builder .sign (signer , "image/jpeg" , source_file , dest_file )
126-
127- # As an alternative, you can also use file paths directly during signing:
128- # builder.sign_file(testFile, testOutputFile, signer)
129-
130- # Clean up native resources (using a with statement works too!)
131- signer .close ()
132- builder .close ()
127+ # As an alternative, you can also use file paths directly during signing:
128+ # builder.sign_file(testFile, testOutputFile, signer)
133129
134130except Exception as err :
135- print ("Exception during signing: " , err )
131+ print (f "Exception during signing: { err } " )
136132
137- print ("\n Successfully added do not train manifest to file " + testOutputFile )
133+ print (f "\n Successfully added do not train manifest to file { testOutputFile } " )
138134
139135# now verify the asset and check the manifest for a do not train assertion...
140136
141137allowed = True # opt out model, assume training is ok if the assertion doesn't exist
142138try :
143139 # Create reader using the Reader API
144- reader = c2pa .Reader (testOutputFile )
145-
146- # Retrieve the manifest store
147- manifest_store = json .loads (reader .json ())
148-
149- # Look at data in the active manifest
150- manifest = manifest_store ["manifests" ][manifest_store ["active_manifest" ]]
151- for assertion in manifest ["assertions" ]:
152- if assertion ["label" ] == "cawg.training-mining" :
153- if getitem (assertion , ("data" ,"entries" ,"cawg.ai_generative_training" ,"use" )) == "notAllowed" :
154- allowed = False
155-
156- # Get the ingredient thumbnail and save it to a file using resource_to_stream
157- uri = getitem (manifest ,("ingredients" , 0 , "thumbnail" , "identifier" ))
158- with open (output_dir + "thumbnail_v2.jpg" , "wb" ) as thumbnail_output :
159- reader .resource_to_stream (uri , thumbnail_output )
160-
161- # Clean up native resources (using a with statement works too!)
162- reader .close ()
140+ with c2pa .Reader (testOutputFile ) as reader :
141+ # Retrieve the manifest store
142+ manifest_store = json .loads (reader .json ())
143+
144+ # Look at data in the active manifest
145+ manifest = manifest_store ["manifests" ][manifest_store ["active_manifest" ]]
146+ for assertion in manifest ["assertions" ]:
147+ if assertion ["label" ] == "cawg.training-mining" :
148+ if getitem (assertion , ("data" ,"entries" ,"cawg.ai_generative_training" ,"use" )) == "notAllowed" :
149+ allowed = False
150+
151+ # Get the ingredient thumbnail and save it to a file using resource_to_stream
152+ uri = getitem (manifest ,("ingredients" , 0 , "thumbnail" , "identifier" ))
153+ with open (output_dir + "thumbnail_v2.jpg" , "wb" ) as thumbnail_output :
154+ reader .resource_to_stream (uri , thumbnail_output )
163155
164156except Exception as err :
165- print ("Exception during assertions reading: " , err )
157+ print (f "Exception during assertions reading: { err } " )
166158
167159if allowed :
168160 print ("Training is allowed" )
0 commit comments