Skip to main content

Thread-safe singleton JSON/YAML configuration loader with JSON Schema validation

Project description

ConfigKit

Thread-safe singleton JSON/YAML configuration loader with JSON Schema validation for Python applications.

Features

  • JSON & YAML support - Load configuration from .json, .yaml, or .yml files
  • Thread-safe singleton - One instance per configuration class
  • JSON Schema validation - Draft 2020-12 support via jsonschema
  • Dot-notation access - config.get("database.host")
  • Runtime reload - Update configuration without restart
  • Type-safe - Full type hints with py.typed marker
  • Extensible - Custom validation via additional_checks()

Installation

From PyPI

pip install pyConfigKit

From Git repository

pip install git+https://github.com/miichoow/ConfigKit.git

Development mode

git clone https://github.com/miichoow/ConfigKit.git
cd ConfigKit
pip install -e ".[dev]"

Quick Start

1. Create your configuration class

from configkit import ConfigKit


class AppConfig(ConfigKit):
    def additional_checks(self) -> None:
        # Custom validation logic
        if self.data.get("debug") and self.data.get("env") == "production":
            raise ValueError("Debug mode not allowed in production")

    def get_database_url(self) -> str:
        host = self.get("database.host")
        port = self.get("database.port", default=5432)
        name = self.get("database.name")
        return f"postgresql://{host}:{port}/{name}"

2. Create configuration files

config.json

{
  "env": "development",
  "debug": true,
  "database": {
    "host": "localhost",
    "port": 5432,
    "name": "myapp"
  }
}

Or config.yaml

env: development
debug: true
database:
  host: localhost
  port: 5432
  name: myapp

schema.json (schema files are always JSON)

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "required": ["env", "database"],
  "properties": {
    "env": {
      "type": "string",
      "enum": ["development", "staging", "production"]
    },
    "debug": {
      "type": "boolean"
    },
    "database": {
      "type": "object",
      "required": ["host", "name"],
      "properties": {
        "host": { "type": "string" },
        "port": { "type": "integer", "minimum": 1, "maximum": 65535 },
        "name": { "type": "string" }
      }
    }
  }
}

3. Use in your application

# Initialize once at startup (JSON)
config = AppConfig(config_file="config.json", schema_file="schema.json")

# Or with YAML
config = AppConfig(config_file="config.yaml", schema_file="schema.json")

# Access anywhere - returns the same instance
config = AppConfig()
print(config.get_database_url())

API Reference

ConfigKit

Base class for configuration. Subclass and implement additional_checks().

Constructor

ConfigKit(*, config_file: str | Path, schema_file: str | Path)
  • config_file - Path to configuration file (.json, .yaml, or .yml)
  • schema_file - Path to JSON Schema file

Properties

Property Type Description
data dict[str, Any] Loaded configuration dictionary
schema dict[str, Any] Loaded JSON Schema dictionary

Methods

Method Description
get(path, *, default=None) Get value by dot-notation path
reload() Reload and re-validate from disk
additional_checks() Override for custom validation

ConfigKitMeta

Metaclass providing singleton behavior.

Method Description
reset() Clear all singleton instances (for testing)

Design Principles

  • Fail fast - Invalid configuration raises exceptions immediately
  • Single source of truth - One instance per configuration class
  • Explicit contracts - Required files on first instantiation
  • No magic - Clear, predictable behavior

Publishing

pip install -e ".[dev]"
python -m build
twine upload dist/*

License

MIT License - see LICENSE for details.

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

pyconfigkit-1.1.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

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

pyconfigkit-1.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file pyconfigkit-1.1.0.tar.gz.

File metadata

  • Download URL: pyconfigkit-1.1.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyconfigkit-1.1.0.tar.gz
Algorithm Hash digest
SHA256 287dd55970068a3f6b0ecd4921eacf06b910b56bffa378d0266e1b52e972fc1a
MD5 21a926717409f27f825e9b5c467dc971
BLAKE2b-256 ce196e4417d6f12b9ed8bfd96a4db3ee07c8083bee19c6d97f81a0cd97b3cbe7

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyconfigkit-1.1.0.tar.gz:

Publisher: python-publish.yml on miichoow/ConfigKit

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

File details

Details for the file pyconfigkit-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyconfigkit-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyconfigkit-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1881866412fcc89480a314d90c195a17c879188cf49ba7319e40e8c22fd77737
MD5 09e904fae6ef77c632e83edb82a31161
BLAKE2b-256 ab406f3d94751212cc54337545442e77c544581f3c2e156dfdece12813b3de8b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyconfigkit-1.1.0-py3-none-any.whl:

Publisher: python-publish.yml on miichoow/ConfigKit

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