Skip to content

Commit e262237

Browse files
committed
fix: Refactor 3
1 parent 496ee20 commit e262237

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

src/c2pa/c2pa.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,7 @@ def read_file(path: Union[str, Path],
581581
"The read_file function is deprecated and will be removed in a future version. "
582582
"Please use the Reader class for reading C2PA metadata instead.",
583583
DeprecationWarning,
584-
stacklevel=2
585-
)
584+
stacklevel=2)
586585

587586
container = _StringContainer()
588587

@@ -626,8 +625,7 @@ def sign_file(
626625
"The sign_file function is deprecated and will be removed in a future version. "
627626
"Please use the Builder class for signing and the Reader class for reading signed data instead.",
628627
DeprecationWarning,
629-
stacklevel=2
630-
)
628+
stacklevel=2)
631629

632630
try:
633631
# Create a signer from the signer info
@@ -641,7 +639,8 @@ def sign_file(
641639
# Get the MIME type from the file extension
642640
mime_type = mimetypes.guess_type(str(source_path))[0]
643641
if not mime_type:
644-
raise C2paError.NotSupported(f"Could not determine MIME type for file: {source_path}")
642+
raise C2paError.NotSupported(
643+
f"Could not determine MIME type for file: {source_path}")
645644

646645
# Sign the file using the builder
647646
manifest_bytes = builder.sign(
@@ -713,14 +712,16 @@ def sign_file_using_callback_signer(
713712
# Get the MIME type from the file extension
714713
mime_type = mimetypes.guess_type(str(source_path))[0]
715714
if not mime_type:
716-
raise C2paError.NotSupported(f"Could not determine MIME type for file: {source_path}")
715+
raise C2paError.NotSupported(
716+
f"Could not determine MIME type for file: {source_path}")
717717

718718
# Convert Python streams to Stream objects
719719
source_stream = Stream(source_file)
720720
dest_stream = Stream(dest_file)
721721

722722
# Use the builder's internal signing logic
723-
result, manifest_bytes = builder._sign_internal(signer, mime_type, source_stream, dest_stream)
723+
result, manifest_bytes = builder._sign_internal(
724+
signer, mime_type, source_stream, dest_stream)
724725

725726
return manifest_bytes
726727

@@ -762,7 +763,8 @@ def __init__(self, file):
762763
Raises:
763764
TypeError: If the file object doesn't implement all required methods
764765
"""
765-
# Initialize _closed first to prevent AttributeError during garbage collection
766+
# Initialize _closed first to prevent AttributeError during garbage
767+
# collection
766768
self._closed = False
767769
self._initialized = False
768770
self._stream = None
@@ -1388,14 +1390,21 @@ def from_callback(
13881390
error_messages['invalid_tsa'].format("Invalid TSA URL format"))
13891391

13901392
# Create a wrapper callback that handles errors and memory management
1391-
def wrapped_callback(context, data_ptr, data_len, signed_bytes_ptr, signed_len):
1393+
def wrapped_callback(
1394+
context,
1395+
data_ptr,
1396+
data_len,
1397+
signed_bytes_ptr,
1398+
signed_len):
13921399
# Returns 0 on error as this case is handled in the native code gracefully
13931400
# The reason is that otherwise we ping-pong errors between native code and Python code,
13941401
# which can become tedious in handling. So we let the native code deal with it and
1395-
# raise the errors accordingly, since it already checks the signature length for correctness.
1402+
# raise the errors accordingly, since it already checks the
1403+
# signature length for correctness.
13961404
try:
13971405
if not data_ptr or data_len <= 0:
1398-
# Error: invalid input, native code will handle if seeing signature size being 0
1406+
# Error: invalid input, native code will handle if seeing
1407+
# signature size being 0
13991408
return 0
14001409

14011410
# Convert C pointer to Python bytes
@@ -1410,7 +1419,8 @@ def wrapped_callback(context, data_ptr, data_len, signed_bytes_ptr, signed_len):
14101419
# Error: empty signature, native code will handle that too!
14111420
return 0
14121421

1413-
# Copy the signature back to the C buffer (since callback is sued in native code)
1422+
# Copy the signature back to the C buffer (since callback is
1423+
# sued in native code)
14141424
actual_len = min(len(signature), signed_len)
14151425
for i in range(actual_len):
14161426
signed_bytes_ptr[i] = signature[i]
@@ -1879,10 +1889,12 @@ def _sign_internal(
18791889
# Convert the C pointer to Python bytes
18801890
manifest_bytes = bytes(manifest_bytes_ptr[:result])
18811891
except Exception:
1882-
# If there's any error accessing the memory, just return empty bytes
1892+
# If there's any error accessing the memory, just return
1893+
# empty bytes
18831894
manifest_bytes = b""
18841895
finally:
1885-
# Always free the C-allocated memory, even if we failed to copy it
1896+
# Always free the C-allocated memory, even if we failed to
1897+
# copy it
18861898
try:
18871899
_lib.c2pa_manifest_bytes_free(manifest_bytes_ptr)
18881900
except Exception:
@@ -1942,7 +1954,8 @@ def sign_file(self,
19421954
# Get the MIME type from the file extension
19431955
mime_type = mimetypes.guess_type(str(source_path))[0]
19441956
if not mime_type:
1945-
raise C2paError.NotSupported(f"Could not determine MIME type for file: {source_path}")
1957+
raise C2paError.NotSupported(
1958+
f"Could not determine MIME type for file: {source_path}")
19461959

19471960
# Open source and destination files
19481961
with open(source_path, 'rb') as source_file, open(dest_path, 'wb') as dest_file:
@@ -1951,7 +1964,8 @@ def sign_file(self,
19511964
dest_stream = Stream(dest_file)
19521965

19531966
# Use the internal stream-base signing logic
1954-
result, manifest_bytes = self._sign_internal(signer, mime_type, source_stream, dest_stream)
1967+
result, manifest_bytes = self._sign_internal(
1968+
signer, mime_type, source_stream, dest_stream)
19551969
return result, manifest_bytes
19561970

19571971

tests/test_unit_tests.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -954,15 +954,6 @@ def sign_callback(data: bytes) -> bytes:
954954
self.assertIsInstance(manifest_bytes, bytes)
955955
self.assertGreater(len(manifest_bytes), 0)
956956

957-
# Try to decode as UTF-8 to see if it's text-based (it shouldn't be)
958-
try:
959-
manifest_bytes.decode('utf-8')
960-
# If we get here, it's text-based, which is unexpected
961-
self.fail("Manifest bytes should be binary data, not UTF-8 text")
962-
except UnicodeDecodeError:
963-
# This is expected - manifest bytes should be binary
964-
pass
965-
966957
# Read the signed file and verify the manifest contains expected content
967958
with open(output_path, "rb") as file:
968959
reader = Reader("image/jpeg", file)

0 commit comments

Comments
 (0)