Skip to content

Commit 28b4e5f

Browse files
committed
fix: Reorg code
1 parent 34f20d3 commit 28b4e5f

File tree

1 file changed

+75
-75
lines changed

1 file changed

+75
-75
lines changed

src/c2pa/c2pa.py

Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,28 +1705,6 @@ class Signer:
17051705
'encoding_error': "Invalid UTF-8 characters in input: {}"
17061706
}
17071707

1708-
def __init__(self, signer_ptr: ctypes.POINTER(C2paSigner)):
1709-
"""Initialize a new Signer instance.
1710-
1711-
Note: This constructor is not meant to be called directly.
1712-
Use from_info() or from_callback() instead.
1713-
1714-
Args:
1715-
signer_ptr: Pointer to the native C2PA signer
1716-
1717-
Raises:
1718-
C2paError: If the signer pointer is invalid
1719-
"""
1720-
# Validate pointer before assignment
1721-
if not signer_ptr:
1722-
raise C2paError("Invalid signer pointer: pointer is null")
1723-
1724-
self._signer = signer_ptr
1725-
self._closed = False
1726-
1727-
# Set only for signers which are callback signers
1728-
self._callback_cb = None
1729-
17301708
@classmethod
17311709
def from_info(cls, signer_info: C2paSignerInfo) -> 'Signer':
17321710
"""Create a new Signer from signer information.
@@ -1893,6 +1871,28 @@ def wrapped_callback(
18931871

18941872
return signer_instance
18951873

1874+
def __init__(self, signer_ptr: ctypes.POINTER(C2paSigner)):
1875+
"""Initialize a new Signer instance.
1876+
1877+
Note: This constructor is not meant to be called directly.
1878+
Use from_info() or from_callback() instead.
1879+
1880+
Args:
1881+
signer_ptr: Pointer to the native C2PA signer
1882+
1883+
Raises:
1884+
C2paError: If the signer pointer is invalid
1885+
"""
1886+
# Validate pointer before assignment
1887+
if not signer_ptr:
1888+
raise C2paError("Invalid signer pointer: pointer is null")
1889+
1890+
self._signer = signer_ptr
1891+
self._closed = False
1892+
1893+
# Set only for signers which are callback signers
1894+
self._callback_cb = None
1895+
18961896
def __enter__(self):
18971897
"""Context manager entry."""
18981898
self._ensure_valid_state()
@@ -2092,6 +2092,51 @@ def get_supported_mime_types(cls) -> list[str]:
20922092

20932093
return cls._supported_mime_types_cache
20942094

2095+
@classmethod
2096+
def from_json(cls, manifest_json: Any) -> 'Builder':
2097+
"""Create a new Builder from a JSON manifest.
2098+
2099+
Args:
2100+
manifest_json: The JSON manifest definition
2101+
2102+
Returns:
2103+
A new Builder instance
2104+
2105+
Raises:
2106+
C2paError: If there was an error creating the builder
2107+
"""
2108+
return cls(manifest_json)
2109+
2110+
@classmethod
2111+
def from_archive(cls, stream: Any) -> 'Builder':
2112+
"""Create a new Builder from an archive stream.
2113+
2114+
Args:
2115+
stream: The stream containing the archive
2116+
(any Python stream-like object)
2117+
2118+
Returns:
2119+
A new Builder instance
2120+
2121+
Raises:
2122+
C2paError: If there was an error creating the builder from archive
2123+
"""
2124+
builder = cls({})
2125+
stream_obj = Stream(stream)
2126+
builder._builder = _lib.c2pa_builder_from_archive(stream_obj._stream)
2127+
2128+
if not builder._builder:
2129+
# Clean up the stream object if builder creation fails
2130+
stream_obj.close()
2131+
2132+
error = _parse_operation_result_for_error(_lib.c2pa_error())
2133+
if error:
2134+
raise C2paError(error)
2135+
raise C2paError("Failed to create builder from archive")
2136+
2137+
builder._initialized = True
2138+
return builder
2139+
20952140
def __init__(self, manifest_json: Any):
20962141
"""Initialize a new Builder instance.
20972142
@@ -2136,50 +2181,16 @@ def __init__(self, manifest_json: Any):
21362181

21372182
self._initialized = True
21382183

2139-
@classmethod
2140-
def from_json(cls, manifest_json: Any) -> 'Builder':
2141-
"""Create a new Builder from a JSON manifest.
2142-
2143-
Args:
2144-
manifest_json: The JSON manifest definition
2145-
2146-
Returns:
2147-
A new Builder instance
2148-
2149-
Raises:
2150-
C2paError: If there was an error creating the builder
2151-
"""
2152-
return cls(manifest_json)
2153-
2154-
@classmethod
2155-
def from_archive(cls, stream: Any) -> 'Builder':
2156-
"""Create a new Builder from an archive stream.
2157-
2158-
Args:
2159-
stream: The stream containing the archive
2160-
(any Python stream-like object)
2161-
2162-
Returns:
2163-
A new Builder instance
2164-
2165-
Raises:
2166-
C2paError: If there was an error creating the builder from archive
2167-
"""
2168-
builder = cls({})
2169-
stream_obj = Stream(stream)
2170-
builder._builder = _lib.c2pa_builder_from_archive(stream_obj._stream)
2171-
2172-
if not builder._builder:
2173-
# Clean up the stream object if builder creation fails
2174-
stream_obj.close()
2184+
def __del__(self):
2185+
"""Ensure resources are cleaned up if close() wasn't called."""
2186+
self._cleanup_resources()
21752187

2176-
error = _parse_operation_result_for_error(_lib.c2pa_error())
2177-
if error:
2178-
raise C2paError(error)
2179-
raise C2paError("Failed to create builder from archive")
2188+
def __enter__(self):
2189+
self._ensure_valid_state()
2190+
return self
21802191

2181-
builder._initialized = True
2182-
return builder
2192+
def __exit__(self, exc_type, exc_val, exc_tb):
2193+
self.close()
21832194

21842195
def _ensure_valid_state(self):
21852196
"""Ensure the builder is in a valid state for operations.
@@ -2225,10 +2236,6 @@ def _cleanup_resources(self):
22252236
# Ensure we don't raise exceptions during cleanup
22262237
pass
22272238

2228-
def __del__(self):
2229-
"""Ensure resources are cleaned up if close() wasn't called."""
2230-
self._cleanup_resources()
2231-
22322239
def close(self):
22332240
"""Release the builder resources.
22342241
@@ -2251,13 +2258,6 @@ def close(self):
22512258
finally:
22522259
self._closed = True
22532260

2254-
def __enter__(self):
2255-
self._ensure_valid_state()
2256-
return self
2257-
2258-
def __exit__(self, exc_type, exc_val, exc_tb):
2259-
self.close()
2260-
22612261
def set_no_embed(self):
22622262
"""Set the no-embed flag.
22632263

0 commit comments

Comments
 (0)