Skip to main content

Introduction

This project aim is to implement a simple and extensible way of accessing configuration. The configuration values can be provided in json files, environment variables, secrets, etc.

Getting Started

installation

you can install the package from pypi:

pip install configlookup

usage

The configuration class is implemented as a singleton. Main usage:

  • define environment vars:
    • CONFIGLOOKUP_ENV - {dev|prod} - default: dev
    • CONFIGLOOKUP_DIR - absolute path where the config json files can be found - default: local dir
    • CONFIGLOOKUP_FILE_PREFIX - prefix of configuration files name - default: configlookup
from configlookup.main import Configuration
...
config_value = Configuration.get("root.config_group.key")

rationale

As a singleton, Configuration is loaded when it's called by the first time. The initialization process can be depicted in the following diagram

( read env vars:
  CONFIGLOOKUP_ENV, CONFIGLOOKUP_DIR [D], CONFIGLOOKUP_FILE_PREFIX [P]
) 
=> ( find json files with prefix [P] and with suffixes ["", "_all", "_local"] in [D] ) =>
=> ( process json files by natural order:
  - build a dict structure with all the values,
  - override the value if equivalent key is found in any variable overrider,
    - environment overrider is always the last one to be scanned, so it has higher priority
)

Do note the possibility of extending the configuration in runtime, so as an example, we can have the following primitive configurations in the json file

...
"dev": {
    "server": {
      "password": "gonna-get-it-from-a-new-overrider",
      "user": "gonna-get-it-from-env",
...

remember the environment overrider is enabled always, and once we enable a secrets overrider, for instance azure keyvault, we can read the secret there with key SERVER--PASSWORD

...bear in mind the translation of config keys:

  • property notation *.*-*.* => *__*_*__* env var notation
  • property notation *.*-*.* => *--*-*--* key secret notation

defining config values

json file

{
  "common": {...},
  "dev": {
    "server": {
      "url": "http://www.dev.site.com",
      "resources": {
        "mem": 2048,
        "color": "yellow",
        "mem_min": 1024
      }
  ...
  "prod": {
    "server": {
      "url": "https://www.site.com",
      "resources": {
        "color": "green",
      }
    }
  }

The main idea is to define values in the common section and then override it accordingly to the runtime environment, dev or prod. The environment is resolved by reading the environment variable CONFIGLOOKUP_ENV. By default, it assumes dev.

In the snippet above if eventually the env var is set: CONFIGLOOKUP_ENV=prod then:

Configuration.get("server.url") == "https://www.site.com"

...and as a leaf primitive value it can also be obtained by a var-like key:

Configuration.get("SERVER__URL") == "https://www.site.com"

...the same doesn't happen with

Configuration.get("server.url.resources") == {"mem": 2048, "color": "green", "mem_min": 1024} Configuration.get("SERVER__URL__RESOURCES") == None

...as it is not a primitive value.

Build

  • check the helper.sh script

Publish

  • we publish to pypi, you need to have a pypi account token in ~/.pypirc to be able to publish it

Test

  • check the helper.sh script

Contribute

  • just submit a PR to our repository when you want, we'll look at it

Metadata

Release files for configlookup 0.1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for configlookup 0.1.7
File Size Uploaded
configlookup-0.1.7.tar.gz 13.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for configlookup 0.1.7
File Interpreter ABI Platform
configlookup-0.1.7-py3-none-any.whl Python 3 none any Details

Total release size: 25.4 kB

Release files / configlookup-0.1.7.tar.gz

Download URL configlookup-0.1.7.tar.gz
Size 13.2 kB
Tags Source
SHA-256 checksum
How to use checksums
0e053b9b420dd33650d8ac24e47fabbe1e087440ebc556ad1a520151f9e17ec5
BLAKE2b-256 checksum
How to use checksums
239b000448de42d69b3a98ac8912f3bd0ccdb289c224d7fc121c2e36e7aa56b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Release files / configlookup-0.1.7-py3-none-any.whl

Download URL configlookup-0.1.7-py3-none-any.whl
Size 12.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7008c42b92b9dee6a3ae6d5d8c12f8226ff4e7a2c7a8ab356afea42d023afdea
BLAKE2b-256 checksum
How to use checksums
cc6f366989e130f8327f0e41ab3b4ad7b0cda8f4e6c75c3af4a4c1c5ea8aa8b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Release history Release notifications | RSS feed

This release

0.1.7 This release

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page