Skip to main content

Application configuration engine

Project description

Tweak is a Python helper class to ingest and serialize app-specific configuration.

Tweak provides a self-contained (no dependencies outside the standard library), Python 2 and 3 compatible configuration manager. It automatically saves and restores your application’s configuration in your user home directory. It uses JSON or (optionally) YAML for serialization. It supports dict-like methods and access semantics, hierarchical configuration sources, and array merge operators for layering configuration options (see below).

Installation

If your package does not permit dependency management, you can copy the Config class directly into your application from https://github.com/kislyuk/tweak/blob/master/tweak/__init__.py. Otherwise:

pip install tweak

Synopsis

from tweak import Config

config = Config()
config.host, config.port = "example.com", 9000
config.nested_config = {}
config.nested_config.foo = True

After restarting your application:

config = Config()
print(config)
>>> {'host': 'example.com', 'port': 9000, 'nested_config': {'foo': True}}

Using an argparse.Namespace object returned by argparse.parse_args():

parser = argparse.ArgumentParser()
...
args = parser.parse_args()
if args.foo is not None:
    config.foo = args.foo
elif "foo" not in config:
    raise Exception("foo unconfigured")

config.update(vars(args))

Using YAML:

config = Config(use_yaml=True)
...

Pass Config(save_on_exit=False) to disable automatic configuration saving on Python shutdown (this is useful if you only want to read the config, never write it, or if you want to call config.save() manually). Pass Config(autosave=True) to make save() run any time an assignment happens to a config object.

Configuration ingestion order

Tweak supports ingesting configuration from a configurable array of sources. Each source is a JSON or YAML file. Configuration sources that follow the first source update the configuration using recursive dictionary merging. Sources are enumerated in the following order:

  • Site-wide configuration source, /etc/NAME/config.(yml|json)

  • User configuration source, ~/.config/NAME/config.(yml|json)

  • Any sources listed in the colon-delimited variable NAME_CONFIG_FILE

Array merge operators

When loading a chain of configuration sources, Tweak uses recursive dictionary merging to combine the sources. Additionally, when the original config value is a list, Tweak supports array manipulation operators:

In [1]: from tweak import Config

In [2]: c = Config()

In [3]: c.update(x=[1, 2, 3])

In [4]: c
Out[4]: {'x': [1, 2, 3]}

In [5]: c.update(x={"$append": 4})

In [6]: c
Out[6]: {'x': [1, 2, 3, 4]}

In [7]: c.update(x={"$extend": [5, 6]})

In [8]: c
Out[8]: {'x': [1, 2, 3, 4, 5, 6]}

In [9]: c.update(x={"$insert": {0: 0}})

In [10]: c
Out[10]: {'x': [0, 1, 2, 3, 4, 5, 6]}

In [11]: c.update(x={"$extendleft": [-2, -1]})

In [12]: c
Out[12]: {'x': [-2, -1, 0, 1, 2, 3, 4, 5, 6]}

In [13]: c.update(x={"$remove": 0})

In [14]: c
Out[14]: {'x': [-2, -1, 1, 2, 3, 4, 5, 6]}

Each operator ($append, $extend, $insert, $extendleft, $remove) must be the only key in the dictionary representing the update, and the value being updated must be a list. For example, in the following set of two YAML files, the second file extends the list in the first file.

/etc/NAME/config.yml:

x:
 - y
 - z

~/.config/NAME/config.yml:

x:
 $extend:
   - a
   - b

Include directives

The optional Config(allow_includes=True) keyword argument can be used to trigger processing of include directives in config files. For each config source file ingested, a top level include key can contain a string or array of strings. Each of these strings will be globbed and ingested before the file contianing the directive (e.g. {"include": "config.d/*"} to ingest a directory of config files).

Authors

  • Andrey Kislyuk

License

Licensed under the terms of the Apache License, Version 2.0.

https://travis-ci.org/kislyuk/tweak.png https://img.shields.io/coveralls/kislyuk/tweak.svg https://img.shields.io/pypi/v/tweak.svg https://img.shields.io/pypi/l/tweak.svg https://readthedocs.org/projects/tweak/badge/?version=latest

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

tweak-1.0.4.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

tweak-1.0.4-py2.py3-none-any.whl (9.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file tweak-1.0.4.tar.gz.

File metadata

  • Download URL: tweak-1.0.4.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.7

File hashes

Hashes for tweak-1.0.4.tar.gz
Algorithm Hash digest
SHA256 9a349a68122fd86eaf0400616cb7bf026feab666190ad6824c492a9d012d9a62
MD5 ee7442dcdaf3c2ab349d0e3ee8de1c0d
BLAKE2b-256 34ca1689a9442d2f8e6bb298402ca0ab5e96d9039d423536ec57b2861987e495

See more details on using hashes here.

File details

Details for the file tweak-1.0.4-py2.py3-none-any.whl.

File metadata

  • Download URL: tweak-1.0.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.7

File hashes

Hashes for tweak-1.0.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1581dda37e279fc4a1af05ca3ff23121233f9f900fd8985873956df5ebffdaf5
MD5 9f92190588b3885fec7af315dbf61d94
BLAKE2b-256 b161f209c9792cb186dd8c6848b8c4ad40a85acff314cc68566b6954ae1305d0

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