Skip to main content

Configuration library for Python 🔧 Load from multiple sources

Project description

ilexconf

Configuration Library 🔧 for Python

Build Status Coverage Report License: MIT PyPI Code style: black

ilexconf is a Python library to load and merge configs from multiple sources, access & change the values, and write them back, if needed. It has no dependencies by default but provides additional functions, relying on popular libraries to parse yaml, toml, provide CLI app, etc.

Table of contents

Quick Start

Install

$ pip install ilexconf

Populate Config with values

Config object is initialized using arbitrary number of Mapping objects and keyword arguments. It can even be empty.

from ilexconf import Config, from_json

# All of these are valid methods to initialize a config
config = Config()
config = Config({ "database": { "connection": { "host": "test.local" } } })
config = Config(database__connection__port=4000)
config = from_json("settings.json")
config = from_env()

# Or, you can combine them
config = Config(
    # Take the basic settings from JSON file
    from_json("settings.json"),

    # Merge the dictionary into that
    { "database": { "connection": { "host": "test.local" } } },

    # Merge the keyword arguments on top
    database__connection__port=4000
)

When we initialize config all the values are merged. Arguments are merged in order. Every next argument is merged on top of the previous mapping values. And keyword arguments override even that. For more details read about merging strategy below.

For a settings file settings.json with the following content ...

{
    "database": {
        "connection": {
            "host": "localhost",
            "port": 5432
        }
    }
}

The code above will produce a merged config with merged values:

{
    "database": {
        "connection": {
            "host": "test.local",
            "port": 4000
        }
    }
}

Read from files & environment variables

Files like .json, .yaml, .toml, .ini, .env, .py as well as environment variables can all be read & loaded using a set of from_ functions.

from ilexconf import (
    from_json,      # from JSON file or string
    from_yaml,      # from YAML file or string
    from_toml,      # from TOML file or string
    from_ini,       # from INI file or string
    from_python,    # from .py module
    from_dotenv,    # from .env file
    from_env        # from environment variables
)

cfg1 = from_json("settings.json")

cfg2 = Config(
    from_yaml("settings.yaml"),
    from_toml("settings.toml")
)

cfg3 = Config(
    from_ini("settings.ini"),
    from_python("settings.py"),
    from_dotenv(".env"),
    from_env()
)

Access values however you like

You can access any key in the hierarchical structure using classical Python dict notation, dotted keys, attributes, or any combination of this methods.

# Classic way
assert config["database"]["conection"]["host"] == "test.local"

# Dotted key
assert config["database.connection.host"] == "test.local"

# Attributes
assert config.database.connection.host == "test.local"

# Any combination of the above
assert config["database"].connection.host == "test.local"
assert config.database["connection.host"] == "test.local"
assert config.database["connection"].host == "test.local"
assert config.database.connection["host"] == "test.local"

Change existing values and create new ones

Similarly, you can set values of any key (even if it doesn't exist in the Config) using all of the ways above.

Notice, contrary to what you would expect from the Python dictionaries, setting nested keys that do not exist is allowed.

# Classic way
config["database"]["connection"]["port"] = 8080
assert config["database"]["connection"]["port"] == 8080

# Dotted key (that does not exist yet)
config["database.connection.user"] = "root"
assert config["database.connection.user"] == "root"

# Attributes (also does not exist yet)
config.database.connection.password = "secret stuff"
assert config.database.connection.password == "secret stuff"

Merge with another Mapping object

If you just assign a value to any key, you override any previous value of that key.

In order to merge assigned value with an existing one, use merge method.

config.database.connection.merge({ "password": "different secret" })
assert config.database.connection.password == "different secret"

merge respects the contents of each value. For example, merging two dictionaries with the same key would not override that key completely. Instead, it will recursively look into each key and try to merge the contents. Take this example:

config = Config(
    { "a1": { "c1": 1, "c2": 2, "c3": 3 } },
    { "a1": { "c3": "other" } }
)

# Instead of overriding the value of the "a1" key completely, `merge` method
# will recursively look inside and merge nested values.
assert config.as_dict() == { "a1": { "c1": 1, "c2": 2, "c3": 3 } }

Represent as dictionary

For any purposes you might find fit you can convert entire structure of the Config object into dictionary, which will be essentially returned to you as a deep copy of the object.

assert config.as_dict() == {
    "database": {
        "connection": {
            "host": "test.local",
            "port": 8080,
            "user": "root",
            "password": "different secret"
        }
    }
}

Write to file

You can serialize the file as JSON or other types any time using the to_ functions.

# Write updated config back as JSON file
from ilexconf import to_json

to_json(config, "settings.json")

WARNING: This might throw a serialization error if any of the values contained in the Config are custom objects that cannot be converted to str. Also, obviously, you might not be able to correctly parse an object back, if it's saved to JSON as MyObject(<function MyObject.__init__.<locals>.<lambda> at 0x108927af0>, {}) or something.

Subclass

Subclassing Config class is very convenient for implementation of your own config classes with custom logic.

Consider this example:

import ilexconf

class Config(ilexconf.Config):
    """
    Your custom Configuration class
    """

    def __init__(self, do_stuff=False):
        # Initialize your custom config with JSON by default
        super().__init__(self, ilexconf.from_json("setting.json"))

        # Add some custom value depending on some logic
        if do_stuff:
            self.my.custom.key = "Yes, do stuff"

        self.merge({
            "Horizon": "Up"
        })

# Now you can use your custom Configuration everywhere
config = Config(do_stuff=True)
assert config.my.custom.key == "Yes, do stuff"
assert config.Horizon == "Up"

Internals

Under the hood ilexconf is implemented as a defaultdict where every key with Mapping value is represented as another Config object. This creates a hierarchy of Config objects.

__getitem__, __setitem__, __getattr__, and __setattr__ methods are overloaded with custom logic to support convenient get/set approach presented by the library.

Contributing

Contributions are welcome!

Kudos

ilexconf heavily borrows from python-configuration library and is inspired by it.

License

MIT

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

ilexconf-0.6.16.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

ilexconf-0.6.16-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file ilexconf-0.6.16.tar.gz.

File metadata

  • Download URL: ilexconf-0.6.16.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.0rc1 CPython/3.8.0 Linux/4.15.0-1077-gcp

File hashes

Hashes for ilexconf-0.6.16.tar.gz
Algorithm Hash digest
SHA256 2ce5d9a5281a41d557046c9ed080914e4e5a9a679824139cb93e736661fc8fb7
MD5 3dbf051aecdd42a13ac86ed7a5463e86
BLAKE2b-256 4685dae4fd00e063a8aebbdf7bf98e483fe83f356bcbdf9b8bd69e827cdf5ff9

See more details on using hashes here.

File details

Details for the file ilexconf-0.6.16-py3-none-any.whl.

File metadata

  • Download URL: ilexconf-0.6.16-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.0rc1 CPython/3.8.0 Linux/4.15.0-1077-gcp

File hashes

Hashes for ilexconf-0.6.16-py3-none-any.whl
Algorithm Hash digest
SHA256 ef615aab46407ed38e90571b40c788dcef701aef6e0d81f5e91bb2c3d0b65ff2
MD5 dc97988505b34a83455cd65d0194abe3
BLAKE2b-256 aff42de73a9fbab70f57dfa2140faa8a9684214f1c5705ea23aa26a3f1ed5a1d

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