betty.config module - Betty documentation

Hide navigation sidebar
Hide table of contents sidebar
Skip to content
Back to top
View this page
Toggle table of contents sidebar

# betty.config module

Provide the Configuration API.

class betty.config.Configurable

  Bases: `Generic`[`ConfigurationT`]

  Any configurable object.

  Parameters:

    - args (`typing.Any`)

    - kwargs (`typing.Any`)

  __init__(*args: Any, **kwargs: Any)
    Parameters:

      - args (`typing.Any`)

      - kwargs (`typing.Any`)

  property configuration: ConfigurationT

    The object’s configuration.

class betty.config.Configuration

  Bases: `Dumpable`

  Any configuration object.

  Parameters:

    - args (`typing.Any`)

    - kwargs (`typing.Any`)

  __init__(*args: Any, **kwargs: Any)
    Parameters:

      - args (`typing.Any`)

      - kwargs (`typing.Any`)

  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]

    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

    Load dumped configuration into a new configuration instance.

    Parameters:

      - dump (`typing.Union`[`bool`, `int`, `float`, `str`, `None`, `typing.Sequence`[Dump], `typing.Mapping`[`str`, Dump]])

      - configuration (`typing.Optional`[`typing.Self`])

    Return type:

      `typing.Self`

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

    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

    Remove an on-change listener.

    Parameters:

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

    Return type:

      `None`

  update(other: Self) → None

    Update this configuration with the values from `other`.

    Parameters:

      other (`typing.Self`)

    Return type:

      `None`

class betty.config.ConfigurationCollection

  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)
    Parameters:

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

  append(*configurations: betty.config.ConfigurationT) → None

    Append the given values to the end of the sequence.

    Parameters:

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

    Return type:

      `None`

  clear() → None

    Clear all items from the collection.

    Return type:

      `None`

  insert(index: int, *configurations: betty.config.ConfigurationT) → None

    Insert the given values at the given index.

    Parameters:

      - index (`int`)

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

    Return type:

      `None`

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

    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

    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

    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

    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

    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

    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

    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

    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]

    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

    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]

    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]

    Get all values in this collection.

    Return type:

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

class betty.config.ConfigurationMapping

  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)
    Parameters:

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

  append(*configurations: betty.config.ConfigurationT) → None

    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]

    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

    Insert the given values at the given index.

    Parameters:

      - index (`int`)

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

    Return type:

      `None`

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

    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

    Load dumped configuration into a new configuration instance.

    Parameters:

      - dump (`typing.Union`[`bool`, `int`, `float`, `str`, `None`, `typing.Sequence`[Dump], `typing.Mapping`[`str`, Dump]])

      - configuration (`typing.Optional`[`typing.Self`])

    Return type:

      `typing.Self`

  move_to_beginning(*configuration_keys: betty.config.ConfigurationKeyT) → None

    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

    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

    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

    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

    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

    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

    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

    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

    Update this configuration with the values from `other`.

    Parameters:

      other (`typing.Self`)

    Return type:

      `None`

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

    Get all values in this collection.

    Return type:

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

class betty.config.ConfigurationSequence

  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)
    Parameters:

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

  append(*configurations: betty.config.ConfigurationT) → None

    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]

    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

    Insert the given values at the given index.

    Parameters:

      - index (`int`)

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

    Return type:

      `None`

  keys() → Iterator[int]

    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

    Load dumped configuration into a new configuration instance.

    Parameters:

      - dump (`typing.Union`[`bool`, `int`, `float`, `str`, `None`, `typing.Sequence`[Dump], `typing.Mapping`[`str`, Dump]])

      - configuration (`typing.Optional`[`typing.Self`])

    Return type:

      `typing.Self`

  move_to_beginning(*configuration_keys: int) → None

    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

    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

    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

    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

    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

    Get the index for the given key.

    Parameters:

      configuration_key (`int`)

    Return type:

      `int`

  to_key(index: int) → int

    Get the key for the item at the given index.

    Parameters:

      index (`int`)

    Return type:

      `int`

  update(other: Self) → None

    Update this configuration with the values from `other`.

    Parameters:

      other (`typing.Self`)

    Return type:

      `None`

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

    Get all values in this collection.

    Return type:

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

class betty.config.FileBasedConfiguration

  Bases: `Configuration`

  Any configuration that is stored in a file on disk.

  __init__()
  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._local.Path | None = None) → None

    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._local.Path` | `None`)

    Return type:

      `None`

  async write(configuration_file_path: pathlib._local.Path | None = None) → None

    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._local.Path` | `None`)

    Return type:

      `None`