Skip to main content

Help developers with config files

Project description

Mergeconfigs is a package that help managing configuration files.

For now only YAML is supported.

Mergeconfigs makes easy to merge several yaml files into one, using includes, placeholders and extends. That way you can split your configurations into several files and merge them into one single yaml.

Install

Run pip install mergeconfigs to install.

Run python -m mergeconfigs --help for help.

Syntax

In a YAML file, one can use the following syntax:

  • Include this file in another file: #extends path/to/file.yaml, if the current file contain common keys with the other file, the current file will override it.
  • Load context from a file: #load path/to/file.yaml, all the variables of this file will be loaded into a context and can be used as a placeholder.
  • Include the content of another file: #include path/to/file.yaml, if the file contain keys that are shared by the current file, it will override it.
  • Use a variable from the context: ${namespace@variable}, namespace here refer to the name of the file (without its extension) that contain the variable. It supports accessing dictionary keys, for example a variable foo["bar"] from logging.yaml can be used with ${logging@foo.bar}. The current file can be referenced with the special namespace this: ${this@myvar}.

Note: The file is read line by line, which mean if an #include is used in a file, some values can still be overriden in the following lines.

Hierarchy descriptor

In most cases your configs will follow some hierarachy, we didn't wanna impose anything here so we give to the user the possibility to pass a hierarchy file which contain the hierarchical order of the environments. Every environment should be in a single line, with the root level at the top, i.e. if a file is not found in a directory, it will be searched in the directory above him. For example:

prod
stage
dev
local

Example

Mergeconfigs use basic templating functions like variables insertion and including or extending other templates. It also provides support for different environment. It is best to explain how it works with an example:

Say we have an application directory with two settings config file, settings-prod.yaml is the production settings file and settings-local.yaml is a copy of settings-prod.yaml but use a local mongoDB:

| app/
  | src/
     | my_program.py
     | settings-prod.yaml  
     | settings-local.yaml  

The config files contain the following content:

settings-prod.yaml

app_name: "MY_APP"
app_port: 5000
app_secret_key = "my-very-secret-key"

my_service:
    health_check_url: "http://my_service.com/check"
    
mongodb:
    uri: "mongodb://user:passwd@
    auto_connect: false
    print_errors: true
    db_alias: "my_database_alias"
  
log_messages: true
log_level: info 
log_prefix: "my_app_logger:"

settings-local.yaml

app_name: "MY_APP"
app_port: 5000
app_secret_key = "my-very-secret-key"

my_service:
    health_check_url: "http://my_service.com/check"
    
mongodb:
    uri: "mongodb://127.0.0.1:27017/my_database?readpreferenceaen=rest"
    auto_connect: false
    print_errors: true
    db_alias: "my_database_alias"
  
log_messages: true
log_level: info 
log_prefix: "my_app_logger:"

Big config files can be overwhelming, especially if you need to update/edit them to fit your needs. Mergeconfigs help to split them to multiple files, here is an example of how it can be used, add a config directory to app/, move all the core content into core.yaml and split the other contents into other files, adding a hosts.yaml file with all the hosts can be very useful:

| app/
  | src/
  |  | my_program.py
  | config/
     | core.yaml
     | logging.yaml
     | prod
     |  | hosts.yaml
     | local
     |  | hosts.yaml
     | .hierarchy

Then generate a single settings.yaml file using:

python -m mergeconfigs --workdir config --file core.yaml --env local --hierarchy .hierarchy

Here is the content of the files:

.hierarchy

prod
local

core.yaml

#load $$ENV$$/hosts.yaml

app_name: "MY_APP"
app_port: 5000
app_secret_key = "my-very-secret-key"

my_service:
    health_check_url: "http://${hosts@my_service.host}/check"
    
mongodb:
    uri: "mongodb://${hosts@mongodb.host}/my_database?readpreferenceaen=rest"
    auto_connect: false
    print_errors: true
    db_alias: "my_database_alias"

#include logging.yaml

The first line load all the variables in the $$ENV$$/hosts.yaml file, here $$ENV$$ is replaced by local because of the --env local parameter. Therefore, it will load the local/hosts.yaml file, which itself extends the prod/hosts.yaml file.

The hosts are then replaced using placeholders, like ${hosts@my_service.host}

Then logging.yaml is added to the content of the yaml file.

logging.yaml

log_messages: true
log_level: info 
log_prefix: "my_app_logger:"

prod/hosts.yaml

my_service:
    host: my_service.com
   
mongodb:
    host: mymongodb.net:27017

local/hosts.yaml

#extends prod/hosts.yaml

mongodb:
    host: 127.0.0.1:27017

The local/hosts.yaml file extends prod/hosts.yaml file, it only overrides one of its attribute, the mongodb.host one.

TODO

  • Add support for list indices in variables: ${file@variable.0}

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

mergeconfigs-0.10.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

mergeconfigs-0.10-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file mergeconfigs-0.10.tar.gz.

File metadata

  • Download URL: mergeconfigs-0.10.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for mergeconfigs-0.10.tar.gz
Algorithm Hash digest
SHA256 bb46818f3e3bfb70945cadbbf65005b1976542fe207ed1de1d50b49edb2cfbc1
MD5 869244e6bf2a3ede9ceba77ccd415b45
BLAKE2b-256 97c8246fbb5b2dc6df846643bdb8f8fcb3ceb2003bbe7ae49b609ab6a2c2122e

See more details on using hashes here.

File details

Details for the file mergeconfigs-0.10-py3-none-any.whl.

File metadata

  • Download URL: mergeconfigs-0.10-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for mergeconfigs-0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 f405096914bbadc649d071c4dc59bf8e8d2d347eccaa9af3a551a8c8283d23b2
MD5 f368182f3116d016b75ae66350fb4e5e
BLAKE2b-256 11dfa0106277d31e5b7846adf7e6e8862d761c766f1e288a0f8d906f89e215a9

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