<s> """<STR_LIT>""" <EOL> import urllib <EOL> import re <EOL> import sys <EOL> reload ( sys ) <EOL> sys . setdefaultencoding ( '<STR_LIT:utf-8>' ) <EOL> import os <EOL> class cached_property ( object ) : <EOL> def __init__ ( self , func ) : <EOL> self . __name__ = func . __name__ <EOL> self . __module__ = func . __module__ <EOL> self . __doc__ = func . __doc__ <EOL> self . func = func <EOL> def __get__ ( self , obj , type = None ) : <EOL> if obj is None : <EOL> return self <EOL> value = obj . __dict__ . get ( self . __name__ , None ) <EOL> if value is None : <EOL> value = self . func ( obj ) <EOL> obj . __dict__ [ self . __name__ ] = value <EOL> return value <EOL> def secure_path ( path ) : <EOL> _split = re . compile ( r'<STR_LIT>' % re . escape ( '<STR_LIT>' . join ( <EOL> [ os . path . sep , os . path . altsep or '<STR_LIT>' ] ) ) ) <EOL> return _split . sub ( '<STR_LIT>' , path ) <EOL> class BatPod ( object ) : <EOL> def __init__ ( self , import_name ) : <EOL> self . import_name = import_name <EOL> self . url_map = { } <EOL> self . error_handler = { } <EOL> self . before_request_funcs = [ ] <EOL> self . after_request_funcs = [ ] <EOL> self . teardown_request_funcs = [ ] <EOL> static_rule = r'<STR_LIT>' <EOL> static_rule_re = re . compile ( '<STR_LIT>' % static_rule ) <EOL> self . url_map . setdefault ( '<STR_LIT:GET>' , [ ] ) . append ( ( static_rule_re , static_rule , self . serve_static ( ) ) ) <EOL> def wsgi_app ( self , environ , start_response ) : <EOL> request = Request ( environ ) <EOL> response = self . dispatch_request ( request ) <EOL> return Response ( response , start_response ) <EOL> def route ( self , rule , methods = [ '<STR_LIT:GET>' ] ) : <EOL> def decorator ( func ) : <EOL> self . add_url_rule ( methods , rule , func ) <EOL> return func <EOL> return decorator <EOL> def error ( self , code ) : <EOL> def decorator ( func ) : <EOL> self . error_handler [ code ] = func <EOL> return func <EOL> return decorator <EOL> def add_url_rule ( self , methods , rule , func ) : <EOL> if not rule . endswith ( '<STR_LIT:/>' ) : <EOL> rule = rule + '<STR_LIT:/>' <EOL> rule_re = re . compile ( '<STR_LIT>' % rule ) <EOL> for method in methods : <EOL> self . url_map . setdefault ( method , [ ] ) . append ( ( rule_re , rule , func ) ) <EOL> def before_request ( self , func ) : <EOL> self . before_request_funcs . append ( func ) <EOL> return func <EOL> def after_request ( self , func ) : <EOL> self . after_request_funcs . append ( func ) <EOL> return func <EOL> def teardown_request ( self , func ) : <EOL> self . teardown_request_funcs . append ( func ) <EOL> return func <EOL> def get_root ( self ) : <EOL> mod = sys . modules . get ( self . import_name ) <EOL> if mod is not None and self . import_name != '<STR_LIT:__main__>' : <EOL> return os . path . dirname ( os . path . abspath ( mod . __file__ ) ) <EOL> else : <EOL> return os . getcwd ( ) <EOL> def serve_static ( self ) : <EOL> static_root = os . path . join ( self . get_root ( ) , '<STR_LIT>' ) <EOL> def inner ( request , filepath ) : <EOL> filepath = secure_path ( filepath ) <EOL> desired_path = os . path . join ( static_root , filepath ) <EOL> if not os . path . exists ( desired_path ) : <EOL> raise HTTPException ( <NUM_LIT> ) <EOL> if not os . access ( desired_path , os . R_OK ) : <EOL> raise HTTPException ( <NUM_LIT> ) <EOL> import mimetypes <EOL> ct = mimetypes . guess_type ( desired_path ) [ <NUM_LIT:0> ] <EOL> content = open ( desired_path , '<STR_LIT:r>' ) . read ( ) <EOL> response = Response ( content ) <EOL> if ct is not None : <EOL> response . content_type = ct <EOL> return response <EOL> return inner <EOL> def dispatch_request ( self , request ) : <EOL> try : <EOL> try : <EOL> request_rule = request . rule <EOL> for rule_tuple in self . url_map [ request . method ] : <EOL> match = rule_tuple [ <NUM_LIT:0> ] . search ( request_rule ) <EOL> if match is not None : <EOL> view = rule_tuple [ <NUM_LIT:2> ] <EOL> return view ( request , ** match . groupdict ( ) ) <EOL> except KeyError : <EOL> raise HTTPException ( <NUM_LIT> ) <EOL> else : <EOL> raise HTTPException ( <NUM_LIT> ) <EOL> except HTTPException , e : <EOL> if e . code in self . error_handler : <EOL> view = self . error_handler [ e . code ] <EOL> e_response = Response ( view ( ) ) <EOL> e_response . set_status ( e . code ) <EOL> return e_response <EOL> else : <EOL> return e <EOL> def __call__ ( self , environ , start_response ) : <EOL> return self . wsgi_app ( environ , start_response ) <EOL> def run ( self , host = None , port = None , debug = False ) : <EOL> if host is None : <EOL> host = '<STR_LIT:127.0.0.1>' <EOL> if port is None : <EOL> port = <NUM_LIT> <EOL> if debug : <EOL> from exts . serving import run_server <EOL> run_server ( host , port , self ) <EOL> else : <EOL> from wsgiref . simple_server import make_server <EOL> make_server ( host , port , self ) . serve_forever ( ) <EOL> class Request ( object ) : <EOL> charset = '<STR_LIT:utf-8>' <EOL> max_content_length = None <EOL> def __init__ ( self , environ ) : <EOL> self . environ = environ <EOL> @ property <EOL> def url ( self ) : <EOL> parts = [ self . environ [ '<STR_LIT>' ] , '<STR_LIT>' , self . host ] <EOL> parts . append ( urllib . quote ( self . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) . rstrip ( '<STR_LIT:/>' ) ) ) <EOL> parts . append ( urllib . quote ( '<STR_LIT:/>' + self . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) . lstrip ( '<STR_LIT:/>' ) ) ) <EOL> qs = self . environ . get ( '<STR_LIT>' ) <EOL> if qs : <EOL> parts . append ( '<STR_LIT:?>' + qs ) <EOL> return '<STR_LIT>' . join ( parts ) <EOL> @ property <EOL> def rule ( self ) : <EOL> parts = [ ] <EOL> parts . append ( self . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) . rstrip ( '<STR_LIT:/>' ) ) <EOL> parts . append ( '<STR_LIT:/>' + self . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) . lstrip ( '<STR_LIT:/>' ) ) <EOL> return '<STR_LIT>' . join ( parts ) . decode ( '<STR_LIT:utf-8>' ) <EOL> @ property <EOL> def args ( self ) : <EOL> from urlparse import parse_qs <EOL> raw_args = parse_qs ( self . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) , keep_blank_values = <NUM_LIT:1> ) <EOL> args = { } <EOL> for key , value in raw_args . items ( ) : <EOL> if len ( value ) <= <NUM_LIT:1> : <EOL> args [ key ] = value [ <NUM_LIT:0> ] <EOL> else : <EOL> args [ key ] = value <EOL> return args <EOL> @ property <EOL> def headers ( self ) : <EOL> return self . environ <EOL> @ property <EOL> def method ( self ) : <EOL> return self . environ . get ( '<STR_LIT>' , '<STR_LIT:GET>' ) . upper ( ) <EOL> @ property <EOL> def cookies ( self ) : <EOL> from Cookie import SimpleCookie <EOL> cookie = SimpleCookie ( ) <EOL> cookie . load ( self . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> result = { } <EOL> for key , value in cookie . iteritems ( ) : <EOL> result [ key ] = value <EOL> return result <EOL> @ cached_property <EOL> def forms ( self ) : <EOL> import StringIO <EOL> import cgi <EOL> body = self . environ [ '<STR_LIT>' ] . read ( self . content_length ) <EOL> raw_forms = cgi . FieldStorage ( fp = StringIO . StringIO ( body ) , environ = self . environ , keep_blank_values = True ) <EOL> forms = { } <EOL> for field , data in raw_forms : <EOL> if isinstance ( data , list ) : <EOL> forms [ field ] = [ d . value for d in data ] <EOL> elif data . filename : <EOL> forms [ field ] = data <EOL> else : <EOL> forms [ field ] = data . value <EOL> return forms <EOL> @ property <EOL> def content_length ( self ) : <EOL> return int ( self . environ . get ( '<STR_LIT>' , '<STR_LIT:0>' ) ) <EOL> @ property <EOL> def host ( self ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in self . environ : <EOL> return self . environ [ '<STR_LIT>' ] <EOL> elif '<STR_LIT>' in self . environ : <EOL> return self . environ [ '<STR_LIT>' ] <EOL> result = environ [ '<STR_LIT>' ] <EOL> if ( environ [ '<STR_LIT>' ] , environ [ '<STR_LIT>' ] ) not in ( ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT:http>' , '<STR_LIT>' ) ) : <EOL> result += '<STR_LIT::>' + environ [ '<STR_LIT>' ] <EOL> return result <EOL> @ property <EOL> def is_xhr ( self ) : <EOL> return self . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) . lower ( ) == '<STR_LIT>' <EOL> @ property <EOL> def is_secure ( self ) : <EOL> return self . environ [ '<STR_LIT>' ] == '<STR_LIT>' <EOL> class Response ( object ) : <EOL> def __init__ ( self , response = '<STR_LIT>' , start_response = None ) : <EOL> self . status = '<STR_LIT>' <EOL> self . content_type = '<STR_LIT>' <EOL> self . headers = [ ] <EOL> self . body = '<STR_LIT>' <EOL> if isinstance ( response , Response ) : <EOL> self . __dict__ = response . __dict__ <EOL> elif isinstance ( response , HTTPException ) : <EOL> self . status = '<STR_LIT>' % ( response . code , response . name ) <EOL> self . body = response . get_error_body ( ) <EOL> else : <EOL> self . body = response <EOL> if isinstance ( self . body , unicode ) : <EOL> self . body = self . body . encode ( '<STR_LIT:utf-8>' ) <EOL> if start_response is not None : <EOL> headers = [ ( '<STR_LIT:Content-Type>' , "<STR_LIT>" % self . content_type ) ] + self . headers <EOL> start_response ( self . status , headers ) <EOL> def add_header ( self , key , value ) : <EOL> self . headers . append ( ( key , value ) ) <EOL> def set_status ( self , code ) : <EOL> self . status = "<STR_LIT>" % ( code , HTTP_STATUS_CODES [ code ] ) <EOL> def __iter__ ( self ) : <EOL> yield self . body <EOL> HTTP_STATUS_CODES = { <EOL> <NUM_LIT:100> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT:200> : '<STR_LIT:OK>' , <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> <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> <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> <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> <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> <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> <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> class HTTPException ( Exception ) : <EOL> def __init__ ( self , code ) : <EOL> self . code = code <EOL> if code not in HTTP_STATUS_CODES : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> self . name = HTTP_STATUS_CODES [ code ] <EOL> super ( HTTPException , self ) . __init__ ( '<STR_LIT>' % ( self . code , self . name ) ) <EOL> def get_error_body ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) % { <EOL> '<STR_LIT:code>' : self . code , <EOL> '<STR_LIT:name>' : self . name <EOL> } <EOL> def abort ( code ) : <EOL> """<STR_LIT>""" <EOL> raise HTTPException ( code ) <EOL> def redirect ( url ) : <EOL> response = Response ( ) <EOL> response . set_status ( <NUM_LIT> ) <EOL> response . content_type = '<STR_LIT>' <EOL> response . add_header ( '<STR_LIT>' , url ) <EOL> return response </s>
