{"id":22863880,"url":"https://github.com/mdcatapult/py-config","last_synced_at":"2025-04-14T11:56:54.958Z","repository":{"id":50377809,"uuid":"475886533","full_name":"mdcatapult/py-config","owner":"mdcatapult","description":"Python lib that allows environment variables to override hocon style yaml or json config","archived":false,"fork":false,"pushed_at":"2024-02-14T17:16:06.000Z","size":17582,"stargazers_count":0,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-14T11:56:29.687Z","etag":null,"topics":["config","environment-variables","hocon","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mdcatapult.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-03-30T13:16:10.000Z","updated_at":"2022-07-29T10:34:55.000Z","dependencies_parsed_at":"2025-02-06T13:45:47.311Z","dependency_job_id":"8079ce9f-1556-4004-b844-c2a05433a2da","html_url":"https://github.com/mdcatapult/py-config","commit_stats":null,"previous_names":[],"tags_count":31,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdcatapult%2Fpy-config","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdcatapult%2Fpy-config/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdcatapult%2Fpy-config/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdcatapult%2Fpy-config/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mdcatapult","download_url":"https://codeload.github.com/mdcatapult/py-config/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248878016,"owners_count":21176242,"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":["config","environment-variables","hocon","python"],"created_at":"2024-12-13T11:17:55.835Z","updated_at":"2025-04-14T11:56:54.676Z","avatar_url":"https://github.com/mdcatapult.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Klein Config\n\nModule to provide config management\n\n## Usage\n\n```python\nfrom klein_config import get_config\n\n# Can be overriden with env variable MY_CONFIG_SETTING\nconfig = get_config({\"my\": {\"config\": {\"setting\": \"initialised value\"}})\n\n# Access via `get` accessor with no backup (raises ConfigMissingException if not found).\nvalue = config.get(\"my.config.setting\")\n\n# Access via `get` accessor method with a backup.\nbackup_value = config.get(\"not.a.setting\", \"backup value\")\n\n# Access via `dict` (raises KeyError if not found).\nsame_value = config[\"my.config.setting\"]\n\n# Sub-configs are created if the value is another `dict`.\nintermediate_config = config[\"my.config\"]\nsame_value_again = intermediate_config[\"setting\"]\n```\n\n### Structure\nInternally the config object uses the ConfigTree structure that is part of pyhocon. This can be traversed easily with the get method using dot notation as outlined above.\n\n### Config Initialisation\nThe `get_config` function looks for :\n- argument `--common` or environmental variable `KLEIN_COMMON` to specify a valid filepath for a common config file (in either JSON or YAML format); and\n- argument `--config` or environmental variable `KLEIN_CONFIG` to specify a valid filepath for a config file \n\n\nN.B. Passing both environmental variables _and_ arguments for either config or common is ambiguous and is therefore NOT accepted.\n\nYou can also pass a `dict` into the `get_config` function.\n\n#### Example configs\nJSON:\n```json\n{\n  \"rabbitmq\": {\n    \"host\": [\n      \"localhost\"\n    ],\n    \"port\": 5672,\n    \"username\": \"username\",\n    \"password\": \"password\"\n  },\n  \"mongo\": {\n    \"host\": [\n      \"mongo.domain.com\"\n    ],\n    \"username\": \"username\",\n    \"password\": \"password\"\n  }\n}\n```\nYAML:\n```yaml\nmongo:\n  host:\n    - mongo.domain.com\n  password: password\n  username: username\nrabbitmq:\n  host:\n    - localhost\n  password: username\n  port: 5672\n  username: password\n```\n\nExample config files are also provided in [json](example.config.json) and [yaml](example.config.yaml) formats.\n\n### Order precedence\nThe configs are applied to the config object in the following order: \n\n1. Common config as identified via argument `--common` or environmental variable `KLEIN_COMMON`\n2. Config that is injected via the Class constructor\n3. Config that is identified via the argument `--config` or environmental variable `KLEIN_CONFIG`\n\n\nConfigs will override any previous values as they are applied.\n\n### Environment Aware\nThe module is \"Environment Aware\", i.e. it will look for environment variables in the first instance. If a valid variable exists then this will be used regardless of any config that may have been supplied.\n\nThe path is transformed by converting the string to uppercase and replacing all dots with underscores.\n\n```\nmy.config.setting =\u003e MY_CONFIG_SETTING\n```\n\nSub-config items are still overriden by the same environment variables as in the root config.\n\n## Development\nThis project uses [pipenv](https://github.com/pypa/pipenv). To install it, run `pip install pipenv`.\n\n### Development\n```\npipenv install --dev\n```\n\n### Testing\n```bash\npipenv run python -m pytest\n```\nFor test coverage you can run:\n```bash\npipenv shell\npipenv run python -m pytest --cov-report term --cov src/ tests/\n```\n\n## Unit testing config in another library.\nYou need to monkey patch the config library and override what the methods do in your test code.\nFirst create your test version of config. Let's say it is in the module `tests.test_config`.\n\n```python\nfrom klein_config.config import EnvironmentAwareConfig\n\nconfig_dict = {\n    'consumer': {\n        'name': 'text_classifier',\n        'queue': 'text_classifier',\n    },\n    'mongo': {\n        'host': 'localhost',\n        'port': 27017,\n        'doclib_database': 'doclib',\n        'documents_collection': 'documents',\n        'text_classification_collection': 'text_classification',\n    }\n}\n\nconfig = EnvironmentAwareConfig(initial=config_dict)\n\ndef get_config():\n    return config\n\n```\nThen in your test code you can monkey patch the config library like this:\n\n```python\nimport sys\nsys.modules['klein_config'] = __import__('tests.test_config')\n\nfrom tests.test_config import config\n```\nNow any test code that uses klein_config will use this test version of the config library.\n\n### Troubleshooting\n\nIf you are unable to run `pipenv shell` and are having permission errors, you can spin up a virtual environment in which to run \nthe `pipenv` commands:\n\n```bash\npip install virtualenv      // install virtualenv module\nvirtual env venv            // create your virtual environment (run command from project root directory)\nsource venv/bin/activate    // start the virtual environment\npipenv install --dev        // install dependencies - you should now be able to run the tests with the above commands\n```\n\n### Known issue\n\n#### Library does not install on some python versions.\nThis issue has appeared on a few occasions due to a tagged dependency on pyyaml. This library requires certain version of python to install. \n\nIf this library does not install on pyyaml failure, you can still install and use this library with using --no-deps flag and manually install the dependencies. \n\n```\npip install --no-deps klein_config   # Don't forget to install the dependencies as needed\n```\n\n\n### License\nThis project is licensed under the terms of the Apache 2 license, which can be found in the repository as `LICENSE.txt`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmdcatapult%2Fpy-config","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmdcatapult%2Fpy-config","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmdcatapult%2Fpy-config/lists"}