{"id":16346643,"url":"https://github.com/zillionare/cfg4py","last_synced_at":"2025-03-20T23:32:50.023Z","repository":{"id":38492644,"uuid":"258503103","full_name":"zillionare/cfg4py","owner":"zillionare","description":"Easy config (template, auto-complete), hierarchichal design, monitor config change and hot reload","archived":false,"fork":false,"pushed_at":"2023-02-08T03:50:13.000Z","size":4560,"stargazers_count":17,"open_issues_count":2,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-12T00:36:01.021Z","etag":null,"topics":["autocomplete","connectionstrings","logging","pyconfig","python"],"latest_commit_sha":null,"homepage":"https://zillionare.github.io/cfg4py/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zillionare.png","metadata":{"files":{"readme":"README.md","changelog":"HISTORY.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-24T12:14:11.000Z","updated_at":"2024-04-30T20:20:44.000Z","dependencies_parsed_at":"2023-01-25T12:15:16.861Z","dependency_job_id":null,"html_url":"https://github.com/zillionare/cfg4py","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zillionare%2Fcfg4py","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zillionare%2Fcfg4py/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zillionare%2Fcfg4py/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zillionare%2Fcfg4py/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zillionare","download_url":"https://codeload.github.com/zillionare/cfg4py/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221810224,"owners_count":16884070,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["autocomplete","connectionstrings","logging","pyconfig","python"],"created_at":"2024-10-11T00:35:52.063Z","updated_at":"2024-10-28T08:53:50.032Z","avatar_url":"https://github.com/zillionare.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Overview\n\n[![Version](http://img.shields.io/pypi/v/cfg4py?color=brightgreen)](https://pypi.python.org/pypi/cfg4py)\n[![CI Status](https://github.com/zillionare/cfg4py/actions/workflows/release.yml/badge.svg)](https://github.com/zillionare/cfg4py)\n[![Code Coverage](https://img.shields.io/codecov/c/github/zillionare/cfg4py)](https://app.codecov.io/gh/zillionare/cfg4py)\n[![Downloads](https://pepy.tech/badge/cfg4py)](https://pepy.tech/project/cfg4py)\n[![License](https://img.shields.io/badge/License-MIT.svg)](https://opensource.org/licenses/MIT)\n[![Style](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n\n* Free software: MIT license\n* Documentation: https://zillionare.github.io/cfg4py\n\n\nA python config module that:\n\n1. Adaptive deployment (default, dev, test, production) support\n2. Cascading configuration (central vs local) support\n3. Auto-complete\n4. Templates (logging, database, cache, message queue,...)\n5. Environment variables macro support\n6. Enable logging in one line\n7. Built on top of yaml\n\n## Features\n\nIt'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.\n\nFor 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.\n\nCfg4Py has perfect solution supporting for this: adaptive deployment environment support.\n\n### Adaptive Deployment Environment Support\n\nIn 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.\n\nTo 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`.\n\ncfg4py 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.\n\n!!! important\n\n    Since 0.9.0, cfg4py can still work if __cfg4py_server_role__ is not set, when it work at non-strict mode.\n\n### Cascading design\n\n\nAssuming 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.\n\nThis is how Cfg4Py solves the problem:\n\n1. 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.\n2. Change the settings on local machine, after the period you've set, these changes are popluated to all machines.\n\n### Auto-complete\n\n[auto-complete](http://images.jieyu.ai/images/projects/cfg4py/auto-complete.gif)\n\n\nWith 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.\n\nWhen 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:\n\n```python\n\n        cfg[\"services\"][\"redis\"][\"host\"]\n```\nvs:\n\n```python\n\n        cfg.services.redis.host\n```\n\nApparently the latter is the better.\n\nAnd, 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!\n\n### Templates\n\nIt's hard to remember how to configure log, database, cache and etc, so cfg4py provide templates.\n\nJust run cfg4py scaffold, follow the tips then you're done.\n\n[scaffold](http://images.jieyu.ai/images/projects/cfg4py/scaffold.png)\n\n\n### Environment variables macro\n\nThe 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.\n\nWith 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):\n\n```text\n\n        postgres:\n                dsn: postgres://${postgres_account}:${postgres_password}@localhost\n```\n\nthen cfg4py will lookup postgres_account, postgres_password from environment variables and make replacement.\n\n\n### Enable logging with one line\n\nwith one line, you can enable file-rotating logging:\n\n```python\n\n    cfg.enable_logging(level, filename=None)\n```\n\n### Apply configuration change on-the-fly\n\nCfg4Py 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.\n\n### On top of yaml\n\nThe raw config format is backed by yaml, with macro enhancement. YAML is the best for configurations.\n\n\n\n### Credits\n\n\nThis package was created [ppw](https://zillionare.github.io/python-project-wizard)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzillionare%2Fcfg4py","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzillionare%2Fcfg4py","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzillionare%2Fcfg4py/lists"}