devctrl.utility.settings_db.SettingsDb#

class SettingsDb(backend: SettingsBackend[K, S], settings_class: type[S])[source]#

Bases: Generic[K, S]

Database-like structure for storing SettingsClass instances.

Supports dictionary-like access with [] and ‘in’ operators. All entries are loaded from the backend on initialization. Use .save() to persist all changes to the backend.

Parameters:
  • backend – Backend instance for persistence

  • settings_class – The SettingsClass subclass this DB stores

Methods

get

Get a settings instance by key, with optional default.

get_valid_key_description

Message describing a valid key, shown to the user when an invalid key is entered.

is_key_allowed

Check if a key is allowed.

items

Return all (key, settings) pairs.

keys

Return all keys.

pop

Remove and return a value.

save

Save all entries to the backend.

values

Return all settings instances.

__annotations__ = {'_data': 'dict[K, S]'}#
classmethod __class_getitem__()#

Parameterizes a generic class.

At least, parameterizing a generic class is the main thing this method does. For example, for some generic class Foo, this is called when we do Foo[int] - there, with cls=Foo and params=int.

However, note that this method is also called when defining generic classes in the first place with class Foo[T]: ....

__contains__(key: K) bool[source]#

Check if a key exists.

Parameters:

key – The key to check

Returns:

True if the key exists, False otherwise

__delitem__(key: K) None[source]#

Delete a settings instance by key.

Parameters:

key – The key to delete

Raises:

KeyError – If the key is not found

__dict__ = mappingproxy({'__module__': 'devctrl.utility.settings_db', '__firstlineno__': 92, '__doc__': "Database-like structure for storing SettingsClass instances.\n\nSupports dictionary-like access with [] and 'in' operators.\nAll entries are loaded from the backend on initialization.\nUse .save() to persist all changes to the backend.\n\n:param backend: Backend instance for persistence\n:param settings_class: The SettingsClass subclass this DB stores\n", '__init__': <function SettingsDb.__init__>, '_load_from_backend': <function SettingsDb._load_from_backend>, 'save': <function SettingsDb.save>, '__getitem__': <function SettingsDb.__getitem__>, '__setitem__': <function SettingsDb.__setitem__>, '__delitem__': <function SettingsDb.__delitem__>, '__contains__': <function SettingsDb.__contains__>, '__iter__': <function SettingsDb.__iter__>, '__len__': <function SettingsDb.__len__>, 'keys': <function SettingsDb.keys>, 'values': <function SettingsDb.values>, 'items': <function SettingsDb.items>, 'get': <function SettingsDb.get>, 'pop': <function SettingsDb.pop>, 'get_valid_key_description': <classmethod(<function SettingsDb.get_valid_key_description>)>, 'is_key_allowed': <function SettingsDb.is_key_allowed>, '__repr__': <function SettingsDb.__repr__>, '__static_attributes__': ('_backend', '_data', '_settings_class', 'e_value_changed', 'e_value_removed'), '__orig_bases__': (typing.Generic[~K, ~S],), '__dict__': <attribute '__dict__' of 'SettingsDb' objects>, '__weakref__': <attribute '__weakref__' of 'SettingsDb' objects>, '__parameters__': (~K, ~S), '__annotations__': {'_data': 'dict[K, S]'}})#
__firstlineno__ = 92#
__getitem__(key: K) S[source]#

Get a settings instance by key.

Parameters:

key – The key to look up

Returns:

The settings instance

Raises:

KeyError – If the key is not found

__init__(backend: SettingsBackend[K, S], settings_class: type[S])[source]#
classmethod __init_subclass__()#

Function to initialize subclasses.

__iter__()[source]#

Iterate over keys.

__len__() int[source]#

Return number of entries.

__module__ = 'devctrl.utility.settings_db'#
__orig_bases__ = (typing.Generic[~K, ~S],)#
__parameters__ = (~K, ~S)#
__repr__() str[source]#

Return repr(self).

__setitem__(key: K, value: S) None[source]#

Set a settings instance by key.

Parameters:
  • key – The key to store under

  • value – The settings instance to store

Raises:

TypeError – If value is not an instance of the expected SettingsClass

__static_attributes__ = ('_backend', '_data', '_settings_class', 'e_value_changed', 'e_value_removed')#
__weakref__#

list of weak references to the object

_data: dict[K, S]#
_load_from_backend() None[source]#

Load all entries from the backend.

get(key: K, default: S | None = None) S | None[source]#

Get a settings instance by key, with optional default.

Parameters:
  • key – The key to look up

  • default – Default value if key is not found

Returns:

The settings instance, or default if not found

classmethod get_valid_key_description() str[source]#

Message describing a valid key, shown to the user when an invalid key is entered.

is_key_allowed(key: K) bool[source]#

Check if a key is allowed.

Method may be overwritten in a subclass.

items()[source]#

Return all (key, settings) pairs.

keys()[source]#

Return all keys.

pop(key: K) S[source]#

Remove and return a value.

save() None[source]#

Save all entries to the backend.

values()[source]#

Return all settings instances.