Skip to main content

Simple Python configuration loading interface I wrote for use in KAGAYAKI

Project description

kagaconf

Simple Python configuration loading interface I wrote for use in KAGAYAKI. Actually most of this code was written during the development of gmsh, but I've finally found a nice name to turn this into a standalone package.

Usage

kagaconf provides a global configuration dictionary, which can be nested arbitrarily. Adding items to this dictionary is easy:

import kagaconf

# The simplest way to fill the dict is by copying another dict.
# I recommend this for default values.
kagaconf.from_dict({"key": "value"})

# Filling configuration from a file or all files in a directory is also very easy.
# There are many options for controlling this process, please refer to the API docs.
# Right now it only supports yaml, but adding at least json and toml should be trivial.
kagaconf.from_path('./path/to/your/config.yaml')
kagaconf.from_path('./path/to/your/config/directory', filter=r'[^_].*\.yaml', recursive=True)

# Lastly you can use a list of named variables to fill a skeleton dict with data.
# I imagine this will mainly be used to import environment variables into the config,
# that's why os.environ is the default data source (but you can obviously specify your own).
kagaconf.from_env_mapping({"this_key_will_be_filled": "BY_THIS_ENV_VARIABLE"})

# Found values are applied in order, so default values should go first
# and values with high precedence (e.g. environment overrides) last.

Once the configuration dict is prepared, it can easily be accessed from anywhere:

from kagaconf import cfg

# Use normal python dot syntax to get to the key you want.
# Call it like a function to get the value behind it.
cfg.hello()

# This will throw a KeyError if the key has no value, unless you provide a default value.
# If no default was provided here, resolution would stop at "nonexistent".
cfg.nonexistent.key("this is fine")

# You can also use dict syntax to get subsequent elements.
# This can be freely mixed with the dot syntax.
cfg["this is cool"].and_even_required_for_using_number_indices[1234]\
    .but_also.you_can_do["something.like.this"]()

The paths you build are stored as PathChainer objects and can be completely arbitrary in structure. They are only evaluated when you call them like a function, at which point a value will be resolved. So you will never encounter a KeyError when building the path, only when resolving without a fallback value.

Installation

You can install this library via pip install kagaconf. That is a sentence I never thought I could write, but here we are.

License

If you find this, think the API looks sweet and wanna use it, go ahead. If you think the code magic I did to make this work seamlessly is cool, 1) stop bc it's really not, and 2) feel free to use and abuse as much of it as you like.

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

kagaconf-0.1.2.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kagaconf-0.1.2-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file kagaconf-0.1.2.tar.gz.

File metadata

  • Download URL: kagaconf-0.1.2.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for kagaconf-0.1.2.tar.gz
Algorithm Hash digest
SHA256 fdf59379798c7a4ee15a12968849589d4567a530a09d6854d09cf6cdd97db677
MD5 80f299538b976f2af5ce3868ac0d02bc
BLAKE2b-256 515f2c2acaedc4bb130f67d65353e32fbb385ea696a20a30f11a6e5d4599c80c

See more details on using hashes here.

File details

Details for the file kagaconf-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: kagaconf-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for kagaconf-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 46794898ea505349bf1e325bd2aafabe78d09d1fa0d3e44ae88dc94f5844136f
MD5 8b666766074f4293d08e94d1e937aaba
BLAKE2b-256 c982c3e6ea68b9cd4875ec4df0b3fb1469a5a9298c528a64e13adb801aab5360

See more details on using hashes here.

Supported by

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