Skip to main content

Simple TOML Configurator

PyPI version PyPI - License PyPI - Python Version PyPI - Wheel Build

The Simple TOML Configurator is a versatile Python library designed to streamline the handling and organization of configuration settings across various types of applications. This library facilitates the management of configuration values through a user-friendly interface and stores these settings in TOML file format for easy readability and accessibility.

Documentation

https://gilbn.github.io/Simple-TOML-Configurator/

Features

  1. Effortless Configuration Management: The heart of the library is the Configuration class, which simplifies the management of configuration settings. It provides intuitive methods to load, update, and store configurations, ensuring a smooth experience for developers.

  2. Universal Applicability: The Simple TOML Configurator is designed to work seamlessly with any type of Python application, ranging from web frameworks like Flask, Django, and FastAPI to standalone scripts and command-line utilities.

  3. TOML File Storage: Configuration settings are stored in TOML files, a popular human-readable format. This enables developers to review, modify, and track configuration changes easily.

  4. Attribute-Based Access: Accessing configuration values is straightforward, thanks to the attribute-based approach. Settings can be accessed and updated as attributes, making it convenient for both reading and modifying values.

  5. Environment Variable Support: Configuration values are automatically set as environment variables, making it easier to use the configuration values in your application. Environment variable are set as uppercase. e.g. APP_HOST and APP_PORT or PROJECT_APP_HOST and PROJECT_APP_PORT if env_prefix is set to "project". This also works for nested values. ex: TABLE_KEY_LEVEL1_KEY_LEVEL2_KEY. This works for any level of nesting.Environment variables set before the configuration is loaded will not be overwritten, but instead will overwrite the existing config value.

  6. Default Values: Developers can define default values for various configuration sections and keys. The library automatically incorporates new values and manages the removal of outdated ones.

  7. Customization Capabilities: The Configuration class can be extended and customized to cater to application-specific requirements. Developers can implement custom logic with getters and setters to handle unique settings or scenarios.

Installation

Install with

pip install simple-toml-configurator

Usage Example

See Usage for more examples.

import os
from simple_toml_configurator import Configuration

# Define default configuration values
default_config = {
    "app": {
        "ip": "0.0.0.0",
        "host": "",
        "port": 5000,
        "upload_folder": "uploads",
    },
    "mysql": {
        "user": "root",
        "password": "root",
        "databases": {
            "prod": "db1",
            "dev": "db2",
            },
    }
}

# Set environment variables
os.environ["PROJECT_APP_UPLOAD_FOLDER"] = "overridden_uploads"

# Initialize the Simple TOML Configurator
settings = Configuration(config_path="config", defaults=default_config, config_file_name="app_config", env_prefix="project")
# Creates an `app_config.toml` file in the `config` folder at the current working directory.

# Access and update configuration values
print(settings.app.ip)  # Output: '0.0.0.0'
settings.app.ip = "1.2.3.4"
settings.update()
print(settings.app_ip)  # Output: '1.2.3.4'

# Access nested configuration values
print(settings.mysql.databases.prod)  # Output: 'db1'
settings.mysql.databases.prod = 'new_value'
settings.update()
print(settings.mysql.databases.prod)  # Output: 'new_value'

# Access and update configuration values
print(settings.app_ip)  # Output: '1.2.3.4'
settings.update_config({"app_ip": "1.1.1.1"})
print(settings.app_ip)  # Output: '1.1.1.1'

# Access all settings as a dictionary
all_settings = settings.get_settings()
print(all_settings)
# Output: {'app_ip': '1.1.1.1', 'app_host': '', 'app_port': 5000, 'app_upload_folder': 'overridden_uploads', 'mysql_user': 'root', 'mysql_password': 'root', 'mysql_databases': {'prod': 'new_value', 'dev': 'db2'}}

# Modify values directly in the config dictionary
settings.config["mysql"]["databases"]["prod"] = "db3"
settings.update()
print(settings.mysql_databases["prod"])  # Output: 'db3'

# Access environment variables
print(os.environ["PROJECT_MYSQL_DATABASES_PROD"])  # Output: 'db3'
print(os.environ["PROJECT_APP_UPLOAD_FOLDER"])  # Output: 'overridden_uploads'

app_config.toml contents

[app]
ip = "1.1.1.1"
host = ""
port = 5000
upload_folder = "overridden_uploads"

[mysql]
user = "root"
password = "root"

[mysql.databases]
prod = "db3"
dev = "db2"

Release files for Simple-TOML-Configurator 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Simple-TOML-Configurator 1.3.0
File Size Uploaded
simple_toml_configurator-1.3.0.tar.gz 10.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Simple-TOML-Configurator 1.3.0
File Interpreter ABI Platform
Simple_TOML_Configurator-1.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.9 kB

Release files / simple_toml_configurator-1.3.0.tar.gz

Download URL simple_toml_configurator-1.3.0.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
2b783c92a46c0f857666b9e7505c91d808a48b31043d2aa45680355f06bbbc5a
BLAKE2b-256 checksum
How to use checksums
d40771d1fe8ba5bb1a2b5aa4092104dea0a088cae0696da351a47a0020e78ada
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.4

Release files / Simple_TOML_Configurator-1.3.0-py3-none-any.whl

Download URL Simple_TOML_Configurator-1.3.0-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d8aeaf6db7b11be09122d9eec882790f5fed1b51896d21327cb8f6cd72006d7b
BLAKE2b-256 checksum
How to use checksums
88487978ca499336d98326369b47b2c3a3b3f7ea1358f2b63428d1ead41850c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.4

Release history Release notifications | RSS feed

This release

1.3.0 This release

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page