YAML Configuration Management: Local Files + Nacos Dynamic Configuration
This project provides a unified YAML configuration loading solution, supporting configuration retrieval from both local files and the Nacos configuration center. It also supports hot updates, variable substitution, decryption of encrypted values, and other advanced features.
Install the base dependency:
pip install yamlpyconfig
Enable Nacos support:
pip install yamlpyconfig[nacos]
1. Local YAML Configuration Loading
In the specified configuration directory (config_dir, e.g., /config), the following files must exist:
application.yaml(required)application-{profile}.yaml(optional)
1.1 Profile Resolution and Loading Order
When initializing ConfigManager, the effective profile is determined using the following priority (highest → lowest):
base_profileexplicitly passed to theConfigManagerconstructor- Environment variable
APP_PROFILE - Environment variable
SPRING_PROFILES_ACTIVE - The
profilefield insideapplication.yaml - If none of the above is provided → no
application-{profile}.yamlwill be loaded
1.1.1 Notes
application.yamlmust exist; otherwise an exception is raised.- If a profile is determined but the corresponding
application-{profile}.yamlis not found, an exception is thrown.
1.2 Extended Configuration: extend-profiles
Extended configurations come from two sources, and both are loaded (merged):
- The
extend-profilesfield insideapplication.yamlorapplication-{profile}.yaml - The
extend_profilesparameter passed to theConfigManagerconstructor
Both sources are comma-separated strings, where each entry represents one extension item.
Each extension item supports two forms.
1.2.1 Two Extension Item Formats
| Type | Example | Loading Behavior |
|---|---|---|
| Explicit file | file:/full/path/to/xxx.yaml |
Loads the file at the given path; missing file → error |
| Extension profile | dev-ext |
Loads config_dir/application-dev-ext.yaml |
1.2.2 Priority Rules
- Constructor-passed
extend_profileshas higher priority thanextend-profilesin YAML - Within the same source, items appearing later have higher priority
- All extension configurations override values from
application.yamlandapplication-{profile}.yaml
1.3 Overall Priority of Local Configurations (Low → High)
application.yamlapplication-{profile}.yamlextend-profiles(including both explicit file items and extension profile items)
2. Nacos Configuration Loading
To enable Nacos, define the config-sources.nacos section in application.yaml:
config-sources:
nacos:
server-addr: "192.168.30.36:9090"
namespace: "dev"
group: "DEFAULT_GROUP"
username: "nacos"
password: "{encrypted}VuFvNZOg/q7ZQoIUGWydBw=="
imports:
- data-id: "gateway.yaml"
- data-id: "application-ext.yaml"
2.1 Nacos Configuration Merge Order (Low → High)
- Final merged result of all local configurations
- Nacos
importsin declared order (later items have higher priority)
3. Key Features and Usage Examples
3.1 Basic Usage
The following example demonstrates how configurations are loaded automatically and updated in real time:
@pytest.mark.asyncio
async def test_config_manager_with_nacos(self):
async with ConfigManager("./") as config_manager:
logger.info(config_manager.get_config())
while True:
await asyncio.sleep(5)
logger.info(config_manager.get_config())
# Specify base_profile and extend_profiles
async with ConfigManager("./",
base_profile="dev",
extend_profiles="file:./demo.yaml,dev-ext") as config_manager:
logger.info(config_manager.get_config())
while True:
await asyncio.sleep(5)
logger.info(config_manager.get_config())
Once inside the async with block:
- Local configuration is loaded
- If
config-sources.nacosexists → connects to Nacos and enables hot updates
3.2 Environment Variable Placeholder Support
Local YAML files support Spring-style placeholders:
key-with-default: ${KEY1:DEFAULT_VALUE}
key: ${KEY2}
Behavior:
- If the environment variable exists → use its value
- If not, and a default is provided → use the default
- If no default is provided → returns
None
3.3 Encrypted Field Support (SM2 / SM4)
Sensitive fields (passwords, keys, etc.) can be declared with the {encrypted} prefix:
password: "{encrypted}VuFvNZOg/q7ZQoIUGWydBw=="
To enable automatic decryption, specify the algorithm and key when initializing ConfigManager:
@pytest.mark.asyncio
async def test_config_manager_with_nacos_decrypt(self):
# Example using SM4 (symmetric key encryption)
async with ConfigManager(
"./",
crypto_algorithm=AlgorithmEnum.SM4,
key="lSU543Tes6wmjnb+PMVQNg=="
) as config_manager:
logger.info(config_manager.get_config())
Notes:
- SM4 → Symmetric encryption (a shared key is required)
- SM2 → Asymmetric encryption (a private key must be provided)
Release files for yamlpyconfig 0.1.10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| yamlpyconfig-0.1.10.tar.gz | 15.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| yamlpyconfig-0.1.10-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 38.8 kB
Release files / yamlpyconfig-0.1.10.tar.gz
| Download URL | yamlpyconfig-0.1.10.tar.gz |
|---|---|
| Size | 15.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
84d5ef8cf904b03df0c166f2665d7f8340a925fd3e4b4f4b0485717d639b0276
|
|
BLAKE2b-256 checksum How to use checksums |
7f0f00cbc74b2a0c44f6a3cb715ce729419e0bffd834b7234e163092b9419a71
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.8
|
Release files / yamlpyconfig-0.1.10-py3-none-any.whl
| Download URL | yamlpyconfig-0.1.10-py3-none-any.whl |
|---|---|
| Size | 23.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
759cd0ec8c6a26ac32931dd71a67fa79d96ddf1a6f4eaec015ff7eb5a4f71488
|
|
BLAKE2b-256 checksum How to use checksums |
78ea4faa889d073ebb298bc3a6ffdaf53fda67429d6f237ce12f6614d2b4aba0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.8
|