Skip to main content

Gen3 Configuration Library

Project description

gen3config

Handle YAML configurations with a given default/expected configuration. Library will overlay a provided configration over the default and ignore unknown values.

Quickstart

Example config-default.yaml for your application:

---
SOME_VALUE: 'default'

EXAMPLE:
    nested:
        key: 'value'
        is_boolean: true

Example config.py in your application:

from gen3config import Config

class AppConfig(Config):
    def __init__(self, *args, **kwargs):
        super(AppConfig, self).__init__(*args, **kwargs)

    def post_process(self):
        # do custom stuff here after parsing config
        pass

default_cfg_path = os.path.join(
    os.path.dirname(os.path.abspath(__file__)), "config-default.yaml"
)

config = AppConfig(default_cfg_path)

Now we want to get a specific configuration.

Example file ~/path/to/app-config.yaml:

SOME_VALUE: 'app-specific configuration'

EXAMPLE:
    nested:
        is_boolean: true

Example initialization function in your application:

from app.config import config

config.load('~/path/to/app-config.yaml')

Then from other files:

from app.config import config

print(config["SOME_VALUE"])  # 'app-specific configuration'
print(config["EXAMPLE"]["nested"]["key"])  # 'value'

NOTE: config["EXAMPLE"]["nested"]["key"] does not exist in the provided configuration, but it does exist in the default configuration. Therefore, the default value, 'value' is retrieved.

Details:

  • Create a config-default.yaml and config.py in the top-level folder your app
  • Inside config-default.yaml add keys and reasonable default values
  • Inside config.py, create a class that inherits from this Config class
    • See above example
  • Add a final line to your config.py that instantiates your custom class:
    • Ensure that you provide the default config path
      • If placed in same directory as config.py you can use something like:
        default_cfg_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)), "config-default.yaml"
        )
        config = FenceConfig(default_cfg_path)
        
  • Import your instaniated object whenever you need to get configuration
    • Example: from fence.config import config
  • Load in application configuration during init of your app
    • Example: config.load('path/to/fence-config.yaml')
  • Now you can safely access anything that was in your config-default.yaml from this object as if it were a dictionary
    • Example: storage_creds = config["STORAGE_CREDENTIALS"]
    • Example: if config["SOME_BOOLEAN"]: ...
    • Example: nested_value = config["TOP_LEVEL"]["nested"]
  • And of course you can import that into any file you want and will have access to keys/values
    • Example: from fence.config import config

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

gen3config-2.0.0.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

gen3config-2.0.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file gen3config-2.0.0.tar.gz.

File metadata

  • Download URL: gen3config-2.0.0.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.9.20 Linux/6.5.0-1025-azure

File hashes

Hashes for gen3config-2.0.0.tar.gz
Algorithm Hash digest
SHA256 4e4480a5d2b0d8b4263150ad1b354b3dc37406427e010a8acb161e32861c3acb
MD5 0489ff1899bda6bb0de7e9ba35429987
BLAKE2b-256 84b971a4461002596208e43f84ffb3b14b0343f70e1bbd8000dac7e5788eaf2e

See more details on using hashes here.

File details

Details for the file gen3config-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: gen3config-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.9.20 Linux/6.5.0-1025-azure

File hashes

Hashes for gen3config-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0d56a64f82e7c4f9a417e7be5645b745e6450431b77a1a4d38f52bea19e56a49
MD5 1d032926766a5703b8ec98958b773a64
BLAKE2b-256 65667f63da008e0380bd9aa3d40eaaa73faf3a67ef89e91c6b99eeb6343d6f71

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