Skip to content

hololinked.core.interfaces.configuration.BaseConfigurationRepository

Repository/Persistence for a Thing instance that can be used for storing configuration.

Model your device setting's as properties and set db_persist=True/db_commit=True in the property's argument to activate persistance. If your server dies and restarts, the settings will be reloaded and applied to your device.

Different storage backends (like JSON file, SQLAlchemy, MongoDB) are supported and custom backends can be implemented by inheriting from this class.

Source code in repo/hololinked/hololinked/core/interfaces/configuration.py
class BaseConfigurationRepository:
    """
    Repository/Persistence for a `Thing` instance that can be used for storing configuration.

    Model your device setting's as properties and set `db_persist=True`/`db_commit=True` in the property's argument
    to activate persistance. If your server dies and restarts, the settings will be reloaded and applied to your device.

    Different storage backends (like JSON file, SQLAlchemy, MongoDB) are supported and custom backends can be
    implemented by inheriting from this class.
    """

    def __init__(self, thing: Thing) -> None:
        self.thing = thing

    @classmethod
    def from_thing(cls, thing: Thing, **kwargs: Any) -> Self:
        """
        Build the repository for a `Thing` from the keyword arguments the `Thing` was created with.

        Parameters
        ----------
        thing: Thing
            the `Thing` instance the repository stores the configuration of
        kwargs: dict[str, Any]
            the keyword arguments given to the `Thing`, for example `db_config_file` or `json_filename`

        Returns
        -------
        Self
            the repository, ready to be used as the `Thing`'s storage backend
        """
        return cls(thing=thing)

    def fetch_own_info(self) -> Any:
        """Fetch `Thing` instance's own information (some useful metadata which could help the `Thing` run)."""

    def get_property(self, property: str | Property, deserialized: bool = True) -> Any:
        """
        Fetch a single property from the configuration storage.

        Parameters
        ----------
        property: str | Property
            string name or descriptor object
        deserialized: bool, default True
            deserialize the property if True

        Returns
        -------
        Any
            property value
        """

    def set_property(self, property: str | Property, value: Any) -> None:
        """
        Change the value of an already existing property in the configuration storage.

        Parameters
        ----------
        property: str | Property
            string name or descriptor object
        value: Any
            value of the property
        """

    def get_properties(self, properties: dict[str | Property, Any], deserialized: bool = True) -> dict[str, Any]:  # ty: ignore[empty-body]
        """
        Get multiple properties at once from the configuration storage.

        Parameters
        ----------
        properties: List[str | Property]
            string names or the descriptor of the properties as a list
        deserialized: bool, default True
            deserialize the properties if True

        Returns
        -------
        dict[str, Any]
            property names and values as items
        """

    def set_properties(self, properties: dict[str | Property, Any]) -> None:
        """
        Change the values of already existing properties in the configuration storage (at once).

        Parameters
        ----------
        properties: Dict[str | Property, Any]
            string names or the descriptor of the properties and their values.
        """

    def get_all_properties(self, deserialized: bool = True) -> dict[str, Any] | Sequence:  # ty: ignore[empty-body]
        """
        Get all properties of the `Thing` instance from the configuration storage.

        Parameters
        ----------
        deserialized: bool, default True
            deserialize the properties if True

        Returns
        -------
        dict[str, Any]
            property names and values as items
        """

    def create_missing_properties(
        self,
        properties: dict[str, Property],
        get_missing_property_names: bool = False,
    ) -> None | list[str]:
        """
        Create any and all missing properties of `Thing` instance in the configuration storage.

        Auto-invoked at `Thing.__init__()` and takes effect if new properties are introduced.

        Parameters
        ----------
        properties: Dict[str, Property]
            descriptors of the properties
        get_missing_property_names: bool, default False
            whether to return the list of missing property names

        Returns
        -------
        List[str]
            list of missing properties if get_missing_property_names is True
        """

    def create_init_properties(self, thing_id: str, thing_class: str, **properties: Any) -> None:
        """
        Create properties that are supposed to be initialized from configuration storage.

        Invoke this method once before running the thing instance to store their initial values.

        The `Thing` instance is not accepted as an argument as the method would be used outside its lifecycle.
        ID, class name and initial property names and valeus must match.

        Parameters
        ----------
        thing_id: str
            ID of the thing instance to which these properties belong
        thing_class: str
            Class name of the thing instance to which these properties belong (must be thing.__class__.__name__)
        properties: dict[str, Any]
            property names and their initial values as dictionary pairs
        """

Attributes

thing instance-attribute

thing = thing

Functions

__init__

__init__(thing: Thing) -> None
Source code in repo/hololinked/hololinked/core/interfaces/configuration.py
def __init__(self, thing: Thing) -> None:
    self.thing = thing

from_thing classmethod

from_thing(thing: Thing, **kwargs: Any) -> Self

Build the repository for a Thing from the keyword arguments the Thing was created with.

Parameters:

Name Type Description Default

thing

Thing

the Thing instance the repository stores the configuration of

required

kwargs

Any

the keyword arguments given to the Thing, for example db_config_file or json_filename

{}

Returns:

Type Description
Self

the repository, ready to be used as the Thing's storage backend

Source code in repo/hololinked/hololinked/core/interfaces/configuration.py
@classmethod
def from_thing(cls, thing: Thing, **kwargs: Any) -> Self:
    """
    Build the repository for a `Thing` from the keyword arguments the `Thing` was created with.

    Parameters
    ----------
    thing: Thing
        the `Thing` instance the repository stores the configuration of
    kwargs: dict[str, Any]
        the keyword arguments given to the `Thing`, for example `db_config_file` or `json_filename`

    Returns
    -------
    Self
        the repository, ready to be used as the `Thing`'s storage backend
    """
    return cls(thing=thing)

fetch_own_info

fetch_own_info() -> Any

Fetch Thing instance's own information (some useful metadata which could help the Thing run).

Source code in repo/hololinked/hololinked/core/interfaces/configuration.py
def fetch_own_info(self) -> Any:
    """Fetch `Thing` instance's own information (some useful metadata which could help the `Thing` run)."""

get_property

get_property(property: str | Property, deserialized: bool = True) -> Any

Fetch a single property from the configuration storage.

Parameters:

Name Type Description Default

property

str | Property

string name or descriptor object

required

deserialized

bool

deserialize the property if True

True

Returns:

Type Description
Any

property value

Source code in repo/hololinked/hololinked/core/interfaces/configuration.py
def get_property(self, property: str | Property, deserialized: bool = True) -> Any:
    """
    Fetch a single property from the configuration storage.

    Parameters
    ----------
    property: str | Property
        string name or descriptor object
    deserialized: bool, default True
        deserialize the property if True

    Returns
    -------
    Any
        property value
    """

get_properties

get_properties(properties: dict[str | Property, Any], deserialized: bool = True) -> dict[str, Any]

Get multiple properties at once from the configuration storage.

Parameters:

Name Type Description Default

properties

dict[str | Property, Any]

string names or the descriptor of the properties as a list

required

deserialized

bool

deserialize the properties if True

True

Returns:

Type Description
dict[str, Any]

property names and values as items

Source code in repo/hololinked/hololinked/core/interfaces/configuration.py
def get_properties(self, properties: dict[str | Property, Any], deserialized: bool = True) -> dict[str, Any]:  # ty: ignore[empty-body]
    """
    Get multiple properties at once from the configuration storage.

    Parameters
    ----------
    properties: List[str | Property]
        string names or the descriptor of the properties as a list
    deserialized: bool, default True
        deserialize the properties if True

    Returns
    -------
    dict[str, Any]
        property names and values as items
    """

get_all_properties

get_all_properties(deserialized: bool = True) -> dict[str, Any] | Sequence

Get all properties of the Thing instance from the configuration storage.

Parameters:

Name Type Description Default

deserialized

bool

deserialize the properties if True

True

Returns:

Type Description
dict[str, Any]

property names and values as items

Source code in repo/hololinked/hololinked/core/interfaces/configuration.py
def get_all_properties(self, deserialized: bool = True) -> dict[str, Any] | Sequence:  # ty: ignore[empty-body]
    """
    Get all properties of the `Thing` instance from the configuration storage.

    Parameters
    ----------
    deserialized: bool, default True
        deserialize the properties if True

    Returns
    -------
    dict[str, Any]
        property names and values as items
    """

set_property

set_property(property: str | Property, value: Any) -> None

Change the value of an already existing property in the configuration storage.

Parameters:

Name Type Description Default

property

str | Property

string name or descriptor object

required

value

Any

value of the property

required
Source code in repo/hololinked/hololinked/core/interfaces/configuration.py
def set_property(self, property: str | Property, value: Any) -> None:
    """
    Change the value of an already existing property in the configuration storage.

    Parameters
    ----------
    property: str | Property
        string name or descriptor object
    value: Any
        value of the property
    """

set_properties

set_properties(properties: dict[str | Property, Any]) -> None

Change the values of already existing properties in the configuration storage (at once).

Parameters:

Name Type Description Default

properties

dict[str | Property, Any]

string names or the descriptor of the properties and their values.

required
Source code in repo/hololinked/hololinked/core/interfaces/configuration.py
def set_properties(self, properties: dict[str | Property, Any]) -> None:
    """
    Change the values of already existing properties in the configuration storage (at once).

    Parameters
    ----------
    properties: Dict[str | Property, Any]
        string names or the descriptor of the properties and their values.
    """

create_missing_properties

create_missing_properties(properties: dict[str, Property], get_missing_property_names: bool = False) -> None | list[str]

Create any and all missing properties of Thing instance in the configuration storage.

Auto-invoked at Thing.__init__() and takes effect if new properties are introduced.

Parameters:

Name Type Description Default

properties

dict[str, Property]

descriptors of the properties

required

get_missing_property_names

bool

whether to return the list of missing property names

False

Returns:

Type Description
List[str]

list of missing properties if get_missing_property_names is True

Source code in repo/hololinked/hololinked/core/interfaces/configuration.py
def create_missing_properties(
    self,
    properties: dict[str, Property],
    get_missing_property_names: bool = False,
) -> None | list[str]:
    """
    Create any and all missing properties of `Thing` instance in the configuration storage.

    Auto-invoked at `Thing.__init__()` and takes effect if new properties are introduced.

    Parameters
    ----------
    properties: Dict[str, Property]
        descriptors of the properties
    get_missing_property_names: bool, default False
        whether to return the list of missing property names

    Returns
    -------
    List[str]
        list of missing properties if get_missing_property_names is True
    """

create_init_properties

create_init_properties(thing_id: str, thing_class: str, **properties: Any) -> None

Create properties that are supposed to be initialized from configuration storage.

Invoke this method once before running the thing instance to store their initial values.

The Thing instance is not accepted as an argument as the method would be used outside its lifecycle. ID, class name and initial property names and valeus must match.

Parameters:

Name Type Description Default

thing_id

str

ID of the thing instance to which these properties belong

required

thing_class

str

Class name of the thing instance to which these properties belong (must be thing.class.name)

required

properties

Any

property names and their initial values as dictionary pairs

{}
Source code in repo/hololinked/hololinked/core/interfaces/configuration.py
def create_init_properties(self, thing_id: str, thing_class: str, **properties: Any) -> None:
    """
    Create properties that are supposed to be initialized from configuration storage.

    Invoke this method once before running the thing instance to store their initial values.

    The `Thing` instance is not accepted as an argument as the method would be used outside its lifecycle.
    ID, class name and initial property names and valeus must match.

    Parameters
    ----------
    thing_id: str
        ID of the thing instance to which these properties belong
    thing_class: str
        Class name of the thing instance to which these properties belong (must be thing.__class__.__name__)
    properties: dict[str, Any]
        property names and their initial values as dictionary pairs
    """