Skip to main content

A type-safe, YAML-based configuration management library built on Pydantic

Project description

gpconfig

简体中文 | English

General Purpose Configuration management library for Python projects.

A type-safe, YAML-based configuration management library built on Pydantic.

Features

  • Type-safe configuration - Built on Pydantic with full type validation
  • YAML-based - Human-readable configuration files
  • Nested configs - Organize configs in directories (e.g., llm/openai.yaml)
  • Auto-detection - Automatically detect config classes from YAML files
  • Configurable objects - Create object instances directly from configs
  • Environment variable support - Configure paths via environment variables
  • Readonly configs - Protect sensitive configurations

Installation

pip install gpconfig

Quick Start

1. Define Config Classes

from typing import ClassVar
from gpconfig import GPConfig

class DatabaseConfig(GPConfig):
    cfg_class_name: ClassVar[str] = "DatabaseConfig"
    host: str
    port: int = 5432
    username: str
    password: str
    database: str

2. Create Config Folder

myapp/
├── global_env.yaml    # Required: global environment config
├── database.yaml      # Your config files
└── llm/               # Nested configs
    ├── openai.yaml
    └── anthropic.yaml

global_env.yaml:

version: "1.0.0"
debug: true
log_level: INFO

database.yaml:

cfg_class_name: "DatabaseConfig"
host: localhost
port: 5432
username: admin
password: secret
database: myapp

3. Initialize Manager and Load Configs

from gpconfig import GPConfigManager

# Config folder search order:
# 1. Explicit cfg_folder parameter
# 2. Environment variable: {PROJECT_NAME}_CFG_PATH
# 3. User directory: ~/.{project_name}/
manager = GPConfigManager("myapp", cfg_folder="/path/to/myapp")

# Read global_env values
debug = manager.get_config("global_env.debug")

# Load config (auto-detect class by cfg_class_name)
db_config = manager.get_config("database")

# Load nested config
llm_config = manager.get_config("llm.openai")

# Read specific field
host = manager.get_config("database.host")

4. Create Configurable Objects

from gpconfig import GPConfigurable

class Database(GPConfigurable):
    def __init__(self, config: DatabaseConfig) -> None:
        super().__init__(config)
        self.host = config.host
        self.port = config.port
        self.username = config.username
        self.password = config.password
        self.database = config.database

    @property
    def connection_string(self) -> str:
        return f"postgresql://{self.username}:{self.password}@{self.host}:{self.port}/{self.database}"

# Register classes
GPConfigManager.register_config_class(DatabaseConfig)
GPConfigManager.register_configurable_class(Database)

# Create object instance
db = manager.get_object("database")
print(db.connection_string)

Note: Add configured_class_name to your YAML for get_object():

cfg_class_name: "DatabaseConfig"
configured_class_name: "Database"
host: localhost
port: 5432

5. Save Configs

# Modify and save
db_config.port = 5433
db_config.save()

# Save to new location
manager.save(db_config, "backups/database_backup")

Core Components

Component Description
GPConfig Base class for all config classes
GPConfigurable Base class for objects created from configs
GPConfigManager Manages config folder, loading, and object creation

Exceptions

Exception Description
GPConfigError Base exception for all gpconfig errors
ConfigFolderError Config folder not found or invalid
ConfigNotFoundError Requested config path does not exist
IllegalPathError Config path is malformed or escapes cfg_folder
ConfigReadonlyError Attempted to modify readonly config
RegistrationError Class registration issues
ConfigValidationError Config file validation failed

API Reference

See the API documentation for detailed usage.

License

MIT

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

gpconfig-0.3.3.tar.gz (69.4 kB view details)

Uploaded Source

Built Distribution

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

gpconfig-0.3.3-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

Details for the file gpconfig-0.3.3.tar.gz.

File metadata

  • Download URL: gpconfig-0.3.3.tar.gz
  • Upload date:
  • Size: 69.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for gpconfig-0.3.3.tar.gz
Algorithm Hash digest
SHA256 ed5d1d94efaf5ec8e62e9ec6b129ddef74b1c0c517422c30033f8980154d8a66
MD5 90fb23d403632853545724299b768b85
BLAKE2b-256 72896eeba457747f1c6b5fb0f697c54e1444068f6244b7e8385f869dd511591b

See more details on using hashes here.

Provenance

The following attestation bundles were made for gpconfig-0.3.3.tar.gz:

Publisher: python-publish.yml on LinnetCodes/gpconfig

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gpconfig-0.3.3-py3-none-any.whl.

File metadata

  • Download URL: gpconfig-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for gpconfig-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6dcc2dff1581f7b058f4c85e7b72366a6888dfbe4b31097a80d36c188420c88a
MD5 8152c211b57a7ca29151735d0e95604e
BLAKE2b-256 2cea69765af623cddc8d498921085613abe68ca006597c1544370957e552ae57

See more details on using hashes here.

Provenance

The following attestation bundles were made for gpconfig-0.3.3-py3-none-any.whl:

Publisher: python-publish.yml on LinnetCodes/gpconfig

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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