ahriman.core.configuration package

Submodules

ahriman.core.configuration.configuration module

class Configuration(allow_no_value: bool = False, allow_multi_key: bool = True)

Bases: RawConfigParser

extension for built-in configuration parser

ARCHITECTURE_SPECIFIC_SECTIONS

(class attribute) known sections which can be architecture specific. Required by dump and merging functions

Type:

list[str]

SYSTEM_CONFIGURATION_PATH

(class attribute) default system configuration path distributed by package

Type:

Path

includes

list of includes which were read

Type:

list[Path]

path

path to root configuration file

Type:

Path | None

Examples

Configuration class provides additional method in order to handle application configuration. Since this class is derived from built-in configparser.RawConfigParser class, the same flow is applicable here. Nevertheless, it is recommended to use from_path() class method which also calls initialization methods:

>>> from pathlib import Path
>>>
>>> configuration = Configuration.from_path(Path("/etc/ahriman.ini"), RepositoryId("x86_64", "aur"))
>>> repository_name = configuration.get("repository", "name")
>>> makepkg_flags = configuration.getlist("build", "makepkg_flags")

The configuration instance loaded in this way will contain only sections which are defined for the specified architecture according to the merge rules. Moreover, the architecture names will be removed from section names.

In order to get current settings, the check_loaded() method can be used. This method will raise an ahriman.core.exceptions.InitializeError in case if configuration was not yet loaded:

>>> path, repository_id = configuration.check_loaded()
Parameters:
  • allow_no_value (bool, optional) – copies configparser.RawConfigParser behaviour. In case if it is set to True, the keys without values will be allowed (Default value = False)

  • allow_multi_key (bool, optional) – if set to False, then the default dictionary class will be used to store keys internally. Otherwise, the special implementation will be used, which supports arrays (Default value = True)

classmethod from_path(path: Path, repository_id: RepositoryId) Self

constructor with full object initialization

Parameters:
  • path (Path) – path to root configuration file

  • repository_id (RepositoryId) – repository unique identifier

Returns:

configuration instance

Return type:

Self

static section_name(section: str, *suffixes: str | None) str

generate section name for sections which depends on context

Parameters:
  • section (str) – section name

  • *suffixes (str | None) – session suffix, e.g. repository architecture

Returns:

correct section name for repository specific section

Return type:

str

check_loaded() tuple[Path, RepositoryId]

check if service was actually loaded

Returns:

configuration root path and architecture if loaded

Return type:

tuple[Path, RepositoryId]

Raises:

InitializeError – in case if architecture and/or path are not set

copy_from(configuration: Self) None

copy values from another instance overriding existing

Parameters:

configuration (Self) – configuration instance to merge from

dump() dict[str, dict[str, str]]

dump configuration to dictionary

Returns:

configuration dump for specific architecture

Return type:

dict[str, dict[str, str]]

gettype(section: str, repository_id: RepositoryId, *, fallback: str | None = None) tuple[str, str]

get type variable with fallback to old logic. Despite the fact that it has same semantics as other get* methods, but it has different argument list

Parameters:
  • section (str) – section name

  • repository_id (RepositoryId) – repository unique identifier

  • fallback (str | None, optional) – optional fallback type if any. If set, second element of the tuple will be always set to this value (Default value = None)

Returns:

section name and found type name

Return type:

tuple[str, str]

Raises:

configparser.NoSectionError – in case if no section found

load(path: Path) None

fully load configuration

Parameters:

path (Path) – path to root configuration file

load_environment() None

load environment variables into configuration

load_includes(path: Path | None = None) None

load configuration includes from specified path

Parameters:

path (Path | None, optional) – path to directory with include files. If none set, the default path will be used (Default value = None)

merge_sections(repository_id: RepositoryId) None

merge architecture and repository specific sections into main configuration

Parameters:

repository_id (RepositoryId) – repository unique identifier

override_sections(section: str, repository_id: RepositoryId) list[str]

extract override sections

Parameters:
  • section (str) – section name

  • repository_id (RepositoryId) – repository unique identifier

Returns:

architecture and repository specific sections in correct order

Return type:

list[str]

reload() None

reload configuration if possible or raise exception otherwise

set_option(section: str, option: str, value: str) None

set option. Unlike default configparser.RawConfigParser.set() it also creates section if it does not exist

Parameters:
  • section (str) – section name

  • option (str) – option name

  • value (str) – option value as string in parsable format

property architecture: str

repository architecture for backward compatibility

Returns:

repository architecture

Return type:

str

property include: Path

get full path to include directory

Returns:

path to directory with configuration includes

Return type:

Path

property logging_path: Path

get full path to logging configuration

Returns:

path to logging configuration

Return type:

Path

property repository_id: RepositoryId | None

repository identifier

Returns:

repository unique identifier

Return type:

RepositoryId

property repository_name: str

repository name for backward compatibility

Returns:

repository name

Return type:

str

property repository_paths: RepositoryPaths

construct RepositoryPaths instance based on the configuration

Returns:

repository paths instance

Return type:

RepositoryPaths

ahriman.core.configuration.configuration_multi_dict module

class ConfigurationMultiDict

Bases: dict[str, Any]

wrapper around dict to handle multiple configuration keys as lists if they end with [].

Examples

This class is designed to be used only with configparser.RawConfigParser class, but idea is that if the key ends with [] it will be treated as array and the result will be appended to the current value. In addition, if the value is empty, then it will clear previous values, e.g.:

>>> data = ConfigurationMultiDict()
>>>
>>> data["single"] = "value"  # append normal key
>>> print(data)  # {"single": "value"}
>>>
>>> data["array[]"] = ["value1"]  # append array value
>>> data["array[]"] = ["value2"]
>>> print(data)  # {"single": "value", "array": ["value1 value2"]}
>>>
>>> data["array[]"] = [""]  # clear previous values
>>> data["array[]"] = ["value3"]
>>> print(data)  # {"single": "value", "array": ["value3"]}

ahriman.core.configuration.schema module

ahriman.core.configuration.shell_interpolator module

class ShellInterpolator

Bases: Interpolation

custom string interpolator, because we cannot use defaults argument due to config validation

static environment() dict[str, str]

extract environment variables

Returns:

environment variables and some custom variables

Return type:

dict[str, str]

before_get(parser: MutableMapping[str, Mapping[str, str]], section: Any, option: Any, value: str, defaults: Mapping[str, str]) str

interpolate option value

Notes

This method is using string.Template class in order to render both cross-references and environment variables, because it seems that it is the most legit way to handle it. In addition, we are using shell-like variables in some cases (see alpm.mirror option), thus we would like to keep them alive.

First this method resolves substitution from the configuration and then renders environment variables

Parameters:
  • parser (MutableMapping[str, Mapping[str, str]]) – option parser

  • section (Any) – section name

  • option (Any) – option name

  • value (str) – source (not-converted) value

  • defaults (Mapping[str, str]) – default values

Returns:

substituted value

Return type:

str

ahriman.core.configuration.shell_template module

class ShellTemplate(template)

Bases: Template

extension to the default Template class, which also adds additional tokens to braced regex and enables bash expansion

shell_substitute(mapping: Mapping[str, str], /, **kwargs: str) str

this method behaves the same as safe_substitute(), however also expands bash string operations

Parameters:
  • mapping (Mapping[str, str]) – key-value dictionary of variables

  • **kwargs (str) – key-value dictionary of variables passed as kwargs

Returns:

string with replaced values

Return type:

str

ahriman.core.configuration.validator module

class Validator(*args: Any, **kwargs: Any)

Bases: Validator

class which defines custom validation methods for the service configuration

configuration

configuration instance

Type:

Configuration

Parameters:
  • configuration (Configuration) – configuration instance used for extraction

  • *args (Any) – positional arguments to be passed to base validator

  • **kwargs (Any) – keyword arguments to be passed to base validator

types_mapping = {'binary': ('binary', (<class 'bytes'>, <class 'bytearray'>), ()), 'boolean': ('boolean', (<class 'bool'>,), ()), 'container': ('container', (<class 'collections.abc.Container'>,), (<class 'str'>,)), 'date': ('date', (<class 'datetime.date'>,), ()), 'datetime': ('datetime', (<class 'datetime.datetime'>,), ()), 'dict': ('dict', (<class 'collections.abc.Mapping'>,), ()), 'float': ('float', (<class 'float'>, (<class 'int'>,)), ()), 'integer': ('integer', ((<class 'int'>,),), ()), 'list': ('list', (<class 'collections.abc.Sequence'>,), (<class 'str'>,)), 'number': ('number', ((<class 'int'>,), <class 'float'>), (<class 'bool'>,)), 'path': ('path', (<class 'pathlib.Path'>,), ()), 'set': ('set', (<class 'set'>,), ()), 'string': ('string', (<class 'str'>,), ())}

This mapping holds all available constraints for the type rule and their assigned TypeDefinition.

Module contents