"""The FileBrowser/ WSGI application""" import os from paste.cascade import Cascade from paste.registry import RegistryManager from paste.urlparser import StaticURLParser from paste.deploy.converters import asbool from pylons import config from pylons.error import error_template from pylons.middleware import error_mapper, ErrorDocuments, ErrorHandler, \ StaticJavascripts from pylons.wsgiapp import PylonsApp import filebrowser.helpers from filebrowser.routing import make_map def load_environment(global_conf, app_conf): """Configure the Pylons environment via the ``pylons.config`` object """ # Pylons paths root = os.path.dirname(os.path.abspath(__file__)) paths = dict(root=root, controllers=os.path.join(root, 'controllers'), static_files=os.path.join(root, 'public'), templates=[os.path.join(root, 'templates')]) # Initialize config with the basic options config.init_app(global_conf, app_conf, package='filebrowser', template_engine='mako', paths=paths) config['routes.map'] = make_map() config['pylons.g'] = Globals() config['pylons.h'] = filebrowser.helpers # Customize templating options via this variable tmpl_options = config['buffet.template_options'] # CONFIGURATION OPTIONS HERE (note: all config options will override # any Pylons config options) def make_app(global_conf, edit_dir, dir_href, full_stack=True, **app_conf): """Create a Pylons WSGI application and return it ``global_conf`` The inherited configuration for this application. Normally from the [DEFAULT] section of the Paste ini file. ``full_stack`` Whether or not this application provides a full WSGI stack (by default, meaning it handles its own exceptions and errors). Disable full_stack when this application is "managed" by another WSGI middleware. ``edit_dir`` The filesystem directory that this instance will edit. ``dir_href`` The base URL of the directory that is edited, relative to the base URL of *this* application. """ app_conf.update(dict(edit_dir=edit_dir, dir_href=dir_href)) # Configure the Pylons environment load_environment(global_conf, app_conf) # The Pylons WSGI app app = PylonsApp() # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares) if asbool(full_stack): # Handle Python exceptions app = ErrorHandler(app, global_conf, error_template=error_template, **config['pylons.errorware']) # Display error documents for 401, 403, 404 status codes (and # 500 when debug is disabled) app = ErrorDocuments(app, global_conf, mapper=error_mapper, **app_conf) # Establish the Registry for this application app = RegistryManager(app) # Static files javascripts_app = StaticJavascripts() static_app = StaticURLParser(config['pylons.paths']['static_files']) app = Cascade([static_app, javascripts_app, app]) return app class Globals(object): """Globals acts as a container for objects available throughout the life of the application """ def __init__(self): """One instance of Globals is created during application initialization and is available during requests via the 'g' variable. """ pass def make_full_app(global_conf, edit_dir, admin_href='/_admin', **kw): """ Create an application that serves ``edit_dir`` at the root, and edits ``edit_dir`` under ``admin_href`` (``/_admin/`` by default). """ from paste.urlmap import URLMap from paste.urlparser import StaticURLParser map = URLMap() map[''] = StaticURLParser(edit_dir) map[admin_href] = make_app( global_conf, edit_dir=edit_dir, dir_href='../', **kw) return map