<s> from __future__ import absolute_import <EOL> import types <EOL> import warnings <EOL> import webob as wo <EOL> from pkg_resources import iter_entry_points , DistributionNotFound <EOL> from paste . deploy . converters import asbool , asint <EOL> from . import core <EOL> import logging <EOL> import six <EOL> log = logging . getLogger ( __name__ ) <EOL> class Config ( object ) : <EOL> '''<STR_LIT>''' <EOL> translator = lambda self , s : s <EOL> default_engine = '<STR_LIT:string>' <EOL> inject_resources_location = '<STR_LIT>' <EOL> inject_resources = True <EOL> serve_resources = True <EOL> res_prefix = '<STR_LIT>' <EOL> res_max_age = <NUM_LIT> <EOL> serve_controllers = True <EOL> controller_prefix = '<STR_LIT>' <EOL> bufsize = <NUM_LIT:4> * <NUM_LIT> <EOL> params_as_vars = False <EOL> debug = True <EOL> validator_msgs = { } <EOL> encoding = '<STR_LIT:utf-8>' <EOL> auto_reload_templates = None <EOL> preferred_rendering_engines = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> strict_engine_selection = True <EOL> rendering_extension_lookup = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT:html>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT:html>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT:html>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT:html>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] <EOL> } <EOL> script_name = '<STR_LIT>' <EOL> def __init__ ( self , ** kw ) : <EOL> for k , v in kw . items ( ) : <EOL> setattr ( self , k , v ) <EOL> boolean_props = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> for prop in boolean_props : <EOL> setattr ( self , prop , asbool ( getattr ( self , prop ) ) ) <EOL> for prop in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> setattr ( self , prop , asint ( getattr ( self , prop ) ) ) <EOL> if self . auto_reload_templates is None : <EOL> self . auto_reload_templates = self . debug <EOL> class TwMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app , controllers = None , ** config ) : <EOL> from . import resources <EOL> self . _resources_module = resources <EOL> self . app = app <EOL> self . config = Config ( ** config ) <EOL> self . resources = resources . ResourcesApp ( self . config ) <EOL> self . controllers = controllers or ControllersApp ( ) <EOL> rl = core . request_local ( ) <EOL> for widget , path in rl . get ( '<STR_LIT>' , [ ] ) : <EOL> self . controllers . register ( widget , path ) <EOL> rl [ '<STR_LIT>' ] = [ ] <EOL> for modname , filename , whole_dir in rl . get ( '<STR_LIT>' , [ ] ) : <EOL> self . resources . register ( modname , filename , whole_dir ) <EOL> rl [ '<STR_LIT>' ] = [ ] <EOL> rl [ '<STR_LIT>' ] = self <EOL> def __call__ ( self , environ , start_response ) : <EOL> rl = core . request_local ( ) <EOL> rl . clear ( ) <EOL> rl [ '<STR_LIT>' ] = self <EOL> req = wo . Request ( environ ) <EOL> path = req . path_info <EOL> if self . config . serve_resources and path . startswith ( self . config . res_prefix ) : <EOL> return self . resources ( environ , start_response ) <EOL> else : <EOL> if self . config . serve_controllers and path . startswith ( self . config . controller_prefix ) : <EOL> resp = self . controllers ( req ) <EOL> else : <EOL> if self . app : <EOL> resp = req . get_response ( self . app , catch_exc_info = True ) <EOL> else : <EOL> resp = wo . Response ( status = "<STR_LIT>" ) <EOL> ct = resp . headers . get ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) . lower ( ) <EOL> should_inject = ( <EOL> self . config . inject_resources <EOL> and '<STR_LIT:html>' in ct <EOL> and not isinstance ( resp . app_iter , types . GeneratorType ) <EOL> ) <EOL> if should_inject : <EOL> if resp . charset : <EOL> body = self . _resources_module . inject_resources ( <EOL> resp . body . decode ( resp . charset ) , <EOL> ) . encode ( resp . charset ) <EOL> else : <EOL> body = self . _resources_module . inject_resources ( <EOL> resp . body , <EOL> ) <EOL> if isinstance ( body , six . text_type ) : <EOL> resp . unicode_body = body <EOL> else : <EOL> resp . body = body <EOL> core . request_local ( ) . clear ( ) <EOL> return resp ( environ , start_response ) <EOL> class ControllersApp ( object ) : <EOL> """<STR_LIT:U+0020>""" <EOL> def __init__ ( self ) : <EOL> self . _widgets = { } <EOL> def register ( self , widget , path = None ) : <EOL> log . info ( "<STR_LIT>" % ( path , widget ) ) <EOL> if path is None : <EOL> path = widget . id <EOL> self . _widgets [ path ] = widget <EOL> def controller_path ( self , target_widget ) : <EOL> """<STR_LIT>""" <EOL> for path , widget in six . iteritems ( self . _widgets ) : <EOL> if target_widget == widget : <EOL> return path <EOL> return None <EOL> def __call__ ( self , req ) : <EOL> config = rl = core . request_local ( ) [ '<STR_LIT>' ] . config <EOL> path = req . path_info . split ( '<STR_LIT:/>' ) [ <NUM_LIT:1> : ] <EOL> pre = config . controller_prefix . strip ( '<STR_LIT:/>' ) <EOL> if pre and path [ <NUM_LIT:0> ] != pre : <EOL> return wo . Response ( status = "<STR_LIT>" ) <EOL> path = path [ <NUM_LIT:1> ] if pre else path [ <NUM_LIT:0> ] <EOL> widget_name = path or '<STR_LIT:index>' <EOL> try : <EOL> widget = self . _widgets [ widget_name ] <EOL> except KeyError : <EOL> resp = wo . Response ( status = "<STR_LIT>" ) <EOL> else : <EOL> resp = widget . request ( req ) <EOL> return resp <EOL> def register_resource ( modname , filename , whole_dir ) : <EOL> """<STR_LIT>""" <EOL> rl = core . request_local ( ) <EOL> mw = rl . get ( '<STR_LIT>' ) <EOL> if mw : <EOL> mw . resources . register ( modname , filename , whole_dir ) <EOL> else : <EOL> rl [ '<STR_LIT>' ] = rl . get ( '<STR_LIT>' , [ ] ) + [ <EOL> ( modname , filename , whole_dir ) <EOL> ] <EOL> log . debug ( "<STR_LIT>" % <EOL> ( modname , filename , whole_dir ) ) <EOL> def register_controller ( widget , path ) : <EOL> """<STR_LIT>""" <EOL> rl = core . request_local ( ) <EOL> mw = rl . get ( '<STR_LIT>' ) <EOL> if mw : <EOL> mw . controllers . register ( widget , path ) <EOL> else : <EOL> rl [ '<STR_LIT>' ] = rl . get ( '<STR_LIT>' , [ ] ) + [ ( widget , path ) ] <EOL> log . debug ( "<STR_LIT>" % <EOL> ( path , widget ) ) <EOL> def make_middleware ( app = None , config = None , ** kw ) : <EOL> config = ( config or { } ) . copy ( ) <EOL> config . update ( kw ) <EOL> app = TwMiddleware ( app , ** config ) <EOL> return app <EOL> def make_app ( config = None , ** kw ) : <EOL> return make_middleware ( app = None , config = config , ** kw ) </s>
