Application#

class Application(**kwargs)#

Bases: Section

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)

  • kwargs (Any)

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.

Return type:

None

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_config: dict[str, ConfigValue]#

Configuration values obtained from command line arguments.

config: dict[str, ConfigValue]#

Configuration values obtained from command line arguments and configuration files.

config_files: Unicode | List[Unicode]#

  • Default value: ['config.toml']

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

copy(**kwargs)#

Return a copy.

Parameters:

kwargs (Any)

Return type:

Self

exit(exit_status=0)#

Exit python interpreter.

Parameters:

exit_status (int | str)

Return type:

Never

file_config: dict[str, ConfigValue]#

Configuration values obtained from configuration files.

file_loaders: dict[tuple[str, ...], str | type] = {('json',): 'neba.config.loaders.json.JsonLoader', ('py', 'ipy'): 'neba.config.loaders.python.PyLoader', ('toml',): 'neba.config.loaders.toml.TomlkitLoader', ('yaml', 'yml'): 'neba.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: Instance[Logger]#

  • Default value: None

  • Not configurable

Logger instance.

log_datefmt: Unicode#

  • Default value: "%Y-%m-%d %H:%M:%S"

The date format used by logging formatters for ‘asctime’

log_format: Unicode#

  • Default value: "[%(levelname)s]%(name)s:: %(message)s"

The Logging format template

log_level: Enum | Int#

  • Default value: "INFO"

  • Accepted values: "DEBUG", "INFO", "WARN", "ERROR", "CRITICAL"

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 (Any) – 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', comment_default=False, 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

  • comment_default (bool) – If True (default is False), comment the line key = value if the value is equal to the trait 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

Return type:

None