Skip to main content

Thread-safe singleton JSON configuration loader with JSON Schema validation

Project description

ConfigKit

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

Features

  • 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"
  }
}

schema.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
config = AppConfig(json_file="config.json", 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(*, json_file: str | Path, schema_file: str | Path)
  • json_file - Path to JSON configuration file
  • 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.0.0.tar.gz (5.8 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.0.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pyconfigkit-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7c195625f1098b94b6961433d61698cb592efc361823eb1625fb5c170b69dc3b
MD5 8ef31de6c02e3ccfbf1357bb9567496f
BLAKE2b-256 8fae754778940b1d8c408b9aece05a68acd9193fdba9c9c466812cf927b2cc86

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyconfigkit-1.0.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.0.0-py3-none-any.whl.

File metadata

  • Download URL: pyconfigkit-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 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.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c72c5804efbf7d8d69f6f4834bd0e92e2e9db065ef4cb82abde95f2248e2243d
MD5 bffb6e32d295aa5d39dce7bd64082e5e
BLAKE2b-256 2b26872880916da8af8bb6f1b1b50ff74d34c0a2800f36bf25e12da11a3516bc

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyconfigkit-1.0.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