<s> """<STR_LIT>""" <EOL> import webapi as web <EOL> import webapi , wsgi , utils <EOL> import debugerror <EOL> from utils import lstrips , safeunicode <EOL> import sys <EOL> import urllib <EOL> import traceback <EOL> import itertools <EOL> import os <EOL> import types <EOL> from exceptions import SystemExit <EOL> try : <EOL> import wsgiref . handlers <EOL> except ImportError : <EOL> pass <EOL> __all__ = [ <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> class application : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , mapping = ( ) , fvars = { } , autoreload = None ) : <EOL> if autoreload is None : <EOL> autoreload = web . config . get ( '<STR_LIT>' , False ) <EOL> self . init_mapping ( mapping ) <EOL> self . fvars = fvars <EOL> self . processors = [ ] <EOL> self . add_processor ( loadhook ( self . _load ) ) <EOL> self . add_processor ( unloadhook ( self . _unload ) ) <EOL> if autoreload : <EOL> def main_module_name ( ) : <EOL> mod = sys . modules [ '<STR_LIT:__main__>' ] <EOL> file = getattr ( mod , '<STR_LIT>' , None ) <EOL> return file and os . path . splitext ( os . path . basename ( file ) ) [ <NUM_LIT:0> ] <EOL> def modname ( fvars ) : <EOL> """<STR_LIT>""" <EOL> file , name = fvars . get ( '<STR_LIT>' ) , fvars . get ( '<STR_LIT>' ) <EOL> if file is None or name is None : <EOL> return None <EOL> if name == '<STR_LIT:__main__>' : <EOL> name = main_module_name ( ) <EOL> return name <EOL> mapping_name = utils . dictfind ( fvars , mapping ) <EOL> module_name = modname ( fvars ) <EOL> def reload_mapping ( ) : <EOL> """<STR_LIT>""" <EOL> mod = __import__ ( module_name , None , None , [ '<STR_LIT>' ] ) <EOL> mapping = getattr ( mod , mapping_name , None ) <EOL> if mapping : <EOL> self . fvars = mod . __dict__ <EOL> self . init_mapping ( mapping ) <EOL> self . add_processor ( loadhook ( Reloader ( ) ) ) <EOL> if mapping_name and module_name : <EOL> self . add_processor ( loadhook ( reload_mapping ) ) <EOL> if main_module_name ( ) and '<STR_LIT:__main__>' in sys . argv : <EOL> try : <EOL> __import__ ( main_module_name ( ) ) <EOL> except ImportError : <EOL> pass <EOL> def _load ( self ) : <EOL> web . ctx . app_stack . append ( self ) <EOL> def _unload ( self ) : <EOL> web . ctx . app_stack = web . ctx . app_stack [ : - <NUM_LIT:1> ] <EOL> if web . ctx . app_stack : <EOL> oldctx = web . ctx . get ( '<STR_LIT>' ) <EOL> if oldctx : <EOL> web . ctx . home = oldctx . home <EOL> web . ctx . homepath = oldctx . homepath <EOL> web . ctx . path = oldctx . path <EOL> web . ctx . fullpath = oldctx . fullpath <EOL> def _cleanup ( self ) : <EOL> utils . ThreadedDict . clear_all ( ) <EOL> def init_mapping ( self , mapping ) : <EOL> self . mapping = list ( utils . group ( mapping , <NUM_LIT:2> ) ) <EOL> def add_mapping ( self , pattern , classname ) : <EOL> self . mapping . append ( ( pattern , classname ) ) <EOL> def add_processor ( self , processor ) : <EOL> """<STR_LIT>""" <EOL> self . processors . append ( processor ) <EOL> def request ( self , localpart = '<STR_LIT:/>' , method = '<STR_LIT:GET>' , data = None , <EOL> host = "<STR_LIT>" , headers = None , https = False , ** kw ) : <EOL> """<STR_LIT>""" <EOL> path , maybe_query = urllib . splitquery ( localpart ) <EOL> query = maybe_query or "<STR_LIT>" <EOL> if '<STR_LIT>' in kw : <EOL> env = kw [ '<STR_LIT>' ] <EOL> else : <EOL> env = { } <EOL> env = dict ( env , HTTP_HOST = host , REQUEST_METHOD = method , PATH_INFO = path , QUERY_STRING = query , HTTPS = str ( https ) ) <EOL> headers = headers or { } <EOL> for k , v in headers . items ( ) : <EOL> env [ '<STR_LIT>' + k . upper ( ) . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) ] = v <EOL> if '<STR_LIT>' in env : <EOL> env [ '<STR_LIT>' ] = env . pop ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in env : <EOL> env [ '<STR_LIT>' ] = env . pop ( '<STR_LIT>' ) <EOL> if method not in [ "<STR_LIT>" , "<STR_LIT:GET>" ] : <EOL> data = data or '<STR_LIT>' <EOL> import StringIO <EOL> if isinstance ( data , dict ) : <EOL> q = urllib . urlencode ( data ) <EOL> else : <EOL> q = data <EOL> env [ '<STR_LIT>' ] = StringIO . StringIO ( q ) <EOL> if not env . get ( '<STR_LIT>' , '<STR_LIT>' ) . lower ( ) . startswith ( '<STR_LIT>' ) and '<STR_LIT>' not in env : <EOL> env [ '<STR_LIT>' ] = len ( q ) <EOL> response = web . storage ( ) <EOL> def start_response ( status , headers ) : <EOL> response . status = status <EOL> response . headers = dict ( headers ) <EOL> response . header_items = headers <EOL> response . data = "<STR_LIT>" . join ( self . wsgifunc ( ) ( env , start_response ) ) <EOL> return response <EOL> def browser ( self ) : <EOL> import browser <EOL> return browser . AppBrowser ( self ) <EOL> def handle ( self ) : <EOL> fn , args = self . _match ( self . mapping , web . ctx . path ) <EOL> return self . _delegate ( fn , self . fvars , args ) <EOL> def handle_with_processors ( self ) : <EOL> def process ( processors ) : <EOL> try : <EOL> if processors : <EOL> p , processors = processors [ <NUM_LIT:0> ] , processors [ <NUM_LIT:1> : ] <EOL> return p ( lambda : process ( processors ) ) <EOL> else : <EOL> return self . handle ( ) <EOL> except web . HTTPError : <EOL> raise <EOL> except ( KeyboardInterrupt , SystemExit ) : <EOL> raise <EOL> except : <EOL> print >> web . debug , traceback . format_exc ( ) <EOL> raise self . internalerror ( ) <EOL> return process ( self . processors ) <EOL> def wsgifunc ( self , * middleware ) : <EOL> """<STR_LIT>""" <EOL> def peep ( iterator ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> firstchunk = iterator . next ( ) <EOL> except StopIteration : <EOL> firstchunk = '<STR_LIT>' <EOL> return itertools . chain ( [ firstchunk ] , iterator ) <EOL> def is_generator ( x ) : return x and hasattr ( x , '<STR_LIT>' ) <EOL> def wsgi ( env , start_resp ) : <EOL> self . _cleanup ( ) <EOL> self . load ( env ) <EOL> try : <EOL> if web . ctx . method . upper ( ) != web . ctx . method : <EOL> raise web . nomethod ( ) <EOL> result = self . handle_with_processors ( ) <EOL> if is_generator ( result ) : <EOL> result = peep ( result ) <EOL> else : <EOL> result = [ result ] <EOL> except web . HTTPError , e : <EOL> result = [ e . data ] <EOL> result = web . safestr ( iter ( result ) ) <EOL> status , headers = web . ctx . status , web . ctx . headers <EOL> start_resp ( status , headers ) <EOL> def cleanup ( ) : <EOL> self . _cleanup ( ) <EOL> yield '<STR_LIT>' <EOL> return itertools . chain ( result , cleanup ( ) ) <EOL> for m in middleware : <EOL> wsgi = m ( wsgi ) <EOL> return wsgi <EOL> def run ( self , * middleware ) : <EOL> """<STR_LIT>""" <EOL> return wsgi . runwsgi ( self . wsgifunc ( * middleware ) ) <EOL> def cgirun ( self , * middleware ) : <EOL> """<STR_LIT>""" <EOL> wsgiapp = self . wsgifunc ( * middleware ) <EOL> try : <EOL> from google . appengine . ext . webapp . util import run_wsgi_app <EOL> return run_wsgi_app ( wsgiapp ) <EOL> except ImportError : <EOL> return wsgiref . handlers . CGIHandler ( ) . run ( wsgiapp ) <EOL> def load ( self , env ) : <EOL> """<STR_LIT>""" <EOL> ctx = web . ctx <EOL> ctx . clear ( ) <EOL> ctx . status = '<STR_LIT>' <EOL> ctx . headers = [ ] <EOL> ctx . output = '<STR_LIT>' <EOL> ctx . environ = ctx . env = env <EOL> ctx . host = env . get ( '<STR_LIT>' ) <EOL> if env . get ( '<STR_LIT>' ) in [ '<STR_LIT:http>' , '<STR_LIT>' ] : <EOL> ctx . protocol = env [ '<STR_LIT>' ] <EOL> elif env . get ( '<STR_LIT>' , '<STR_LIT>' ) . lower ( ) in [ '<STR_LIT>' , '<STR_LIT:true>' , '<STR_LIT:1>' ] : <EOL> ctx . protocol = '<STR_LIT>' <EOL> else : <EOL> ctx . protocol = '<STR_LIT:http>' <EOL> ctx . homedomain = ctx . protocol + '<STR_LIT>' + env . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ctx . homepath = os . environ . get ( '<STR_LIT>' , env . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> ctx . home = ctx . homedomain + ctx . homepath <EOL> ctx . realhome = ctx . home <EOL> ctx . ip = env . get ( '<STR_LIT>' ) <EOL> ctx . method = env . get ( '<STR_LIT>' ) <EOL> ctx . path = env . get ( '<STR_LIT>' ) <EOL> if env . get ( '<STR_LIT>' , '<STR_LIT>' ) . startswith ( '<STR_LIT>' ) : <EOL> ctx . path = lstrips ( env . get ( '<STR_LIT>' ) . split ( '<STR_LIT:?>' ) [ <NUM_LIT:0> ] , ctx . homepath ) <EOL> ctx . path = urllib . unquote ( ctx . path ) <EOL> if env . get ( '<STR_LIT>' ) : <EOL> ctx . query = '<STR_LIT:?>' + env . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> ctx . query = '<STR_LIT>' <EOL> ctx . fullpath = ctx . path + ctx . query <EOL> for k , v in ctx . iteritems ( ) : <EOL> if isinstance ( v , str ) : <EOL> ctx [ k ] = v . decode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) <EOL> ctx . status = '<STR_LIT>' <EOL> ctx . app_stack = [ ] <EOL> def _delegate ( self , f , fvars , args = [ ] ) : <EOL> def handle_class ( cls ) : <EOL> meth = web . ctx . method <EOL> if meth == '<STR_LIT>' and not hasattr ( cls , meth ) : <EOL> meth = '<STR_LIT:GET>' <EOL> if not hasattr ( cls , meth ) : <EOL> raise web . nomethod ( cls ) <EOL> tocall = getattr ( cls ( ) , meth ) <EOL> return tocall ( * args ) <EOL> def is_class ( o ) : return isinstance ( o , ( types . ClassType , type ) ) <EOL> if f is None : <EOL> raise web . notfound ( ) <EOL> elif isinstance ( f , application ) : <EOL> return f . handle_with_processors ( ) <EOL> elif is_class ( f ) : <EOL> return handle_class ( f ) <EOL> elif isinstance ( f , basestring ) : <EOL> if f . startswith ( '<STR_LIT>' ) : <EOL> url = f . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> if web . ctx . method == "<STR_LIT:GET>" : <EOL> x = web . ctx . env . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if x : <EOL> url += '<STR_LIT:?>' + x <EOL> raise web . redirect ( url ) <EOL> elif '<STR_LIT:.>' in f : <EOL> mod , cls = f . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> mod = __import__ ( mod , None , None , [ '<STR_LIT>' ] ) <EOL> cls = getattr ( mod , cls ) <EOL> else : <EOL> cls = fvars [ f ] <EOL> return handle_class ( cls ) <EOL> elif hasattr ( f , '<STR_LIT>' ) : <EOL> return f ( ) <EOL> else : <EOL> return web . notfound ( ) <EOL> def _match ( self , mapping , value ) : <EOL> for pat , what in mapping : <EOL> if isinstance ( what , application ) : <EOL> if value . startswith ( pat ) : <EOL> f = lambda : self . _delegate_sub_application ( pat , what ) <EOL> return f , None <EOL> else : <EOL> continue <EOL> elif isinstance ( what , basestring ) : <EOL> what , result = utils . re_subm ( '<STR_LIT>' + pat + '<STR_LIT:$>' , what , value ) <EOL> else : <EOL> result = utils . re_compile ( '<STR_LIT>' + pat + '<STR_LIT:$>' ) . match ( value ) <EOL> if result : <EOL> return what , [ x for x in result . groups ( ) ] <EOL> return None , None <EOL> def _delegate_sub_application ( self , dir , app ) : <EOL> """<STR_LIT>""" <EOL> web . ctx . _oldctx = web . storage ( web . ctx ) <EOL> web . ctx . home += dir <EOL> web . ctx . homepath += dir <EOL> web . ctx . path = web . ctx . path [ len ( dir ) : ] <EOL> web . ctx . fullpath = web . ctx . fullpath [ len ( dir ) : ] <EOL> return app . handle_with_processors ( ) <EOL> def get_parent_app ( self ) : <EOL> if self in web . ctx . app_stack : <EOL> index = web . ctx . app_stack . index ( self ) <EOL> if index > <NUM_LIT:0> : <EOL> return web . ctx . app_stack [ index - <NUM_LIT:1> ] <EOL> def notfound ( self ) : <EOL> """<STR_LIT>""" <EOL> parent = self . get_parent_app ( ) <EOL> if parent : <EOL> return parent . notfound ( ) <EOL> else : <EOL> return web . _NotFound ( ) <EOL> def internalerror ( self ) : <EOL> """<STR_LIT>""" <EOL> parent = self . get_parent_app ( ) <EOL> if parent : <EOL> return parent . internalerror ( ) <EOL> elif web . config . get ( '<STR_LIT>' ) : <EOL> import debugerror <EOL> return debugerror . debugerror ( ) <EOL> else : <EOL> return web . _InternalError ( ) <EOL> class auto_application ( application ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> application . __init__ ( self ) <EOL> class metapage ( type ) : <EOL> def __init__ ( klass , name , bases , attrs ) : <EOL> type . __init__ ( klass , name , bases , attrs ) <EOL> path = attrs . get ( '<STR_LIT:path>' , '<STR_LIT:/>' + name ) <EOL> if path is not None : <EOL> self . add_mapping ( path , klass ) <EOL> class page : <EOL> path = None <EOL> __metaclass__ = metapage <EOL> self . page = page <EOL> subdir_application = application <EOL> class subdomain_application ( application ) : <EOL> """<STR_LIT>""" <EOL> def handle ( self ) : <EOL> host = web . ctx . host . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] <EOL> fn , args = self . _match ( self . mapping , host ) <EOL> return self . _delegate ( fn , self . fvars , args ) <EOL> def _match ( self , mapping , value ) : <EOL> for pat , what in mapping : <EOL> if isinstance ( what , basestring ) : <EOL> what , result = utils . re_subm ( '<STR_LIT>' + pat + '<STR_LIT:$>' , what , value ) <EOL> else : <EOL> result = utils . re_compile ( '<STR_LIT>' + pat + '<STR_LIT:$>' ) . match ( value ) <EOL> if result : <EOL> return what , [ x for x in result . groups ( ) ] <EOL> return None , None <EOL> def loadhook ( h ) : <EOL> """<STR_LIT>""" <EOL> def processor ( handler ) : <EOL> h ( ) <EOL> return handler ( ) <EOL> return processor <EOL> def unloadhook ( h ) : <EOL> """<STR_LIT>""" <EOL> def processor ( handler ) : <EOL> try : <EOL> result = handler ( ) <EOL> is_generator = result and hasattr ( result , '<STR_LIT>' ) <EOL> except : <EOL> h ( ) <EOL> raise <EOL> if is_generator : <EOL> return wrap ( result ) <EOL> else : <EOL> h ( ) <EOL> return result <EOL> def wrap ( result ) : <EOL> def next ( ) : <EOL> try : <EOL> return result . next ( ) <EOL> except : <EOL> h ( ) <EOL> raise <EOL> result = iter ( result ) <EOL> while True : <EOL> yield next ( ) <EOL> return processor <EOL> def autodelegate ( prefix = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> def internal ( self , arg ) : <EOL> if '<STR_LIT:/>' in arg : <EOL> first , rest = arg . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> func = prefix + first <EOL> args = [ '<STR_LIT:/>' + rest ] <EOL> else : <EOL> func = prefix + arg <EOL> args = [ ] <EOL> if hasattr ( self , func ) : <EOL> try : <EOL> return getattr ( self , func ) ( * args ) <EOL> except TypeError : <EOL> raise web . notfound ( ) <EOL> else : <EOL> raise web . notfound ( ) <EOL> return internal <EOL> class Reloader : <EOL> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> if sys . platform . startswith ( '<STR_LIT>' ) : <EOL> SUFFIX = '<STR_LIT>' <EOL> else : <EOL> SUFFIX = '<STR_LIT>' <EOL> def __init__ ( self ) : <EOL> self . mtimes = { } <EOL> def __call__ ( self ) : <EOL> for mod in sys . modules . values ( ) : <EOL> self . check ( mod ) <EOL> def check ( self , mod ) : <EOL> if not ( mod and hasattr ( mod , '<STR_LIT>' ) and mod . __file__ ) : <EOL> return <EOL> try : <EOL> mtime = os . stat ( mod . __file__ ) . st_mtime <EOL> except ( OSError , IOError ) : <EOL> return <EOL> if mod . __file__ . endswith ( self . __class__ . SUFFIX ) and os . path . exists ( mod . __file__ [ : - <NUM_LIT:1> ] ) : <EOL> mtime = max ( os . stat ( mod . __file__ [ : - <NUM_LIT:1> ] ) . st_mtime , mtime ) <EOL> if mod not in self . mtimes : <EOL> self . mtimes [ mod ] = mtime <EOL> elif self . mtimes [ mod ] < mtime : <EOL> try : <EOL> reload ( mod ) <EOL> self . mtimes [ mod ] = mtime <EOL> except ImportError : <EOL> pass <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import doctest <EOL> doctest . testmod ( ) </s>
