Skip to main content

A configuration management library

Project description

catenaconf logo

CatenaConf

PyPI - Format build passing License

Introduction

CatenaConf is a lightweight Python library designed for managing and operating configurations. It extends the Python dictionary type to manage configurations using key-value pairs and provides flexible operation functionalities.

Features

  • Lightweight: The code size is small and no third-party dependencies need to be installed (if you need to create a configuration from a Pydantic model or a yaml file, you need to install the corresponding dependencies).
  • Dictionary based: Use Python dictionaries to create and manage configurations.
  • Attribute access: Access and modify configuration values ​​through attributes, which is convenient and intuitive.
  • Flexible update mechanism: Provides flexible update function and supports merging dictionaries.
  • Reference resolution: Supports referencing other configuration values ​​in configuration values ​​and being able to resolve these references.

Installation

Install using pip:

pip install catenaconf

Usage

Creating Configuration

Create from dictionary

Catenaconf.create(config)

Description: Create a KvConfig instance (a built-in type of the library) from a dictionary.

Parameters:

  • config (dict): A dictionary containing the configuration data.

Returns:

  • Returns a KvConfig object created from the dictionary.

Usage:

from catenaconf import Catenaconf

config = {
    "database": {
        "host": "localhost",
        "port": 3306
    }
}

cfg = Catenaconf.create(config)

Load from file

Catenaconf.load(file)

Description: Load a KvConfig instance from a file. Supports JSON, YAML, and XML formats.

Parameters:

  • file (str | pathlib.Path): Path to the configuration file.

Returns:

  • Returns a KvConfig object created from the loaded data.

Usage:

cfg = Catenaconf.load("config.json")

Create from Pydantic Model

Catenaconf.structured(model)

Description: Creates a KvConfig instance from a Pydantic model.

Parameters:

  • model (pydantic.BaseModel): A Pydantic model object to construct the configuration.

Returns:

  • A KvConfig object containing the structured configuration.

Usage:

from pydantic import BaseModel

class MyModel(BaseModel):
    field: str
cfg = Catenaconf.structured(MyModel(field="value"))

Selecting Configuration Values

Catenaconf.select(cfg, key, *, default="NOT FOUND", throw_on_resolution_failure=True, throw_on_missing=False)

Description: Selects a value from the configuration by key, with options for default values and error handling.

Parameters:

  • cfg (KvConfig): The configuration instance to select from.
  • key (str): The key to locate within the configuration.
  • default (Any, optional): The default value to return if the key is not found. Defaults to "NOT FOUND".
  • throw_on_resolution_failure (bool, optional): Whether to raise an error if key resolution fails. Defaults to True.
  • throw_on_missing (bool, optional): Whether to raise an error for missing keys. Defaults to False.

Returns:

  • The selected value, or the default value if the key is not found.

Usage:

value = Catenaconf.select(cfg, "database.user", default=None, throw_on_resolution_failure=False)

Updating Configuration

Catenaconf.update(cfg, key, value=None, *, merge=True)

Description: Updates the value of a specified key in the configuration.

Parameters:

  • cfg (KvConfig): The configuration instance to update.
  • key (str): The location of the value to be updated, specified as a dotted string.
  • value (Any, optional): The new value to set.
  • merge (bool, optional): Whether to merge dictionaries. Defaults to True.

Usage:

Catenaconf.update(cfg, "database.user", "root")
Catenaconf.update(cfg, "database", {"root": "root"})
Catenaconf.update(cfg, "database", {"root": "root"}, merge=True)

Notes:

  • If merge=True, existing dictionaries are merged with the new value.

  • If merge=False, the new value replaces the existing one.

Merging Configurations

Catenaconf.merge(*configs)

Description: Merges multiple configurations into one.

Parameters:

  • *configs (KvConfig or dict): The configurations to merge, passed as positional arguments.

Returns:

  • A merged KvConfig instance.

Usage:

config1 = {"database": {"host": "localhost"}}
config2 = {"database": {"port": 3306}}

merged_cfg = Catenaconf.merge(config1, config2)

References and Resolving References

Catenaconf.resolve(cfg)

Description: Resolves all references in the configuration. References are defined with the @{} format.

Parameters:

  • cfg (KvConfig): The configuration instance containing the references.

Usage:

config = {
    "info": {
        "path": "/data",
        "filename": "a.txt"
    },
    "backup_path": "@{info.path}/backup/@{info.filename}"
}

cfg = Catenaconf.create(config)
Catenaconf.resolve(cfg)

Notes:

  • Resolves references by replacing placeholders with their actual values.

Converting to Dictionary

Catenaconf.to_container(cfg, resolve=True)

Description: Converts a KvConfig instance into a standard dictionary.

Parameters:

  • cfg (KvConfig): The configuration instance to convert.
  • resolve (bool, optional): Whether to resolve references in the dictionary. Defaults to True.

Returns:

  • A standard dictionary containing the configuration data.

Usage:

dict_config = Catenaconf.to_container(cfg, resolve=True)
dict_config = Catenaconf.to_container(cfg, resolve=False)

Notes:

  • When resolve=True, all references in the configuration are resolved.
  • When resolve=False, references remain unresolved.

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

catenaconf-0.1.4.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

catenaconf-0.1.4-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file catenaconf-0.1.4.tar.gz.

File metadata

  • Download URL: catenaconf-0.1.4.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for catenaconf-0.1.4.tar.gz
Algorithm Hash digest
SHA256 2a2396de512aa7205550f0a0f6ec914821d0e47f5d540706814b24e51afa92a1
MD5 872415cf3a1b685c58338558d8886fc9
BLAKE2b-256 de73e98b0b105b9ae7ca7b0869b041d3c81bc4a7409949e466438749176970fe

See more details on using hashes here.

File details

Details for the file catenaconf-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: catenaconf-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for catenaconf-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6bd5ecb28a9f7d7cde9ceb319c4666f0f989c408019b168d5b8e60026da0749e
MD5 c02fa882f0987a271311d3cf57ac0e7e
BLAKE2b-256 14d52bbdc1c3bc2b845c8e83bf19d496930437310f1e8652fc67ff4afc4a0ed7

See more details on using hashes here.

Supported by

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