Maison
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
- breaking The `Config.default` method was removed in version 2.0.0. It was previously used to retrieve a setting with a fallback value.
- breaking The `Config` class initializer's `project_name` argument was renamed to `name` in version 2.0.0. This argument specifies the base name for configuration files.
- gotcha Maison searches for configuration files in a predefined order (script directory, current working directory, XDG config dir, /etc/). If multiple files with the same name exist in these locations, the one found first will be loaded, which might lead to unexpected settings being used.
- gotcha If `maison` does not find any configuration file for the given `name` in its search paths, the `Config` object will be initialized but will contain no settings. Accessing keys will return `None` (or your provided default) without raising an error.
Install
-
pip install maison
Imports
- Config
from maison import Config
Quickstart
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)