batconf package
Subpackages
- 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
- batconf.sources.tests.toml_test module
- batconf.sources.tests.types_test module
- batconf.sources.tests.yaml_test module
- Module contents
- batconf.sources.tests package
- Submodules
- batconf.sources.argparse module
- batconf.sources.args module
- batconf.sources.dataclass module
- batconf.sources.env module
- batconf.sources.file module
- batconf.sources.ini module
- batconf.sources.toml module
- batconf.sources.types module
- batconf.sources.yaml module
- Module contents
- Subpackages
- batconf.tests package
- Submodules
- batconf.tests.lib_test module
- batconf.tests.manager_test module
ConfigurationTestsConfigurationTests.setUp()ConfigurationTests.test___getattr__()ConfigurationTests.test___getitem__()ConfigurationTests.test___repr__()ConfigurationTests.test___str__()ConfigurationTests.test__configuration_repr()ConfigurationTests.test__configuration_repr_level1()ConfigurationTests.test__module()ConfigurationTests.test_configuration_priority()ConfigurationTests.test_from_sources()
Source
- batconf.tests.namespace_test module
- batconf.tests.source_test module
- batconf.tests.types_test module
- Module contents
Submodules
batconf.lib module
- class batconf.lib.ConfigSingleton(get_config_fn)
Bases:
objectLazy singleton proxy for a
Configurationinstance.Wraps a
get_config_fnfactory and creates the underlyingConfigurationon first attribute access, then caches it for subsequent accesses. Importing the sameConfigSingletoninstance in multiple modules shares a single configuration object across the application.Parameters
- get_config_fnCallable[[], Configuration]
Callable that returns a
Configurationinstance. Usefunctools.partial()to pre-fill any arguments.
Examples
>>> CFG = ConfigSingleton(get_config_fn=get_config) >>> CFG.some_option 'value'
Using a partially applied config factory:
>>> from functools import partial >>> CFG = ConfigSingleton( ... get_config_fn=partial(get_config, config_env='prod') ... )
- type get_config_fn:
Callable- param get_config_fn:
- batconf.lib.insert_source(cfg, source, index=0)
Insert a configuration source into the configuration’s source list.
Configuration sources are queried in order, with lower indices taking precedence. By default, new sources are inserted at index 0 (highest priority).
- Return type:
None
Parameters
- cfgConfiguration or ConfigSingleton
Configuration object or singleton to modify.
- sourceSourceInterfaceP
Configuration source to insert (e.g., CLI args, environment variables, config file).
- indexint, default=0
Position to insert the source at (0 = highest priority).
Examples
Insert CLI arguments as the highest priority source:
>>> from batconf import NamespaceSource >>> from argparse import Namespace >>> args = Namespace() >>> setattr(args, 'project.key', 'value') >>> insert_source(cfg=CFG, source=NamespaceSource(namespace=args))
Insert a lower priority source:
>>> insert_source(cfg=CFG, source=env_source, index=2)
See Also
Configuration : Main configuration manager class. SourceList : Container that manages configuration sources. ConfigSingleton : Singleton proxy for global configuration.
- type cfg:
_HasConfigSources- param cfg:
- type source:
- param source:
- type index:
int- param index:
batconf.manager module
- class batconf.manager.Configuration(source_list, config_class, path=None)
Bases:
objectResolves configuration values from an ordered
SourceList.Values are looked up by walking the
config_classdataclass schema to determine the dotted path, then querying each source in theSourceListin priority order until a value is found.Configuration hierarchy (highest to lowest priority):
Command-line arguments — passed in via a
NamespaceSourceEnvironment variables — via
EnvSourceConfig file — via
IniSource,TomlSource, etc.Dataclass defaults — values declared on the config schema
Parameters
- source_listSourceListP
Ordered collection of configuration sources to query.
- config_classConfigP
Dataclass whose fields define the configuration schema.
- pathstr or None, default=None
Dotted namespace path for this configuration node. Defaults to the module of
config_classwhen not provided.
Examples
>>> cfg = Configuration( ... source_list=SourceList(sources=[EnvSource(), IniSource(file_path='config.ini')]), ... config_class=AppConfigSchema, ... ) >>> cfg.database.host 'localhost'
- type source_list:
- param source_list:
- type config_class:
ConfigP|Any- param config_class:
- type path:
str|None- param path:
batconf.source module
- class batconf.source.SourceInterface(*args, **kwargs)
Bases:
SourceInterfaceP- abstractmethod get(key, path=None)
- Parameters:
key (
str)path (
str|None)
- Return type:
str|None
- class batconf.source.SourceList(sources)
Bases:
objectAn ordered list of configuration sources.
Sources are queried in order; the first non-
Nonevalue returned wins.Noneentries in the constructor sequence are silently filtered out, making it easy to conditionally include sources.Parameters
- sourcesSequence[SourceInterfaceP | None]
Ordered sequence of configuration sources. Earlier entries take precedence over later ones.
Examples
>>> source_list = SourceList(sources=[NamespaceSource(args), EnvSource(), IniSource(file_path='config.ini')])
- type sources:
Sequence[SourceInterfaceP|None]- param sources:
- get(key, path=None)
- Parameters:
key (
str)path (
str|None)
- Return type:
str|None
- insert_source(source, index=0)
- Parameters:
source (
SourceInterfaceP)index (
int)
- Return type:
None
batconf.types module
Protocol types and type aliases for use in type annotations.
Import from here when you need to annotate parameters that accept batconf objects, rather than importing implementation classes directly.
Examples
>>> from batconf.types import ConfigP, SourceInterfaceP
>>> def get_config(config_class: ConfigP, source: SourceInterfaceP) -> Configuration:
... ...
- class batconf.types.ConfigP(*args, **kwargs)
Bases:
Protocol
- class batconf.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:
- class batconf.types.SourceInterfaceP(*args, **kwargs)
Bases:
Protocol- get(key, path)
- Parameters:
key (
str)path (
str|None)
- Return type:
str|None
- class batconf.types.SourceListP(*args, **kwargs)
Bases:
SourceInterfaceP,Protocol- insert_source(source, index=0)
- Parameters:
source (
SourceInterfaceP)index (
int)
- Return type:
None
Module contents
batconf — layered configuration for Python applications.
Public API
- Configuration
The main configuration manager. Resolves values from an ordered
SourceListagainst a dataclass-based config schema.- ConfigSingleton
Lazy, singleton-style proxy around a
Configurationinstance, suitable for use as a module-level global.- SourceList
Ordered collection of configuration sources.
- insert_source
Helper to insert a new source into an existing configuration at runtime.
Sources
- NamespaceSource
Reads from an
argparse.Namespaceobject using fully-qualified dotted key paths (e.g.project.host).- EnvSource
Reads from environment variables.
- IniSource
Reads from INI files.
- TomlSource
Reads from TOML files.
- YamlSource
Reads from YAML files.
Type annotations
See batconf.types for Protocol types used in type hints.
- class batconf.ConfigSingleton(get_config_fn)
Bases:
objectLazy singleton proxy for a
Configurationinstance.Wraps a
get_config_fnfactory and creates the underlyingConfigurationon first attribute access, then caches it for subsequent accesses. Importing the sameConfigSingletoninstance in multiple modules shares a single configuration object across the application.Parameters
- get_config_fnCallable[[], Configuration]
Callable that returns a
Configurationinstance. Usefunctools.partial()to pre-fill any arguments.
Examples
>>> CFG = ConfigSingleton(get_config_fn=get_config) >>> CFG.some_option 'value'
Using a partially applied config factory:
>>> from functools import partial >>> CFG = ConfigSingleton( ... get_config_fn=partial(get_config, config_env='prod') ... )
- type get_config_fn:
Callable- param get_config_fn:
- class batconf.Configuration(source_list, config_class, path=None)
Bases:
objectResolves configuration values from an ordered
SourceList.Values are looked up by walking the
config_classdataclass schema to determine the dotted path, then querying each source in theSourceListin priority order until a value is found.Configuration hierarchy (highest to lowest priority):
Command-line arguments — passed in via a
NamespaceSourceEnvironment variables — via
EnvSourceConfig file — via
IniSource,TomlSource, etc.Dataclass defaults — values declared on the config schema
Parameters
- source_listSourceListP
Ordered collection of configuration sources to query.
- config_classConfigP
Dataclass whose fields define the configuration schema.
- pathstr or None, default=None
Dotted namespace path for this configuration node. Defaults to the module of
config_classwhen not provided.
Examples
>>> cfg = Configuration( ... source_list=SourceList(sources=[EnvSource(), IniSource(file_path='config.ini')]), ... config_class=AppConfigSchema, ... ) >>> cfg.database.host 'localhost'
- type source_list:
- param source_list:
- type config_class:
ConfigP|Any- param config_class:
- type path:
str|None- param path:
- class batconf.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
- class batconf.Namespace(**kwargs)
Bases:
_AttributeHolderSimple object for storing attributes.
Implements equality by attribute names and values, and provides a simple string representation.
- batconf.NamespaceSource
alias of
NamespaceConfig
- class batconf.SourceList(sources)
Bases:
objectAn ordered list of configuration sources.
Sources are queried in order; the first non-
Nonevalue returned wins.Noneentries in the constructor sequence are silently filtered out, making it easy to conditionally include sources.Parameters
- sourcesSequence[SourceInterfaceP | None]
Ordered sequence of configuration sources. Earlier entries take precedence over later ones.
Examples
>>> source_list = SourceList(sources=[NamespaceSource(args), EnvSource(), IniSource(file_path='config.ini')])
- type sources:
Sequence[SourceInterfaceP|None]- param sources:
- get(key, path=None)
- Parameters:
key (
str)path (
str|None)
- Return type:
str|None
- insert_source(source, index=0)
- Parameters:
source (
SourceInterfaceP)index (
int)
- Return type:
None
- class batconf.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]
- class batconf.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.insert_source(cfg, source, index=0)
Insert a configuration source into the configuration’s source list.
Configuration sources are queried in order, with lower indices taking precedence. By default, new sources are inserted at index 0 (highest priority).
- Return type:
None
Parameters
- cfgConfiguration or ConfigSingleton
Configuration object or singleton to modify.
- sourceSourceInterfaceP
Configuration source to insert (e.g., CLI args, environment variables, config file).
- indexint, default=0
Position to insert the source at (0 = highest priority).
Examples
Insert CLI arguments as the highest priority source:
>>> from batconf import NamespaceSource >>> from argparse import Namespace >>> args = Namespace() >>> setattr(args, 'project.key', 'value') >>> insert_source(cfg=CFG, source=NamespaceSource(namespace=args))
Insert a lower priority source:
>>> insert_source(cfg=CFG, source=env_source, index=2)
See Also
Configuration : Main configuration manager class. SourceList : Container that manages configuration sources. ConfigSingleton : Singleton proxy for global configuration.
- type cfg:
_HasConfigSources- param cfg:
- type source:
- param source:
- type index:
int- param index: