Setoptconf-tmp

0.3.1 · abandoned · verified Wed Apr 15

Setoptconf-tmp is a temporary fork of the `setoptconf` library, designed to retrieve program settings from various sources like the command line, environment variables, INI files, JSON files, YAML files, and Python objects/modules. This fork was created to resolve compatibility issues with `setuptools >= 58` that affected the original `setoptconf` library, particularly for tools like `prospector`. The library is explicitly stated as a temporary solution, not actively maintained, and is expected to be purged once `prospector` updates or the original `setoptconf` is fixed. The current version is 0.3.1, last released in September 2021.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define settings and retrieve them from multiple sources. It prioritizes command line arguments, then environment variables (prefixed with `MYPROGRAM_`), and finally INI configuration files. It also shows how to define different setting types and provide defaults or mark them as required.

import setoptconf as soc
import os

# Simulate environment variables
os.environ['MYPROGRAM_API_KEY'] = os.environ.get('MYPROGRAM_API_KEY', 'default_api_key_from_env')
os.environ['MYPROGRAM_DEBUG'] = os.environ.get('MYPROGRAM_DEBUG', 'True')

# Create a dummy config file for demonstration
with open('.myprogramrc', 'w') as f:
    f.write('[myprogram]\n')
    f.write('setting_from_file = 123\n')

manager = soc.ConfigurationManager('myprogram')

manager.add(soc.StringSetting('api_key', default='no_key_found'))
manager.add(soc.IntegerSetting('setting_from_file', required=False))
manager.add(soc.BooleanSetting('debug', default=False))
manager.add(soc.StringSetting('app_name', default='MyApp'))

# The order of sources implies priority (later sources override earlier ones)
config = manager.retrieve(
    soc.CommandLineSource, # Pulls from command line (e.g., --api-key <value>)
    soc.EnvironmentVariableSource, # Pulls from environment variables prefixed with MYPROGRAM_
    soc.ConfigFileSource(('.myprogramrc', '/etc/myprogram.conf')) # Pulls from INI files
)

print(f"API Key: {config.api_key}")
print(f"Setting from file: {config.setting_from_file}")
print(f"Debug mode: {config.debug}")
print(f"App Name: {config.app_name}")

# Clean up dummy config file
os.remove('.myprogramrc')

view raw JSON →