betty.config module

Provide the Configuration API.

class betty.config.Configurable[source]

Bases: Generic[ConfigurationT]

Any configurable object.

Parameters:
__init__(*args: Any, **kwargs: Any)[source]
Parameters:
property configuration: ConfigurationT

The object’s configuration.

class betty.config.Configuration[source]

Bases: Dumpable

Any configuration object.

Parameters:
__init__(*args: Any, **kwargs: Any)[source]
Parameters:
classmethod assert_load(configuration: betty.config.ConfigurationT | None = None) Callable[[bool | int | float | str | None | Sequence[bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]] | Mapping[str, bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]]], betty.config.ConfigurationT][source]

Assert that the dumped configuration can be loaded.

Parameters:

configuration (typing.Optional[typing.TypeVar(ConfigurationT, bound= betty.config.Configuration)])

Return type:

typing.Callable[[typing.Union[bool, int, float, str, None, typing.Sequence[typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]], typing.Mapping[str, typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]]]], typing.TypeVar(ConfigurationT, bound= betty.config.Configuration)]

classmethod load(dump: bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump], configuration: Self | None = None) Self[source]

Load dumped configuration into a new configuration instance.

Parameters:
Return type:

typing.Self

on_change(listener: betty.config.Configuration | collections.abc.Callable[[], None]) None[source]

Add an on-change listener.

Parameters:

listener (betty.config.Configuration | collections.abc.Callable[[], None])

Return type:

None

remove_on_change(listener: betty.config.Configuration | collections.abc.Callable[[], None]) None[source]

Remove an on-change listener.

Parameters:

listener (betty.config.Configuration | collections.abc.Callable[[], None])

Return type:

None

update(other: Self) None[source]

Update this configuration with the values from other.

Parameters:

other (typing.Self)

Return type:

None

class betty.config.ConfigurationCollection[source]

Bases: Configuration, Generic[ConfigurationKeyT, ConfigurationT]

Any collection of betty.config.Configuration values.

Parameters:

configurations (typing.Optional[typing.Iterable[typing.TypeVar(ConfigurationT, bound= betty.config.Configuration)]])

__init__(configurations: Iterable[betty.config.ConfigurationT] | None = None)[source]
Parameters:

configurations (typing.Optional[typing.Iterable[typing.TypeVar(ConfigurationT, bound= betty.config.Configuration)]])

append(*configurations: betty.config.ConfigurationT) None[source]

Append the given values to the end of the sequence.

Parameters:

configurations (typing.TypeVar(ConfigurationT, bound= betty.config.Configuration))

Return type:

None

clear() None[source]

Clear all items from the collection.

Return type:

None

insert(index: int, *configurations: betty.config.ConfigurationT) None[source]

Insert the given values at the given index.

Parameters:
Return type:

None

keys() Iterator[betty.config.ConfigurationKeyT][source]

Get all keys in this collection.

Return type:

typing.Iterator[typing.TypeVar(ConfigurationKeyT, bound= typing.Union[typing.SupportsIndex, typing.Hashable, type[typing.Any]])]

move_to_beginning(*configuration_keys: betty.config.ConfigurationKeyT) None[source]

Move the given keys (and their values) to the beginning of the sequence.

Parameters:

configuration_keys (typing.TypeVar(ConfigurationKeyT, bound= typing.Union[typing.SupportsIndex, typing.Hashable, type[typing.Any]]))

Return type:

None

move_to_end(*configuration_keys: betty.config.ConfigurationKeyT) None[source]

Move the given keys (and their values) to the end of the sequence.

Parameters:

configuration_keys (typing.TypeVar(ConfigurationKeyT, bound= typing.Union[typing.SupportsIndex, typing.Hashable, type[typing.Any]]))

Return type:

None

move_towards_beginning(*configuration_keys: betty.config.ConfigurationKeyT) None[source]

Move the given keys (and their values) one place towards the beginning of the sequence.

Parameters:

configuration_keys (typing.TypeVar(ConfigurationKeyT, bound= typing.Union[typing.SupportsIndex, typing.Hashable, type[typing.Any]]))

Return type:

None

move_towards_end(*configuration_keys: betty.config.ConfigurationKeyT) None[source]

Move the given keys (and their values) one place towards the end of the sequence.

Parameters:

configuration_keys (typing.TypeVar(ConfigurationKeyT, bound= typing.Union[typing.SupportsIndex, typing.Hashable, type[typing.Any]]))

Return type:

None

prepend(*configurations: betty.config.ConfigurationT) None[source]

Prepend the given values to the beginning of the sequence.

Parameters:

configurations (typing.TypeVar(ConfigurationT, bound= betty.config.Configuration))

Return type:

None

remove(*configuration_keys: betty.config.ConfigurationKeyT) None[source]

Remove the given keys from the collection.

Parameters:

configuration_keys (typing.TypeVar(ConfigurationKeyT, bound= typing.Union[typing.SupportsIndex, typing.Hashable, type[typing.Any]]))

Return type:

None

to_index(configuration_key: betty.config.ConfigurationKeyT) int[source]

Get the index for the given key.

Parameters:

configuration_key (typing.TypeVar(ConfigurationKeyT, bound= typing.Union[typing.SupportsIndex, typing.Hashable, type[typing.Any]]))

Return type:

int

to_indices(*configuration_keys: betty.config.ConfigurationKeyT) Iterator[int][source]

Get the indices for the given keys.

Parameters:

configuration_keys (typing.TypeVar(ConfigurationKeyT, bound= typing.Union[typing.SupportsIndex, typing.Hashable, type[typing.Any]]))

Return type:

typing.Iterator[int]

to_key(index: int) betty.config.ConfigurationKeyT[source]

Get the key for the item at the given index.

Parameters:

index (int)

Return type:

typing.TypeVar(ConfigurationKeyT, bound= typing.Union[typing.SupportsIndex, typing.Hashable, type[typing.Any]])

to_keys(*indices: int | slice) Iterator[betty.config.ConfigurationKeyT][source]

Get the keys for the items at the given indices.

Parameters:

indices (int | slice)

Return type:

typing.Iterator[typing.TypeVar(ConfigurationKeyT, bound= typing.Union[typing.SupportsIndex, typing.Hashable, type[typing.Any]])]

values() Iterator[betty.config.ConfigurationT][source]

Get all values in this collection.

Return type:

typing.Iterator[typing.TypeVar(ConfigurationT, bound= betty.config.Configuration)]

class betty.config.ConfigurationMapping[source]

Bases: ConfigurationCollection[ConfigurationKeyT, ConfigurationT], Generic[ConfigurationKeyT, ConfigurationT]

A key-value mapping where values are betty.config.Configuration.

Parameters:

configurations (typing.Optional[typing.Iterable[typing.TypeVar(ConfigurationT, bound= betty.config.Configuration)]])

__init__(configurations: Iterable[betty.config.ConfigurationT] | None = None)[source]
Parameters:

configurations (typing.Optional[typing.Iterable[typing.TypeVar(ConfigurationT, bound= betty.config.Configuration)]])

append(*configurations: betty.config.ConfigurationT) None[source]

Append the given values to the end of the sequence.

Parameters:

configurations (typing.TypeVar(ConfigurationT, bound= betty.config.Configuration))

Return type:

None

dump() bool | int | float | str | None | Sequence[bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]] | Mapping[str, bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]] | type[betty.serde.dump.Void][source]

Dump this instance to a portable format.

Return type:

typing.Union[bool, int, float, str, None, typing.Sequence[typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]], typing.Mapping[str, typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]], type[betty.serde.dump.Void]]

insert(index: int, *configurations: betty.config.ConfigurationT) None[source]

Insert the given values at the given index.

Parameters:
Return type:

None

keys() Iterator[betty.config.ConfigurationKeyT][source]

Get all keys in this collection.

Return type:

typing.Iterator[typing.TypeVar(ConfigurationKeyT, bound= typing.Union[typing.SupportsIndex, typing.Hashable, type[typing.Any]])]

classmethod load(dump: bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump], configuration: Self | None = None) Self[source]

Load dumped configuration into a new configuration instance.

Parameters:
Return type:

typing.Self

move_to_beginning(*configuration_keys: betty.config.ConfigurationKeyT) None[source]

Move the given keys (and their values) to the beginning of the sequence.

Parameters:

configuration_keys (typing.TypeVar(ConfigurationKeyT, bound= typing.Union[typing.SupportsIndex, typing.Hashable, type[typing.Any]]))

Return type:

None

move_to_end(*configuration_keys: betty.config.ConfigurationKeyT) None[source]

Move the given keys (and their values) to the end of the sequence.

Parameters:

configuration_keys (typing.TypeVar(ConfigurationKeyT, bound= typing.Union[typing.SupportsIndex, typing.Hashable, type[typing.Any]]))

Return type:

None

move_towards_beginning(*configuration_keys: betty.config.ConfigurationKeyT) None[source]

Move the given keys (and their values) one place towards the beginning of the sequence.

Parameters:

configuration_keys (typing.TypeVar(ConfigurationKeyT, bound= typing.Union[typing.SupportsIndex, typing.Hashable, type[typing.Any]]))

Return type:

None

move_towards_end(*configuration_keys: betty.config.ConfigurationKeyT) None[source]

Move the given keys (and their values) one place towards the end of the sequence.

Parameters:

configuration_keys (typing.TypeVar(ConfigurationKeyT, bound= typing.Union[typing.SupportsIndex, typing.Hashable, type[typing.Any]]))

Return type:

None

prepend(*configurations: betty.config.ConfigurationT) None[source]

Prepend the given values to the beginning of the sequence.

Parameters:

configurations (typing.TypeVar(ConfigurationT, bound= betty.config.Configuration))

Return type:

None

replace(*values: betty.config.ConfigurationT) None[source]

Replace any existing values with the given ones.

Parameters:

values (typing.TypeVar(ConfigurationT, bound= betty.config.Configuration))

Return type:

None

to_index(configuration_key: betty.config.ConfigurationKeyT) int[source]

Get the index for the given key.

Parameters:

configuration_key (typing.TypeVar(ConfigurationKeyT, bound= typing.Union[typing.SupportsIndex, typing.Hashable, type[typing.Any]]))

Return type:

int

to_key(index: int) betty.config.ConfigurationKeyT[source]

Get the key for the item at the given index.

Parameters:

index (int)

Return type:

typing.TypeVar(ConfigurationKeyT, bound= typing.Union[typing.SupportsIndex, typing.Hashable, type[typing.Any]])

update(other: Self) None[source]

Update this configuration with the values from other.

Parameters:

other (typing.Self)

Return type:

None

values() Iterator[betty.config.ConfigurationT][source]

Get all values in this collection.

Return type:

typing.Iterator[typing.TypeVar(ConfigurationT, bound= betty.config.Configuration)]

class betty.config.ConfigurationSequence[source]

Bases: ConfigurationCollection[int, ConfigurationT], Generic[ConfigurationT]

A sequence of configuration values.

Parameters:

configurations (typing.Optional[typing.Iterable[typing.TypeVar(ConfigurationT, bound= betty.config.Configuration)]])

__init__(configurations: Iterable[betty.config.ConfigurationT] | None = None)[source]
Parameters:

configurations (typing.Optional[typing.Iterable[typing.TypeVar(ConfigurationT, bound= betty.config.Configuration)]])

append(*configurations: betty.config.ConfigurationT) None[source]

Append the given values to the end of the sequence.

Parameters:

configurations (typing.TypeVar(ConfigurationT, bound= betty.config.Configuration))

Return type:

None

dump() bool | int | float | str | None | Sequence[bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]] | Mapping[str, bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]] | type[betty.serde.dump.Void][source]

Dump this instance to a portable format.

Return type:

typing.Union[bool, int, float, str, None, typing.Sequence[typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]], typing.Mapping[str, typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]], type[betty.serde.dump.Void]]

insert(index: int, *configurations: betty.config.ConfigurationT) None[source]

Insert the given values at the given index.

Parameters:
Return type:

None

keys() Iterator[int][source]

Get all keys in this collection.

Return type:

typing.Iterator[int]

classmethod load(dump: bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump], configuration: Self | None = None) Self[source]

Load dumped configuration into a new configuration instance.

Parameters:
Return type:

typing.Self

move_to_beginning(*configuration_keys: int) None[source]

Move the given keys (and their values) to the beginning of the sequence.

Parameters:

configuration_keys (int)

Return type:

None

move_to_end(*configuration_keys: int) None[source]

Move the given keys (and their values) to the end of the sequence.

Parameters:

configuration_keys (int)

Return type:

None

move_towards_beginning(*configuration_keys: int) None[source]

Move the given keys (and their values) one place towards the beginning of the sequence.

Parameters:

configuration_keys (int)

Return type:

None

move_towards_end(*configuration_keys: int) None[source]

Move the given keys (and their values) one place towards the end of the sequence.

Parameters:

configuration_keys (int)

Return type:

None

prepend(*configurations: betty.config.ConfigurationT) None[source]

Prepend the given values to the beginning of the sequence.

Parameters:

configurations (typing.TypeVar(ConfigurationT, bound= betty.config.Configuration))

Return type:

None

to_index(configuration_key: int) int[source]

Get the index for the given key.

Parameters:

configuration_key (int)

Return type:

int

to_key(index: int) int[source]

Get the key for the item at the given index.

Parameters:

index (int)

Return type:

int

update(other: Self) None[source]

Update this configuration with the values from other.

Parameters:

other (typing.Self)

Return type:

None

values() Iterator[betty.config.ConfigurationT][source]

Get all values in this collection.

Return type:

typing.Iterator[typing.TypeVar(ConfigurationT, bound= betty.config.Configuration)]

class betty.config.FileBasedConfiguration[source]

Bases: Configuration

Any configuration that is stored in a file on disk.

__init__()[source]
property autowrite: bool

Whether to write this configuration to file whenever it changes.

property configuration_file_path: Path

The path to the configuration’s file.

async read(configuration_file_path: pathlib.Path | None = None) None[source]

Read the configuration from file.

If a configuration file path is given, it will become this configuration’s new file path, and its contents will be read.

If no configuration file path is given, the previously set file path will be read, if that file exists.

Parameters:

configuration_file_path (pathlib.Path | None)

Return type:

None

async write(configuration_file_path: pathlib.Path | None = None) None[source]

Write the configuration to file.

If a configuration file path is given, it will become this configuration’s new file path, and it will be written to.

If no configuration file path is given, the previously set file path will be written to, if that file exists.

Parameters:

configuration_file_path (pathlib.Path | None)

Return type:

None