Plaster
Plaster is a loader interface designed to abstract away multiple configuration file formats. It provides a common API for applications to load settings, supporting pluggable loaders discovered via entrypoints. The library focuses on providing a basic interface, leaving specific constraints and implementations to these pluggable loaders. The current version is 1.1.2, released in November 2022, and it is actively maintained by the Pylons Project.
Warnings
- breaking Version 1.1 of Plaster dropped support for older Python versions (2.7, 3.4, 3.5, 3.6). Projects on these Python versions must either upgrade their Python environment or pin Plaster to a version older than 1.1.
- breaking In version 1.1, Plaster switched from `setuptools`/`pkg_resources` to `importlib.metadata` for entry point discovery. While this generally improves performance and reduces runtime dependencies, it might impact advanced use cases that directly interact with entry point mechanisms previously relying on `pkg_resources`.
- gotcha `plaster.exceptions.MultipleLoadersFound` can be raised if multiple installed loaders match the criteria for a `config_uri`. This ambiguity prevents Plaster from choosing a specific loader.
- gotcha Prior to version 1.1.2, if a `config_uri` specified a distribution that lacked the expected entry points, Plaster could crash unexpectedly due to a bad unpacking of tuples. This is now caught and raises a `LoaderNotFound` exception.
- gotcha The `config_uri` parsing behavior for schemes changed in version 0.5. Schemes are now automatically generated as `file+<ext>` for file paths without an explicit scheme (e.g., `file+ini` instead of just `ini`). Also, the order for absolute lookups changed from `scheme+package` to `package+scheme`.
- gotcha Plaster's core functionality relies on external 'loader' plugins (e.g., `plaster_pastedeploy`, `plaster_yaml`) to support specific configuration file formats. Without the relevant plugin installed for a given `config_uri` scheme, Plaster will not be able to load the configuration.
Install
-
pip install plaster
Imports
- get_settings
from plaster import get_settings
- setup_logging
from plaster import setup_logging
- get_loader
from plaster import get_loader
- parse_uri
from plaster import parse_uri
Quickstart
import plaster
import sys
# Example config_uri. In a real app, this might come from command line args or an environment variable.
# For demonstration, we'll use a placeholder. Replace 'development.ini#myapp' with your actual config URI.
# You might need a loader like 'plaster_pastedeploy' installed for .ini files.
config_uri = 'development.ini#myapp'
try:
# Load settings for a specific section (e.g., 'myapp')
settings = plaster.get_settings(config_uri, section='myapp')
print("Loaded settings:", settings)
# Optionally configure logging based on the config file
plaster.setup_logging(config_uri)
print("Logging configured.")
# Example: Accessing a specific setting
if 'my_setting_key' in settings:
print(f"Value of 'my_setting_key': {settings['my_setting_key']}")
else:
print("No 'my_setting_key' found in settings.")
except plaster.LoaderNotFound:
print(f"Error: No loader found for URI '{config_uri}'. Make sure the appropriate plaster plugin (e.g., plaster_pastedeploy) is installed.")
except Exception as e:
print(f"An error occurred: {e}")