<s> import werkzeug . wrappers <EOL> from werkzeug . exceptions import * <EOL> from werkzeug . utils import cached_property <EOL> from werkzeug . utils import redirect <EOL> import jsonpickle <EOL> from putils . patterns import Singleton <EOL> from putils . dynamics import Importer <EOL> from jinja2 import Environment , PackageLoader , PrefixLoader , FileSystemLoader , ChoiceLoader <EOL> import gettext <EOL> class Templater ( Singleton ) : <EOL> def __init__ ( self , app ) : <EOL> self . app = app <EOL> self . options = app . options <EOL> if "<STR_LIT>" in self . options [ "<STR_LIT>" ] : <EOL> self . add_template_paths ( self . options [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) <EOL> if "<STR_LIT>" in self . options [ "<STR_LIT>" ] : <EOL> self . add_translation_paths ( self . options [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) <EOL> if "<STR_LIT>" in self . options [ "<STR_LIT>" ] : <EOL> for name , func in self . options [ "<STR_LIT>" ] [ "<STR_LIT>" ] . iteritems ( ) : <EOL> self . app . templates_environment . filters [ name ] = func <EOL> def add_template_paths ( self , paths , prefix = "<STR_LIT>" ) : <EOL> tps = [ ] <EOL> for tp in paths : <EOL> loader = None <EOL> try : <EOL> if Importer . module_path ( tp ) : <EOL> loader = PackageLoader ( tp , "<STR_LIT>" ) <EOL> except : <EOL> loader = FileSystemLoader ( tp ) <EOL> if loader : <EOL> if prefix : <EOL> tps . append ( PrefixLoader ( { prefix : loader } ) ) <EOL> else : <EOL> tps . append ( loader ) <EOL> if hasattr ( self . app , "<STR_LIT>" ) : <EOL> for tl in tps : <EOL> self . app . templates_environment . loader . loaders . append ( tl ) <EOL> else : <EOL> self . app . templates_environment = Environment ( loader = ChoiceLoader ( tps ) , extensions = self . options [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) <EOL> def add_translation_paths ( self , paths ) : <EOL> if not paths : <EOL> return <EOL> for tr_path in paths : <EOL> try : <EOL> tr_path = Importer . module_path ( tr_path ) <EOL> except : <EOL> pass <EOL> try : <EOL> self . app . templates_environment . install_gettext_translations ( gettext . translation ( "<STR_LIT>" , tr_path , codeset = "<STR_LIT>" ) ) <EOL> gettext . install ( "<STR_LIT>" , tr_path , codeset = "<STR_LIT>" ) <EOL> except : <EOL> pass <EOL> class Request ( werkzeug . wrappers . Request ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , options , ** argw ) : <EOL> super ( Request , self ) . __init__ ( options , ** argw ) <EOL> @ cached_property <EOL> def session ( self ) : <EOL> return self . environ [ "<STR_LIT>" ] <EOL> class Response ( werkzeug . wrappers . Response ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , text , ** argw ) : <EOL> if "<STR_LIT>" not in argw : <EOL> argw [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> super ( Response , self ) . __init__ ( text , ** argw ) <EOL> class RedirectResponse ( werkzeug . wrappers . Response ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , path ) : <EOL> return redirect ( path ) <EOL> class JsonResponse ( Response ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , inp , ** argw ) : <EOL> json_str = jsonpickle . encode ( inp , unpicklable = False ) <EOL> super ( JsonResponse , self ) . __init__ ( json_str , mimetype = "<STR_LIT:application/json>" , ** argw ) </s>
