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
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.2.tar.gz (44.6 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.2-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: gpconfig-0.3.2.tar.gz
  • Upload date:
  • Size: 44.6 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.2.tar.gz
Algorithm Hash digest
SHA256 67bb01c08376273194217614f59c97b3317e6036ee3cbcc1f13af39db2bd1ffd
MD5 e8c2c832ce299ac555183a3a5e621a9a
BLAKE2b-256 24169569d3430194835984b047df6195e6d09a0e1ccc4b8afe5c8a7e607110ef

See more details on using hashes here.

Provenance

The following attestation bundles were made for gpconfig-0.3.2.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.2-py3-none-any.whl.

File metadata

  • Download URL: gpconfig-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 12.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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f0e0ce10e97c33445a2d33a71ce87fc805d320b21b7a89754c67018c11b6a12e
MD5 f7c4be999c7be83ae75764042b31733a
BLAKE2b-256 d0aaa8e5f5db10b32002f6888ecf231b4128c4970a0a006c7d7ea0693a5e0669

See more details on using hashes here.

Provenance

The following attestation bundles were made for gpconfig-0.3.2-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