PasteDeploy

3.1.0 · active · verified Fri Apr 10

PasteDeploy is a system for loading, configuring, and composing WSGI applications and servers from configuration files (typically INI files). It provides a standardized way to define and chain middleware, applications, and servers for Python web projects. The current version is 3.1.0, with ongoing maintenance for compatibility and minor enhancements, typically released yearly or as needed.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a WSGI application factory, configure it in an `INI` file, and load it using `paste.deploy.loadapp`. It also shows how to pass `global_conf` values and use `eval()` to pull environment variables into the configuration, which is a common pattern for sensitive values (though with security caveats as noted in warnings). Running this code will create a temporary `config.ini`, load a dummy WSGI app, print its configuration, and then clean up.

import os
import sys
from io import StringIO
from paste.deploy import loadapp

# --- 1. Create a dummy config.ini for demonstration ---
# This config uses 'call:__main__:create_app' to reference an app factory
# defined directly in this script. It also shows `eval` for env vars.
config_content = """
[app:main]
use = call:__main__:create_app
some_setting = hello
another_setting = world
my_env_var = eval(os.environ.get('QUICKSTART_ENV_VAR', 'default_env_val'))

[server:main]
use = egg:waitress#main  # Waitress is a common WSGI server
host = 127.0.0.1
port = 8080
"""

with open("config.ini", "w") as f:
    f.write(config_content)

# --- 2. Define the WSGI application factory ---
# This factory function will be called by PasteDeploy to create the app.
# It receives `global_conf` and other settings from the INI file.
def create_app(global_conf, some_setting, another_setting, my_env_var):
    # Using wsgiref.simple_server.demo_app as a placeholder WSGI app
    from wsgiref.simple_server import demo_app
    print(f"[create_app] App factory called with global_conf: {global_conf}")
    print(f"[create_app]   some_setting: {some_setting}")
    print(f"[create_app]   another_setting: {another_setting}")
    print(f"[create_app]   my_env_var (from os.environ via eval): {my_env_var}")
    return demo_app

# --- 3. Load the application using paste.deploy ---
print("\n--- Attempting to load WSGI application from config.ini ---")

# Set an environment variable for testing `eval` in the config.
os.environ['QUICKSTART_ENV_VAR'] = 'set_from_env_value'

# `global_conf` is passed to the app factory (read-only configuration).
# The '#main' refers to the `[app:main]` section in config.ini.
try:
    app = loadapp('config:config.ini#main', global_conf={'process_id': os.getpid(), 'api_key': os.environ.get('PASTEDEPLOY_API_KEY', '')})
    print(f"Successfully loaded WSGI application: {app}")

    # Verify the loaded app is callable (a basic WSGI check)
    assert callable(app)
    # A minimal test of the WSGI app (not actually running a server)
    response_body = []
    def start_response(status, headers):
        pass
    list(app({'REQUEST_METHOD': 'GET', 'PATH_INFO': '/'}, start_response))
    print("WSGI app is callable and passes a basic test.")

finally:
    # --- 4. Clean up (important for a self-contained quickstart) ---
    os.remove("config.ini")
    if 'QUICKSTART_ENV_VAR' in os.environ:
        del os.environ['QUICKSTART_ENV_VAR']
    print("\n--- Cleanup: Removed config.ini and environment variable ---")

view raw JSON →