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.3.tar.gz (5.7 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.3-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kagaconf-0.1.3.tar.gz
  • Upload date:
  • Size: 5.7 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.3.tar.gz
Algorithm Hash digest
SHA256 9249f802ca5ac4ade7ea767589b3c1ff9cc35c099e45743af8174c071707d9f5
MD5 763ee1b23ef1bc9843c7692325905867
BLAKE2b-256 23a68c81a24cbc42135cadbfd29e960d376e2686e48a6e5c19d837971424d5d6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kagaconf-0.1.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 362bfa8e86d496c877e0ea241b353982125ad1cd481583deb5dc8d8777f4a71a
MD5 92bb01762764c94e8bb865f17d3474b1
BLAKE2b-256 91caa945de2d3d3d83d27f42498995411bb0701d2044e826889abc1c5c6c040b

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