Setoptconf-tmp
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
- breaking This library is a temporary, unmaintained fork. It was created solely to address a specific compatibility issue with `setuptools >= 58` affecting the original `setoptconf` for `prospector` builds. Users should be aware that it will not receive updates, may contain unpatched vulnerabilities, and is explicitly stated to be purged once its original purpose is fulfilled.
- gotcha The primary `setoptconf` library, which this package forks, historically had issues with `setuptools >= 58`. While `setoptconf-tmp` was created to mitigate this for specific build environments, it highlights a potential underlying fragility or dependency on older Python ecosystem components in the original project design.
Install
-
pip install setoptconf-tmp
Imports
- ConfigurationManager
from setoptconf import ConfigurationManager
- StringSetting
from setoptconf import StringSetting
- IntegerSetting
from setoptconf import IntegerSetting
- BooleanSetting
from setoptconf import BooleanSetting
- CommandLineSource
from setoptconf import CommandLineSource
- EnvironmentVariableSource
from setoptconf import EnvironmentVariableSource
- ConfigFileSource
from setoptconf import ConfigFileSource
Quickstart
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')