From 61888cf318878774627400fd556af549bdf1befe Mon Sep 17 00:00:00 2001 From: Brian Potchik Date: Sun, 30 Mar 2025 00:17:27 -0400 Subject: Add Python Metadata API improvements for handling Metadata as values, dictionary item assignment, and array appending. (skip-ci) --- python/metadata.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'python') diff --git a/python/metadata.py b/python/metadata.py index 35a0af37..6d8912a5 100644 --- a/python/metadata.py +++ b/python/metadata.py @@ -34,11 +34,14 @@ class Metadata: handle: Optional[core.BNMetadata] = None ): """ - The 'raw' parameter is no longer needed it was a work around for a python 2 limitation. - To pass raw data into this api simply use a `bytes` object + The 'raw' parameter is no longer needed it was a workaround for a Python 2 limitation. + To pass raw data into this API, simply use a `bytes` object. """ if handle is not None: self.handle = handle + elif isinstance(value, Metadata): + # If the value is already a Metadata object, reuse its handle + self.handle = value.handle elif isinstance(value, bool): self.handle = core.BNCreateMetadataBooleanData(value) elif isinstance(value, int): @@ -64,7 +67,7 @@ class Metadata: md = Metadata(value[elm], signed, raw) core.BNMetadataSetValueForKey(self.handle, str(elm), md.handle) else: - raise ValueError(f"{type(value)} doesn't contain type of: int, bool, str, float, list, dict") + raise ValueError(f"{type(value)} is not a supported type: int, bool, str, bytes, float, list, tuple, dict, or Metadata") def __len__(self): if self.is_array or self.is_dict or self.is_string or self.is_bytes: @@ -146,6 +149,14 @@ class Metadata: raise NotImplementedError("Metadata object doesn't support indexing") + def __setitem__(self, key, value): + if not self.is_dict: + raise TypeError("Metadata object is not a dictionary and does not support item assignment") + if not isinstance(key, str): + raise ValueError("Metadata keys must be strings") + md_value = Metadata(value) + core.BNMetadataSetValueForKey(self.handle, key, md_value.handle) + def __str__(self): if self.is_string: return str(core.BNMetadataGetString(self.handle)) @@ -264,6 +275,13 @@ class Metadata: def is_dict(self): return core.BNMetadataIsKeyValueStore(self.handle) + def append(self, value): + """Appends a value to the Metadata array.""" + if not self.is_array: + raise TypeError("Metadata object is not an array and does not support append") + md_value = Metadata(value) # Convert value to Metadata object + core.BNMetadataArrayAppend(self.handle, md_value.handle) + def remove(self, key_or_index): if isinstance(key_or_index, str) and self.is_dict: core.BNMetadataRemoveKey(self.handle, key_or_index) -- cgit v1.3.1