<s> """<STR_LIT>""" <EOL> import md5 <EOL> import os <EOL> try : <EOL> from django import v0_96 <EOL> except ImportError : <EOL> pass <EOL> import django <EOL> import django . conf <EOL> try : <EOL> django . conf . settings . configure ( <EOL> DEBUG = False , <EOL> TEMPLATE_DEBUG = False , <EOL> TEMPLATE_LOADERS = ( <EOL> '<STR_LIT>' , <EOL> ) , <EOL> ) <EOL> except ( EnvironmentError , RuntimeError ) : <EOL> pass <EOL> import django . template <EOL> import django . template . loader <EOL> from google . appengine . ext import webapp <EOL> def render ( template_path , template_dict , debug = False ) : <EOL> """<STR_LIT>""" <EOL> t = load ( template_path , debug ) <EOL> return t . render ( Context ( template_dict ) ) <EOL> _original_template_render = django . template . Template . render <EOL> _render_stack = [ ] <EOL> def _template_render_replacement ( self , context ) : <EOL> """<STR_LIT>""" <EOL> _render_stack . append ( self . name ) <EOL> try : <EOL> return _original_template_render ( self , context ) <EOL> finally : <EOL> _render_stack . pop ( ) <EOL> django . template . Template . render = _template_render_replacement <EOL> template_cache = { } <EOL> def load ( path , debug = False ) : <EOL> """<STR_LIT>""" <EOL> abspath = os . path . abspath ( path ) <EOL> if not debug : <EOL> template = template_cache . get ( abspath , None ) <EOL> else : <EOL> template = None <EOL> if not template : <EOL> directory , file_name = os . path . split ( abspath ) <EOL> new_settings = { <EOL> '<STR_LIT>' : ( directory , ) , <EOL> '<STR_LIT>' : debug , <EOL> '<STR_LIT>' : debug , <EOL> } <EOL> old_settings = _swap_settings ( new_settings ) <EOL> try : <EOL> template = django . template . loader . get_template ( file_name ) <EOL> finally : <EOL> _swap_settings ( old_settings ) <EOL> if not debug : <EOL> template_cache [ abspath ] = template <EOL> def wrap_render ( context , orig_render = template . render ) : <EOL> URLNode = django . template . defaulttags . URLNode <EOL> save_urlnode_render = URLNode . render <EOL> old_settings = _swap_settings ( new_settings ) <EOL> try : <EOL> URLNode . render = _urlnode_render_replacement <EOL> return orig_render ( context ) <EOL> finally : <EOL> _swap_settings ( old_settings ) <EOL> URLNode . render = save_urlnode_render <EOL> template . render = wrap_render <EOL> return template <EOL> def _swap_settings ( new ) : <EOL> """<STR_LIT>""" <EOL> settings = django . conf . settings <EOL> old = { } <EOL> for key , value in new . iteritems ( ) : <EOL> old [ key ] = getattr ( settings , key , None ) <EOL> setattr ( settings , key , value ) <EOL> return old <EOL> parser_cache = { } <EOL> _original_template_compile_string = django . template . compile_string <EOL> def _compile_string_replacement ( template_string , origin ) : <EOL> """<STR_LIT>""" <EOL> debug = django . conf . settings . TEMPLATE_DEBUG <EOL> if not debug : <EOL> template_hash = md5 . md5 ( template_string ) . digest ( ) <EOL> key = ( template_hash , origin ) + tuple ( _render_stack ) <EOL> nodelist = parser_cache . get ( key , None ) <EOL> else : <EOL> nodelist = None <EOL> if not nodelist : <EOL> nodelist = _original_template_compile_string ( template_string , origin ) <EOL> if not debug : <EOL> parser_cache [ key ] = nodelist <EOL> return nodelist <EOL> django . template . compile_string = _compile_string_replacement <EOL> def create_template_register ( ) : <EOL> """<STR_LIT>""" <EOL> return django . template . Library ( ) <EOL> def register_template_library ( package_name ) : <EOL> """<STR_LIT>""" <EOL> if not django . template . libraries . get ( package_name , None ) : <EOL> django . template . add_to_builtins ( package_name ) <EOL> Template = django . template . Template <EOL> Context = django . template . Context <EOL> def _urlnode_render_replacement ( self , context ) : <EOL> """<STR_LIT>""" <EOL> args = [ arg . resolve ( context ) for arg in self . args ] <EOL> try : <EOL> app = webapp . WSGIApplication . active_instance <EOL> handler = app . get_registered_handler_by_name ( self . view_name ) <EOL> return handler . get_url ( implicit_args = True , * args ) <EOL> except webapp . NoUrlFoundError : <EOL> return '<STR_LIT>' </s>
