Skip to main content

A configuration builder package

Project description

Configuration Builder Package

This package provides a flexible and extensible way to load and validate configuration data from JSON files or dictionaries. It supports the Builder design pattern to allow customization and extension of configuration properties.

Installation

Install the package using pip:

pip install locobuzz-python-configuration

Usage

Basic Usage

To create a configuration object use the create_configuration function. You can load configuration data from a JSON file or dictionary.

from locobuzz_python_configuration import create_configuration

config = create_configuration(file_path='settings.json', required_components=["sql", "clickhouse", "aws", "elastic"])
print(config.__dict__)

Extending Configuration

You can extend the existing builder classes to add new properties. Here's how to extend the SQL configuration:

Step 1: Create an Extended Builder Class Create a new file named extended_sql_config_builder.py and define the extended builder class:

from locobuzz_python_configuration.sql_config_builder import SQLConfigurationBuilder, InvalidSQLConfigurationError

class ExtendedSQLConfigurationBuilder(SQLConfigurationBuilder):
    def __init__(self, config):
        super().__init__(config)
        self._sql_database_name = None

    def build(self, data):
        super().build(data)
        self._sql_database_name = data.get('sql_database_name')
        if not self._sql_database_name:
            raise InvalidSQLConfigurationError("Missing SQL database name configuration property")
        self._config._sql_database_name = self._sql_database_name

Step 2: Use the New Builder Class Update your main script to use the new builder class:

from locobuzz_python_configuration import create_configuration
from new_builder_class import NewBuilderClass

custom_builders = {
    'new_builder': NewBuilderClass
}

config = create_configuration(file_path='settings.json', required_components=["sql", "clickhouse", "aws", "elastic", "new_builder"], builder_classes=custom_builders)
print(config.__dict__)

Handling Errors

The package includes specific error classes for different components. Here's how to handle errors:

from locobuzz_python_configuration import create_configuration, InvalidConfigurationError
from extended_sql_config_builder import ExtendedSQLConfigurationBuilder

try:
    custom_builders = {
        'sql': ExtendedSQLConfigurationBuilder
    }

    config = create_configuration(file_path='settings.json', required_components=["sql", "clickhouse", "aws", "elastic"], builder_classes=custom_builders)
    if config:
        print("Configuration loaded successfully:", config.__dict__)
except InvalidConfigurationError as e:
    print(f"Error loading configuration: {e}")

Customizing Validation

If you need to customize the validation process for a specific component, extend the validation logic in your builder class:

from locobuzz_python_configuration.sql_config_builder import SQLConfigurationBuilder, InvalidSQLConfigurationError

class CustomSQLConfigurationBuilder(SQLConfigurationBuilder):
    def __init__(self, config):
        super().__init__(config)

    def validate(self, data):
        super().validate(data)
        if not data.get('sql_custom_property'):
            raise InvalidSQLConfigurationError("Missing SQL custom property")

    def build(self, data):
        self.validate(data)
        super().build(data)
        self._config._sql_custom_property = data.get('sql_custom_property')

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

locobuzz_python_configuration-0.1.13.tar.gz (847.0 kB view details)

Uploaded Source

Built Distribution

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

locobuzz_python_configuration-0.1.13-cp311-cp311-win_amd64.whl (440.9 kB view details)

Uploaded CPython 3.11Windows x86-64

File details

Details for the file locobuzz_python_configuration-0.1.13.tar.gz.

File metadata

File hashes

Hashes for locobuzz_python_configuration-0.1.13.tar.gz
Algorithm Hash digest
SHA256 40cdeb9c8ed812b674d590c0a81cd88c0d22556c7ae5f54cd29b65279647a9d2
MD5 361aa0f7f802a40f47f2c4f04d920bbb
BLAKE2b-256 c30e8de8d162632bd55f30e005f62e9561dfed6cd5925a9b555f0e6d56efb881

See more details on using hashes here.

File details

Details for the file locobuzz_python_configuration-0.1.13-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for locobuzz_python_configuration-0.1.13-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 362f5400b63c266e037403b3f68acb952d75ae6c163e58bdf4394b1fc17c66fb
MD5 0609966381a22d8c6e2059b2d2ae66fa
BLAKE2b-256 c45ae1e7f6f6110d9fd320a742eddd5244ab92e9a16443ced05fee454cb5897e

See more details on using hashes here.

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