Skip to main content

A Python script that generates a configuration file from a YAML template, dynamically creating corresponding Python classes and methods for easy configuration management.

Project description

ConfigCreator

A Python script that generates a configuration file from a YAML template, dynamically creating corresponding Python classes and methods for easy configuration management.

GitHub, PyPi

Installation

Use the package manager pip to install ConfigCreator.

pip install pyyaml
pip install ct-configcreator

Simple usage

from ct_configcreator import createConfig

# Set the configuration file to load
template = "example_config.yaml"

# Set the output file to create. (Optional. Default = 'configuration.py')
output_file = "example_configuration.py"

# Create the python configuration file.
createConfig(template=template, output_file=output_file)

Overview

This Python script provides functionality to generate a configuration file in Python from a YAML template. The script reads a YAML configuration file, dynamically creates corresponding Python classes, and generates a `` file with these classes, including methods for loading and initializing configuration values.

Features:

  • Parses YAML configuration files.
  • Dynamically generates Python classes based on the YAML structure.
  • Creates a configuration.py file with generated classes and methods for loading YAML files.
  • Includes logging for debugging and tracking the configuration generation process.

Usage Example:

1. Prepare your YAML configuration file (e.g., example_config.yaml):

database:
  user:
    username: root
    password: password
  host: localhost
  port: 3306
logging:
  level: DEBUG
  file: app.log

cat1:
  sub1:
    sub2:
      sub3:
        sub4:
          sub5:
            sub6: Sub category 6 test

Notice you can use as many sub categories as you want.

2. Run the script:

from ct_configcreator import createConfig

# Set the configuration file to load
template = "example_config.yaml"

# Set the output file to create. (Optional. Default = 'configuration.py')
output_file = "example_configuration.py"

# Create the python configuration file.
createConfig(template=template, output_file=output_file)

3. The 'example_config.yaml' will look like this:

import yaml
import logging

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('Configuration')

def load_yaml(config_file):
    with open(config_file, 'r') as file:
        logging.info(f'Loaded {config_file} into config.')
        return yaml.safe_load(file)

class Config:
    def __init__(self, config_file):
        config = load_yaml(config_file)
        logging.debug("Initializing Config with config file")
        self.database = self.Database(config.get("database", {}))
        logging.debug("Initialized Config.database with nested config")
        self.logging = self.Logging(config.get("logging", {}))
        logging.debug("Initialized Config.logging with nested config")
        self.cat1 = self.Cat1(config.get("cat1", {}))
        logging.debug("Initialized Config.cat1 with nested config")
    __module__ = 'ct_configcreator.configcreator'
    class Database:
        def __init__(self, config):
            logging.debug("Initializing Database")
            self.user = self.User(config.get("user", {}))
            logging.debug("Initialized Database.user with nested config")
            self.host = config.get("host", 'localhost')
            logging.debug("Set Database.host to 'localhost'")
            self.port = config.get("port", 3306)
            logging.debug("Set Database.port to 3306")
        __module__ = 'ct_configcreator.configcreator'
        class User:
            def __init__(self, config):
                logging.debug("Initializing User")
                self.username = config.get("username", 'root')
                logging.debug("Set User.username to 'root'")
                self.password = config.get("password", 'password')
                logging.debug("Set User.password to 'password'")
            __module__ = 'ct_configcreator.configcreator'
            username = 'root'
            password = 'password'
            __doc__ = None
        host = 'localhost'
        port = 3306
        __doc__ = None
    class Logging:
        def __init__(self, config):
            logging.debug("Initializing Logging")
            self.level = config.get("level", 'DEBUG')
            logging.debug("Set Logging.level to 'DEBUG'")
            self.file = config.get("file", 'app.log')
            logging.debug("Set Logging.file to 'app.log'")
        __module__ = 'ct_configcreator.configcreator'
        level = 'DEBUG'
        file = 'app.log'
        __doc__ = None
    class Cat1:
        def __init__(self, config):
            logging.debug("Initializing Cat1")
            self.sub1 = self.Sub1(config.get("sub1", {}))
            logging.debug("Initialized Cat1.sub1 with nested config")
        __module__ = 'ct_configcreator.configcreator'
        class Sub1:
            def __init__(self, config):
                logging.debug("Initializing Sub1")
                self.sub2 = self.Sub2(config.get("sub2", {}))
                logging.debug("Initialized Sub1.sub2 with nested config")
            __module__ = 'ct_configcreator.configcreator'
            class Sub2:
                def __init__(self, config):
                    logging.debug("Initializing Sub2")
                    self.sub3 = self.Sub3(config.get("sub3", {}))
                    logging.debug("Initialized Sub2.sub3 with nested config")
                __module__ = 'ct_configcreator.configcreator'
                class Sub3:
                    def __init__(self, config):
                        logging.debug("Initializing Sub3")
                        self.sub4 = self.Sub4(config.get("sub4", {}))
                        logging.debug("Initialized Sub3.sub4 with nested config")
                    __module__ = 'ct_configcreator.configcreator'
                    class Sub4:
                        def __init__(self, config):
                            logging.debug("Initializing Sub4")
                            self.sub5 = self.Sub5(config.get("sub5", {}))
                            logging.debug("Initialized Sub4.sub5 with nested config")
                        __module__ = 'ct_configcreator.configcreator'
                        class Sub5:
                            def __init__(self, config):
                                logging.debug("Initializing Sub5")
                                self.sub6 = config.get("sub6", 'Sub category 6 test')
                                logging.debug("Set Sub5.sub6 to 'Sub category 6 test'")
                            __module__ = 'ct_configcreator.configcreator'
                            sub6 = 'Sub category 6 test'
                            __doc__ = None
                        __doc__ = None
                    __doc__ = None
                __doc__ = None
            __doc__ = None
        __doc__ = None
    __doc__ = None

4. Use the configuration in your main code:

from example_configuration import Config

# Initialise the config file and load the newest values from the file
config = Config("example_config.yaml")

# Access the config values examples
print(config.database.user.password)
print(config.logging.file)
print(config.cat1.sub1.sub2.sub3.sub4.sub5.sub6)

The initialization will always read the latest values from the config file, so the original values in the config file when configuration.py was created do not matter.

5. This example will output the following:

>>> password
>>> app.log
>>> Sub category 6 test

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub. You can also suggest a new sensor by opening an issue.

Support my work

If you like my work you can always buy me a coffee!

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

ct_configcreator-0.1.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

ct_configcreator-0.1.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file ct_configcreator-0.1.0.tar.gz.

File metadata

  • Download URL: ct_configcreator-0.1.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.9

File hashes

Hashes for ct_configcreator-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cede38094d3e368f13d1c3a01aaf8e14adba094982a3f64a54b40d8470e4adc6
MD5 ae2f71d8e23b8136c91c2c1413b18fc3
BLAKE2b-256 5a0faa239f3a86eadb93299ac4e0fad833ee59abacc52c7dad651b1a83fdf8a4

See more details on using hashes here.

File details

Details for the file ct_configcreator-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ct_configcreator-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 07a63fdf7b19acd722f956cc23e7460000c294dccd91f86e467c188d12eb04dc
MD5 cb516a9f7708dd82ea4d88646a43a5cb
BLAKE2b-256 f5f9e7e0971a64a8aeaebd2d5683ce89b920ef6288d946d2a52b1d96f6e9843b

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