Skip to main content

Easy config (template, auto-complete), hierarchichal design, monitor config change and load on-the-fly .

Project description

Overview

Version CI Status Code Coverage Downloads License Style

A python config module that:

  1. Adaptive deployment (default, dev, test, production) support
  2. Cascading configuration (central vs local) support
  3. Auto-complete
  4. Templates (logging, database, cache, message queue,...)
  5. Environment variables macro support
  6. Enable logging in one line
  7. Built on top of yaml
  8. Path expansion support

Features

It's common to see that you have different settings for development machine, test machine and production site. They share many common settings, but a few of them has to be different.

For example, developers should connect to local database server when performing unittest, and tester should connect to their own database server. All these servers should be deployed separately and no data should be messed up.

Cfg4Py has perfect solution supporting for this: adaptive deployment environment support.

Adaptive Deployment Environment Support

In any serious projects, your application may run at both development, testing and production site. Except for effort of copying similar settings here and there, sometimes we'll mess up with development environment and production site. Once this happen, it could result in very serious consequence.

To solve this, Cfg4Py developed a mechanism, that you provide different sets for configurations: dev for development machine, test for testing environment and production for production site, and all common settings are put into a file called defaults.

cfg4py module knows which environment it's running on by looking up environment variable cfg4py_server_role. It should be one of DEV, TEST and PRODUCTION. If nothing found, it means setup is not finished, and Cfg4Py will refuse to work. If the environment is set, then Cfg4Py will read settings from defaults set, then apply update from either of DEV, TEST and PRODUCTION set, according to the environment the application is running on.

!!! important

Since 0.9.0, cfg4py can still work if __cfg4py_server_role__ is not set, when it work at non-strict mode.

Cascading design

Assuming you have a bunch of severs for load-balance, which usually share same configurations. So you'd like put the configurations on a central repository, which could be a redis server or a relational database. Once you update configuration settings at central repository, you update configurations for all servers. But somehow for troubleshooting or maintenance purpose, you'd like some machines could have its own settings at a particular moment.

This is how Cfg4Py solves the problem:

  1. Configure your application general settings at remote service, then implement a RemoteConfigFetcher (Cfg4Py has already implemented one, that read settings from redis), which pull configuration from remote serivce periodically.
  2. Change the settings on local machine, after the period you've set, these changes are popluated to all machines.

Auto-complete

auto-complete

With other python config module, you have to remember all the configuration keys, and refer to each settings by something like cfg["services"]["redis"]["host"] and etc. Keys are hard to rememb, prone to typo, and way too much tedious.

When cfg4py load raw settigns from yaml file, it'll compile all the settings into a Python class, then Cfg4Py let you access your settings by attributes. Compares the two ways to access configure item:

cfg["services"]["redis"]["host"]

vs:

cfg.services.redis.host

Apparently the latter is the better.

And, if you trigger a build against your configurations, it'll generate a python class file. After you import this file (named 'schema.py') into your project, then you can enjoy code auto-complete!

Templates

It's hard to remember how to configure log, database, cache and etc, so cfg4py provide templates.

Just run cfg4py scaffold, follow the tips then you're done.

scaffold

Environment variables macro

The best way to keep secret, is never share them. If you put account/password files, and these files may be leak to the public. For example, push to github by accident.

With cfg4py, you can set these secret as environment variables, then use marco in config files. For example, if you have the following in defaults.yaml (any other files will do too):

postgres:
    dsn: postgres://${postgres_account}:${postgres_password}@localhost:5432/postgres

then cfg4py will lookup postgres_account, postgres_password from environment variables and make replacement.

Path expansion macro

Cfg4py also supports path expansion in configuration files using the ${path:...} macro syntax. This is especially useful for specifying file paths relative to the user's home directory:

logging:
    handlers:
        file:
            filename: ${path:~/logs/myapp.log}

When cfg4py processes this configuration, it will expand ~/logs/myapp.log to the full path, for example /home/user/logs/myapp.log.

Enable logging with one line

with one line, you can enable file-rotating logging:

    cfg.enable_logging(level, filename=None)

Apply configuration change on-the-fly

Cfg4Py provides mechanism to automatically apply configuration changes without restart your application. For local files configuration change, it may take effect immediately. For remote config change, it take effect up to refresh_interval settings.

On top of yaml

The raw config format is backed by yaml, with macro enhancement. YAML is the best for configurations.

Credits

This package was created ppw

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

cfg4py-0.12.1.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cfg4py-0.12.1-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file cfg4py-0.12.1.tar.gz.

File metadata

  • Download URL: cfg4py-0.12.1.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.9 Darwin/24.6.0

File hashes

Hashes for cfg4py-0.12.1.tar.gz
Algorithm Hash digest
SHA256 c99535bbfceb02058ea3b8ef624ac2105a7b53e4230bdb4e8447bb6c9b1de8c1
MD5 4ffaa32e06e76d907f1cb3ed031758c8
BLAKE2b-256 1f1f0ea7dac70aded6b2086799ce363d208e09cf55820721418542e6136198b3

See more details on using hashes here.

File details

Details for the file cfg4py-0.12.1-py3-none-any.whl.

File metadata

  • Download URL: cfg4py-0.12.1-py3-none-any.whl
  • Upload date:
  • Size: 16.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.9 Darwin/24.6.0

File hashes

Hashes for cfg4py-0.12.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5d7c499a13f7d8c2bf05465bd163501dd1ad558633cef8425de085b76c9283cb
MD5 47f75b2ccdb671b38f30ab820a0f8e71
BLAKE2b-256 eb709ad1704ab4c516ff2d97b2a3b6e5b2c1cb4970c8cbf379bd30ae17e269dc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page