Skip to main content

A simple Python app configuration file helper

Project description

pyflexconfig

  • You prefer using direct Python files to provide configuration data to your Python application, just because you can provide any object type you'd prefer in your configuration file.

  • You want to place your custom configuration file anywhere you'd prefer, in traditional places like /etc or wherever you prefer.

  • You want your app to provide some default values to some or all configuration data.

  • But you may prefer something else than Python files (YAML, JSON, XML, etc.), just provide your own parser.

pyflexconfig is just another kid in the block in the settings / options / configuration data management game that fulfills these requirements.

Features

  • Provide the default configuration programmatically, with a default file in your package or through environment variables.

  • Override the default configuration options with a custom configuration file. You can override one, two or all default configuration options.

  • You don't like Python configuration files for onr reason or another, even when executed in a sandbox. Okay, provide your own parser.

  • The configuration container is just a global object on which you can add attributes. I recommend using a types.SimpleNamespace object but anything may do the job.

  • You may provide the custom configuration file either through conventional places (/etc/my_custom_config.py), explicitly through a dedicated command line option, or through an environment variable.

  • The default filter keeps only UPPERCASED options not starting with an underscore. This allows to use temporary variables with lowercase names in your configuration files, that are not exposed in the resulting configuration object. You may provide your own filter if you want other rules or add other rules to the default one.

  • You may finally provide a validator if you need to check the type / value of some or all configuration options.

Installation

Using pyflexconfig

pyflexconfig requires Python 3.6 or later. Install it with the usual pip dance :

pip install pyflexconfig

Note that pyflexconfig runs on any operating system and does not require packages outside the stdlib.

Develop on a fork of pyflexconfig

Of course, provide and activate a dedicated virtual environment with python 3.6 or later, fork pyflexconfig :

git clone <your fork URL>
cd pyflexconfig
pip install -e .[dev]

Please work from the develop branch that supposed to include the latest validated developments.

Please rebase your fork on the develop branch and fix the unit tests (run pytest) before issuing a pull / merge request on the develop branch of the original Git repository.

Ah ! And of course file an issue that explains your changes.

Using pyflexconfig

A basic usage :

Your default config file defaultsettings.py:

# Warning: do not import here somethong that's not in the stdlib
ONE = 1
TWO = 2
# ...

The settings.py module in the same directory:

import pathlib
from types import SimpleNamespace
from pyflexconfig import bootstrap

config = SimpleNamespace(
    # "hardcoded" default options
    THREE = 3,
    # ...
)

# Load the default config that ships in the package
_default_config_path = pathlib.Path(__file__).resolve().parent / "defaultsettings.py"
bootstrap(config, default_path=_default_config_path)

And your main application:

from .settings import config

def main():
    print(config.ONE)
    print(config.TWO)
    print(config.THREE)

if __name__ == "__main__":
    main()

For more examples, please browse the demo/ directory of the repository as well as the tests.py module.

View the full API issuing in the console:

python -c "import pyflexconfig; help(pyflexconfig)"

License

This software is provided under the terms of the MIT license. See the LICENSE file in the Git repository or here.

Author

This software comes from Gilles Lenfant's damaged brain.

The development of pyflexconfig is sponsored by the Caisse des Depots.

Links

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

pyflexconfig-1.0.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

pyflexconfig-1.0.0-1-none-any.whl (6.2 kB view details)

Uploaded

File details

Details for the file pyflexconfig-1.0.0.tar.gz.

File metadata

  • Download URL: pyflexconfig-1.0.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for pyflexconfig-1.0.0.tar.gz
Algorithm Hash digest
SHA256 47371493c911bf0388815ad799e36d422b39c0ca57dfff9c632a8a8d8f0aa8fb
MD5 34ce8d0a2b61d266bf86397d0cee2aa3
BLAKE2b-256 4d2c8db28bb9e0387c360001e2c40b50fe39094ea1da2df7f9b05e8d3f13a2bc

See more details on using hashes here.

File details

Details for the file pyflexconfig-1.0.0-1-none-any.whl.

File metadata

  • Download URL: pyflexconfig-1.0.0-1-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags:
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for pyflexconfig-1.0.0-1-none-any.whl
Algorithm Hash digest
SHA256 431abf7871599bcceeb9a472474274ca79e65767c9f2bc9b8a885b8a756353bf
MD5 a2d39f88743b7bb2d757ade9edd7e3cc
BLAKE2b-256 216a397d403ee721d90c38657650845446e4b27c5de31b7901011936ef5d3a7f

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