Skip to main content

Declarative configuration settings for Python.

Project description

declare_config

This Python module allows a declarative syntax for defining the contents of a configuration file. It also enables easy support for features such as default configuration values, nested configuration settings (and nested defaults), and various other useful automatic sugars.

Example:

from pathlib import Path

from declare_config import Configuration,
    Setting, \
    configuration_source, \
    enable_nested_settings

@configuration_provider("$MYAPP_CONFIG")
@configuration_provider("./myapp.yaml")
@configuration_provider("~/init/myapp.yaml")
@configuration_provider("~/.myapp.yaml")
@enable_nested_settings
@enable_expanduser
class MyAppSettings(declare_config.Configuration):
    '''Settings for myapp.'''
    homepage = Setting('urls.root')
    api_root = Setting('urls.api_root', '${homepage}/api')
    timeout_ms = Setting('timeout_ms', 5000, int)
    log_file = Setting("log_location", ~/logs/myapp.log", Path)

settings = MyAppSettings.load()

The above code defines a configuration file for a sample application. It defines four locations that will be searched for the configuration file when MyAppSettings.load() is called: first it will check the value of the environment variable $MYAPP_CONFIG for a file location, and then if no such environment variable is defined it will check the other three file locations in order for a configuration file. Whenever it finds a valid file location, it will load the settings from that file.

Because of the @enable_nested_settings decorator, settings are allowed to reference one another (and default definitions are allowed to reference other settings as well) through the ${setting_name} syntax. All settings have type information,and the @enable_expanduser decorator ensures that for any setting declared to be a filesystem path, the ~/ directory will be expanded to the user's home directory.

Currently only yaml configuration files are supported, though there are plans to add support for JSON and INI syntaxes.

Development

Dependencies are managed with pipenv. To install dependencies for development, run pipenv install --dev from the project root.

To run tests once with coverage, use pipenv run pytest from the project root. To run tests continuously during development, run pipenv run ptw instead.

To upload to PyPi, update setup.py with the version and any new dependencies and run pipenv run python setup.py upload.

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

declare-config-1.0.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

declare_config-1.0.1-py2.py3-none-any.whl (5.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file declare-config-1.0.1.tar.gz.

File metadata

  • Download URL: declare-config-1.0.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for declare-config-1.0.1.tar.gz
Algorithm Hash digest
SHA256 5c02a51bbf8a3e83b8eed98aac3a8daa602a9b7077801d00884a2ab470446639
MD5 bb5e717f2bb44b8164497a404f757c01
BLAKE2b-256 2d98a81ba3345fa57234f6e07ba822067c36f3db402c3e0af909d2232cf084d9

See more details on using hashes here.

File details

Details for the file declare_config-1.0.1-py2.py3-none-any.whl.

File metadata

  • Download URL: declare_config-1.0.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for declare_config-1.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ac6cb32f3fb599fca4b88a89ae74959e8049cbcf3f7fd4fefb53f952d72559e7
MD5 e46b9508a92db467e6756f4efef0c30e
BLAKE2b-256 826390a38bf98ef72e6849a1972cc425fb1463d12a5b7628c6c9f01ced890064

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