Maison

2.0.2 · active · verified Sun Apr 12

Maison is a lightweight Python library designed to simplify reading settings from various configuration files (TOML, YAML, JSON). It automatically searches common system and user locations for configuration, providing a unified access interface. Current version is 2.0.2, with a moderate release cadence, focusing on stability and ease of use.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple TOML configuration file and load its settings using `maison.Config`. It shows accessing nested keys with dot notation and providing default values for missing keys.

import os
from maison import Config

# Create a temporary config file for the example
app_name = "my_app_quickstart"
config_file_name = f"{app_name}.toml"
config_content = """
[general]
api_key = "sk_example_123"
debug_mode = true
environment = "development"

[database]
host = "localhost"
port = 5432
username = "admin"
"""

# Place the config file in the current working directory
# where maison will look for it by default for the given name.
with open(config_file_name, "w") as f:
    f.write(config_content)

# Initialize Config. It will look for '{app_name}.toml' in common locations.
# Since we created it in CWD, it will find it.
config = Config(name=app_name)

print(f"--- Settings for '{app_name}' ---")
print(f"API Key: {config.get('general.api_key', 'N/A')}")
print(f"Debug Mode: {config.get('general.debug_mode', False)}")
print(f"Environment: {config.get('general.environment', 'production')}")
print(f"Database Host: {config.get('database.host', '127.0.0.1')}")
print(f"Database Port: {config.get('database.port', 3306)}")
print(f"Non-existent setting (with default): {config.get('general.timeout', 30)}")
print(f"Non-existent setting (no default): {config.get('general.unknown')}")

# Clean up the created config file
os.remove(config_file_name)

view raw JSON →