batconf.sources package

Subpackages

Submodules

batconf.sources.argparse module

class batconf.sources.argparse.NamespaceConfig(namespace)

Bases: SourceInterface

A configuration source that retrieves values from an argparse.Namespace object.

parameters

namespaceargparse.Namespace:

An argparse.Namespace instance.

Examples

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--host', dest='root.host', default='localhost')
>>> args = parser.parse_args()
>>> config = NamespaceConfig(args)
>>> config.get('root.host')
'localhost'
type namespace:

Namespace

param namespace:

get(key, module=None)
Parameters:
  • key (str)

  • module (str | None)

Return type:

str | None

batconf.sources.args module

class batconf.sources.args.CliArgsConfig(args)

Bases: SourceInterface

Legacy argparse.Namespace configuration source.

Using this source, the key value will overwrite every option where the final key in its path matches. project.key1, project.thing1.key1, project.thing2.key1, etc. would all be set to “crash”.

Example:

> bat print_config key1=crash key2=override
project <class 'project.cfg.ProjectConfig'>:
    |- submodule <class 'project.submodule.SubmoduleConfig'>:
    |    |- sub <class 'project.submodule.sub.MyClient.Config'>:
    |    |    |- key1: "crash"
    |    |    |- key2: "override"
    |- clients <class 'project.cfg.ClientsSchema'>:
    |    |- key1: "crash"

This is often sufficient for smaller projects, where the name collisions do not cause a problem, and allows for simpler argparser setup where you do not need to specify a ‘dest=’ parameter for arguments.

Parameters:

args (Namespace)

get(key, module=None)
Parameters:
  • key (str)

  • module (str | None)

Return type:

str | None

batconf.sources.dataclass module

class batconf.sources.dataclass.DataclassConfig(ConfigClass, path=None)

Bases: SourceInterface

Extract default values from the Config dataclass. Properties without defaults are set to None.

Parameters:
  • ConfigClass (ConfigP | Any) – a Config dataclass or ConfigP obj

  • path (str | None)

get(key, module=None)
Parameters:
  • key (str)

  • module (str | None)

Return type:

str | None

batconf.sources.env module

class batconf.sources.env.EnvConfig

Bases: SourceInterface

Configuration source that reads from environment variables.

Keys are resolved by converting the dotted config path into an upper-case, underscore-separated environment variable name. For example, project.database.host becomes PROJECT_DATABASE_HOST. When no path is provided the prefix BAT is used instead.

Examples

>>> import os
>>> os.environ['PROJECT_DATABASE_HOST'] = 'localhost'
>>> src = EnvSource()
>>> src.get(key='host', path='project.database')
'localhost'
env_name(key, module=None)
Parameters:
  • key (str)

  • module (str | None)

Return type:

str

get(key, module=None)
Parameters:
  • key (str)

  • module (str | None)

Return type:

str | None

batconf.sources.file module

class batconf.sources.file.FileConfigReprP(*args, **kwargs)

Bases: Protocol

class batconf.sources.file.FileLoaderP(*args, **kwargs)

Bases: Protocol

class batconf.sources.file.MissingFileHandlerP(*args, **kwargs)

Bases: Protocol

batconf.sources.file.file_config_repr(self)
Parameters:

self (FileConfigReprP)

Return type:

str

batconf.sources.file.load_file_error_when_missing(loader_fn, file_path, empty_fallback=Ellipsis)
Parameters:
  • loader_fn (FileLoaderP)

  • file_path (Path)

  • empty_fallback (Any)

batconf.sources.file.load_file_ignore_when_missing(loader_fn, file_path, empty_fallback)
Parameters:
  • loader_fn (FileLoaderP)

  • file_path (Path)

  • empty_fallback (Any)

Return type:

Any

batconf.sources.file.load_file_warn_when_missing(loader_fn, file_path, empty_fallback)
Parameters:
  • loader_fn (FileLoaderP)

  • file_path (Path)

  • empty_fallback (Any)

Return type:

Any

batconf.sources.ini module

class batconf.sources.ini.ConfigParserP(*args, **kwargs)

Bases: Protocol

get(section, option, fallback=None)
Parameters:
  • section (str)

  • option (str)

  • fallback (str | None)

Return type:

str | None

class batconf.sources.ini.EmptyConfigParser

Bases: object

get(section, option, fallback=None)
Parameters:
  • section (str)

  • option (str)

Return type:

None

class batconf.sources.ini.IniSource(file_path, file_format='environments', config_env=None, missing_file_option='warn')

Bases: FileSourceP

Configuration source backed by an INI file.

Parameters

file_pathstr

Path to the INI configuration file.

file_format{‘environments’, ‘sections’, ‘flat’}, default=’environments’

INI file layout. 'environments' expects top-level sections named after environments; 'sections' uses sections as config namespaces; 'flat' reads all keys from a single [root] section.

config_envstr or None, default=read from file

Active configuration environment. When not provided, the value of batconf.default_env in the INI file is used.

missing_file_option{‘warn’, ‘ignore’, ‘error’}, default=’warn’

Behaviour when the specified file is missing.

Examples

>>> src = IniSource(file_path='config.ini', config_env='dev')
type file_path:

str

param file_path:

type file_format:

Literal['flat', 'sections', 'environments']

param file_format:

type config_env:

str | None

param config_env:

type missing_file_option:

Literal['ignore', 'warn', 'error']

param missing_file_option:

get(key, path=None)
Parameters:
  • key (str)

  • path (str | None)

Return type:

str | None

batconf.sources.toml module

class batconf.sources.toml.TomlSource(file_path, file_format='environments', config_env=None, missing_file_option='warn')

Bases: FileSourceP

Configuration source backed by a TOML file.

Parameters

file_pathstr

Path to the TOML configuration file.

file_format{‘environments’, ‘sections’, ‘flat’}, default=’environments’

TOML file layout. 'environments' expects a [batconf] table with a default_env key and per-environment top-level tables; 'sections' uses tables as config namespaces; 'flat' reads all keys from the top-level table.

config_envstr or None, default=read from file

Active configuration environment. When not provided, the value of batconf.default_env in the TOML file is used.

missing_file_option{‘warn’, ‘ignore’, ‘error’}, default=’warn’

Behaviour when the specified file is missing.

Examples

>>> src = TomlSource(file_path='config.toml', config_env='dev')
type file_path:

str

param file_path:

type file_format:

Literal['flat', 'sections', 'environments']

param file_format:

type config_env:

str | None

param config_env:

type missing_file_option:

Literal['ignore', 'warn', 'error']

param missing_file_option:

get(key, path=None)
Parameters:
  • key (str)

  • path (str | None)

Return type:

str | None

keys()
Return type:

list[str]

batconf.sources.types module

class batconf.sources.types.FileSourceP(file_path, file_format='environments', config_env=None, missing_file_option='warn')

Bases: SourceInterfaceP, Protocol

Protocol for file-backed configuration sources.

Defines the standard constructor and query interface that all file-based configuration sources must satisfy.

Parameters

file_pathstr

Path to the configuration file.

file_format{‘environments’, ‘sections’, ‘flat’}, default=’environments’

File layout. 'environments' reads a per-environment subtree selected by config_env; 'sections' uses top-level sections as namespaces; 'flat' reads from a single top-level mapping.

config_envstr or None, default=None

Active configuration environment. When None, the default defined inside the file is used.

missing_file_option{‘warn’, ‘ignore’, ‘error’}, default=’warn’

Behaviour when the specified file does not exist.

type file_path:

str

param file_path:

type file_format:

Literal['flat', 'sections', 'environments']

param file_format:

type config_env:

str | None

param config_env:

type missing_file_option:

Literal['ignore', 'warn', 'error']

param missing_file_option:

class batconf.sources.types.SourceInterfaceP(*args, **kwargs)

Bases: Protocol

get(key, path)
Parameters:
  • key (str)

  • path (str | None)

Return type:

str | None

batconf.sources.yaml module

class batconf.sources.yaml.YamlSource(file_path, file_format='environments', config_env=None, missing_file_option='warn')

Bases: FileSourceP

Configuration source backed by a YAML file.

Parameters

file_pathstr

Path to the YAML configuration file.

file_format{‘environments’, ‘sections’, ‘flat’}, default=’environments’

YAML file layout. 'environments' expects a batconf mapping with a default_env key and per-environment top-level mappings; 'sections' uses top-level keys as config namespaces; 'flat' reads all keys from the top level.

config_envstr or None, default=read from file

Active configuration environment. When not provided, the value of batconf.default_env in the YAML file is used.

missing_file_option{‘warn’, ‘ignore’, ‘error’}, default=’warn’

Behaviour when the specified file is missing.

Examples

>>> src = YamlSource(file_path='config.yaml', config_env='dev')
type file_path:

str

param file_path:

type file_format:

Literal['flat', 'sections', 'environments']

param file_format:

type config_env:

str | None

param config_env:

type missing_file_option:

Literal['ignore', 'warn', 'error']

param missing_file_option:

get(key, path=None)
Parameters:
  • key (str)

  • path (str | None)

Return type:

str | None

keys()
batconf.sources.yaml.get_file_path(file_name, when_missing='warn')
Parameters:
  • file_name (str)

  • when_missing (Literal['ignore', 'warn', 'error'])

Return type:

Path

Module contents