combined_config
A Python library for stitching multiple configuration sources together in order or priority.
The intention of this library is to provide a type, CombinedConfig, that represents all of the
configuration options for a Python project in a format conversant with Python's
configparser and
argparse modules. A CombinedConfig instance
represents an ordering in which ingested configuration values should be searched, and optional
default values for configuration values that have not been provided. A CombinedConfig is
instantiated with one or more ConfigVar instances, which present fields corresponding to those
provided to
argparse.ArgumentParser.add_argument.
CombinedConfig
import configparser
from combined_config import CombinedConfig, ConfigVar
my_config = CombinedConfig(
ConfigVar(name="arg_with_default", shortname="a", default="my default"),
ConfigVar(name="switch", action="store_true", default=False, help="Turns on the frobnicator"),
ConfigVar(name="no_default", type=int),
)
# You can create a `ArgumentParser` from the `CombinedConfig`.
parser = my_config.make_parser()
parsed = parser.parse_args(["-a", "other value", "--no-default", "10", "--switch"])
# Appending a config gives it lower priority than those already added.
my_config.append(parsed)
print(my_config.values.arg_with_default) # => other value
print(my_config.values.no_default) # => 10
print(my_config.values.switch) # => True
config_parser = ConfigParser()
config_parser.read({"my_section": {"no_default": 35}})
# Example contents of "some-other-conf.ini"
# [my_section]
# no_default = 35
# Prepending a config gives it higher priority than those already added.
my_config.prepend(config_parser["my_section"])
print(my_config.values.no_default) # => 35
# We can also add dictionaries.
my_config.prepend({"arg_with_default": "another other value"})
print(my_config.values.arg_with_default) # => another other value
FileBackedConfigMixin
Commonly, you'll want to be able to easily read and write the configuration from/to file. This can
be done with a subclass of CombinedConfig that also subclasses FileBackedConfigMixin.
from combined_config import CombinedConfig, ConfigVar, FileBackedConfigMixin
class MyBackedConfig(CombinedConfig, FileBackedConfigMixin):
# Should be a mapping of ini section names to the `ConfigVar` names that will be stored there.
# Special value `"__ALL__"` means all keys.
ini_section_names = {"my_section": "__ALL__"}
filename = "/tmp/my-config.ini"
my_config = MyBackedConfig(
ConfigVar(name="arg_with_default", shortname="a", default="my default"),
ConfigVar(name="switch", action="store_true", default=False, help="Turns on the frobnicator"),
ConfigVar(name="no_default", type=int),
)
# Load the config from file.
my_config.read()
my_config.prepend({"switch": True})
# Write the config back to file. Only configuration values that differ from their defaults will be
# written.
my_config.write()
Release files for combined-config 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| combined_config-0.1.0.tar.gz | 5.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| combined_config-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.7 kB
Release files / combined_config-0.1.0.tar.gz
| Download URL | combined_config-0.1.0.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
24fc26a9b83db7615b298cfa093248632650ee08247271036a5517b955f2cf4a
|
|
BLAKE2b-256 checksum How to use checksums |
0a21ed282f72b77c1735467447f65fc1ac00323514786ce92e530f37d0e969a8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.10.12 Linux/6.2.0-26-generic
|
Release files / combined_config-0.1.0-py3-none-any.whl
| Download URL | combined_config-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
05501fd2f92f4b2f68479e2c8a7cf2c32f99d71d8a26701faa2a4b6b63598641
|
|
BLAKE2b-256 checksum How to use checksums |
065b312f7b3aba19eb07e157ec601eeadb3b4e20eb9dfd02a9a447409adf9cbb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.10.12 Linux/6.2.0-26-generic
|