Name | cfgengine JSON |
Version |
2.0.0
JSON |
| download |
home_page | None |
Summary | A simple Python library to manage and load configuration files |
upload_time | 2025-01-15 07:30:42 |
maintainer | None |
docs_url | None |
author | None |
requires_python | None |
license | MIT License Copyright (c) 2024 ZhuJiaDong Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
keywords |
config
|
VCS |
 |
bugtrack_url |
|
requirements |
jinja2
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Configuration Engine
## Overview
This project provides a robust and extensible configuration engine for managing configuration files in various formats (e.g., JSON, INI). It supports custom parsing, Jinja2 template rendering, and dynamic function registration for templates. The library is highly extensible, allowing users to add support for additional file formats and integrate custom global functions or filters.
## Features
1. **Flexible Configuration Loading**:
- Load configurations from predefined locations or user-specified paths.
- Automatically detect and parse configuration files based on their extensions.
2. **Dynamic Template Rendering**:
- Use Jinja2 templates to render configuration values dynamically.
3. **Extensible Parsing**:
- Easily add support for new configuration formats by registering custom parsers.
4. **Global Function and Filter Registration**:
- Add reusable functions and filters for use in Jinja2 templates.
5. **DotDict Support**:
- Access configuration data using both dictionary and attribute-style access.
## Installation
```bash
pip install cfgengine
```
## Usage
### Loading a Configuration File
```python
from cfgengine import ConfigLoader
# Load a configuration file (searches default paths if not specified)
config_data = ConfigLoader.load_config(config_dir_or_path="/path/to/config")
# Access configuration values
print(config_data.some_key)
```
### Using Jinja2 Templates in Configuration Files
Example JSON file:
```json
{
"key1": "{{ env_var('HOME') }}",
"key2": "{{ 3 + 5 }}"
}
```
Loading and rendering the file:
```python
config_data = ConfigLoader.load_config("config.json")
print(config_data.key1) # Outputs the HOME environment variable
print(config_data.key2) # Outputs 8
```
## Decorators and Their Usage
### `@register_cfg_parser`
Register a parser for a specific file extension.
#### Example
```python
from cfgengine.parser import CfgParser, register_cfg_parser
@register_cfg_parser("yaml")
class YAMLParser(CfgParser):
def load(self):
import yaml
with open(self.file_path, "r") as f:
return yaml.safe_load(f)
```
### `@register_global_function`
Register a global function for use in Jinja2 templates.
#### Example
```python
from cfgengine import register_global_function, returns_native_non_string
@register_global_function()
@returns_native_non_string
def multiply(a, b):
return a * b
# Usage in a configuration file
{
"result": "{{ multiply(2, 3) }}"
}
```
### `@register_filter`
Register a custom filter for Jinja2 templates.
#### Example
```python
from cfgengine import register_filter
@register_filter()
def uppercase(value):
return value.upper()
# Usage in a configuration file
{
"name": "{{ 'hello' | uppercase }}"
}
```
### `@returns_native_non_string`
Mark a global function as returning a native (non-string) object.
#### Example
```python
from cfgengine.config_loader import returns_native_non_string
@register_global_function()
@returns_native_non_string
def return_list():
return [1, 2, 3]
# Usage in a configuration file
{
"data": "{{ return_list() }}"
}
```
## Key Classes
### `ConfigLoader`
Handles loading configuration files and determining the file path.
#### Key Methods
- `set_default_file_name(file_name)`: Updates the default configuration file name.
- `get_config_file_path(config_dir_or_path)`: Resolves the configuration file path.
- `load_config(config_dir_or_path, jinja2_env)`: Loads and parses the configuration file.
### `DotDict`
A dictionary with attribute-style access.
#### Example
```python
data = DotDict({"key": "value"})
print(data.key) # Outputs "value"
data.new_key = "new_value"
print(data["new_key"]) # Outputs "new_value"
```
### `ParserRegistry`
Manages the registration of parsers for file extensions.
#### Key Methods
- `register_parser(extension, parser_clz)`: Registers a parser for a file extension.
- `get_parser_class(extension)`: Retrieves the parser class for a given extension.
### `CfgParser`
Abstract base class for all configuration parsers.
#### Key Methods
- `parse(jinja_env)`: Parses the configuration using Jinja2 templates.
- `load()`: Abstract method for loading raw configuration data.
## Notes and Best Practices
1. Ensure the appropriate parser is registered for the file format you intend to use.
2. Always validate Jinja2 templates in your configuration files to avoid runtime errors.
3. Use the `returns_native_non_string` decorator when functions return non-string objects to avoid unintended parsing issues.
4. To extend the library, create custom parsers and register them using `@register_cfg_parser`.
## License
This project is licensed under the MIT License. See the LICENSE file for details.
## Changelog
For the full list of changes, see the [Changelog](CHANGELOG.md).
Raw data
{
"_id": null,
"home_page": null,
"name": "cfgengine",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "config",
"author": null,
"author_email": "zjd-melo <zhujiadong.melo@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/64/1c/822ace5bac6018e1929679e2243eb83f3910859c97621802865fefc46bff/cfgengine-2.0.0.tar.gz",
"platform": null,
"description": "# Configuration Engine\n\n## Overview\n\nThis project provides a robust and extensible configuration engine for managing configuration files in various formats (e.g., JSON, INI). It supports custom parsing, Jinja2 template rendering, and dynamic function registration for templates. The library is highly extensible, allowing users to add support for additional file formats and integrate custom global functions or filters.\n\n## Features\n\n1. **Flexible Configuration Loading**:\n - Load configurations from predefined locations or user-specified paths.\n - Automatically detect and parse configuration files based on their extensions.\n\n2. **Dynamic Template Rendering**:\n - Use Jinja2 templates to render configuration values dynamically.\n\n3. **Extensible Parsing**:\n - Easily add support for new configuration formats by registering custom parsers.\n\n4. **Global Function and Filter Registration**:\n - Add reusable functions and filters for use in Jinja2 templates.\n\n5. **DotDict Support**:\n - Access configuration data using both dictionary and attribute-style access.\n\n## Installation\n\n```bash\npip install cfgengine\n```\n\n## Usage\n\n### Loading a Configuration File\n\n```python\nfrom cfgengine import ConfigLoader\n\n# Load a configuration file (searches default paths if not specified)\nconfig_data = ConfigLoader.load_config(config_dir_or_path=\"/path/to/config\")\n\n# Access configuration values\nprint(config_data.some_key)\n```\n\n### Using Jinja2 Templates in Configuration Files\n\nExample JSON file:\n\n```json\n{\n \"key1\": \"{{ env_var('HOME') }}\",\n \"key2\": \"{{ 3 + 5 }}\"\n}\n```\n\nLoading and rendering the file:\n\n```python\nconfig_data = ConfigLoader.load_config(\"config.json\")\nprint(config_data.key1) # Outputs the HOME environment variable\nprint(config_data.key2) # Outputs 8\n```\n\n## Decorators and Their Usage\n\n### `@register_cfg_parser`\n\nRegister a parser for a specific file extension.\n\n#### Example\n\n```python\nfrom cfgengine.parser import CfgParser, register_cfg_parser\n\n@register_cfg_parser(\"yaml\")\nclass YAMLParser(CfgParser):\n def load(self):\n import yaml\n with open(self.file_path, \"r\") as f:\n return yaml.safe_load(f)\n```\n\n### `@register_global_function`\n\nRegister a global function for use in Jinja2 templates.\n\n#### Example\n\n```python\nfrom cfgengine import register_global_function, returns_native_non_string\n\n@register_global_function()\n@returns_native_non_string\ndef multiply(a, b):\n return a * b\n\n# Usage in a configuration file\n{\n \"result\": \"{{ multiply(2, 3) }}\"\n}\n```\n\n### `@register_filter`\n\nRegister a custom filter for Jinja2 templates.\n\n#### Example\n\n```python\nfrom cfgengine import register_filter\n\n@register_filter()\ndef uppercase(value):\n return value.upper()\n\n# Usage in a configuration file\n{\n \"name\": \"{{ 'hello' | uppercase }}\"\n}\n```\n\n### `@returns_native_non_string`\n\nMark a global function as returning a native (non-string) object.\n\n#### Example\n\n```python\nfrom cfgengine.config_loader import returns_native_non_string\n\n@register_global_function()\n@returns_native_non_string\ndef return_list():\n return [1, 2, 3]\n\n# Usage in a configuration file\n{\n \"data\": \"{{ return_list() }}\"\n}\n```\n\n## Key Classes\n\n### `ConfigLoader`\n\nHandles loading configuration files and determining the file path.\n\n#### Key Methods\n\n- `set_default_file_name(file_name)`: Updates the default configuration file name.\n- `get_config_file_path(config_dir_or_path)`: Resolves the configuration file path.\n- `load_config(config_dir_or_path, jinja2_env)`: Loads and parses the configuration file.\n\n### `DotDict`\n\nA dictionary with attribute-style access.\n\n#### Example\n\n```python\ndata = DotDict({\"key\": \"value\"})\nprint(data.key) # Outputs \"value\"\ndata.new_key = \"new_value\"\nprint(data[\"new_key\"]) # Outputs \"new_value\"\n```\n\n### `ParserRegistry`\n\nManages the registration of parsers for file extensions.\n\n#### Key Methods\n\n- `register_parser(extension, parser_clz)`: Registers a parser for a file extension.\n- `get_parser_class(extension)`: Retrieves the parser class for a given extension.\n\n### `CfgParser`\n\nAbstract base class for all configuration parsers.\n\n#### Key Methods\n\n- `parse(jinja_env)`: Parses the configuration using Jinja2 templates.\n- `load()`: Abstract method for loading raw configuration data.\n\n## Notes and Best Practices\n\n1. Ensure the appropriate parser is registered for the file format you intend to use.\n2. Always validate Jinja2 templates in your configuration files to avoid runtime errors.\n3. Use the `returns_native_non_string` decorator when functions return non-string objects to avoid unintended parsing issues.\n4. To extend the library, create custom parsers and register them using `@register_cfg_parser`.\n\n## License\n\nThis project is licensed under the MIT License. See the LICENSE file for details.\n\n## Changelog\n\nFor the full list of changes, see the [Changelog](CHANGELOG.md).\n",
"bugtrack_url": null,
"license": "MIT License Copyright (c) 2024 ZhuJiaDong Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
"summary": "A simple Python library to manage and load configuration files",
"version": "2.0.0",
"project_urls": {
"Homepage": "https://github.com/zjd-melo/cfgengine",
"Issues": "https://github.com/zjd-melo/cfgengine/issues",
"Repository": "https://github.com/zjd-melo/cfgengine.git"
},
"split_keywords": [
"config"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "68938cc6876fa3c0ae21b09bbac3d236ffb1cd150372789fb3fe87e6ad90aa52",
"md5": "4eb8bf9797ec21797d6d739d0a5e65af",
"sha256": "731ce10b20fc1a4214186e50227957885ad68768085fb13ab90e07bb0a61cbd6"
},
"downloads": -1,
"filename": "cfgengine-2.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "4eb8bf9797ec21797d6d739d0a5e65af",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 9398,
"upload_time": "2025-01-15T07:30:39",
"upload_time_iso_8601": "2025-01-15T07:30:39.993830Z",
"url": "https://files.pythonhosted.org/packages/68/93/8cc6876fa3c0ae21b09bbac3d236ffb1cd150372789fb3fe87e6ad90aa52/cfgengine-2.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "641c822ace5bac6018e1929679e2243eb83f3910859c97621802865fefc46bff",
"md5": "d377b104bae699d20f7cd5232a74953d",
"sha256": "7cefb2c211f115eb9f5aa0fc8c88a27ea74073d4298dc54dc6be68c54de8f70e"
},
"downloads": -1,
"filename": "cfgengine-2.0.0.tar.gz",
"has_sig": false,
"md5_digest": "d377b104bae699d20f7cd5232a74953d",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 11920,
"upload_time": "2025-01-15T07:30:42",
"upload_time_iso_8601": "2025-01-15T07:30:42.785276Z",
"url": "https://files.pythonhosted.org/packages/64/1c/822ace5bac6018e1929679e2243eb83f3910859c97621802865fefc46bff/cfgengine-2.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-01-15 07:30:42",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "zjd-melo",
"github_project": "cfgengine",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "jinja2",
"specs": [
[
">=",
"3.0.0"
]
]
}
],
"lcname": "cfgengine"
}