StorageBackends
hololinked.injection.StorageBackends
Bases: Registry
A singleton registry of the storage backends a Thing can persist its configuration to.
All members are class attributes and settings are applied process-wide (python process).
A storage backend is a BaseConfigurationRepository subclass, instantiated once per Thing through its from_thing().
The storage is used for persisting device configuration only, not captured data. One needs to currently arrange that
on one's own. JSON file, MongoDB and SQLAlchemy backends are built in, but others can be registered. For example:
from hololinked import StorageBackends
StorageBackends.register(TomlFile, "tomlfile", flag="use_toml_file")
class MyThing(Thing):
use_toml_file = True
Source code in repo/hololinked/hololinked/injection.py
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 | |
Attributes
sqlalchemy
instance-attribute
SQLAlchemy based storage, supporting SQLite, PostgreSQL and MySQL.
json_file
instance-attribute
Plain JSON file based storage, needing no database server.
modules
class-attribute
modules: dict[str, str | tuple[str, str]] = {'sqlalchemy': 'SQLAlchemyDB', 'mongo': 'MongoDB', 'json_file': 'JSONFileStorage'}
flags
class-attribute
flags: dict[str, str] = {'use_default_db': 'sqlalchemy', 'use_mongo_db': 'mongo', 'use_json_file': 'json_file'}
Name of a Thing keyword argument mapped to the backend it selects.
default_name
class-attribute
The backend used when no flag is set but a database configuration file is supplied.
Functions
for_thing
classmethod
Get the backend class a Thing persists to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Thing
|
the |
required |
|
Any
|
the keyword arguments the |
{}
|
Returns:
| Type | Description |
|---|---|
type[BaseConfigurationRepository] | None
|
the backend class, to be built with its |
Source code in repo/hololinked/hololinked/injection.py
name_for_thing
classmethod
Get the name of the backend selected for a Thing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Thing
|
the |
required |
|
Any
|
the keyword arguments the |
{}
|
Returns:
| Type | Description |
|---|---|
str | None
|
the name the selected backend is registered under, None if the |
Source code in repo/hololinked/hololinked/injection.py
register
classmethod
Register a storage backend under a given name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
type[BaseConfigurationRepository]
|
the backend class to register, must be a subclass of |
required |
|
str
|
the name to register the backend under, for example 'sqlalchemy' or 'mongo' |
required |
|
str
|
the keyword argument, or class attribute, that selects this backend, for example 'use_mongo_db' |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
if the backend is not a subclass of |
Source code in repo/hololinked/hololinked/injection.py
hololinked.injection.prepare_object_storage
Attach the storage backend a Thing persists its configuration to, if it asked for one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Thing
|
The |
required |
|
Any
|
Additional keyword arguments to configure storage backend
|
{}
|