<s> """<STR_LIT>""" <EOL> from __future__ import with_statement <EOL> import cgi <EOL> import inspect <EOL> import logging <EOL> import os <EOL> import re <EOL> import sys <EOL> import threading <EOL> import traceback <EOL> import urllib <EOL> import urlparse <EOL> from wsgiref import handlers <EOL> import webob <EOL> from webob import exc <EOL> _webapp = _webapp_util = _local = None <EOL> try : <EOL> from webob . statusreasons import status_reasons <EOL> from webob . headerdict import HeaderDict as BaseResponseHeaders <EOL> except ImportError : <EOL> from webob . util import status_reasons <EOL> from webob . headers import ResponseHeaders as BaseResponseHeaders <EOL> if os . environ . get ( '<STR_LIT>' ) != '<STR_LIT>' : <EOL> try : <EOL> from google . appengine . ext import webapp as _webapp <EOL> except ImportError : <EOL> pass <EOL> try : <EOL> from webapp2_extras import local <EOL> _local = local . Local ( ) <EOL> except ImportError : <EOL> logging . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> __version_info__ = ( <NUM_LIT:2> , <NUM_LIT:5> , <NUM_LIT:1> ) <EOL> __version__ = '<STR_LIT:.>' . join ( str ( n ) for n in __version_info__ ) <EOL> HTTPException = exc . HTTPException <EOL> _route_re = re . compile ( r"""<STR_LIT>""" , re . VERBOSE ) <EOL> _charset_re = re . compile ( r'<STR_LIT>' , re . I ) <EOL> _debug_template = """<STR_LIT>""" <EOL> _webapp_status_reasons = { <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> } <EOL> status_reasons . update ( _webapp_status_reasons ) <EOL> for code , message in _webapp_status_reasons . iteritems ( ) : <EOL> cls = exc . status_map . get ( code ) <EOL> if cls : <EOL> cls . title = message <EOL> class Request ( webob . Request ) : <EOL> """<STR_LIT>""" <EOL> app = None <EOL> response = None <EOL> route = None <EOL> route_args = None <EOL> route_kwargs = None <EOL> registry = None <EOL> request_body_tempfile_limit = <NUM_LIT:0> <EOL> uri = property ( lambda self : self . url ) <EOL> query = property ( lambda self : self . query_string ) <EOL> def __init__ ( self , environ , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if kwargs . get ( '<STR_LIT>' ) is None and not hasattr ( webob , '<STR_LIT>' ) : <EOL> match = _charset_re . search ( environ . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if match : <EOL> charset = match . group ( <NUM_LIT:1> ) . lower ( ) . strip ( ) . strip ( '<STR_LIT:">' ) . strip ( ) <EOL> else : <EOL> charset = '<STR_LIT:utf-8>' <EOL> kwargs [ '<STR_LIT>' ] = charset <EOL> super ( Request , self ) . __init__ ( environ , * args , ** kwargs ) <EOL> self . registry = { } <EOL> def get ( self , argument_name , default_value = '<STR_LIT>' , allow_multiple = False ) : <EOL> """<STR_LIT>""" <EOL> param_value = self . get_all ( argument_name ) <EOL> if allow_multiple : <EOL> logging . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if len ( param_value ) > <NUM_LIT:0> : <EOL> if allow_multiple : <EOL> return param_value <EOL> return param_value [ <NUM_LIT:0> ] <EOL> else : <EOL> if allow_multiple and not default_value : <EOL> return [ ] <EOL> return default_value <EOL> def get_all ( self , argument_name , default_value = None ) : <EOL> """<STR_LIT>""" <EOL> if self . charset : <EOL> argument_name = argument_name . encode ( self . charset ) <EOL> if default_value is None : <EOL> default_value = [ ] <EOL> param_value = self . params . getall ( argument_name ) <EOL> if param_value is None or len ( param_value ) == <NUM_LIT:0> : <EOL> return default_value <EOL> for i in xrange ( len ( param_value ) ) : <EOL> if isinstance ( param_value [ i ] , cgi . FieldStorage ) : <EOL> param_value [ i ] = param_value [ i ] . value <EOL> return param_value <EOL> def arguments ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( set ( self . params . keys ( ) ) ) <EOL> def get_range ( self , name , min_value = None , max_value = None , default = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> value = self . get ( name , default ) <EOL> if value is None : <EOL> return value <EOL> try : <EOL> value = int ( value ) <EOL> except ValueError : <EOL> value = default <EOL> if value is not None : <EOL> if max_value is not None : <EOL> value = min ( value , max_value ) <EOL> if min_value is not None : <EOL> value = max ( value , min_value ) <EOL> return value <EOL> @ classmethod <EOL> def blank ( cls , path , environ = None , base_url = None , <EOL> headers = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return super ( Request , cls ) . blank ( path , environ = environ , <EOL> base_url = base_url , <EOL> headers = headers , ** kwargs ) <EOL> except TypeError : <EOL> if not kwargs : <EOL> raise <EOL> data = kwargs . pop ( '<STR_LIT:POST>' , None ) <EOL> if data is not None : <EOL> from cStringIO import StringIO <EOL> environ = environ or { } <EOL> environ [ '<STR_LIT>' ] = '<STR_LIT:POST>' <EOL> if hasattr ( data , '<STR_LIT>' ) : <EOL> data = data . items ( ) <EOL> if not isinstance ( data , str ) : <EOL> data = urllib . urlencode ( data ) <EOL> environ [ '<STR_LIT>' ] = StringIO ( data ) <EOL> environ [ '<STR_LIT>' ] = True <EOL> environ [ '<STR_LIT>' ] = str ( len ( data ) ) <EOL> environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> base = super ( Request , cls ) . blank ( path , environ = environ , <EOL> base_url = base_url , headers = headers ) <EOL> if kwargs : <EOL> obj = cls ( base . environ , ** kwargs ) <EOL> obj . headers . update ( base . headers ) <EOL> return obj <EOL> else : <EOL> return base <EOL> class ResponseHeaders ( BaseResponseHeaders ) : <EOL> """<STR_LIT>""" <EOL> get_all = BaseResponseHeaders . getall <EOL> def add_header ( self , _name , _value , ** _params ) : <EOL> """<STR_LIT>""" <EOL> parts = [ ] <EOL> if _value is not None : <EOL> parts . append ( _value ) <EOL> for k , v in _params . items ( ) : <EOL> k = k . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) <EOL> if v is not None and len ( v ) > <NUM_LIT:0> : <EOL> v = v . replace ( '<STR_LIT:\\>' , '<STR_LIT>' ) . replace ( '<STR_LIT:">' , r'<STR_LIT>' ) <EOL> parts . append ( '<STR_LIT>' % ( k , v ) ) <EOL> else : <EOL> parts . append ( k ) <EOL> self . add ( _name , '<STR_LIT>' . join ( parts ) ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT:\r\n>' . join ( [ '<STR_LIT>' % v for v in self . items ( ) ] + [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> class Response ( webob . Response ) : <EOL> """<STR_LIT>""" <EOL> default_charset = '<STR_LIT:utf-8>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( Response , self ) . __init__ ( * args , ** kwargs ) <EOL> self . headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> @ property <EOL> def out ( self ) : <EOL> """<STR_LIT>""" <EOL> return self <EOL> def write ( self , text ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( text , basestring ) : <EOL> text = unicode ( text ) <EOL> if isinstance ( text , unicode ) and not self . charset : <EOL> self . charset = self . default_charset <EOL> super ( Response , self ) . write ( text ) <EOL> def _set_status ( self , value ) : <EOL> """<STR_LIT>""" <EOL> message = None <EOL> if isinstance ( value , ( int , long ) ) : <EOL> code = int ( value ) <EOL> else : <EOL> if isinstance ( value , unicode ) : <EOL> value = str ( value ) <EOL> if not isinstance ( value , str ) : <EOL> raise TypeError ( <EOL> '<STR_LIT>' % <EOL> type ( value ) ) <EOL> parts = value . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> code = int ( parts [ <NUM_LIT:0> ] ) <EOL> if len ( parts ) == <NUM_LIT:2> : <EOL> message = parts [ <NUM_LIT:1> ] <EOL> message = message or Response . http_status_message ( code ) <EOL> self . _status = '<STR_LIT>' % ( code , message ) <EOL> def _get_status ( self ) : <EOL> return self . _status <EOL> status = property ( _get_status , _set_status , doc = _set_status . __doc__ ) <EOL> def set_status ( self , code , message = None ) : <EOL> """<STR_LIT>""" <EOL> if message : <EOL> self . status = '<STR_LIT>' % ( code , message ) <EOL> else : <EOL> self . status = code <EOL> def _get_status_message ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . status . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> def _set_status_message ( self , message ) : <EOL> self . status = '<STR_LIT>' % ( self . status_int , message ) <EOL> status_message = property ( _get_status_message , _set_status_message , <EOL> doc = _get_status_message . __doc__ ) <EOL> def _get_headers ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _headers is None : <EOL> self . _headers = ResponseHeaders . view_list ( self . headerlist ) <EOL> return self . _headers <EOL> def _set_headers ( self , value ) : <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> value = value . items ( ) <EOL> elif not isinstance ( value , list ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> self . headerlist = value <EOL> self . _headers = None <EOL> headers = property ( _get_headers , _set_headers , doc = _get_headers . __doc__ ) <EOL> def has_error ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . status_int >= <NUM_LIT> <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> self . body = '<STR_LIT>' <EOL> def wsgi_write ( self , start_response ) : <EOL> """<STR_LIT>""" <EOL> if ( self . headers . get ( '<STR_LIT>' ) == '<STR_LIT>' and <EOL> not self . headers . get ( '<STR_LIT>' ) ) : <EOL> self . headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . headers [ '<STR_LIT>' ] = str ( len ( self . body ) ) <EOL> write = start_response ( self . status , self . headerlist ) <EOL> write ( self . body ) <EOL> @ staticmethod <EOL> def http_status_message ( code ) : <EOL> """<STR_LIT>""" <EOL> message = status_reasons . get ( code ) <EOL> if not message : <EOL> raise KeyError ( '<STR_LIT>' % code ) <EOL> return message <EOL> class RequestHandler ( object ) : <EOL> """<STR_LIT>""" <EOL> request = None <EOL> response = None <EOL> app = None <EOL> def __init__ ( self , request = None , response = None ) : <EOL> """<STR_LIT>""" <EOL> self . initialize ( request , response ) <EOL> def initialize ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> self . request = request <EOL> self . response = response <EOL> self . app = WSGIApplication . active_instance <EOL> def dispatch ( self ) : <EOL> """<STR_LIT>""" <EOL> request = self . request <EOL> method_name = request . route . handler_method <EOL> if not method_name : <EOL> method_name = _normalize_handler_method ( request . method ) <EOL> method = getattr ( self , method_name , None ) <EOL> if method is None : <EOL> valid = '<STR_LIT:U+002CU+0020>' . join ( _get_handler_methods ( self ) ) <EOL> self . abort ( <NUM_LIT> , headers = [ ( '<STR_LIT>' , valid ) ] ) <EOL> args , kwargs = request . route_args , request . route_kwargs <EOL> if kwargs : <EOL> args = ( ) <EOL> try : <EOL> return method ( * args , ** kwargs ) <EOL> except Exception , e : <EOL> return self . handle_exception ( e , self . app . debug ) <EOL> def error ( self , code ) : <EOL> """<STR_LIT>""" <EOL> self . response . status = code <EOL> self . response . clear ( ) <EOL> def abort ( self , code , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> abort ( code , * args , ** kwargs ) <EOL> def redirect ( self , uri , permanent = False , abort = False , code = None , <EOL> body = None ) : <EOL> """<STR_LIT>""" <EOL> return redirect ( uri , permanent = permanent , abort = abort , code = code , <EOL> body = body , request = self . request , <EOL> response = self . response ) <EOL> def redirect_to ( self , _name , _permanent = False , _abort = False , _code = None , <EOL> _body = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> uri = self . uri_for ( _name , * args , ** kwargs ) <EOL> return self . redirect ( uri , permanent = _permanent , abort = _abort , <EOL> code = _code , body = _body ) <EOL> def uri_for ( self , _name , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . app . router . build ( self . request , _name , args , kwargs ) <EOL> url_for = uri_for <EOL> def handle_exception ( self , exception , debug ) : <EOL> """<STR_LIT>""" <EOL> raise <EOL> class RedirectHandler ( RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> uri = kwargs . pop ( '<STR_LIT>' , '<STR_LIT:/>' ) <EOL> permanent = kwargs . pop ( '<STR_LIT>' , True ) <EOL> code = kwargs . pop ( '<STR_LIT>' , None ) <EOL> func = getattr ( uri , '<STR_LIT>' , None ) <EOL> if func : <EOL> uri = func ( self , * args , ** kwargs ) <EOL> self . redirect ( uri , permanent = permanent , code = code ) <EOL> class cached_property ( object ) : <EOL> """<STR_LIT>""" <EOL> _default_value = object ( ) <EOL> def __init__ ( self , func , name = None , doc = None ) : <EOL> self . __name__ = name or func . __name__ <EOL> self . __module__ = func . __module__ <EOL> self . __doc__ = doc or func . __doc__ <EOL> self . func = func <EOL> self . lock = threading . RLock ( ) <EOL> def __get__ ( self , obj , type = None ) : <EOL> if obj is None : <EOL> return self <EOL> with self . lock : <EOL> value = obj . __dict__ . get ( self . __name__ , self . _default_value ) <EOL> if value is self . _default_value : <EOL> value = self . func ( obj ) <EOL> obj . __dict__ [ self . __name__ ] = value <EOL> return value <EOL> class BaseRoute ( object ) : <EOL> """<STR_LIT>""" <EOL> template = None <EOL> name = None <EOL> build_only = False <EOL> handler = None <EOL> handler_method = None <EOL> handler_adapter = None <EOL> def __init__ ( self , template , handler = None , name = None , build_only = False ) : <EOL> """<STR_LIT>""" <EOL> if build_only and name is None : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % self ) <EOL> self . template = template <EOL> self . handler = handler <EOL> self . name = name <EOL> self . build_only = build_only <EOL> def match ( self , request ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def build ( self , request , args , kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def get_routes ( self ) : <EOL> """<STR_LIT>""" <EOL> yield self <EOL> def get_match_routes ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . build_only : <EOL> yield self <EOL> def get_build_routes ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . name is not None : <EOL> yield self . name , self <EOL> class SimpleRoute ( BaseRoute ) : <EOL> """<STR_LIT>""" <EOL> @ cached_property <EOL> def regex ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . template . startswith ( '<STR_LIT>' ) : <EOL> self . template = '<STR_LIT>' + self . template <EOL> if not self . template . endswith ( '<STR_LIT:$>' ) : <EOL> self . template += '<STR_LIT:$>' <EOL> return re . compile ( self . template ) <EOL> def match ( self , request ) : <EOL> """<STR_LIT>""" <EOL> match = self . regex . match ( urllib . unquote ( request . path ) ) <EOL> if match : <EOL> return self , match . groups ( ) , { } <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . template , self . handler ) <EOL> class Route ( BaseRoute ) : <EOL> """<STR_LIT>""" <EOL> defaults = None <EOL> methods = None <EOL> schemes = None <EOL> regex = None <EOL> reverse_template = None <EOL> variables = None <EOL> args_count = <NUM_LIT:0> <EOL> kwargs_count = <NUM_LIT:0> <EOL> def __init__ ( self , template , handler = None , name = None , defaults = None , <EOL> build_only = False , handler_method = None , methods = None , <EOL> schemes = None ) : <EOL> """<STR_LIT>""" <EOL> super ( Route , self ) . __init__ ( template , handler = handler , name = name , <EOL> build_only = build_only ) <EOL> self . defaults = defaults or { } <EOL> self . methods = methods <EOL> self . schemes = schemes <EOL> if isinstance ( handler , basestring ) and '<STR_LIT::>' in handler : <EOL> if handler_method : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % handler ) <EOL> else : <EOL> self . handler , self . handler_method = handler . rsplit ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> else : <EOL> self . handler_method = handler_method <EOL> @ cached_property <EOL> def regex ( self ) : <EOL> """<STR_LIT>""" <EOL> regex , self . reverse_template , self . args_count , self . kwargs_count , self . variables = _parse_route_template ( self . template , <EOL> default_sufix = '<STR_LIT>' ) <EOL> return regex <EOL> def match ( self , request ) : <EOL> """<STR_LIT>""" <EOL> match = self . regex . match ( urllib . unquote ( request . path ) ) <EOL> if not match or self . schemes and request . scheme not in self . schemes : <EOL> return None <EOL> if self . methods and request . method not in self . methods : <EOL> raise exc . HTTPMethodNotAllowed ( ) <EOL> args , kwargs = _get_route_variables ( match , self . defaults . copy ( ) ) <EOL> return self , args , kwargs <EOL> def build ( self , request , args , kwargs ) : <EOL> """<STR_LIT>""" <EOL> scheme = kwargs . pop ( '<STR_LIT>' , None ) <EOL> netloc = kwargs . pop ( '<STR_LIT>' , None ) <EOL> anchor = kwargs . pop ( '<STR_LIT>' , None ) <EOL> full = kwargs . pop ( '<STR_LIT>' , False ) and not scheme and not netloc <EOL> if full or scheme or netloc : <EOL> netloc = netloc or request . host <EOL> scheme = scheme or request . scheme <EOL> path , query = self . _build ( args , kwargs ) <EOL> return _urlunsplit ( scheme , netloc , path , query , anchor ) <EOL> def _build ( self , args , kwargs ) : <EOL> """<STR_LIT>""" <EOL> regex = self . regex <EOL> variables = self . variables <EOL> if self . args_count : <EOL> for index , value in enumerate ( args ) : <EOL> key = '<STR_LIT>' % index <EOL> if key in variables : <EOL> kwargs [ key ] = value <EOL> values = { } <EOL> for name , regex in variables . iteritems ( ) : <EOL> value = kwargs . pop ( name , self . defaults . get ( name ) ) <EOL> if value is None : <EOL> raise KeyError ( '<STR_LIT>' % name . strip ( '<STR_LIT:_>' ) ) <EOL> if not isinstance ( value , basestring ) : <EOL> value = str ( value ) <EOL> if not regex . match ( value ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( value , name . strip ( '<STR_LIT:_>' ) ) ) <EOL> values [ name ] = value <EOL> return ( self . reverse_template % values , kwargs ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . template , self . handler , self . name , self . defaults , <EOL> self . build_only ) <EOL> class BaseHandlerAdapter ( object ) : <EOL> """<STR_LIT>""" <EOL> handler = None <EOL> def __init__ ( self , handler ) : <EOL> self . handler = handler <EOL> def __call__ ( self , request , response ) : <EOL> args , kwargs = request . route_args , request . route_kwargs <EOL> if kwargs : <EOL> args = ( ) <EOL> return self . handler ( request , * args , ** kwargs ) <EOL> class WebappHandlerAdapter ( BaseHandlerAdapter ) : <EOL> """<STR_LIT>""" <EOL> def __call__ ( self , request , response ) : <EOL> handler = self . handler ( ) <EOL> handler . initialize ( request , response ) <EOL> method_name = _normalize_handler_method ( request . method ) <EOL> method = getattr ( handler , method_name , None ) <EOL> if not method : <EOL> abort ( <NUM_LIT> ) <EOL> args , kwargs = request . route_args , request . route_kwargs <EOL> if kwargs : <EOL> args = ( ) <EOL> try : <EOL> method ( * args , ** kwargs ) <EOL> except Exception , e : <EOL> handler . handle_exception ( e , request . app . debug ) <EOL> class Webapp2HandlerAdapter ( BaseHandlerAdapter ) : <EOL> """<STR_LIT>""" <EOL> def __call__ ( self , request , response ) : <EOL> handler = self . handler ( request , response ) <EOL> return handler . dispatch ( ) <EOL> class Router ( object ) : <EOL> """<STR_LIT>""" <EOL> route_class = SimpleRoute <EOL> match_routes = None <EOL> build_routes = None <EOL> handlers = None <EOL> def __init__ ( self , routes = None ) : <EOL> """<STR_LIT>""" <EOL> self . match_routes = [ ] <EOL> self . build_routes = { } <EOL> self . handlers = { } <EOL> if routes : <EOL> for route in routes : <EOL> self . add ( route ) <EOL> def add ( self , route ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( route , tuple ) : <EOL> route = self . route_class ( * route ) <EOL> for r in route . get_match_routes ( ) : <EOL> self . match_routes . append ( r ) <EOL> for name , r in route . get_build_routes ( ) : <EOL> self . build_routes [ name ] = r <EOL> def set_matcher ( self , func ) : <EOL> """<STR_LIT>""" <EOL> self . match = func . __get__ ( self , self . __class__ ) <EOL> def set_builder ( self , func ) : <EOL> """<STR_LIT>""" <EOL> self . build = func . __get__ ( self , self . __class__ ) <EOL> def set_dispatcher ( self , func ) : <EOL> """<STR_LIT>""" <EOL> self . dispatch = func . __get__ ( self , self . __class__ ) <EOL> def set_adapter ( self , func ) : <EOL> """<STR_LIT>""" <EOL> self . adapt = func . __get__ ( self , self . __class__ ) <EOL> def default_matcher ( self , request ) : <EOL> """<STR_LIT>""" <EOL> method_not_allowed = False <EOL> for route in self . match_routes : <EOL> try : <EOL> match = route . match ( request ) <EOL> if match : <EOL> return match <EOL> except exc . HTTPMethodNotAllowed : <EOL> method_not_allowed = True <EOL> if method_not_allowed : <EOL> raise exc . HTTPMethodNotAllowed ( ) <EOL> raise exc . HTTPNotFound ( ) <EOL> def default_builder ( self , request , name , args , kwargs ) : <EOL> """<STR_LIT>""" <EOL> route = self . build_routes . get ( name ) <EOL> if route is None : <EOL> raise KeyError ( '<STR_LIT>' % name ) <EOL> return route . build ( request , args , kwargs ) <EOL> def default_dispatcher ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> route , args , kwargs = rv = self . match ( request ) <EOL> request . route , request . route_args , request . route_kwargs = rv <EOL> if route . handler_adapter is None : <EOL> handler = route . handler <EOL> if isinstance ( handler , basestring ) : <EOL> if handler not in self . handlers : <EOL> self . handlers [ handler ] = handler = import_string ( handler ) <EOL> else : <EOL> handler = self . handlers [ handler ] <EOL> route . handler_adapter = self . adapt ( handler ) <EOL> return route . handler_adapter ( request , response ) <EOL> def default_adapter ( self , handler ) : <EOL> """<STR_LIT>""" <EOL> if inspect . isclass ( handler ) : <EOL> if _webapp and issubclass ( handler , _webapp . RequestHandler ) : <EOL> adapter = WebappHandlerAdapter <EOL> else : <EOL> adapter = Webapp2HandlerAdapter <EOL> else : <EOL> adapter = BaseHandlerAdapter <EOL> return adapter ( handler ) <EOL> def __repr__ ( self ) : <EOL> routes = self . match_routes + [ v for k , v in self . build_routes . iteritems ( ) if v not in self . match_routes ] <EOL> return '<STR_LIT>' % routes <EOL> match = default_matcher <EOL> build = default_builder <EOL> dispatch = default_dispatcher <EOL> adapt = default_adapter <EOL> class Config ( dict ) : <EOL> """<STR_LIT>""" <EOL> loaded = None <EOL> def __init__ ( self , defaults = None ) : <EOL> dict . __init__ ( self , defaults or ( ) ) <EOL> self . loaded = [ ] <EOL> def load_config ( self , key , default_values = None , user_values = None , <EOL> required_keys = None ) : <EOL> """<STR_LIT>""" <EOL> if key in self . loaded : <EOL> config = self [ key ] <EOL> else : <EOL> config = dict ( default_values or ( ) ) <EOL> if key in self : <EOL> config . update ( self [ key ] ) <EOL> self [ key ] = config <EOL> self . loaded . append ( key ) <EOL> if required_keys and not user_values : <EOL> self . _validate_required ( key , config , required_keys ) <EOL> if user_values : <EOL> config = config . copy ( ) <EOL> config . update ( user_values ) <EOL> if required_keys : <EOL> self . _validate_required ( key , config , required_keys ) <EOL> return config <EOL> def _validate_required ( self , key , config , required_keys ) : <EOL> missing = [ k for k in required_keys if config . get ( k ) is None ] <EOL> if missing : <EOL> raise Exception ( <EOL> '<STR_LIT>' % ( key , missing ) ) <EOL> class RequestContext ( object ) : <EOL> """<STR_LIT>""" <EOL> app = None <EOL> environ = None <EOL> def __init__ ( self , app , environ ) : <EOL> """<STR_LIT>""" <EOL> self . app = app <EOL> self . environ = environ <EOL> def __enter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> request = self . app . request_class ( self . environ ) <EOL> response = self . app . response_class ( ) <EOL> request . app = self . app <EOL> request . response = response <EOL> self . app . set_globals ( app = self . app , request = request ) <EOL> return request , response <EOL> def __exit__ ( self , exc_type , exc_value , traceback ) : <EOL> """<STR_LIT>""" <EOL> if exc_type is None or not self . app . debug : <EOL> self . app . clear_globals ( ) <EOL> class WSGIApplication ( object ) : <EOL> """<STR_LIT>""" <EOL> allowed_methods = frozenset ( ( '<STR_LIT:GET>' , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> request_class = Request <EOL> response_class = Response <EOL> router_class = Router <EOL> request_context_class = RequestContext <EOL> config_class = Config <EOL> debug = False <EOL> router = None <EOL> config = None <EOL> registry = None <EOL> error_handlers = None <EOL> app = None <EOL> request = None <EOL> active_instance = None <EOL> def __init__ ( self , routes = None , debug = False , config = None ) : <EOL> """<STR_LIT>""" <EOL> self . debug = debug <EOL> self . registry = { } <EOL> self . error_handlers = { } <EOL> self . set_globals ( app = self ) <EOL> self . config = self . config_class ( config ) <EOL> self . router = self . router_class ( routes ) <EOL> def set_globals ( self , app = None , request = None ) : <EOL> """<STR_LIT>""" <EOL> if _local is not None : <EOL> _local . app = app <EOL> _local . request = request <EOL> else : <EOL> WSGIApplication . app = WSGIApplication . active_instance = app <EOL> WSGIApplication . request = request <EOL> def clear_globals ( self ) : <EOL> """<STR_LIT>""" <EOL> if _local is not None : <EOL> _local . __release_local__ ( ) <EOL> else : <EOL> WSGIApplication . app = WSGIApplication . active_instance = None <EOL> WSGIApplication . request = None <EOL> def __call__ ( self , environ , start_response ) : <EOL> """<STR_LIT>""" <EOL> with self . request_context_class ( self , environ ) as ( request , response ) : <EOL> try : <EOL> if request . method not in self . allowed_methods : <EOL> raise exc . HTTPNotImplemented ( ) <EOL> rv = self . router . dispatch ( request , response ) <EOL> if rv is not None : <EOL> response = rv <EOL> except Exception , e : <EOL> try : <EOL> rv = self . handle_exception ( request , response , e ) <EOL> if rv is not None : <EOL> response = rv <EOL> except HTTPException , e : <EOL> response = e <EOL> except Exception , e : <EOL> response = self . _internal_error ( e ) <EOL> try : <EOL> return response ( environ , start_response ) <EOL> except Exception , e : <EOL> return self . _internal_error ( e ) ( environ , start_response ) <EOL> def _internal_error ( self , exception ) : <EOL> """<STR_LIT>""" <EOL> logging . exception ( exception ) <EOL> if self . debug : <EOL> lines = '<STR_LIT>' . join ( traceback . format_exception ( * sys . exc_info ( ) ) ) <EOL> html = _debug_template % ( cgi . escape ( lines , quote = True ) ) <EOL> return Response ( body = html , status = <NUM_LIT> ) <EOL> return exc . HTTPInternalServerError ( ) <EOL> def handle_exception ( self , request , response , e ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( e , HTTPException ) : <EOL> code = e . code <EOL> else : <EOL> code = <NUM_LIT> <EOL> handler = self . error_handlers . get ( code ) <EOL> if handler : <EOL> if isinstance ( handler , basestring ) : <EOL> self . error_handlers [ code ] = handler = import_string ( handler ) <EOL> return handler ( request , response , e ) <EOL> else : <EOL> raise <EOL> def run ( self , bare = False ) : <EOL> """<STR_LIT>""" <EOL> if _webapp_util : <EOL> if bare : <EOL> _webapp_util . run_bare_wsgi_app ( self ) <EOL> else : <EOL> _webapp_util . run_wsgi_app ( self ) <EOL> else : <EOL> handlers . CGIHandler ( ) . run ( self ) <EOL> def get_response ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . request_class . blank ( * args , ** kwargs ) . get_response ( self ) <EOL> _import_string_error = """<STR_LIT>""" <EOL> class ImportStringError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> import_name = None <EOL> exception = None <EOL> def __init__ ( self , import_name , exception ) : <EOL> self . import_name = import_name <EOL> self . exception = exception <EOL> msg = _import_string_error <EOL> name = '<STR_LIT>' <EOL> tracked = [ ] <EOL> for part in import_name . split ( '<STR_LIT:.>' ) : <EOL> name += ( name and '<STR_LIT:.>' ) + part <EOL> imported = import_string ( name , silent = True ) <EOL> if imported : <EOL> tracked . append ( ( name , imported . __file__ ) ) <EOL> else : <EOL> track = [ '<STR_LIT>' % rv for rv in tracked ] <EOL> track . append ( '<STR_LIT>' % name ) <EOL> msg = msg % ( import_name , exception . __class__ . __name__ , <EOL> str ( exception ) , '<STR_LIT:\n>' . join ( track ) ) <EOL> break <EOL> Exception . __init__ ( self , msg ) <EOL> _get_app_error = '<STR_LIT>' <EOL> _get_request_error = '<STR_LIT>' <EOL> def get_app ( ) : <EOL> """<STR_LIT>""" <EOL> if _local : <EOL> assert getattr ( _local , '<STR_LIT>' , None ) is not None , _get_app_error <EOL> else : <EOL> assert WSGIApplication . app is not None , _get_app_error <EOL> return WSGIApplication . app <EOL> def get_request ( ) : <EOL> """<STR_LIT>""" <EOL> if _local : <EOL> assert getattr ( _local , '<STR_LIT>' , None ) is not None , _get_request_error <EOL> else : <EOL> assert WSGIApplication . request is not None , _get_request_error <EOL> return WSGIApplication . request <EOL> def uri_for ( _name , _request = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> request = _request or get_request ( ) <EOL> return request . app . router . build ( request , _name , args , kwargs ) <EOL> def redirect ( uri , permanent = False , abort = False , code = None , body = None , <EOL> request = None , response = None ) : <EOL> """<STR_LIT>""" <EOL> if uri . startswith ( ( '<STR_LIT:.>' , '<STR_LIT:/>' ) ) : <EOL> request = request or get_request ( ) <EOL> uri = str ( urlparse . urljoin ( request . url , uri ) ) <EOL> if code is None : <EOL> if permanent : <EOL> code = <NUM_LIT> <EOL> else : <EOL> code = <NUM_LIT> <EOL> assert code in ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , '<STR_LIT>' <EOL> if abort : <EOL> _abort ( code , headers = [ ( '<STR_LIT>' , uri ) ] ) <EOL> if response is None : <EOL> request = request or get_request ( ) <EOL> response = request . app . response_class ( ) <EOL> else : <EOL> response . clear ( ) <EOL> response . headers [ '<STR_LIT>' ] = uri <EOL> response . status = code <EOL> if body is not None : <EOL> response . write ( body ) <EOL> return response <EOL> def redirect_to ( _name , _permanent = False , _abort = False , _code = None , <EOL> _body = None , _request = None , _response = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> uri = uri_for ( _name , _request = _request , * args , ** kwargs ) <EOL> return redirect ( uri , permanent = _permanent , abort = _abort , code = _code , <EOL> body = _body , request = _request , response = _response ) <EOL> def abort ( code , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> cls = exc . status_map . get ( code ) <EOL> if not cls : <EOL> raise KeyError ( '<STR_LIT>' % code ) <EOL> raise cls ( * args , ** kwargs ) <EOL> def import_string ( import_name , silent = False ) : <EOL> """<STR_LIT>""" <EOL> import_name = _to_utf8 ( import_name ) <EOL> try : <EOL> if '<STR_LIT:.>' in import_name : <EOL> module , obj = import_name . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> return getattr ( __import__ ( module , None , None , [ obj ] ) , obj ) <EOL> else : <EOL> return __import__ ( import_name ) <EOL> except ( ImportError , AttributeError ) , e : <EOL> if not silent : <EOL> raise ImportStringError ( import_name , e ) , None , sys . exc_info ( ) [ <NUM_LIT:2> ] <EOL> def _urlunsplit ( scheme = None , netloc = None , path = None , query = None , <EOL> fragment = None ) : <EOL> """<STR_LIT>""" <EOL> if not scheme or not netloc : <EOL> scheme = None <EOL> netloc = None <EOL> if path : <EOL> path = urllib . quote ( _to_utf8 ( path ) ) <EOL> if query and not isinstance ( query , basestring ) : <EOL> if isinstance ( query , dict ) : <EOL> query = query . iteritems ( ) <EOL> query = urllib . urlencode ( sorted ( query ) ) <EOL> if fragment : <EOL> fragment = urllib . quote ( _to_utf8 ( fragment ) ) <EOL> return urlparse . urlunsplit ( ( scheme , netloc , path , query , fragment ) ) <EOL> def _get_handler_methods ( handler ) : <EOL> """<STR_LIT>""" <EOL> methods = [ ] <EOL> for method in get_app ( ) . allowed_methods : <EOL> if getattr ( handler , _normalize_handler_method ( method ) , None ) : <EOL> methods . append ( method ) <EOL> return methods <EOL> def _normalize_handler_method ( method ) : <EOL> """<STR_LIT>""" <EOL> return method . lower ( ) . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> def _to_utf8 ( value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , str ) : <EOL> return value <EOL> return value . encode ( '<STR_LIT:utf-8>' ) <EOL> def _parse_route_template ( template , default_sufix = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> variables = { } <EOL> reverse_template = pattern = '<STR_LIT>' <EOL> args_count = last = <NUM_LIT:0> <EOL> for match in _route_re . finditer ( template ) : <EOL> part = template [ last : match . start ( ) ] <EOL> name = match . group ( <NUM_LIT:1> ) <EOL> expr = match . group ( <NUM_LIT:2> ) or default_sufix <EOL> last = match . end ( ) <EOL> if not name : <EOL> name = '<STR_LIT>' % args_count <EOL> args_count += <NUM_LIT:1> <EOL> pattern += '<STR_LIT>' % ( re . escape ( part ) , name , expr ) <EOL> reverse_template += '<STR_LIT>' % ( part , name ) <EOL> variables [ name ] = re . compile ( '<STR_LIT>' % expr ) <EOL> part = template [ last : ] <EOL> kwargs_count = len ( variables ) - args_count <EOL> reverse_template += part <EOL> regex = re . compile ( '<STR_LIT>' % ( pattern , re . escape ( part ) ) ) <EOL> return regex , reverse_template , args_count , kwargs_count , variables <EOL> def _get_route_variables ( match , default_kwargs = None ) : <EOL> """<STR_LIT>""" <EOL> kwargs = default_kwargs or { } <EOL> kwargs . update ( match . groupdict ( ) ) <EOL> if kwargs : <EOL> args = tuple ( value [ <NUM_LIT:1> ] for value in sorted ( <EOL> ( int ( key [ <NUM_LIT:2> : - <NUM_LIT:2> ] ) , kwargs . pop ( key ) ) for key in kwargs . keys ( ) if key . startswith ( '<STR_LIT>' ) and key . endswith ( '<STR_LIT>' ) ) ) <EOL> else : <EOL> args = ( ) <EOL> return args , kwargs <EOL> def _set_thread_safe_app ( ) : <EOL> """<STR_LIT>""" <EOL> if _local is not None : <EOL> WSGIApplication . app = WSGIApplication . active_instance = _local ( '<STR_LIT>' ) <EOL> WSGIApplication . request = _local ( '<STR_LIT>' ) <EOL> Request . ResponseClass = Response <EOL> Response . RequestClass = Request <EOL> _abort = abort <EOL> _set_thread_safe_app ( ) <EOL> try : <EOL> from google . appengine . ext . webapp import util as _webapp_util <EOL> except ImportError : <EOL> pass </s>
