1from __future__
import annotations
3from typing
import Any, Dict, List, Type, TypeAlias
9T: TypeAlias = IArchive.T
14 A simple key-value store with type checking, similar to a dict.
17 - bool, int, float, str
21 @since ids_peak_icv 1.0
22 @ingroup ids_peak_icv_python_serialization
24 AllowedValue = IArchive.AllowedValue | IArchive
26 def __init__(self, data: Dict[str, Archive.AllowedValue] |
None =
None) ->
None:
30 for key, value
in data.items():
34 def set(self, key: str, value: AllowedValue) ->
None:
37 Stores a value under the given key.
39 @param key: Key to store the value under.
40 @param value: The value to store (restricted types only).
42 @since ids_peak_icv 1.0
47 def get(self, key: str, datatype: Type[T] |
None) -> T:
50 Retrieves the value stored under the given key.
52 @param key: Key of the value to retrieve.
53 @param datatype: The expected datatype of the value.
54 If None is given, the type check is not executed.
56 @exception CorruptedException: If the key is not found or the specified datatype is not the type of the stored value.
58 @return The stored value.
60 @since ids_peak_icv 1.0
67 if datatype
is not type(value)
and datatype
is not None:
68 raise CorruptedException(f
"The value of the specified key '{key}' is not of the expected datatype.")
75 Retrieves the archive stored under the given key.
77 @param key: Key of the archive to retrieve.
79 @exception CorruptedException: If the key is not found or the specified datatype is not of type archive.
81 @return The stored archive.
83 @since ids_peak_icv 1.0
89 if not isinstance(value, IArchive):
90 raise CorruptedException(f
"The value of the specified key '{key}' is not of the expected datatype.")
94 def has(self, key: str) -> bool:
97 Checks whether a given key exists in the archive.
99 @param key: The key to check.
100 @return True if the key exists, False otherwise.
102 @since ids_peak_icv 1.0
109 Returns all keys currently stored in the archive.
111 @return List of keys.
113 @since ids_peak_icv 1.0
120 Creates a new, empty archive instance.
121 @since ids_peak_icv 1.0
128 Human-readable string representation of the archive.
130 @since ids_peak_icv 1.0
135 return f
"Archive({str(self)})"
138 if not isinstance(other, Archive):
151 if isinstance(lhs, Archive)
and isinstance(rhs, Archive):
152 return bool(lhs == rhs)
154 if isinstance(lhs, list)
and isinstance(rhs, list):
155 if len(lhs) != len(rhs):
157 return all(self.
_compare_values(val1, val2)
for val1, val2
in zip(lhs, rhs))
159 return bool(lhs == rhs)
161 _ALLOWED_SCALARS = (bool, int, float, str, IArchive)
166 Ensures the provided value matches the allowed types.
168 @param key: The key for which the value is being validated.
169 @param value: The value to check.
170 @exception NotSupportedException: If the type is not supported.
174 if isinstance(value, list):
178 first_type = type(value[0])
179 if all(isinstance(v, first_type)
for v
in value):
183 f
"Unsupported value type for key '{key}': {type(value).__name__}"
Simplified interface for a hierarchical key-value archive system.
Thrown when input data or a file is malformed, unreadable, or otherwise corrupted.
Thrown when a requested operation is not implemented or supported.
A simple key-value store with type checking, similar to a dict.
T get(self, str key, Type[T]|None datatype)
Retrieves the value stored under the given key.
IArchive create_archive(self)
Creates a new, empty archive instance.
None __init__(self, Dict[str, Archive.AllowedValue]|None data=None)
IArchive get_archive(self, str key)
Retrieves the archive stored under the given key.
List[str] keys(self)
Returns all keys currently stored in the archive.
None set(self, str key, AllowedValue value)
Stores a value under the given key.
bool _compare_values(self, Any lhs, Any rhs)
bool has(self, str key)
Checks whether a given key exists in the archive.
str __str__(self)
Human-readable string representation of the archive.
None _validate_value(self, str key, Any value)
Ensures the provided value matches the allowed types.
bool __eq__(self, Any other)