Skip to main content

A unified YAML configuration manager supporting local files and dynamic Nacos configuration with hot-reloading, environment variable substitution, and built-in encryption (SM2/SM4) support.

Project description

YAML Configuration Management: Local Files + Nacos Dynamic Configuration

This project provides a unified YAML configuration loading framework that supports both local YAML files and Nacos configuration center, with features such as hot updates, environment variable substitution, and encrypted value decoding.

Install the core package:

pip install yamlpyconfig

If you need Nacos support:

pip install yamlpyconfig[nacos]

1. Local YAML Configuration

Your configuration folder (e.g., /config) must contain at least:

  • application.yaml
  • Optional: application-{profile}.yaml

1.1 Profile Resolution Rules

The active profile is determined with the following priority (from highest to lowest):

  1. Environment variable: APP_PROFILE
  2. Environment variable: SPRING_PROFILES_ACTIVE
  3. The profile field inside application.yaml
  4. If none of the above exist → application-{profile}.yaml will NOT be loaded

Notes

  1. The configuration directory must contain application.yaml. Missing this file causes an error.
  2. If a profile is resolved but application-{profile}.yaml does not exist, an exception is raised.

2. Loading Configuration from Nacos

To enable Nacos, declare the config-sources.nacos section inside your local configuration:

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 Configuration Merge Priority (Low → High)

  1. Local application.yaml
  2. Local application-{profile}.yaml
  3. Nacos imports (following the order declared; later entries override earlier ones)

3. Key Features and Usage Examples

3.1 Basic Usage

Example: load configuration and print updates automatically:

@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())

When entering the async with block:

  • Local configuration is loaded first
  • If Nacos is configured → it connects automatically and listens for real-time updates

3.2 Environment Variable Interpolation

Local YAML files support Spring-style placeholders:

key-with-default: ${KEY1:DEFAULT_VALUE}
key: ${KEY2}

Behavior:

  • If the environment variable exists → its value is used
  • If it does not exist → use the default value after the colon
  • If no default value is provided → None is returned

3.3 Encrypted Field Support (SM2 / SM4)

Sensitive fields (passwords, secret keys, etc.) can be encrypted and marked 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):

    # SM4 example — uses a symmetric key
    async with ConfigManager(
        "./",
        crypto_algorithm=AlgorithmEnum.SM4,
        key="lSU543Tes6wmjnb+PMVQNg=="
    ) as config_manager:

        logger.info(config_manager.get_config())

Explanation:

  • SM4 → symmetric encryption (pass the secret key)
  • SM2 → asymmetric encryption (provide the private key; public key optional depending on use case)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yamlpyconfig-0.0.7.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yamlpyconfig-0.0.7-py3-none-any.whl (20.0 kB view details)

Uploaded Python 3

File details

Details for the file yamlpyconfig-0.0.7.tar.gz.

File metadata

  • Download URL: yamlpyconfig-0.0.7.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.8

File hashes

Hashes for yamlpyconfig-0.0.7.tar.gz
Algorithm Hash digest
SHA256 57e4237ff1672551f004bb10dcba4eeb947e12c4073173a4fedb8062c0643073
MD5 99a0aa7000c5b257c205dd0d10d0aa8e
BLAKE2b-256 86f8dcbf4f471b0f9b83f3ec9d260b1e7b0518cc3398c95f678e34c1e394be42

See more details on using hashes here.

File details

Details for the file yamlpyconfig-0.0.7-py3-none-any.whl.

File metadata

File hashes

Hashes for yamlpyconfig-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 a750a73f988db14930cd85dd9c946379be3b9ea08ccc5067424a4aa40b6a77f0
MD5 d0b7bb4043ca3c44f887bff1206d5cdc
BLAKE2b-256 67b1e450c0e4e4aa3e70c9d3a4ac24cbd7f578f860c9a548fe404daa345a865b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page