ApplicationBase#

class ApplicationBase(**kwargs)#

Bases: Section, LoggingConfigurable

Base application class.

Orchestrate the loading of configuration keys from files or from command line arguments. Pass the combined configuration keys to the appropriate sections in the configuration tree structure. This validate the values and instantiate the configuration objects.

Parameters:

start (bool)

__init__(start=True, **kwargs)#

Initialize section.

Parameters:
  • config – Flat dictionary containing values for the traits of this section, and its subsections. If a value is missing, the trait default value is used.

  • start (bool)

Return type:

None

_create_cli_loader(argv, log=None, **kwargs)#

Create a CLILoader instance to parse command line arguments.

Parameters:
Return type:

CLILoader

_get_logging_config()#

Return dictionary config for logging.

See logging.config.dictConfig().

Whenever the relevant traits or the logger are modified, callbacks events will use this method to create a configuration dict. It can be overriden in your application class to modify the logging configuration further.

Return type:

dict

_select_file_loader(filename)#

Return the first appropriate FileLoader for this file.

Parameters:

filename (str)

Return type:

type[FileLoader]

_subsections: dict[str, type[Section]] = {}#

Mapping of nested Section classes.

classmethod add_extra_parameters(traits=None, **kwargs)#

Add extra parameters to a section named ‘extra’.

The section will be created if it does not exist already.

Parameters:
  • traits (Mapping[str, TraitType] | None) – Parameters to add as traits.

  • kwargs (TraitType) – Parameters to add as traits.

auto_instantiate = True#

Instantiate all sections in the configuration tree at application start.

Instantiation is necessary to fully validate the values of the configuration parameters, but in case systematic instantiation is unwanted this can be disabled (for example in case of costly instantiations).

cli_conf: dict[str, ConfigValue]#

Configuration values obtained from command line arguments.

conf: dict[str, ConfigValue]#

Configuration values obtained from command line arguments and configuration files.

config_files#

Path to configuration files. Either relative from interpreter working directory or absolute.

copy(**kwargs)#

Return a copy.

Return type:

Self

exit(exit_status=0)#

Exit python interpreter.

Parameters:

exit_status (int | str)

file_conf: dict[str, ConfigValue]#

Configuration values obtained from configuration files.

file_loaders: dict[tuple[str, ...], str | type] = {('json',): 'data_assistant.config.loaders.json.JsonLoader', ('py', 'ipy'): 'data_assistant.config.loaders.python.PyLoader', ('toml',): 'data_assistant.config.loaders.toml.TomlkitLoader', ('yaml', 'yml'): 'data_assistant.config.loaders.yaml.YamlLoader'}#

Mapping from file extension to loader class or location of loader to import.

get_argv()#

Return command line arguments.

Try to detect if launched from ipython or ipykernel (jupyter), in which case strip parameters before the first ‘–’ are stripped.

Return type:

list[str] | None

ignore_cli = False#

If True, do not parse command line arguments.

load_config_files()#

Return configuration loaded from files.

Return type:

dict[str, ConfigValue]

log_datefmt#

The date format used by logging formatters for ‘asctime’

log_format#

The Logging format template

log_level#

Set the log level by value or name.

parse_command_line(argv=None, log=None, **kwargs)#

Return configuration parsed from command line arguments.

Parameters:
  • argv (list[str] | None) – Command line arguments. If None, they are obtained through get_argv().

  • kwargs – Passed to CLILoader initialization.

  • log (Logger | None)

Return type:

dict[str, ConfigValue]

resolve_config_value(cv)#

Validate a ConfigValue.

Keys can use aliases/shortcuts. We normalize all keys as dot separated attributes names, without shortcuts, that point a trait. Keys that do not resolve to any known trait will raise error.

The trait and containing section class will be added to the ConfigValue.

Parameters:

cv (ConfigValue)

Return type:

ConfigValue

start(argv=None, ignore_cli=None, instantiate=None)#

Initialize and start application.

  • Parse command line arguments (optional)

  • Load configuration file(s)

  • Merge configurations

  • (Re)Apply configuration to Application

  • Instantiate sections objects (optional)

Instantiation is necessary to fully validate the values of the configuration parameters, but in case systematic instantiation is unwanted this can be disabled (for example in case of costly instantiations).

Parameters:
  • argv (list[str] | None) – Override command line arguments to parse. If left to None, arguments are obtained from get_argv().

  • ignore_cli (bool | None) – If True, do not parse command line arguments. If not None, this argument overrides ignore_cli.

  • instantiate (bool | None) – If True, instantiate all sections. If not None, this argument overrides auto_instantiate.

Return type:

None

write_config(filename=None, comment='full', use_current_values=True, clobber=None)#

(Over)write a configuration file.

Parameters:
  • filename (str | None) – Write to this file. If None, the first filename from config_files is used.

  • comment (str) –

    Include more or less information in comments. Can be one of:

    • full: all information about traits is included

    • no-help: help string is not included

    • none: no information is included, only the key and default value

    Note that the line containing the key and value, for instance traitname = 2 will be commented if the value is equal to the default.

  • use_current_values (bool) – If True (default), use the current values of the traits instead, otherwise use the trait default value.

  • clobber (str | None) –

    If the target file already exists, either:

    • abort: do nothing

    • overwrite: the file is completely overwritten with the current configuration

    • update: the configuration keys specified in the existing file are kept. They take precedence over the current application config.

    • None: ask what to do interactively in the console