Skip to content

Commit 596dfad

Browse files
committed
fix: Merge in latest main
2 parents 5359bcf + 0d369fa commit 596dfad

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/c2pa/c2pa.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,6 +2066,50 @@ def get_supported_mime_types(cls) -> list[str]:
20662066

20672067
return cls._supported_mime_types_cache
20682068

2069+
def __init__(self, manifest_json: Any):
2070+
"""Initialize a new Builder instance.
2071+
2072+
Args:
2073+
manifest_json: The manifest JSON definition (string or dict)
2074+
2075+
Raises:
2076+
C2paError: If there was an error creating the builder
2077+
C2paError.Encoding: If manifest JSON contains invalid UTF-8 chars
2078+
C2paError.Json: If the manifest JSON cannot be serialized
2079+
"""
2080+
self._closed = False
2081+
self._initialized = False
2082+
self._builder = None
2083+
2084+
if not isinstance(manifest_json, str):
2085+
try:
2086+
manifest_json = json.dumps(manifest_json)
2087+
except (TypeError, ValueError) as e:
2088+
raise C2paError.Json(
2089+
Builder._ERROR_MESSAGES['json_error'].format(
2090+
str(e)))
2091+
2092+
try:
2093+
json_str = manifest_json.encode('utf-8')
2094+
except UnicodeError as e:
2095+
raise C2paError.Encoding(
2096+
Builder._ERROR_MESSAGES['encoding_error'].format(
2097+
str(e)))
2098+
2099+
self._builder = _lib.c2pa_builder_from_json(json_str)
2100+
2101+
if not self._builder:
2102+
error = _parse_operation_result_for_error(_lib.c2pa_error())
2103+
if error:
2104+
raise C2paError(error)
2105+
raise C2paError(
2106+
Builder._ERROR_MESSAGES['builder_error'].format(
2107+
"Unknown error"
2108+
)
2109+
)
2110+
2111+
self._initialized = True
2112+
20692113
@classmethod
20702114
def from_json(cls, manifest_json: Any) -> 'Builder':
20712115
"""Create a new Builder from a JSON manifest.
@@ -2232,6 +2276,13 @@ def close(self):
22322276
finally:
22332277
self._closed = True
22342278

2279+
def __enter__(self):
2280+
self._ensure_valid_state()
2281+
return self
2282+
2283+
def __exit__(self, exc_type, exc_val, exc_tb):
2284+
self.close()
2285+
22352286
def set_no_embed(self):
22362287
"""Set the no-embed flag.
22372288

0 commit comments

Comments
 (0)