batconf.sources package
Subpackages
- batconf.sources.tests package
- Submodules
- batconf.sources.tests.argparse_test module
- batconf.sources.tests.args_test module
- batconf.sources.tests.dataclass_test module
- batconf.sources.tests.env_test module
- batconf.sources.tests.file_test module
- batconf.sources.tests.ini_test module
GetConfigFunctionsTestsGlobalsTestsGlobalsTests.test_CONFIG_PARSER_ENVS()GlobalsTests.test_CONFIG_PARSER_ENVS_contains_all_environments()GlobalsTests.test_ConfigParser_is_not_nested()GlobalsTests.test_get_batconf_default_env()GlobalsTests.test_get_value_from_dev_environment()GlobalsTests.test_get_value_from_dev_environment_subsection()GlobalsTests.test_get_value_from_prod_environment()
IniConfigTestsIniSourceTestsIniSourceTests.setUp()IniSourceTests.test___init__()IniSourceTests.test___init__catches_invalid_file_format()IniSourceTests.test___repr__()IniSourceTests.test___str__()IniSourceTests.test__config_env()IniSourceTests.test__data()IniSourceTests.test__file_format()IniSourceTests.test__get_impl()IniSourceTests.test__loader()IniSourceTests.test_get()IniSourceTests.test_get_legacy_path_parameter()
- batconf.sources.tests.toml_test module
DeprecationTestsImportTomlLoadFunctionTestsTomlLoaderFunctionsTestsTomlSourceTestsTomlSourceTests.setUp()TomlSourceTests.test___init__defaults()TomlSourceTests.test___repr__()TomlSourceTests.test___str__()TomlSourceTests.test__config_env()TomlSourceTests.test__data()TomlSourceTests.test_config_env_argument()TomlSourceTests.test_get()TomlSourceTests.test_get__from_env()TomlSourceTests.test_get__from_sections()TomlSourceTests.test_keys()TomlSourceTests.test_missing_file_warning()
- batconf.sources.tests.types_test module
- batconf.sources.tests.yaml_test module
YamlConfigDeprecationTestsYamlConfigTestsYamlConfigTests.get_file_pathYamlConfigTests.setUp()YamlConfigTests.test___init__()YamlConfigTests.test___repr__()YamlConfigTests.test___str__()YamlConfigTests.test__data()YamlConfigTests.test__file_format()YamlConfigTests.test__getitem__()YamlConfigTests.test_config_env_argument()YamlConfigTests.test_disable_config_environments()YamlConfigTests.test_get()YamlConfigTests.test_keys()YamlConfigTests.test_loads_given_config_file()YamlConfigTests.test_missing_file_warning()
YamlLoaderFunctionsTestsYamlLoaderFunctionsTests.setUp()YamlLoaderFunctionsTests.test__load_yaml()YamlLoaderFunctionsTests.test__load_yaml__error()YamlLoaderFunctionsTests.test__load_yaml__when_missing_option()YamlLoaderFunctionsTests.test__load_yaml_file()YamlLoaderFunctionsTests.test__load_yaml_file_missing_pyyaml_module()
YamlSourceTestsget_file_pathTests
- Module contents
Submodules
batconf.sources.argparse module
- class batconf.sources.argparse.NamespaceConfig(namespace)
Bases:
SourceInterfaceA 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:
- 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:
SourceInterfaceLegacy 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:
SourceInterfaceExtract default values from the Config dataclass. Properties without defaults are set to None.
- Parameters:
ConfigClass (
ConfigP|Any) – a Config dataclass orConfigPobjpath (
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:
SourceInterfaceConfiguration 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.hostbecomesPROJECT_DATABASE_HOST. When no path is provided the prefixBATis 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:
FileSourcePConfiguration 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_envin 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:
FileSourcePConfiguration 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 adefault_envkey 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_envin 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,ProtocolProtocol 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 byconfig_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:
batconf.sources.yaml module
- class batconf.sources.yaml.YamlSource(file_path, file_format='environments', config_env=None, missing_file_option='warn')
Bases:
FileSourcePConfiguration 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 abatconfmapping with adefault_envkey 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_envin 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