A simple Python library to manage and load configuration files
Project description
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
-
Flexible Configuration Loading:
- Load configurations from predefined locations or user-specified paths.
- Automatically detect and parse configuration files based on their extensions.
-
Dynamic Template Rendering:
- Use Jinja2 templates to render configuration values dynamically.
-
Extensible Parsing:
- Easily add support for new configuration formats by registering custom parsers.
-
Global Function and Filter Registration:
- Add reusable functions and filters for use in Jinja2 templates.
-
DotDict Support:
- Access configuration data using both dictionary and attribute-style access.
Installation
pip install cfgengine
Usage
Loading a Configuration File
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:
{
"key1": "{{ env_var('HOME') }}",
"key2": "{{ 3 + 5 }}"
}
Loading and rendering the file:
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
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
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
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
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
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
- Ensure the appropriate parser is registered for the file format you intend to use.
- Always validate Jinja2 templates in your configuration files to avoid runtime errors.
- Use the
returns_native_non_stringdecorator when functions return non-string objects to avoid unintended parsing issues. - 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cfgengine-2.0.0.tar.gz.
File metadata
- Download URL: cfgengine-2.0.0.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7cefb2c211f115eb9f5aa0fc8c88a27ea74073d4298dc54dc6be68c54de8f70e
|
|
| MD5 |
d377b104bae699d20f7cd5232a74953d
|
|
| BLAKE2b-256 |
641c822ace5bac6018e1929679e2243eb83f3910859c97621802865fefc46bff
|
File details
Details for the file cfgengine-2.0.0-py3-none-any.whl.
File metadata
- Download URL: cfgengine-2.0.0-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
731ce10b20fc1a4214186e50227957885ad68768085fb13ab90e07bb0a61cbd6
|
|
| MD5 |
4eb8bf9797ec21797d6d739d0a5e65af
|
|
| BLAKE2b-256 |
68938cc6876fa3c0ae21b09bbac3d236ffb1cd150372789fb3fe87e6ad90aa52
|