<s> import cgi <EOL> import re <EOL> __all__ = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> class HttpError ( Exception ) : <EOL> pass <EOL> class HttpRequest ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , environ ) : <EOL> self . method = environ [ '<STR_LIT>' ] <EOL> if self . method not in ( '<STR_LIT:POST>' , '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise HttpError , "<STR_LIT>" <EOL> self . POST = self . GET = { } <EOL> if len ( environ [ '<STR_LIT>' ] ) : <EOL> self . GET = cgi . parse_qs ( environ [ '<STR_LIT>' ] ) <EOL> if self . method == '<STR_LIT:POST>' : <EOL> self . POST = cgi . FieldStorage ( fp = environ [ '<STR_LIT>' ] , environ = environ , keep_blank_values = <NUM_LIT:1> ) <EOL> self . REQUEST = self . GET . copy ( ) <EOL> self . REQUEST . update ( self . POST ) <EOL> class HttpResponse ( object ) : <EOL> """<STR_LIT>""" <EOL> status_codes = { <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> } <EOL> def __init__ ( self , content = '<STR_LIT>' , headers = { } , status_code = <NUM_LIT:200> ) : <EOL> self . status_code = status_code <EOL> self . set_content ( content ) <EOL> self . _headers = headers <EOL> self . _headers [ '<STR_LIT>' ] = str ( len ( content ) ) <EOL> if not self . _headers . get ( '<STR_LIT>' ) : <EOL> self . _headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> def get_status ( self ) : <EOL> if self . status_code not in self . status_codes : <EOL> self . status_code = <NUM_LIT> <EOL> return "<STR_LIT>" % ( self . status_code , self . status_codes [ self . status_code ] ) <EOL> def set_status ( self , code ) : <EOL> self . status_code = code <EOL> def get_headers ( self ) : <EOL> return list ( self . _headers . iteritems ( ) ) <EOL> def set_headers ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> if type ( args [ <NUM_LIT:0> ] ) . __name__ == '<STR_LIT>' : <EOL> self . _headers . update ( args [ <NUM_LIT:0> ] ) <EOL> else : <EOL> key , value = args <EOL> self . _headers [ key ] = value <EOL> def get_content ( self ) : <EOL> if self . _headers [ '<STR_LIT>' ] . startswith ( '<STR_LIT:text>' ) : <EOL> return [ self . _content , '<STR_LIT:\n>' ] <EOL> return [ self . _content ] <EOL> def set_content ( self , value ) : <EOL> if isinstance ( value , unicode ) : <EOL> value = value . encode ( '<STR_LIT:utf-8>' ) <EOL> self . _content = value <EOL> content = property ( get_content , set_content ) <EOL> status = property ( get_status , set_status ) <EOL> headers = property ( get_headers , set_headers ) <EOL> class UrlRouting ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , routes ) : <EOL> self . routes = [ ( re . compile ( self . _compile_route ( pair [ <NUM_LIT:0> ] ) ) , pair [ <NUM_LIT:1> ] ) for pair in routes ] <EOL> def add_route ( self , route ) : <EOL> self . routes . append ( route ) <EOL> def get_view ( self , url ) : <EOL> """<STR_LIT>""" <EOL> for pair in self . routes : <EOL> route , to_call = pair <EOL> matches = route . match ( url ) <EOL> if matches : <EOL> return to_call , matches . groupdict ( ) <EOL> return None <EOL> def reverse ( self , route_name , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> for pair in self . routes : <EOL> route , view = pair <EOL> def _compile_route ( self , route ) : <EOL> """<STR_LIT>""" <EOL> if route . startswith ( '<STR_LIT:/>' ) : <EOL> route = route [ <NUM_LIT:1> : ] <EOL> trailing_slash = False <EOL> if route . endswith ( '<STR_LIT:/>' ) : <EOL> route = route [ : - <NUM_LIT:1> ] <EOL> trailing_slash = True <EOL> bits = route . split ( '<STR_LIT:/>' ) <EOL> regex = [ '<STR_LIT>' ] <EOL> for path_component in bits : <EOL> if path_component . startswith ( '<STR_LIT::>' ) : <EOL> name = path_component [ <NUM_LIT:1> : ] <EOL> regex . append ( r'<STR_LIT>' % name ) <EOL> else : <EOL> regex . append ( path_component ) <EOL> if trailing_slash : <EOL> regex . append ( '<STR_LIT>' ) <EOL> return '<STR_LIT>' % '<STR_LIT:/>' . join ( regex ) <EOL> class RegexRouting ( UrlRouting ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class WebApplication ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , routes ) : <EOL> if hasattr ( routes , '<STR_LIT>' ) : <EOL> self . routes = routes <EOL> else : <EOL> self . routes = UrlRouting ( routes ) <EOL> def __call__ ( self , environ , start_response ) : <EOL> request = HttpRequest ( environ ) <EOL> response = None <EOL> view , args = self . routes . get_view ( environ [ '<STR_LIT>' ] ) <EOL> if view : <EOL> if hasattr ( view , '<STR_LIT>' ) : <EOL> to_call = view <EOL> else : <EOL> to_call = eval ( view ) <EOL> try : <EOL> response = to_call ( request , ** args ) <EOL> except Exception , error : <EOL> try : <EOL> response = error500 ( request , error ) <EOL> except NameError : <EOL> msg = '<STR_LIT>' % error <EOL> response = HttpResponse ( msg , status_code = <NUM_LIT> ) <EOL> except Exception , e : <EOL> msg = '<STR_LIT>' % e <EOL> response = HttpResponse ( msg , status_code = <NUM_LIT> ) <EOL> if not isinstance ( response , HttpResponse ) : <EOL> try : <EOL> response = error404 ( request ) <EOL> except Exception , e : <EOL> response = HttpResponse ( '<STR_LIT>' , status_code = <NUM_LIT> ) <EOL> start_response ( response . status , response . get_headers ( ) ) <EOL> return response . content <EOL> def run ( self ) : <EOL> from wsgiref . handlers import BaseHandler <EOL> handler = BaseHandler ( ) <EOL> handler . run ( self ) <EOL> def dev_run ( self , port = <NUM_LIT> ) : <EOL> from wsgiref . simple_server import make_server <EOL> server = make_server ( '<STR_LIT>' , port , self ) <EOL> print '<STR_LIT>' % port <EOL> try : <EOL> server . serve_forever ( ) <EOL> except : <EOL> print '<STR_LIT>' <EOL> server . socket . close ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> """<STR_LIT>""" <EOL> routes = ( <EOL> ( '<STR_LIT:/>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ) <EOL> def homepage ( request ) : <EOL> return HttpResponse ( '<STR_LIT>' ) <EOL> def other_thing ( request , stuff ) : <EOL> return HttpResponse ( '<STR_LIT>' % stuff ) <EOL> web_app = WebApplication ( routes ) <EOL> try : <EOL> web_app . dev_run ( <NUM_LIT> ) <EOL> except KeyboardInterrupt : <EOL> import sys <EOL> print "<STR_LIT>" <EOL> sys . exit ( ) </s>
