confattr package

Submodules

Module contents

Config Attributes

A python library to read and write config files with a syntax inspired by vimrc and ranger config.

class confattr.ArgumentParser(prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=<class 'argparse.HelpFormatter'>, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True, exit_on_error=True)

Bases: ArgumentParser

error(message: str) NoReturn

Raise a ParseException.

class confattr.Config(key: str, default: T_co, *, help: str | dict[+T_co, str] | None = None, unit: str | None = None, parent: DictConfig[Any, T_co] | None = None, allowed_values: Sequence[T_co] | None = None)

Bases: Generic[T_co]

Each instance of this class represents a setting which can be changed in a config file.

This class implements the descriptor protocol to return value if an instance of this class is accessed as an instance attribute. If you want to get this object you need to access it as a class attribute.

Parameters:
  • key – The name of this setting in the config file

  • default – The default value of this setting

  • help – A description of this setting

  • unit – The unit of an int or float value

  • parent – Applies only if this is part of a DictConfig

  • allowed_values – The possible values this setting can have. Values read from a config file or an environment variable are checked against this. The default value is not checked.

T_co can be one of:
  • str

  • int

  • float

  • bool

  • a subclass of enum.Enum (the value used in the config file is the name in lower case letters with hyphens instead of underscores)

  • a class where __str__() returns a string representation which can be passed to the constructor to create an equal object. A help which is written to the config file must be provided as a str in the class attribute help or by calling Set.set_help_for_type(). If that class has a str attribute type_name this is used instead of the class name inside of config file.

  • a list of any of the afore mentioned data types. The list may not be empty when it is passed to this constructor so that the item type can be derived but it can be emptied immediately afterwards. (The type of the items is not dynamically enforced—that’s the job of a static type checker—but the type is mentioned in the help.)

Raises:
  • ValueError – if key is not unique

  • ValueError – if default is an empty list because the first element is used to infer the data type to which a value given in a config file is converted

  • TypeError – if this setting is a number or a list of numbers and unit is not given

LIST_SEP = ','
allowed_values: Sequence[T_co] | None

The values which are allowed for this setting. Trying to set this setting to a different value in the config file is considered an error. If you set this setting in the program the value is not checked.

default_config_id = 'general'
format_allowed_values(t: type[Any] | None = None) str
format_allowed_values_or_type(t: type[Any] | None = None) str
format_any_value(value: Any) str
format_type(t: type[Any] | None = None) str
format_value(config_id: ConfigId | None) str
help: str | dict[+T_co, str] | None

A description of this setting or a description for each allowed value.

instances: dict[str, confattr.config.Config[Any]] = {}

A mapping of all Config instances. The key in the mapping is the key attribute. The value is the Config instance. New Config instances add themselves automatically in their constructor.

property key: str

The name of this setting which is used in the config file. This must be unique.

parse_and_set_value(config_id: ConfigId | None, value: str) None
parse_value(value: str) T_co
parse_value_part(t: type[T], value: str) T
Raises:

ValueError – if value is invalid

unit: str | None

The unit of value if value is a number.

value: T_co

The value of this setting.

wants_to_be_exported() bool
class confattr.ConfigFile(*, notification_level: ~confattr.config.Config[~confattr.configfile.NotificationLevel] = NotificationLevel.ERROR, appname: str, authorname: str | None = None, config_instances: dict[str, confattr.config.Config[typing.Any]] = {}, commands: ~collections.abc.Sequence[type[confattr.configfile.ConfigFileCommand]] | None = None, formatter_class: type[argparse.HelpFormatter] = <class 'confattr.utils.HelpFormatter'>, check_config_id: ~collections.abc.Callable[[~confattr.config.ConfigId], None] | None = None, enable_config_ids: bool | None = None)

Bases: object

Read or write a config file.

Parameters:
  • notification_level – A Config which the users of your application can set to choose whether they want to see information which might be interesting for debugging a config file. A Message with a priority lower than this value is not passed to the callback registered with set_ui_callback().

  • appname – The name of the application, required for generating the path of the config file if you use load() or save() and as prefix of environment variable names

  • authorname – The name of the developer of the application, on MS Windows useful for generating the path of the config file if you use load() or save()

  • config_instances – The Config instances to load or save, defaults to Config.instances

  • commands – The ConfigFileCommand`s allowed in this config file, if this is :const:`None: use the return value of ConfigFileCommand.get_command_types()

  • formatter_class – Is used to clean up doc strings and wrap lines in the help

  • check_config_id – Is called every time a configuration group is opened (except for Config.default_config_id—that is always allowed). The callback should raise a ParseException if the config id is invalid.

  • enable_config_ids – see enable_config_ids. If None: Choose True or False automatically based on check_config_id and the existence of MultiConfig/MultiDictConfig

COMMENT = '#'
COMMENT_PREFIXES = ('"', '#')
ENTER_GROUP_PREFIX = '['
ENTER_GROUP_SUFFIX = ']'
config_directory: str | None = None

Override the config directory which is returned by iter_user_site_config_paths(). You should set either this attribute or config_path in your tests with monkeypatch.setattr. If the environment variable APPNAME_CONFIG_DIRECTORY is set this attribute is set to it’s value in the constructor (where APPNAME is the value which is passed as appname to the constructor but in all upper case letters and hyphens and spaces replaced by underscores.)

config_name = 'config'

The name of the config file used by iter_config_paths(). Can be changed with the environment variable APPNAME_CONFIG_NAME (where APPNAME is the value which is passed as appname to the constructor but in all upper case letters and hyphens and spaces replaced by underscores.).

config_path: str | None = None

Override the config file which is returned by iter_config_paths(). You should set either this attribute or config_directory in your tests with monkeypatch.setattr. If the environment variable APPNAME_CONFIG_PATH is set this attribute is set to it’s value in the constructor (where APPNAME is the value which is passed as appname to the constructor but in all upper case letters and hyphens and spaces replaced by underscores.)

context_file_name: str | None = None

The name of the file which is currently loaded. If this equals Message.ENVIRONMENT_VARIABLES it is no file name but an indicator that environment variables are loaded. This is None if parse_line() is called directly (e.g. the input from a command line is parsed).

context_line: str = ''

The line which is currently parsed.

context_line_number: int | None = None

The number of the line which is currently parsed. This is None if context_file_name is not a file name.

create_formatter() HelpFormatterWrapper
enable_config_ids: bool

If true: [config-id] syntax is allowed in config file, config ids are included in help, config id related options are available for include. If false: It is not possible to set different values for different objects (but default values for MultiConfig instances can be set)

enter_group(ln: str) bool

Check if ln starts a new group and set config_id if it does. Call parse_error() if check_config_id raises a ParseException.

Parameters:

ln – The current line

Returns:

True if ln starts a new group

get_app_dirs() AppDirs

Create or get a cached AppDirs instance with multipath support enabled.

When creating a new instance, platformdirs, xdgappdirs and appdirs are tried, in that order. The first one installed is used. appdirs, the original of the two forks and the only one of the three with type stubs, is specified in pyproject.toml as a hard dependency so that at least one of the three should always be available. I am not very familiar with the differences but if a user finds that appdirs does not work for them they can choose to use an alternative with pipx inject appname xdgappdirs|platformdirs.

These libraries should respect the environment variables XDG_CONFIG_HOME and XDG_CONFIG_DIRS.

get_env_name(key: str) str

Convert the key of a setting to the name of the corresponding environment variable.

Returns:

An all upper case version of key with all hyphens, dots and spaces replaced by underscores and envprefix prepended to the result. (envprefix is set in the constructor by first setting it to an empty str and then passing the value of appname to this method and appending an underscore.)

get_help() str

A convenience wrapper around write_help() to return the help as a str instead of writing it to a file.

This uses HelpWriter.

get_help_config_id() str
Returns:

A help how to use MultiConfig. The return value still needs to be cleaned with inspect.cleandoc().

get_save_path() str
Returns:

The first existing and writable file returned by iter_config_paths() or the first path if none of the files are existing and writable.

is_comment(ln: str) bool

Check if ln is a comment.

Parameters:

ln – The current line

Returns:

True if ln is a comment

iter_config_paths() Iterator[str]

Iterate over all paths which are checked for config files, user specific first.

Use this method if you want to tell the user where the application is looking for it’s config file. The first existing file yielded by this method is used by load().

The paths are generated by joining the directories yielded by iter_user_site_config_paths() with ConfigFile.config_name.

If config_path has been set this method yields that path instead and no other paths.

iter_user_site_config_paths() Iterator[str]

Iterate over all directories which are searched for config files, user specific first.

The directories are based on get_app_dirs() unless config_directory has been set. If config_directory has been set it’s value is yielded and nothing else.

load(*, env: bool = True) None

Load the first existing config file returned by iter_config_paths().

If there are several config files a user specific config file is preferred. If a user wants a system wide config file to be loaded, too, they can explicitly include it in their config file. :param _sphinx_paramlinks_confattr.ConfigFile.load.env: If true: call load_env() after loading the config file.

load_env() None

Load settings from environment variables. The name of the environment variable belonging to a setting is generated with get_env_name().

Environment variables not matching a setting or having an invalid value are reported with self.ui_notifier.show_error().

load_file(fn: str) None

Load a config file and change the Config objects accordingly.

Use set_ui_callback() to get error messages which appeared while loading the config file. You can call set_ui_callback() after this method without loosing any messages.

Parameters:

fn – The file name of the config file (absolute or relative path)

load_without_resetting_config_id(fn: str) None
parse_error(msg: str) None

Is called if something went wrong while trying to load a config file.

This method is called when a ParseException or MultipleParseExceptions is caught. This method compiles the given information into an error message and calls self.ui_notifier.show_error().

Parameters:

msg – The error message

parse_line(line: str) None
Parameters:

line – The line to be parsed

parse_error() is called if something goes wrong, e.g. invalid key or invalid value.

parse_splitted_line(ln_splitted: Sequence[str]) None

Call the corresponding command in command_dict. If any ParseException or MultipleParseExceptions is raised catch it and call parse_error().

quote(val: str) str

Quote a value if necessary so that it will be interpreted as one argument.

The default implementation calls readable_quote().

save(**kw: Unpack[SaveKwargs]) str

Save the current values of all settings to the file returned by get_save_path(). Directories are created as necessary.

Parameters:
  • config_instances – Do not save all settings but only those given. If this is a list they are written in the given order. If this is a set they are sorted by their keys.

  • ignore – Do not write these settings to the file.

  • no_multi – Do not write several sections. For MultiConfig instances write the default values only.

  • comments – Write comments with allowed values and help.

Returns:

The path to the file which has been written

save_file(fn: str, **kw: Unpack[SaveKwargs]) None

Save the current values of all settings to a specific file.

Parameters:

fn – The name of the file to write to. If this is not an absolute path it is relative to the current working directory.

Raises:

FileNotFoundError – if the directory does not exist

For an explanation of the other parameters see save().

save_to_open_file(f: TextIO, **kw: Unpack[SaveKwargs]) None

Save the current values of all settings to a file-like object by creating a ConfigFileWriter object and calling save_to_writer().

Parameters:

f – The file to write to

For an explanation of the other parameters see save().

save_to_writer(writer: FormattedWriter, **kw: Unpack[SaveKwargs]) None

Save the current values of all settings.

Ensure that all keyword arguments are passed with set_save_default_arguments(). Iterate over all ConfigFileCommand objects in self.commands and do for each of them:

set_save_default_arguments(kw: SaveKwargs) None

Ensure that all arguments are given in kw.

set_ui_callback(callback: Callable[[Message], None]) None

Register a callback to a user interface in order to show messages to the user like syntax errors or invalid values in the config file.

Messages which occur before this method is called are stored and forwarded as soon as the callback is registered.

Parameters:

ui_callback – A function to display messages to the user

write_config_id(writer: FormattedWriter, config_id: ConfigId) None

Start a new group in the config file so that all following commands refer to the given config_id.

write_help(writer: FormattedWriter) None
class confattr.ConfigFileArgparseCommand(config_file: ConfigFile)

Bases: ConfigFileCommand

An abstract subclass of ConfigFileCommand which uses argparse to make parsing and providing help easier.

You must implement the class method init_parser() to add the arguments to parser. Instead of run() you must implement run_parsed(). You don’t need to add a usage or the possible arguments to the doc string as argparse will do that for you. You should, however, still give a description what this command does in the doc string.

You may specify ConfigFileCommand.name, ConfigFileCommand.aliases and ConfigFileCommand.save() like for ConfigFileCommand.

get_help() str

Creates a help text which can be presented to the user by calling parser.format_help(). The return value of ConfigFileCommand.write_help() has been passed as description to the constructor of ArgumentParser, therefore help/the doc string are included as well.

abstract init_parser(parser: ArgumentParser) None
Parameters:

parser – The parser to add arguments to. This is the same object like parser.

This is an abstract method which must be implemented by subclasses. Use ArgumentParser.add_argument() to add arguments to parser.

run(cmd: Sequence[str]) None

Process one line which has been read from a config file

Raises:
abstract run_parsed(args: Namespace) None

This is an abstract method which must be implemented by subclasses.

class confattr.ConfigFileCommand(config_file: ConfigFile)

Bases: ABC

An abstract base class for commands which can be used in a config file.

Subclasses must implement the run() method which is called when ConfigFile is loading a file. Subclasses should contain a doc string so that get_help() can provide a description to the user. Subclasses may set the name and aliases attributes to change the output of get_name() and get_names().

All subclasses are remembered and can be retrieved with get_command_types(). They are instantiated in the constructor of ConfigFile.

add_help_to(formatter: HelpFormatterWrapper) None

Add the return value of get_help_attr_or_doc_str() to formatter.

aliases: tuple[str, ...] | list[str]

Alternative names which can be used in the config file.

create_formatter() HelpFormatterWrapper
classmethod delete_command_type(cmd_type: type[confattr.configfile.ConfigFileCommand]) None

Delete cmd_type so that it is not returned anymore by get_command_types() and that it’s name can be used by another command. Do nothing if cmd_type has already been deleted.

classmethod get_command_types() tuple[type[confattr.configfile.ConfigFileCommand], ...]
Returns:

All subclasses of ConfigFileCommand which have not been deleted with delete_command_type()

get_help() str
Returns:

A help text which can be presented to the user.

This is generated by creating a formatter with create_formatter(), adding the help to it with add_help_to() and stripping trailing new line characters from the result of HelpFormatterWrapper.format_help().

Most likely you don’t want to override this method but add_help_to() instead.

get_help_attr_or_doc_str() str
Returns:

The help attribute or the doc string if help has not been set, cleaned up with inspect.cleandoc().

classmethod get_name() str
Returns:

The name which is used in config file to call this command.

If name is set it is returned as it is. Otherwise a name is generated based on the class name.

classmethod get_names() Iterator[str]
Returns:

Several alternative names which can be used in a config file to call this command.

The first one is always the return value of get_name(). If aliases is set it’s items are yielded afterwards.

If one of the returned items is the empty string this class is the default command and run() will be called if an undefined command is encountered.

help: str

A description which may be used by an in-app help. If this is not set get_help() uses the doc string instead.

name: str

The name which is used in the config file to call this command. Use an empty string to define a default command which is used if an undefined command is encountered. If this is not set get_name() returns the name of this class in lower case letters and underscores replaced by hyphens.

abstract run(cmd: Sequence[str]) None

Process one line which has been read from a config file

Raises:
save(writer: FormattedWriter, **kw: Unpack[SaveKwargs]) None

Write as many calls to this command as necessary to the config file in order to create the current state. There is the config_file attribute (which was passed to the constructor) which you can use to:

The default implementation does nothing.

should_write_heading: bool = False

If a config file contains only a single section it makes no sense to write a heading for it. This attribute is set by ConfigFile.save_to_writer() if there are several commands which implement the save() method. If you implement save() and this attribute is set then save() should write a section header. If save() writes several sections it should always write the headings regardless of this attribute.

class confattr.ConfigFileWriter(f: TextIO | None, prefix: str)

Bases: TextIOWriter

write_command(cmd: str) None

Write a config file command.

write_heading(lvl: SectionLevel, heading: str) None

Write a heading.

This object should not add an indentation depending on the section because if the indentation is increased the line width should be decreased in order to keep the line wrapping consistent. Wrapping lines is handled by confattr.utils.HelpFormatter, i.e. before the text is passed to this object. It would be possible to use argparse.RawTextHelpFormatter instead and handle line wrapping on a higher level but that would require to understand the help generated by argparse in order to know how far to indent a broken line. One of the trickiest parts would probably be to get the indentation of the usage right. Keep in mind that the term “usage” can differ depending on the language settings of the user.

Parameters:
  • lvl – How to format the heading

  • heading – The heading

write_line(ln: str) None

Write a single line of documentation. ln may not contain a newline. If ln is empty it does not need to be prefixed with a comment character. Empty lines should be dropped if no other lines have been written before.

class confattr.ConfigTrackingChanges(key: str, default: T_co, *, help: str | dict[+T_co, str] | None = None, unit: str | None = None, parent: DictConfig[Any, T_co] | None = None, allowed_values: Sequence[T_co] | None = None)

Bases: Config[T_co]

Parameters:
  • key – The name of this setting in the config file

  • default – The default value of this setting

  • help – A description of this setting

  • unit – The unit of an int or float value

  • parent – Applies only if this is part of a DictConfig

  • allowed_values – The possible values this setting can have. Values read from a config file or an environment variable are checked against this. The default value is not checked.

T_co can be one of:
  • str

  • int

  • float

  • bool

  • a subclass of enum.Enum (the value used in the config file is the name in lower case letters with hyphens instead of underscores)

  • a class where __str__() returns a string representation which can be passed to the constructor to create an equal object. A help which is written to the config file must be provided as a str in the class attribute help or by calling Set.set_help_for_type(). If that class has a str attribute type_name this is used instead of the class name inside of config file.

  • a list of any of the afore mentioned data types. The list may not be empty when it is passed to this constructor so that the item type can be derived but it can be emptied immediately afterwards. (The type of the items is not dynamically enforced—that’s the job of a static type checker—but the type is mentioned in the help.)

Raises:
  • ValueError – if key is not unique

  • ValueError – if default is an empty list because the first element is used to infer the data type to which a value given in a config file is converted

  • TypeError – if this setting is a number or a list of numbers and unit is not given

has_changed() bool
Returns:

True if value has been changed since the last call to save_value()

restore_value() None

Restore value to the value before the last call of save_value().

save_value(new_value: T) None

Save the current value and assign new_value to value.

property value: T

The value of this setting.

class confattr.DictConfig(key_prefix: str, default_values: dict[T_KEY, T], *, ignore_keys: Container[T_KEY] = {}, unit: str | None = None, help: str | None = None, allowed_values: Sequence[T] | None = None)

Bases: Generic[T_KEY, T]

A container for several settings which belong together. It can be indexed like a normal dict but internally the items are stored in Config instances.

In contrast to a Config instance it does not make a difference whether an instance of this class is accessed as a type or instance attribute.

Parameters:
  • key_prefix – A common prefix which is used by format_key() to generate the key by which the setting is identified in the config file

  • default_values – The content of this container. A Config instance is created for each of these values (except if the key is contained in ignore_keys). See format_key().

  • ignore_keys – All items which have one of these keys are not stored in a Config instance, i.e. cannot be set in the config file.

  • unit – The unit of all items

  • help – A help for all items

  • allowed_values – The values which the items can have

Raises:

ValueError – if a key is not unique

format_key(key: T_KEY) str

Generate a key by which the setting can be identified in the config file based on the dict key by which the value is accessed in the python code.

Returns:

key_prefix + dot + key

iter_keys() Iterator[str]

Iterate over the keys by which the settings can be identified in the config file

new_config(key: str, default: T, *, unit: str | None, help: str | dict[T, str] | None) Config[T]

Create a new Config instance to be used internally

class confattr.FormattedWriter

Bases: ABC

abstract write_command(cmd: str) None

Write a config file command.

abstract write_heading(lvl: SectionLevel, heading: str) None

Write a heading.

This object should not add an indentation depending on the section because if the indentation is increased the line width should be decreased in order to keep the line wrapping consistent. Wrapping lines is handled by confattr.utils.HelpFormatter, i.e. before the text is passed to this object. It would be possible to use argparse.RawTextHelpFormatter instead and handle line wrapping on a higher level but that would require to understand the help generated by argparse in order to know how far to indent a broken line. One of the trickiest parts would probably be to get the indentation of the usage right. Keep in mind that the term “usage” can differ depending on the language settings of the user.

Parameters:
  • lvl – How to format the heading

  • heading – The heading

abstract write_line(ln: str) None

Write a single line of documentation. ln may not contain a newline. If ln is empty it does not need to be prefixed with a comment character. Empty lines should be dropped if no other lines have been written before.

write_lines(text: str) None

Write one or more lines of documentation.

class confattr.HelpWriter(f: TextIO | None)

Bases: TextIOWriter

write_command(cmd: str) None

Write a config file command.

write_heading(lvl: SectionLevel, heading: str) None

Write a heading.

This object should not add an indentation depending on the section because if the indentation is increased the line width should be decreased in order to keep the line wrapping consistent. Wrapping lines is handled by confattr.utils.HelpFormatter, i.e. before the text is passed to this object. It would be possible to use argparse.RawTextHelpFormatter instead and handle line wrapping on a higher level but that would require to understand the help generated by argparse in order to know how far to indent a broken line. One of the trickiest parts would probably be to get the indentation of the usage right. Keep in mind that the term “usage” can differ depending on the language settings of the user.

Parameters:
  • lvl – How to format the heading

  • heading – The heading

write_line(ln: str) None

Write a single line of documentation. ln may not contain a newline. If ln is empty it does not need to be prefixed with a comment character. Empty lines should be dropped if no other lines have been written before.

class confattr.Include(config_file: ConfigFile)

Bases: ConfigFileArgparseCommand

Load another config file.

This is useful if a config file is getting so big that you want to split it up or if you want to have different config files for different use cases which all include the same standard config file to avoid redundancy or if you want to bind several commands to one key which executes one command with ConfigFile.parse_line().

help_config_id = '\n\tBy default the loaded config file starts with which ever config id is currently active.\n\tThis is useful if you want to use the same values for several config ids:\n\tWrite the set commands without a config id to a separate config file and include this file for every config id where these settings shall apply.\n\n\tAfter the include the config id is reset to the config id which was active at the beginning of the include\n\tbecause otherwise it might lead to confusion if the config id is changed in the included config file.\n\t'
init_parser(parser: ArgumentParser) None
Parameters:

parser – The parser to add arguments to. This is the same object like parser.

This is an abstract method which must be implemented by subclasses. Use ArgumentParser.add_argument() to add arguments to parser.

run_parsed(args: Namespace) None

This is an abstract method which must be implemented by subclasses.

class confattr.InstanceSpecificDictMultiConfig(dmc: MultiDictConfig[T_KEY, T], config_id: ConfigId)

Bases: Generic[T_KEY, T]

An intermediate instance which is returned when accsessing a MultiDictConfig as an instance attribute. Can be indexed like a normal dict.

class confattr.Message(notification_level: NotificationLevel, message: str | BaseException, file_name: str | None = None, line_number: int | None = None, line: str = '')

Bases: object

A message which should be displayed to the user. This is passed to the callback of the user interface which has been registered with ConfigFile.set_ui_callback().

If you want full control how to display messages to the user you can access the attributes directly. Otherwise you can simply convert this object to a str, e.g. with str(msg). I recommend to use different colors for different values of notification_level.

ENVIRONMENT_VARIABLES = 'environment variables'

The value of file_name while loading environment variables.

file_name: str | None

The name of the config file which has caused this message. If this equals ENVIRONMENT_VARIABLES it is not a file but the message has occurred while reading the environment variables. This is None if ConfigFile.parse_line() is called directly, e.g. when parsing the input from a command line.

format_file_name() str
Returns:

A header including the file_name if the file_name is different from the last time this function has been called or an empty string otherwise

format_file_name_msg_line() str
Returns:

The concatenation of the return values of format_file_name() and format_msg_line()

format_msg_line() str

The return value includes the attributes message, line_number and line if they are set.

line: str

The line where the message occurred. This is an empty str if there is no line, e.g. when loading environment variables.

line_number: int | None

The number of the line in the config file. This is None if file_name is not a file name.

property lvl: NotificationLevel

An abbreviation for notification_level

message: str | BaseException

The string or exception which should be displayed to the user

notification_level: NotificationLevel

The importance of this message. I recommend to display messages of different importance levels in different colors. ConfigFile does not output messages which are less important than the notification_level setting which has been passed to it’s constructor.

classmethod reset() None

If you are using format_file_name_msg_line() or __str__() you must call this method when the widget showing the error messages is cleared.

class confattr.MultiConfig(key: str, default: T_co, *, unit: str | None = None, help: str | dict[+T_co, str] | None = None, parent: MultiDictConfig[Any, T_co] | None = None, allowed_values: Sequence[T_co] | None = None, check_config_id: Callable[[MultiConfig[T_co], ConfigId], None] | None = None)

Bases: Config[T_co]

A setting which can have different values for different objects.

This class implements the descriptor protocol to return one of the values in values depending on a config_id attribute of the owning object if an instance of this class is accessed as an instance attribute. If there is no value for the config_id in values value is returned instead. If the owning instance does not have a config_id attribute an AttributeError is raised.

In the config file a group can be opened with [config-id]. Then all following set commands set the value for the specified config id.

Parameters:
  • key – The name of this setting in the config file

  • default – The default value of this setting

  • help – A description of this setting

  • unit – The unit of an int or float value

  • parent – Applies only if this is part of a MultiDictConfig

  • allowed_values – The possible values this setting can have. Values read from a config file or an environment variable are checked against this. The default value is not checked.

  • check_config_id – Is called every time a value is set in the config file (except if the config id is default_config_id—that is always allowed). The callback should raise a ParseException if the config id is invalid.

config_ids: list[confattr.config.ConfigId] = []

A list of all config ids for which a value has been set in any instance of this class (regardless of via code or in a config file and regardless of whether the value has been deleted later on). This list is cleared by reset().

format_value(config_id: ConfigId | None) str
parse_and_set_value(config_id: ConfigId | None, value: str) None
classmethod reset() None

Clear config_ids and clear values for all instances in Config.instances

value: T_co

Stores the default value which is used if no value for the object is defined in values.

values: dict[confattr.config.ConfigId, +T_co]

Stores the values for specific objects.

class confattr.MultiDictConfig(key_prefix: str, default_values: dict[T_KEY, T], *, ignore_keys: Container[T_KEY] = {}, unit: str | None = None, help: str | None = None, allowed_values: Sequence[T] | None = None, check_config_id: Callable[[MultiConfig[T], ConfigId], None] | None = None)

Bases: DictConfig[T_KEY, T]

A container for several settings which can have different values for different objects.

This is essentially a DictConfig using MultiConfig instead of normal Config. However, in order to return different values depending on the config_id of the owning instance, it implements the descriptor protocol to return an InstanceSpecificDictMultiConfig if it is accessed as an instance attribute.

Parameters:
  • key_prefix – A common prefix which is used by format_key() to generate the key by which the setting is identified in the config file

  • default_values – The content of this container. A Config instance is created for each of these values (except if the key is contained in ignore_keys). See format_key().

  • ignore_keys – All items which have one of these keys are not stored in a Config instance, i.e. cannot be set in the config file.

  • unit – The unit of all items

  • help – A help for all items

  • allowed_values – The values which the items can have

  • check_config_id – Is passed through to MultiConfig

Raises:

ValueError – if a key is not unique

new_config(key: str, default: T, *, unit: str | None, help: str | dict[T, str] | None) MultiConfig[T]

Create a new Config instance to be used internally

exception confattr.MultipleParseExceptions(exceptions: Sequence[ParseException])

Bases: Exception

This is raised by ConfigFileCommand implementations in order to communicate that multiple errors have occured on the same line. Is caught in ConfigFile.

class confattr.NotificationLevel(value)

Bases: SortedEnum

An enumeration.

ERROR = 'error'
INFO = 'info'
descending: bool = False
exception confattr.ParseException

Bases: Exception

This is raised by ConfigFileCommand implementations and functions passed to check_config_id in order to communicate an error in the config file like invalid syntax or an invalid value. Is caught in ConfigFile.

class confattr.SaveKwargs

Bases: TypedDict

comments: bool
config_instances: Iterable[Config[Any] | DictConfig[Any, Any]]
ignore: Iterable[Config[Any] | DictConfig[Any, Any]] | None
no_multi: bool
class confattr.SectionLevel(value)

Bases: SortedEnum

An enumeration.

SECTION = 'section'

Is used to separate different commands in ConfigFile.write_help() and ConfigFileCommand.save()

SUB_SECTION = 'sub-section'

Is used for subsections in ConfigFileCommand.save() such as the “data types” section in the help of the set command

descending: bool = False
class confattr.Set(config_file: ConfigFile)

Bases: ConfigFileCommand

usage: set key1=val1 [key2=val2 …] \

set key [=] val

Change the value of a setting.

In the first form set takes an arbitrary number of arguments, each argument sets one setting. This has the advantage that several settings can be changed at once. That is useful if you want to bind a set command to a key and process that command with ConfigFile.parse_line() if the key is pressed.

In the second form set takes two arguments, the key and the value. Optionally a single equals character may be added in between as third argument. This has the advantage that key and value are separated by one or more spaces which can improve the readability of a config file.

KEY_VAL_SEP = '='

The separator which is used between a key and it’s value

add_config_help(formatter: HelpFormatterWrapper, instance: Config[Any]) None
add_help_for_data_types(formatter: HelpFormatterWrapper, config_instances: Iterable[Config[object]]) None
add_help_to(formatter: HelpFormatterWrapper) None

Add the return value of get_help_attr_or_doc_str() to formatter.

format_value(instance: Config[Any], config_id: ConfigId | None) str
Parameters:
  • instance – The config value to be saved

  • config_id – Which value to be written in case of a MultiConfig, should be None for a normal Config instance

Returns:

A str representation to be written to the config file

Convert the value of the Config instance into a str with Config.format_value().

get_data_type_name_to_help_map(config_instances: Iterable[Config[object]]) dict[str, str]
Parameters:

config_instances – All config values to be saved

Returns:

A dictionary containing the type names as keys and the help as values

The returned dictionary contains the help for all data types except enumerations which occur in config_instances. The help is gathered from the help attribute of the type or the str registered with set_help_for_type().

get_help_for_data_types(config_instances: Iterable[Config[object]]) str
help_for_types = {<class 'str'>: 'A text. If it contains spaces it must be wrapped in single or double quotes.', <class 'int'>: "\t\t\tAn integer number in python 3 syntax, as decimal (e.g. 42), hexadecimal (e.g. 0x2a), octal (e.g. 0o52) or binary (e.g. 0b101010).\n\t\t\tLeading zeroes are not permitted to avoid confusion with python 2's syntax for octal numbers.\n\t\t\tIt is permissible to group digits with underscores for better readability, e.g. 1_000_000.", <class 'float'>: 'A floating point number in python syntax, e.g. 23, 1.414, -1e3, 3.14_15_93.'}

Help for data types. This is used by get_help_for_data_types(). Change this with set_help_for_type().

iter_config_instances_to_be_saved(**kw: Unpack[SaveKwargs]) Iterator[Config[object]]
Parameters:
  • config_instances – The settings to consider

  • ignore – Skip these settings

Iterate over all given config_instances and expand all DictConfig instances into the Config instances they consist of. Sort the resulting list if config_instances is not a list or a tuple. Yield all Config instances which are not (directly or indirectly) contained in ignore and where Config.wants_to_be_exported() returns true.

parse_and_set_value(instance: Config[Any], value: str) None

Parse the given value str and assign it to the given instance by calling Config.parse_and_set_value() with ConfigFile.config_id of config_file. Afterwards call UiNotifier.show_info().

parse_key_and_set_value(key: str, value: str) None

Find the corresponding Config instance for key and pass it to parse_and_set_value().

Raises:

ParseException – if key is invalid or parse_and_set_value() raises a ValueError

run(cmd: Sequence[str]) None

Call set_multiple() if the first argument contains KEY_VAL_SEP otherwise set_with_spaces().

Raises:

ParseException – if something is wrong (no arguments given, invalid syntax, invalid key, invalid value)

save(writer: FormattedWriter, **kw: Unpack[SaveKwargs]) None
Parameters:
  • writer – The file to write to

  • no_multi (bool) – If true: treat MultiConfig instances like normal Config instances and only write their default value. If false: Separate MultiConfig instances and print them once for every MultiConfig.config_ids.

  • comments (bool) – If false: don’t write help for data types

Iterate over all Config instances with iter_config_instances_to_be_saved(), split them into normal Config and MultiConfig and write them with save_config_instance(). But before that set last_name to None (which is used by write_config_help()) and write help for data types based on get_help_for_data_types().

save_config_instance(writer: FormattedWriter, instance: Config[object], config_id: ConfigId | None, **kw: Unpack[SaveKwargs]) None
Parameters:
  • writer – The file to write to

  • instance – The config value to be saved

  • config_id – Which value to be written in case of a MultiConfig, should be None for a normal Config instance

  • comments (bool) – If true: call write_config_help()

Convert the Config instance into a value str with format_value(), wrap it in quotes if necessary with config_file.quote() and write it to writer.

classmethod set_help_for_type(t: type[object], help_text: str) None

get_help_for_data_types() is used by save() and get_help(). Usually it uses the help attribute of the class. But if the class does not have a help attribute or if you want a different help text you can set the help with this method.

Parameters:
  • t – The type for which you want to specify a help

  • help_text – The help for t. It is cleaned up in get_help_for_data_types() with HelpFormatterWrapper.format_text() depending on self.config_file.formatter_class.

set_multiple(cmd: Sequence[str]) None

Process one line of the format set key=value [key2=value2 ...]

Raises:

MultipleParseExceptions – if something is wrong (invalid syntax, invalid key, invalid value)

set_with_spaces(cmd: Sequence[str]) None

Process one line of the format set key [=] value

Raises:

ParseException – if something is wrong (invalid syntax, invalid key, invalid value)

write_config_help(writer: FormattedWriter, instance: Config[Any], *, group_dict_configs: bool = True) None
Parameters:
  • writer – The output to write to

  • instance – The config value to be saved

Write a comment which explains the meaning and usage of this setting based on Config.format_allowed_values_or_type() and Config.help.

Use last_name to write the help only once for all Config instances belonging to the same DictConfig instance.

confattr.readable_quote(value: str) str

This function has the same goal like shlex.quote() but tries to generate better readable output.

Parameters:

value – A value which is intended to be used as a command line argument

Returns:

A POSIX compliant quoted version of value