Config API
mopidy.config
Modules:
-
schemas– -
types– -
validators–
Classes:
-
Boolean–Boolean value.
-
ConfigSchema–Logical group of config values that correspond to a config section.
-
ConfigValue–Represents a config key's value and how to handle it.
-
Deprecated–Deprecated value.
-
Float–Float value.
-
Hostname–Network hostname value.
-
Integer–Integer value.
-
List–List value.
-
LogLevel–Log level value.
-
MapConfigSchema–Schema for handling multiple unknown keys with the same type.
-
Pair–Pair value.
-
Path–File system path.
-
Port–Network port value.
-
Secret–Secret string value.
-
String–String value.
Functions:
-
read–Helper to load config defaults in same way across core and extensions.
Boolean
Boolean(optional: bool = False)
Bases: ConfigValue[bool]
Boolean value.
Accepts 1, yes, true, and on with any casing as True.
Accepts 0, no, false, and off with any casing as False.
ConfigSchema
Bases: UserDict
Logical group of config values that correspond to a config section.
Schemas are set up by assigning config keys with config values to instances. Once setup deserialize can be called with a dict of values to process. For convenience we also support format method that can used for converting the values to a dict that can be printed and serialize for converting the values to a form suitable for persistence.
Methods:
-
deserialize–Validates the given
valuesusing the config schema. -
serialize–Converts the given
valuesto a format suitable for persistence.
deserialize
Validates the given values using the config schema.
Returns a tuple with cleaned values and errors.
ConfigValue
Bases: ABC
Represents a config key's value and how to handle it.
Normally you will only be interacting with sub-classes for config values that encode either deserialization behavior and/or validation.
Each config value should be used for the following actions:
- Deserializing from a raw string and validating, raising ValueError on failure.
- Serializing a value back to a string that can be stored in a config.
- Formatting a value to a printable form (useful for masking secrets).
None values should not be deserialized, serialized or formatted,
the code interacting with the config should simply skip None config values.
Methods:
-
deserialize–Cast raw string to appropriate type.
-
serialize–Convert value back to string for saving.
Deprecated
Bases: ConfigValue[Any]
Deprecated value.
Used for ignoring old config values that are no longer in use, but should not cause the config parser to crash.
Float
Bases: ConfigValue[float]
Float value.
Methods:
-
serialize–Convert value back to string for saving.
Hostname
Hostname(optional: bool = False)
Bases: ConfigValue[str]
Network hostname value.
Methods:
-
serialize–Convert value back to string for saving.
Integer
Integer(
minimum: int | None = None,
maximum: int | None = None,
choices: Iterable[int] | None = None,
optional: bool = False,
)
Bases: ConfigValue[int]
Integer value.
Methods:
-
serialize–Convert value back to string for saving.
List
Bases: ConfigValue[tuple[V, ...] | frozenset[V]]
List value.
Supports elements split by commas or newlines. Newlines take precedence and empty list items will be filtered out.
Enforcing unique entries in the list will result in a set data structure being used. This does not preserve ordering, which could result in the serialized output being unstable.
LogLevel
Bases: ConfigValue[int]
Log level value.
Expects one of critical, error, warning, info, debug,
trace, or all, with any casing.
MapConfigSchema
MapConfigSchema(name: str, value_type: ConfigValue)
Schema for handling multiple unknown keys with the same type.
Does not sub-class ConfigSchema, but implements the same serialize/deserialize interface.
Pair
Pair(
optional: bool = False,
optional_pair: bool = False,
separator: str = "|",
subtypes: tuple[K, V] = (String(), String()),
)
Bases: ConfigValue[tuple[K, V]]
Pair value.
The value is expected to be a pair of elements, separated by a specified delimiter. Values can optionally not be a pair, in which case the whole input is provided for both sides of the value.
Path
Path(optional: bool = False)
Bases: ConfigValue[_ExpandedPath]
File system path.
The following expansions of the path will be done:
~to the current user's home directory$XDG_CACHE_DIRaccording to the XDG spec$XDG_CONFIG_DIRaccording to the XDG spec$XDG_DATA_DIRaccording to the XDG spec$XDG_MUSIC_DIRaccording to the XDG spec
Port
Secret
Secret(
optional: bool = False,
choices: None = None,
transformer: Callable[[str], str] | None = None,
)
Bases: String
Secret string value.
Is decoded as utf-8, and \n and \t escapes should work and be preserved.
Should be used for passwords, auth tokens etc. Will mask value when being displayed.
mopidy.config.schemas
Classes:
-
ConfigSchema–Logical group of config values that correspond to a config section.
-
MapConfigSchema–Schema for handling multiple unknown keys with the same type.
ConfigSchema
Bases: UserDict
Logical group of config values that correspond to a config section.
Schemas are set up by assigning config keys with config values to instances. Once setup deserialize can be called with a dict of values to process. For convenience we also support format method that can used for converting the values to a dict that can be printed and serialize for converting the values to a form suitable for persistence.
Methods:
-
deserialize–Validates the given
valuesusing the config schema. -
serialize–Converts the given
valuesto a format suitable for persistence.
deserialize
Validates the given values using the config schema.
Returns a tuple with cleaned values and errors.
MapConfigSchema
MapConfigSchema(name: str, value_type: ConfigValue)
Schema for handling multiple unknown keys with the same type.
Does not sub-class ConfigSchema, but implements the same serialize/deserialize interface.
mopidy.config.types
Classes:
-
Boolean–Boolean value.
-
ConfigValue–Represents a config key's value and how to handle it.
-
Deprecated–Deprecated value.
-
Float–Float value.
-
Hostname–Network hostname value.
-
Integer–Integer value.
-
List–List value.
-
LogLevel–Log level value.
-
Pair–Pair value.
-
Path–File system path.
-
Port–Network port value.
-
Secret–Secret string value.
-
String–String value.
Boolean
Boolean(optional: bool = False)
Bases: ConfigValue[bool]
Boolean value.
Accepts 1, yes, true, and on with any casing as True.
Accepts 0, no, false, and off with any casing as False.
ConfigValue
Bases: ABC
Represents a config key's value and how to handle it.
Normally you will only be interacting with sub-classes for config values that encode either deserialization behavior and/or validation.
Each config value should be used for the following actions:
- Deserializing from a raw string and validating, raising ValueError on failure.
- Serializing a value back to a string that can be stored in a config.
- Formatting a value to a printable form (useful for masking secrets).
None values should not be deserialized, serialized or formatted,
the code interacting with the config should simply skip None config values.
Methods:
-
deserialize–Cast raw string to appropriate type.
-
serialize–Convert value back to string for saving.
Deprecated
Bases: ConfigValue[Any]
Deprecated value.
Used for ignoring old config values that are no longer in use, but should not cause the config parser to crash.
Float
Bases: ConfigValue[float]
Float value.
Methods:
-
serialize–Convert value back to string for saving.
Hostname
Hostname(optional: bool = False)
Bases: ConfigValue[str]
Network hostname value.
Methods:
-
serialize–Convert value back to string for saving.
Integer
Integer(
minimum: int | None = None,
maximum: int | None = None,
choices: Iterable[int] | None = None,
optional: bool = False,
)
Bases: ConfigValue[int]
Integer value.
Methods:
-
serialize–Convert value back to string for saving.
List
Bases: ConfigValue[tuple[V, ...] | frozenset[V]]
List value.
Supports elements split by commas or newlines. Newlines take precedence and empty list items will be filtered out.
Enforcing unique entries in the list will result in a set data structure being used. This does not preserve ordering, which could result in the serialized output being unstable.
LogLevel
Bases: ConfigValue[int]
Log level value.
Expects one of critical, error, warning, info, debug,
trace, or all, with any casing.
Pair
Pair(
optional: bool = False,
optional_pair: bool = False,
separator: str = "|",
subtypes: tuple[K, V] = (String(), String()),
)
Bases: ConfigValue[tuple[K, V]]
Pair value.
The value is expected to be a pair of elements, separated by a specified delimiter. Values can optionally not be a pair, in which case the whole input is provided for both sides of the value.
Path
Path(optional: bool = False)
Bases: ConfigValue[_ExpandedPath]
File system path.
The following expansions of the path will be done:
~to the current user's home directory$XDG_CACHE_DIRaccording to the XDG spec$XDG_CONFIG_DIRaccording to the XDG spec$XDG_DATA_DIRaccording to the XDG spec$XDG_MUSIC_DIRaccording to the XDG spec
Port
Secret
Secret(
optional: bool = False,
choices: None = None,
transformer: Callable[[str], str] | None = None,
)
Bases: String
Secret string value.
Is decoded as utf-8, and \n and \t escapes should work and be preserved.
Should be used for passwords, auth tokens etc. Will mask value when being displayed.
mopidy.config.validators
Functions:
-
validate_choice–Validate that
valueis one of thechoices. -
validate_maximum–Validate that
valueis at mostmaximum. -
validate_minimum–Validate that
valueis at leastminimum. -
validate_required–Validate that
valueis set ifrequired.
validate_choice
validate_choice[T](value: T, choices: Iterable[T] | None) -> None
Validate that value is one of the choices.
Normally called in ConfigValue.deserialize.
validate_maximum
validate_maximum[CT: Comparable](value: CT, maximum: CT | None) -> None
Validate that value is at most maximum.
Normally called in ConfigValue.deserialize.
validate_minimum
validate_minimum[CT: Comparable](value: CT, minimum: CT | None) -> None
Validate that value is at least minimum.
Normally called in ConfigValue.deserialize.
validate_required
Validate that value is set if required.
Normally called in ConfigValue.deserialize on the raw string, not the converted value.