Skip to main content

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 (ie. /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, then:

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

History

1.0.2 - 2020/7/5

  • Bugfix: Accepting pathlib.Path objects as well as str paths.

1.0.1 - 2020/6/19

  • Removed python-dotenv from requirements (not used)
  • Some typos in README.md
  • History file

1.0.0 - 2020/6/18

  • initial release

Metadata

Release files for pyflexconfig 1.0.2

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

Source distribution (sdist)

Source distribution for pyflexconfig 1.0.2
File Size Uploaded Provenance
pyflexconfig-1.0.2.tar.gz 6.2 kB Without provenance Details

Release files / pyflexconfig-1.0.2.tar.gz

Download URL pyflexconfig-1.0.2.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
703952674a3e579d77253a2310615094b81dc4979c7f8a30f5ff92b701112b28
BLAKE2b-256 checksum
How to use checksums
b211c6885330d17dd4bed898d71742aa52c20b2b0d184ac39dfa0d0a431f1030
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.7

Security for pyflexconfig 1.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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