Skip to main content

Put configuration from one file into multiple files

Project description

Configurematic

Description

Have you found yourself in the situation where you have the same configuration in multiple configuration files? We all know config should be in one place only but sometimes we have several .env files, for example, for related apps, and env file for docker plus some configuration in places like pyproject.toml, package.json, etc.

Configurematic lets you write a single .env file (called config.env by default) and have variable from that file substituted into any other file.
It is a lightweight, compact script with python-dotenv as its only dependency.

Installing

Install with

pip install configurematic

Alternatively, install it with pipx:

pipx install configurematic

Using Configurematic

First create your config.env file. An example might be

APP_NAME="My App Name"
DBPASSWORD="SecretPassword"

This is parsed by dotenv so you can have blank lines, comments starting with #, etc.

Next create files you want to substitute these into. Prefix them with example- (though this can be overridden with the --prefix option).

For example you might have

example-env:

APP_NAME="__APP_NAME__"

app/example-.env:

APP_NAME="__APP_NAME__"
PWD="__DBPASSWORD__"

Anything between the __ pairs is substituted by the variable matching that name in your config.env, provided it exists.

Your example-* files don't have to be .env-like and can have multiple variables on one line, eg

print("The current version is __MAJOR__VERSION__.__MINOR_VERSION__")

Variables starting and ending with __ will only be substituted if they are in your config.env file.

Next, create a file listing your templates (without the example- prefix), one per line:

files.txt:

env
app/.env

This file can also contain blank lines and comments starting with #. Alternatively you can list the files on the command line. Whitespace at the start and end of each line though is stripped, therefore filenames cannot start or end with a whitespace character (not that they should). Whitespace inside a filename is fine. They also cannot start with a #, though it can appear elsewhere in the filename.

If for some reason you do want a space at the beginning or end of a filename or you do want a file to start with #, list the files on the command line instead of in a file.

Finally, run Configurematic:

configurematic

Next to each of your example-* files you will see a new file without the example- prefix.

Run it again and the new files will be overwritten (warning, you will lose any manual changes) but the old versions will be copied to a new file next to it with a .old suffix (which can be overridden with --backup-ext).

Options

Use --conf-file or -c to use a different file instead of config.env.

Use --files or -f to use a different file instead of files.txt.

To output all the files to a different directory (with the same directory hierarchy within it), use --outdir or -o.

Change the prefix with -p or --prefix.

-r or --recursive turns on recusion (see below).

Change __ to something else with -d or --delimeter.

Change the backup extension with -b or --backup-ext. Set this to "" to not write backup files.

Turn off verbose output with -q or --silent.

Instead of listing the files in a file pointed to with -f, you can list them on the command line, eg:

configurematic env app/.env

Recursion

You can make substitution recursive with the -r or --recursive flag.

Say you have the following in your config.env:

VAR1="__VAR2__"
VAR2="Value Here"

and the following in a template:

MY_VAR1="__VAR1__"

The result will be

MY_VAR1="Value Here"

By default, recursion is off, meaning only one level of substitution is made.

Changing files

If you edit a generated file, then run Configurematic again, you will lose your changes (though they are kept in the backup file). Thus it is best to edit your example-* files only (and of course config.env), and rerun Configurematic to generate the files again.

Licence

Configurematic is distributed under the Apache licence.

Author

Matthew Baker is an IT manager at ETH Zurich. He also does consultancy work. Contact him at matt@mattbaker.ch.

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

configurematic-0.0.2.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

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

configurematic-0.0.2-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file configurematic-0.0.2.tar.gz.

File metadata

  • Download URL: configurematic-0.0.2.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for configurematic-0.0.2.tar.gz
Algorithm Hash digest
SHA256 2f499993b1917d7c9dc0d5c3036dd2da1d5bd2b0ee7420ef3985ca07435835b2
MD5 e3f2878650dd2afd9d211ad3ee6a4baa
BLAKE2b-256 34d01a7298cc5a4663ac986644ea8a0a8ca0284e744f3435fe78fa6688d2f10d

See more details on using hashes here.

File details

Details for the file configurematic-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: configurematic-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for configurematic-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 081c5198749de123c2069a75345f5291c5ce0e6e68ef2a8809dbf95d7e18bc48
MD5 b47a863cffd413269735a5e02baaf4a0
BLAKE2b-256 b25f39428a8e8cbd974bb4503651eab2b0bd2dba997dcbf20574087f3e40d7ad

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