duck.meta¶
This module provides the Meta class, which extends the functionality of os.environ to store application metadata of various data types.
os.environ traditionally only supports string values. The Meta class overcomes this limitation by encoding data types within the environment variables, allowing storage and retrieval of integers, floats, dictionaries, lists, tuples, sets, and booleans.
Example:
from duck.meta import Meta
Meta.set_metadata("config", {"debug": True, "port": 8080})
Meta.set_metadata("message", "Hello, world!")
Meta.set_metadata("pi", 3.14159)
print(Meta.compile()) # Outputs: {'config': {'debug': True, 'port': 8080}, 'message': 'Hello, world!', 'pi': 3.14159}
print(Meta.get_metadata("message")) # Outputs: Hello, world!
print(Meta.get_metadata("config")['port']) # Outputs: 8080
Supported Data Types:
intstrfloatdicttuplelistsetbool
Classes:
Meta: Manages application metadata in environment variables.MetaError: Custom exception for metadata-related errors.
Package Contents¶
Classes¶
Manages application metadata by storing and retrieving data in |
API¶
- class duck.meta.Meta[source]¶
Manages application metadata by storing and retrieving data in
os.environwith type encoding.Class Attributes: meta_keys (list): A list of keys for metadata that has been set using
set_metadata.- classmethod compile() dict[source]¶
Retrieves all metadata stored using
set_metadataand returns it as a dictionary.- Returns:
A dictionary containing all stored metadata.
- Return type:
dict
- exceptional_keys: list¶
[‘DUCK_SERVER_DOMAIN’, ‘DUCK_SERVER_ADDR’, ‘DUCK_DJANGO_ADDR’]
List of keys that are allowed to include
:or;in their values thereby bypassingMetaErrorwhen usingset_metadata.
- classmethod get_absolute_server_url() str[source]¶
Constructs and returns the absolute server URL based on metadata stored for domain, port, and protocol.
- Raises:
MetaError – If any of the required server configuration variables (DUCK_SERVER_DOMAIN, DUCK_SERVER_PORT, DUCK_SERVER_PROTOCOL, DUCK_USES_IPV6) are not set.
- Returns:
The absolute server URL.
- Return type:
str
- classmethod get_absolute_ws_server_url() str[source]¶
Constructs and returns the absolute WebSockets server URL based on metadata stored for domain, port, and protocol.
- Raises:
MetaError – If any of the required server configuration variables (DUCK_SERVER_DOMAIN, DUCK_SERVER_PORT, DUCK_SERVER_PROTOCOL, DUCK_USES_IPV6) are not set.
- Returns:
The absolute WebSockets server URL.
- Return type:
str
- classmethod get_metadata(key: str, default_value: Any = None) Any[source]¶
Retrieves the metadata value for the given key, converting it to the appropriate data type.
- Parameters:
key – The key for the metadata.
default_value – The value to return if the key does not exist. Defaults to None.
- Raises:
MetaError – If the stored value is in an incorrect format or uses an unsupported type.
- Returns:
The metadata value, converted to its original data type.
- Return type:
Any
- meta_keys: list¶
[]
A list of keys for metadata that has been set using
set_metadata.
- classmethod set_metadata(key: str, value: Any)[source]¶
Stores metadata in
os.environby encoding the data type along with the value.- Parameters:
key – The key for the metadata.
value – The value to store.
- Raises:
MetaError – If the value is of an unsupported data type or contains disallowed characters.