<s> """<STR_LIT>""" <EOL> import logging <EOL> import sys <EOL> import paste . registry <EOL> import pkg_resources <EOL> from webob . exc import HTTPNotFound <EOL> import pylons <EOL> import pylons . templating <EOL> from pylons . controllers . util import Request , Response <EOL> from pylons . i18n . translation import _get_translator <EOL> from pylons . util import ( AttribSafeContextObj , ContextObj , PylonsContext , <EOL> class_name_from_module_name ) <EOL> __all__ = [ '<STR_LIT>' ] <EOL> log = logging . getLogger ( __name__ ) <EOL> class PylonsApp ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , config = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . config = config = config or pylons . config . _current_obj ( ) <EOL> package_name = config [ '<STR_LIT>' ] <EOL> self . helpers = config [ '<STR_LIT>' ] <EOL> self . globals = config . get ( '<STR_LIT>' ) <EOL> self . environ_config = config [ '<STR_LIT>' ] <EOL> self . package_name = package_name <EOL> self . request_options = config [ '<STR_LIT>' ] <EOL> self . response_options = config [ '<STR_LIT>' ] <EOL> self . controller_classes = { } <EOL> self . log_debug = False <EOL> self . config . setdefault ( '<STR_LIT>' , None ) <EOL> self . _session_key = self . environ_config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . _cache_key = self . environ_config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __call__ ( self , environ , start_response ) : <EOL> """<STR_LIT>""" <EOL> log_debug = self . log_debug = logging . DEBUG >= log . getEffectiveLevel ( ) <EOL> environ [ '<STR_LIT>' ] = log_debug <EOL> self . setup_app_env ( environ , start_response ) <EOL> if '<STR_LIT>' in environ : <EOL> self . load_test_env ( environ ) <EOL> if environ [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> paste . registry . restorer . save_registry_state ( environ ) <EOL> start_response ( '<STR_LIT>' , [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> return [ '<STR_LIT:%s>' % paste . registry . restorer . get_request_id ( environ ) ] <EOL> controller = self . resolve ( environ , start_response ) <EOL> response = self . dispatch ( controller , environ , start_response ) <EOL> response_obj = callable ( response ) <EOL> if '<STR_LIT>' in environ and response_obj : <EOL> environ [ '<STR_LIT>' ] [ '<STR_LIT>' ] = response <EOL> try : <EOL> if response_obj : <EOL> return response ( environ , start_response ) <EOL> elif response is not None : <EOL> return response <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> controller . __name__ ) <EOL> finally : <EOL> if '<STR_LIT>' in environ : <EOL> del environ [ '<STR_LIT>' ] <EOL> def register_globals ( self , environ ) : <EOL> """<STR_LIT>""" <EOL> pylons_obj = environ [ '<STR_LIT>' ] <EOL> registry = environ [ '<STR_LIT>' ] <EOL> registry . register ( pylons . response , pylons_obj . response ) <EOL> registry . register ( pylons . request , pylons_obj . request ) <EOL> registry . register ( pylons . app_globals , self . globals ) <EOL> registry . register ( pylons . config , self . config ) <EOL> registry . register ( pylons . tmpl_context , pylons_obj . tmpl_context ) <EOL> registry . register ( pylons . translator , pylons_obj . translator ) <EOL> if '<STR_LIT>' in pylons_obj . __dict__ : <EOL> registry . register ( pylons . session , pylons_obj . session ) <EOL> if '<STR_LIT>' in pylons_obj . __dict__ : <EOL> registry . register ( pylons . cache , pylons_obj . cache ) <EOL> elif '<STR_LIT>' in pylons_obj . app_globals . __dict__ : <EOL> registry . register ( pylons . cache , pylons_obj . app_globals . cache ) <EOL> if '<STR_LIT>' in environ : <EOL> registry . register ( pylons . url , environ [ '<STR_LIT>' ] ) <EOL> def setup_app_env ( self , environ , start_response ) : <EOL> """<STR_LIT>""" <EOL> if self . log_debug : <EOL> log . debug ( "<STR_LIT>" ) <EOL> req_options = self . request_options <EOL> req = Request ( environ , charset = req_options [ '<STR_LIT>' ] , <EOL> unicode_errors = req_options [ '<STR_LIT>' ] , <EOL> decode_param_names = req_options [ '<STR_LIT>' ] ) <EOL> req . language = req_options [ '<STR_LIT>' ] <EOL> req . config = self . config <EOL> req . link , req . route_dict = environ [ '<STR_LIT>' ] <EOL> response = Response ( <EOL> content_type = self . response_options [ '<STR_LIT>' ] , <EOL> charset = self . response_options [ '<STR_LIT>' ] ) <EOL> response . headers . update ( self . response_options [ '<STR_LIT>' ] ) <EOL> pylons_obj = PylonsContext ( ) <EOL> pylons_obj . config = self . config <EOL> pylons_obj . request = req <EOL> pylons_obj . response = response <EOL> pylons_obj . app_globals = self . globals <EOL> pylons_obj . h = self . helpers <EOL> if '<STR_LIT>' in environ : <EOL> pylons_obj . url = environ [ '<STR_LIT>' ] <EOL> environ [ '<STR_LIT>' ] = pylons_obj <EOL> environ [ '<STR_LIT>' ] = self . environ_config <EOL> lang = self . config [ '<STR_LIT>' ] <EOL> pylons_obj . translator = _get_translator ( lang , pylons_config = self . config ) <EOL> if self . config [ '<STR_LIT>' ] : <EOL> tmpl_context = ContextObj ( ) <EOL> else : <EOL> tmpl_context = AttribSafeContextObj ( ) <EOL> pylons_obj . tmpl_context = req . tmpl_context = tmpl_context <EOL> if self . _session_key in environ : <EOL> pylons_obj . session = req . session = environ [ self . _session_key ] <EOL> if self . _cache_key in environ : <EOL> pylons_obj . cache = environ [ self . _cache_key ] <EOL> if '<STR_LIT>' in environ : <EOL> self . register_globals ( environ ) <EOL> def resolve ( self , environ , start_response ) : <EOL> """<STR_LIT>""" <EOL> match = environ [ '<STR_LIT>' ] [ <NUM_LIT:1> ] <EOL> environ [ '<STR_LIT>' ] = match <EOL> controller = match . get ( '<STR_LIT>' , match . get ( '<STR_LIT>' ) ) <EOL> if not controller : <EOL> return <EOL> if self . log_debug : <EOL> log . debug ( "<STR_LIT>" , controller ) <EOL> return self . find_controller ( controller ) <EOL> def find_controller ( self , controller ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( controller , basestring ) : <EOL> return controller <EOL> if controller in self . controller_classes : <EOL> return self . controller_classes [ controller ] <EOL> if '<STR_LIT:.>' in controller or '<STR_LIT::>' in controller : <EOL> mycontroller = pkg_resources . EntryPoint . parse ( <EOL> '<STR_LIT>' % controller ) . load ( False ) <EOL> self . controller_classes [ controller ] = mycontroller <EOL> return mycontroller <EOL> full_module_name = self . package_name + '<STR_LIT>' + controller . replace ( '<STR_LIT:/>' , '<STR_LIT:.>' ) <EOL> __traceback_hide__ = '<STR_LIT>' <EOL> __import__ ( full_module_name ) <EOL> if hasattr ( sys . modules [ full_module_name ] , '<STR_LIT>' ) : <EOL> mycontroller = getattr ( sys . modules [ full_module_name ] , <EOL> sys . modules [ full_module_name ] . __controller__ ) <EOL> else : <EOL> module_name = controller . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> class_name = class_name_from_module_name ( module_name ) + '<STR_LIT>' <EOL> if self . log_debug : <EOL> log . debug ( "<STR_LIT>" , <EOL> full_module_name , class_name ) <EOL> mycontroller = getattr ( sys . modules [ full_module_name ] , class_name ) <EOL> self . controller_classes [ controller ] = mycontroller <EOL> return mycontroller <EOL> def dispatch ( self , controller , environ , start_response ) : <EOL> """<STR_LIT>""" <EOL> log_debug = self . log_debug <EOL> if not controller : <EOL> if log_debug : <EOL> log . debug ( "<STR_LIT>" ) <EOL> return HTTPNotFound ( ) ( environ , start_response ) <EOL> if '<STR_LIT>' in environ [ '<STR_LIT>' ] : <EOL> return controller ( environ [ '<STR_LIT>' ] . request ) <EOL> if hasattr ( controller , '<STR_LIT>' ) : <EOL> if log_debug : <EOL> log . debug ( "<STR_LIT>" ) <EOL> controller = controller ( ) <EOL> controller . _pylons_log_debug = log_debug <EOL> environ [ '<STR_LIT>' ] = controller <EOL> if log_debug : <EOL> log . debug ( "<STR_LIT>" ) <EOL> return controller ( environ , start_response ) <EOL> def load_test_env ( self , environ ) : <EOL> """<STR_LIT>""" <EOL> if self . log_debug : <EOL> log . debug ( "<STR_LIT>" ) <EOL> testenv = environ [ '<STR_LIT>' ] <EOL> pylons_obj = environ [ '<STR_LIT>' ] <EOL> testenv [ '<STR_LIT>' ] = pylons_obj . request <EOL> testenv [ '<STR_LIT>' ] = pylons_obj . response <EOL> testenv [ '<STR_LIT>' ] = pylons_obj . tmpl_context <EOL> testenv [ '<STR_LIT>' ] = testenv [ '<STR_LIT:g>' ] = pylons_obj . app_globals <EOL> testenv [ '<STR_LIT:h>' ] = self . config [ '<STR_LIT>' ] <EOL> testenv [ '<STR_LIT>' ] = self . config <EOL> if hasattr ( pylons_obj , '<STR_LIT>' ) : <EOL> testenv [ '<STR_LIT>' ] = pylons_obj . session <EOL> if hasattr ( pylons_obj , '<STR_LIT>' ) : <EOL> testenv [ '<STR_LIT>' ] = pylons_obj . cache <EOL> elif hasattr ( pylons_obj . app_globals , '<STR_LIT>' ) : <EOL> testenv [ '<STR_LIT>' ] = pylons_obj . app_globals . cache </s>
