Skip to main content

No project description provided

Project description

combined_config

check

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()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

combined_config-0.1.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

combined_config-0.1.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file combined_config-0.1.0.tar.gz.

File metadata

  • Download URL: combined_config-0.1.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.10.12 Linux/6.2.0-26-generic

File hashes

Hashes for combined_config-0.1.0.tar.gz
Algorithm Hash digest
SHA256 24fc26a9b83db7615b298cfa093248632650ee08247271036a5517b955f2cf4a
MD5 3326ae6edd5c0ac1e8ae72745e5bfa28
BLAKE2b-256 0a21ed282f72b77c1735467447f65fc1ac00323514786ce92e530f37d0e969a8

See more details on using hashes here.

File details

Details for the file combined_config-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: combined_config-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.10.12 Linux/6.2.0-26-generic

File hashes

Hashes for combined_config-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 05501fd2f92f4b2f68479e2c8a7cf2c32f99d71d8a26701faa2a4b6b63598641
MD5 0129e94eda739e3870df894b049b28fd
BLAKE2b-256 065b312f7b3aba19eb07e157ec601eeadb3b4e20eb9dfd02a9a447409adf9cbb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page