config Module

exception tallypi.config.OptionError(opt: tallypi.config.Option, opt_value: Optional[Any] = None)[source]

Bases: ValueError

exception tallypi.config.RequiredError(opt: tallypi.config.Option, opt_value: Optional[Any] = None)[source]

Bases: tallypi.config.OptionError

exception tallypi.config.ChoiceError(opt: tallypi.config.Option, opt_value: Optional[Any] = None)[source]

Bases: tallypi.config.OptionError

exception tallypi.config.InvalidTypeError(opt: tallypi.config.Option, opt_value: Optional[Any] = None)[source]

Bases: tallypi.config.OptionError

exception tallypi.config.InvalidLengthError(opt: tallypi.config.Option, opt_value: Optional[Any] = None)[source]

Bases: tallypi.config.OptionError

class tallypi.config.Option(name: str, type: Any, title: Optional[str] = None, required: bool = True, default: Optional[Any] = None, choices: Optional[Tuple[Any]] = <factory>, sub_options: Optional[Tuple[tallypi.config.Option]] = <factory>, doc: Optional[str] = '', validate_cb: Optional[Callable] = None, serialize_cb: Optional[Callable] = None)[source]

Bases: object

A configuration option definition

name: str

The parameter name

type: Any

The python value type

title: Optional[str] = None

Friendly name for the option. If not given, name is used

required: bool = True

If True (default), the parameter is required

default: Optional[Any] = None

The default value for the parameter

choices: Optional[Tuple[Any]]

If present, a tuple of allowed values

sub_options: Optional[Tuple[tallypi.config.Option]]

If present, a tuple of Option instances providing nested fields

validate_cb: Optional[Callable] = None

A callback to provide custom validation

The callback must accept a single argument, the value to be validated

serialize_cb: Optional[Callable] = None

A callback to provide custom serialization

The callback must accept a single argument, the value to be serialized

validate(value: Any)Any[source]

Validate and transform the given value to the defined type

If sub_options are defined, the value given must be a dict formatted as is returned from the serialize() method. The values within it are then validated by this method called in each sub option.

Note

If validate_cb is defined, no sub_options will be processed.

serialize(value: Any)Any[source]

Serialize the given value of type type

If sub_options are defined, this method will be called on each with their values looked up by their name and a dict will be returned.

Note

If serialize_cb is defined, no sub_options will be processed.

class tallypi.config.ListOption(name: str, type: Any, title: Optional[str] = None, required: bool = True, default: Optional[Any] = None, choices: Optional[Tuple[Any]] = <factory>, sub_options: Optional[Tuple[tallypi.config.Option]] = <factory>, doc: Optional[str] = '', validate_cb: Optional[Callable] = None, serialize_cb: Optional[Callable] = None, min_length: Optional[int] = None, max_length: Optional[int] = None)[source]

Bases: tallypi.config.Option

Option definition for lists

The type is used for the list elements themselves

min_length: Optional[int] = None

If present, the minimum length of the list

max_length: Optional[int] = None

If present, the maximum length of the list

validate(value: Any)Any[source]

Validate the given value to a list of properly-typed items

The length is checked using min_length and max_length (if defined).

The base class Option.validate() method is then called for each element of the input.

serialize(value: Sequence)List[source]

Serialize the given list of items

The base class Option.serialize() is called for each element of the input.

class tallypi.config.Config(filename: Optional[Union[str, pathlib.Path]] = PosixPath('/home/docs/.config/tallypi.yaml'))[source]

Bases: object

Config data storage using YAML

DEFAULT_FILENAME: ClassVar[pathlib.Path] = PosixPath('/home/docs/.config/tallypi.yaml')

The default config filename

filename: pathlib.Path

Path to configuration file

read()Dict[source]

Read data from filename and return the result

If the file does not exist, an empty dictionary is returned

write(data: Dict)[source]

Write the given dict data to the config filename