<s> """<STR_LIT>""" <EOL> from os import path <EOL> from werkzeug . wrappers import Request as RequestBase , Response as ResponseBase <EOL> from werkzeug . templates import Template <EOL> from werkzeug . exceptions import HTTPException <EOL> from werkzeug . routing import RequestRedirect <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> from warnings import warn <EOL> warn ( DeprecationWarning ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> class Request ( RequestBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , environ , url_map , <EOL> session_store = None , cookie_name = None ) : <EOL> RequestBase . __init__ ( self , environ ) <EOL> self . url_adapter = url_map . bind_to_environ ( environ ) <EOL> self . session_store = session_store <EOL> self . cookie_name = cookie_name <EOL> if session_store is not None and cookie_name is not None : <EOL> if cookie_name in self . cookies : <EOL> self . session = session_store . get ( self . cookies [ cookie_name ] ) <EOL> else : <EOL> self . session = session_store . new ( ) <EOL> def url_for ( self , callback , ** values ) : <EOL> return self . url_adapter . build ( callback , values ) <EOL> class Response ( ResponseBase ) : <EOL> """<STR_LIT>""" <EOL> default_mimetype = '<STR_LIT>' <EOL> def __call__ ( self , environ , start_response ) : <EOL> request = environ [ '<STR_LIT>' ] <EOL> if request . session_store is not None : <EOL> request . session_store . save_if_modified ( request . session ) <EOL> if request . cookie_name not in request . cookies : <EOL> self . set_cookie ( request . cookie_name , request . session . sid ) <EOL> return ResponseBase . __call__ ( self , environ , start_response ) <EOL> class Processor ( object ) : <EOL> """<STR_LIT>""" <EOL> def process_request ( self , request ) : <EOL> return request <EOL> def process_response ( self , request , response ) : <EOL> return response <EOL> def process_view ( self , request , view_func , view_args , view_kwargs ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def process_exception ( self , request , exception ) : <EOL> return None <EOL> class Application ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , url_map , session = False , processors = None ) : <EOL> self . name = name <EOL> self . url_map = url_map <EOL> self . processors = processors or [ ] <EOL> if session : <EOL> self . store = session <EOL> else : <EOL> self . store = None <EOL> def __call__ ( self , environ , start_response ) : <EOL> if self . store is not None : <EOL> request = Request ( environ , self . url_map , <EOL> session_store = self . store , cookie_name = '<STR_LIT>' % self . name ) <EOL> else : <EOL> request = Request ( environ , self . url_map ) <EOL> for processor in self . processors : <EOL> request = processor . process_request ( request ) <EOL> try : <EOL> callback , args = request . url_adapter . match ( request . path ) <EOL> except ( HTTPException , RequestRedirect ) , e : <EOL> response = e <EOL> else : <EOL> for processor in self . processors : <EOL> action = processor . process_view ( request , callback , ( ) , args ) <EOL> if action is not None : <EOL> return action ( environ , start_response ) <EOL> try : <EOL> response = callback ( request , ** args ) <EOL> except Exception , exception : <EOL> for processor in reversed ( self . processors ) : <EOL> action = processor . process_exception ( request , exception ) <EOL> if action is not None : <EOL> return action ( environ , start_response ) <EOL> raise <EOL> for processor in reversed ( self . processors ) : <EOL> response = processor . process_response ( request , response ) <EOL> return response ( environ , start_response ) <EOL> def config_session ( self , store , expiration = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . store = store <EOL> class TemplateNotFound ( IOError , LookupError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name ) : <EOL> IOError . __init__ ( self , name ) <EOL> self . name = name <EOL> class TemplateLoader ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , search_path , encoding = '<STR_LIT:utf-8>' ) : <EOL> self . search_path = path . abspath ( search_path ) <EOL> self . encoding = encoding <EOL> def get_template ( self , name ) : <EOL> """<STR_LIT>""" <EOL> filename = path . join ( self . search_path , * [ p for p in name . split ( '<STR_LIT:/>' ) <EOL> if p and p [ <NUM_LIT:0> ] != '<STR_LIT:.>' ] ) <EOL> if not path . exists ( filename ) : <EOL> raise TemplateNotFound ( name ) <EOL> return Template . from_file ( filename , self . encoding ) <EOL> def render_to_response ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return Response ( self . render_to_string ( * args , ** kwargs ) ) <EOL> def render_to_string ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> template_name , args = args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> : ] <EOL> except IndexError : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> return self . get_template ( template_name ) . render ( * args , ** kwargs ) <EOL> class GenshiTemplateLoader ( TemplateLoader ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , search_path , encoding = '<STR_LIT:utf-8>' , ** kwargs ) : <EOL> TemplateLoader . __init__ ( self , search_path , encoding ) <EOL> from genshi . template import TemplateLoader as GenshiLoader <EOL> from genshi . template . loader import TemplateNotFound <EOL> self . not_found_exception = TemplateNotFound <EOL> reload_template = kwargs . pop ( '<STR_LIT>' , True ) <EOL> kwargs . pop ( '<STR_LIT>' , None ) <EOL> self . loader = GenshiLoader ( search_path , default_encoding = encoding , <EOL> auto_reload = reload_template , ** kwargs ) <EOL> self . output_type = '<STR_LIT:html>' <EOL> self . encoding = encoding <EOL> def get_template ( self , template_name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . loader . load ( template_name , encoding = self . encoding ) <EOL> except self . not_found_exception , e : <EOL> raise TemplateNotFound ( template_name ) <EOL> def render_to_string ( self , template_name , context = None ) : <EOL> """<STR_LIT>""" <EOL> context = context or { } <EOL> tmpl = self . get_template ( template_name ) <EOL> return tmpl . generate ( ** context ) . render ( self . output_type , encoding = None ) </s>
