# gen3config
Handle YAML configurations with a given default/expected configuration.
Library will overlay a provided configration over the default and ignore unknown values.
## Quickstart
Example `config-default.yaml` for your application:
```
---
SOME_VALUE: 'default'
EXAMPLE:
nested:
key: 'value'
is_boolean: true
```
Example `config.py` in your application:
```
from gen3config import Config
class AppConfig(Config):
def __init__(self, *args, **kwargs):
super(AppConfig, self).__init__(*args, **kwargs)
def post_process(self):
# do custom stuff here after parsing config
pass
default_cfg_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "config-default.yaml"
)
config = AppConfig(default_cfg_path)
```
---
Now we want to get a specific configuration.
Example file `~/path/to/app-config.yaml`:
```
SOME_VALUE: 'app-specific configuration'
EXAMPLE:
nested:
is_boolean: true
```
Example initialization function in your application:
```
from app.config import config
config.load('~/path/to/app-config.yaml')
```
Then from other files:
```
from app.config import config
print(config["SOME_VALUE"]) # 'app-specific configuration'
print(config["EXAMPLE"]["nested"]["key"]) # 'value'
```
> NOTE: `config["EXAMPLE"]["nested"]["key"]` does not exist in the provided configuration, but it does exist in the default configuration. Therefore, the default value, `'value'` is retrieved.
## Details:
- Create a `config-default.yaml` and `config.py` in the top-level folder your app
- Inside `config-default.yaml` add keys and reasonable default values
- Inside `config.py`, create a class that inherits from this Config class
- See above example
- Add a final line to your `config.py` that instantiates your custom class:
- Ensure that you provide the default config path
- If placed in same directory as `config.py` you can use something like:
```
default_cfg_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "config-default.yaml"
)
config = FenceConfig(default_cfg_path)
```
- Import your instaniated object whenever you need to get configuration
- Example: `from fence.config import config`
- Load in application configuration during init of your app
- Example: `config.load('path/to/fence-config.yaml')`
- Now you can safely access anything that was in your `config-default.yaml` from this
object as if it were a dictionary
- Example: `storage_creds = config["STORAGE_CREDENTIALS"]`
- Example: `if config["SOME_BOOLEAN"]: ...`
- Example: `nested_value = config["TOP_LEVEL"]["nested"]`
- And of course you can import that into any file you want and will have access to
keys/values
- Example: `from fence.config import config`
Raw data
{
"_id": null,
"home_page": "https://github.com/uc-cdis/gen3config",
"name": "gen3config",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.9",
"maintainer_email": null,
"keywords": null,
"author": "CTDS UChicago",
"author_email": "cdis@uchicago.edu",
"download_url": "https://files.pythonhosted.org/packages/84/b9/71a4461002596208e43f84ffb3b14b0343f70e1bbd8000dac7e5788eaf2e/gen3config-2.0.0.tar.gz",
"platform": null,
"description": "# gen3config\n\nHandle YAML configurations with a given default/expected configuration.\nLibrary will overlay a provided configration over the default and ignore unknown values.\n\n## Quickstart\n\nExample `config-default.yaml` for your application:\n\n```\n---\nSOME_VALUE: 'default'\n\nEXAMPLE:\n nested:\n key: 'value'\n is_boolean: true\n```\n\nExample `config.py` in your application:\n\n```\nfrom gen3config import Config\n\nclass AppConfig(Config):\n def __init__(self, *args, **kwargs):\n super(AppConfig, self).__init__(*args, **kwargs)\n\n def post_process(self):\n # do custom stuff here after parsing config\n pass\n\ndefault_cfg_path = os.path.join(\n os.path.dirname(os.path.abspath(__file__)), \"config-default.yaml\"\n)\n\nconfig = AppConfig(default_cfg_path)\n```\n\n---\n\nNow we want to get a specific configuration.\n\nExample file `~/path/to/app-config.yaml`:\n\n```\nSOME_VALUE: 'app-specific configuration'\n\nEXAMPLE:\n nested:\n is_boolean: true\n```\n\nExample initialization function in your application:\n\n```\nfrom app.config import config\n\nconfig.load('~/path/to/app-config.yaml')\n```\n\nThen from other files:\n```\nfrom app.config import config\n\nprint(config[\"SOME_VALUE\"]) # 'app-specific configuration'\nprint(config[\"EXAMPLE\"][\"nested\"][\"key\"]) # 'value'\n```\n\n> NOTE: `config[\"EXAMPLE\"][\"nested\"][\"key\"]` does not exist in the provided configuration, but it does exist in the default configuration. Therefore, the default value, `'value'` is retrieved.\n\n## Details:\n\n- Create a `config-default.yaml` and `config.py` in the top-level folder your app\n- Inside `config-default.yaml` add keys and reasonable default values\n- Inside `config.py`, create a class that inherits from this Config class\n - See above example\n- Add a final line to your `config.py` that instantiates your custom class:\n - Ensure that you provide the default config path\n - If placed in same directory as `config.py` you can use something like:\n ```\n default_cfg_path = os.path.join(\n os.path.dirname(os.path.abspath(__file__)), \"config-default.yaml\"\n )\n config = FenceConfig(default_cfg_path)\n ```\n- Import your instaniated object whenever you need to get configuration\n - Example: `from fence.config import config`\n- Load in application configuration during init of your app\n - Example: `config.load('path/to/fence-config.yaml')`\n- Now you can safely access anything that was in your `config-default.yaml` from this\n object as if it were a dictionary\n - Example: `storage_creds = config[\"STORAGE_CREDENTIALS\"]`\n - Example: `if config[\"SOME_BOOLEAN\"]: ...`\n - Example: `nested_value = config[\"TOP_LEVEL\"][\"nested\"]`\n- And of course you can import that into any file you want and will have access to\n keys/values\n - Example: `from fence.config import config`\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "Gen3 Configuration Library",
"version": "2.0.0",
"project_urls": {
"Homepage": "https://github.com/uc-cdis/gen3config",
"Repository": "https://github.com/uc-cdis/gen3config"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "65667f63da008e0380bd9aa3d40eaaa73faf3a67ef89e91c6b99eeb6343d6f71",
"md5": "1d032926766a5703b8ec98958b773a64",
"sha256": "0d56a64f82e7c4f9a417e7be5645b745e6450431b77a1a4d38f52bea19e56a49"
},
"downloads": -1,
"filename": "gen3config-2.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "1d032926766a5703b8ec98958b773a64",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.9",
"size": 11936,
"upload_time": "2024-11-18T16:31:06",
"upload_time_iso_8601": "2024-11-18T16:31:06.874786Z",
"url": "https://files.pythonhosted.org/packages/65/66/7f63da008e0380bd9aa3d40eaaa73faf3a67ef89e91c6b99eeb6343d6f71/gen3config-2.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "84b971a4461002596208e43f84ffb3b14b0343f70e1bbd8000dac7e5788eaf2e",
"md5": "0489ff1899bda6bb0de7e9ba35429987",
"sha256": "4e4480a5d2b0d8b4263150ad1b354b3dc37406427e010a8acb161e32861c3acb"
},
"downloads": -1,
"filename": "gen3config-2.0.0.tar.gz",
"has_sig": false,
"md5_digest": "0489ff1899bda6bb0de7e9ba35429987",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.9",
"size": 9483,
"upload_time": "2024-11-18T16:31:07",
"upload_time_iso_8601": "2024-11-18T16:31:07.794361Z",
"url": "https://files.pythonhosted.org/packages/84/b9/71a4461002596208e43f84ffb3b14b0343f70e1bbd8000dac7e5788eaf2e/gen3config-2.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-18 16:31:07",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "uc-cdis",
"github_project": "gen3config",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "gen3config"
}