<s> """<STR_LIT>""" <EOL> from mako import exceptions , util , compat <EOL> from mako . compat import compat_builtins <EOL> import inspect <EOL> import sys <EOL> import collections <EOL> class Context ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , buffer , ** data ) : <EOL> self . _buffer_stack = [ buffer ] <EOL> self . _data = data <EOL> self . _kwargs = data . copy ( ) <EOL> self . _with_template = None <EOL> self . _outputting_as_unicode = None <EOL> self . namespaces = { } <EOL> self . _data [ '<STR_LIT>' ] = compat . partial ( capture , self ) <EOL> self . caller_stack = self . _data [ '<STR_LIT>' ] = CallerStack ( ) <EOL> def _set_with_template ( self , t ) : <EOL> self . _with_template = t <EOL> illegal_names = t . reserved_names . intersection ( self . _data ) <EOL> if illegal_names : <EOL> raise exceptions . NameConflictError ( <EOL> "<STR_LIT>" % <EOL> "<STR_LIT:U+002CU+0020>" . join ( illegal_names ) ) <EOL> @ property <EOL> def lookup ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _with_template . lookup <EOL> @ property <EOL> def kwargs ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _kwargs . copy ( ) <EOL> def push_caller ( self , caller ) : <EOL> """<STR_LIT>""" <EOL> self . caller_stack . append ( caller ) <EOL> def pop_caller ( self ) : <EOL> """<STR_LIT>""" <EOL> del self . caller_stack [ - <NUM_LIT:1> ] <EOL> def keys ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . _data . keys ( ) ) <EOL> def __getitem__ ( self , key ) : <EOL> if key in self . _data : <EOL> return self . _data [ key ] <EOL> else : <EOL> return compat_builtins . __dict__ [ key ] <EOL> def _push_writer ( self ) : <EOL> """<STR_LIT>""" <EOL> buf = util . FastEncodingBuffer ( ) <EOL> self . _buffer_stack . append ( buf ) <EOL> return buf . write <EOL> def _pop_buffer_and_writer ( self ) : <EOL> """<STR_LIT>""" <EOL> buf = self . _buffer_stack . pop ( ) <EOL> return buf , self . _buffer_stack [ - <NUM_LIT:1> ] . write <EOL> def _push_buffer ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _push_writer ( ) <EOL> def _pop_buffer ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _buffer_stack . pop ( ) <EOL> def get ( self , key , default = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _data . get ( key , <EOL> compat_builtins . __dict__ . get ( key , default ) <EOL> ) <EOL> def write ( self , string ) : <EOL> """<STR_LIT>""" <EOL> self . _buffer_stack [ - <NUM_LIT:1> ] . write ( string ) <EOL> def writer ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _buffer_stack [ - <NUM_LIT:1> ] . write <EOL> def _copy ( self ) : <EOL> c = Context . __new__ ( Context ) <EOL> c . _buffer_stack = self . _buffer_stack <EOL> c . _data = self . _data . copy ( ) <EOL> c . _kwargs = self . _kwargs <EOL> c . _with_template = self . _with_template <EOL> c . _outputting_as_unicode = self . _outputting_as_unicode <EOL> c . namespaces = self . namespaces <EOL> c . caller_stack = self . caller_stack <EOL> return c <EOL> def _locals ( self , d ) : <EOL> """<STR_LIT>""" <EOL> if not d : <EOL> return self <EOL> c = self . _copy ( ) <EOL> c . _data . update ( d ) <EOL> return c <EOL> def _clean_inheritance_tokens ( self ) : <EOL> """<STR_LIT>""" <EOL> c = self . _copy ( ) <EOL> x = c . _data <EOL> x . pop ( '<STR_LIT>' , None ) <EOL> x . pop ( '<STR_LIT>' , None ) <EOL> x . pop ( '<STR_LIT>' , None ) <EOL> return c <EOL> class CallerStack ( list ) : <EOL> def __init__ ( self ) : <EOL> self . nextcaller = None <EOL> def __nonzero__ ( self ) : <EOL> return self . __bool__ ( ) <EOL> def __bool__ ( self ) : <EOL> return len ( self ) and self . _get_caller ( ) and True or False <EOL> def _get_caller ( self ) : <EOL> return self [ - <NUM_LIT:1> ] <EOL> def __getattr__ ( self , key ) : <EOL> return getattr ( self . _get_caller ( ) , key ) <EOL> def _push_frame ( self ) : <EOL> frame = self . nextcaller or None <EOL> self . append ( frame ) <EOL> self . nextcaller = None <EOL> return frame <EOL> def _pop_frame ( self ) : <EOL> self . nextcaller = self . pop ( ) <EOL> class Undefined ( object ) : <EOL> """<STR_LIT>""" <EOL> def __str__ ( self ) : <EOL> raise NameError ( "<STR_LIT>" ) <EOL> def __nonzero__ ( self ) : <EOL> return self . __bool__ ( ) <EOL> def __bool__ ( self ) : <EOL> return False <EOL> UNDEFINED = Undefined ( ) <EOL> class LoopStack ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . stack = [ ] <EOL> def _enter ( self , iterable ) : <EOL> self . _push ( iterable ) <EOL> return self . _top <EOL> def _exit ( self ) : <EOL> self . _pop ( ) <EOL> return self . _top <EOL> @ property <EOL> def _top ( self ) : <EOL> if self . stack : <EOL> return self . stack [ - <NUM_LIT:1> ] <EOL> else : <EOL> return self <EOL> def _pop ( self ) : <EOL> return self . stack . pop ( ) <EOL> def _push ( self , iterable ) : <EOL> new = LoopContext ( iterable ) <EOL> if self . stack : <EOL> new . parent = self . stack [ - <NUM_LIT:1> ] <EOL> return self . stack . append ( new ) <EOL> def __getattr__ ( self , key ) : <EOL> raise exceptions . RuntimeException ( "<STR_LIT>" ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . _top ) <EOL> class LoopContext ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , iterable ) : <EOL> self . _iterable = iterable <EOL> self . index = <NUM_LIT:0> <EOL> self . parent = None <EOL> def __iter__ ( self ) : <EOL> for i in self . _iterable : <EOL> yield i <EOL> self . index += <NUM_LIT:1> <EOL> @ util . memoized_instancemethod <EOL> def __len__ ( self ) : <EOL> return len ( self . _iterable ) <EOL> @ property <EOL> def reverse_index ( self ) : <EOL> return len ( self ) - self . index - <NUM_LIT:1> <EOL> @ property <EOL> def first ( self ) : <EOL> return self . index == <NUM_LIT:0> <EOL> @ property <EOL> def last ( self ) : <EOL> return self . index == len ( self ) - <NUM_LIT:1> <EOL> @ property <EOL> def even ( self ) : <EOL> return not self . odd <EOL> @ property <EOL> def odd ( self ) : <EOL> return bool ( self . index % <NUM_LIT:2> ) <EOL> def cycle ( self , * values ) : <EOL> """<STR_LIT>""" <EOL> if not values : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return values [ self . index % len ( values ) ] <EOL> class _NSAttr ( object ) : <EOL> def __init__ ( self , parent ) : <EOL> self . __parent = parent <EOL> def __getattr__ ( self , key ) : <EOL> ns = self . __parent <EOL> while ns : <EOL> if hasattr ( ns . module , key ) : <EOL> return getattr ( ns . module , key ) <EOL> else : <EOL> ns = ns . inherits <EOL> raise AttributeError ( key ) <EOL> class Namespace ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , context , <EOL> callables = None , inherits = None , <EOL> populate_self = True , calling_uri = None ) : <EOL> self . name = name <EOL> self . context = context <EOL> self . inherits = inherits <EOL> if callables is not None : <EOL> self . callables = dict ( [ ( c . __name__ , c ) for c in callables ] ) <EOL> callables = ( ) <EOL> module = None <EOL> """<STR_LIT>""" <EOL> template = None <EOL> """<STR_LIT>""" <EOL> context = None <EOL> """<STR_LIT>""" <EOL> filename = None <EOL> """<STR_LIT>""" <EOL> uri = None <EOL> """<STR_LIT>""" <EOL> _templateuri = None <EOL> @ util . memoized_property <EOL> def attr ( self ) : <EOL> """<STR_LIT>""" <EOL> return _NSAttr ( self ) <EOL> def get_namespace ( self , uri ) : <EOL> """<STR_LIT>""" <EOL> key = ( self , uri ) <EOL> if key in self . context . namespaces : <EOL> return self . context . namespaces [ key ] <EOL> else : <EOL> ns = TemplateNamespace ( uri , self . context . _copy ( ) , <EOL> templateuri = uri , <EOL> calling_uri = self . _templateuri ) <EOL> self . context . namespaces [ key ] = ns <EOL> return ns <EOL> def get_template ( self , uri ) : <EOL> """<STR_LIT>""" <EOL> return _lookup_template ( self . context , uri , self . _templateuri ) <EOL> def get_cached ( self , key , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . cache . get ( key , ** kwargs ) <EOL> @ property <EOL> def cache ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . template . cache <EOL> def include_file ( self , uri , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> _include_file ( self . context , uri , self . _templateuri , ** kwargs ) <EOL> def _populate ( self , d , l ) : <EOL> for ident in l : <EOL> if ident == '<STR_LIT:*>' : <EOL> for ( k , v ) in self . _get_star ( ) : <EOL> d [ k ] = v <EOL> else : <EOL> d [ ident ] = getattr ( self , ident ) <EOL> def _get_star ( self ) : <EOL> if self . callables : <EOL> for key in self . callables : <EOL> yield ( key , self . callables [ key ] ) <EOL> def __getattr__ ( self , key ) : <EOL> if key in self . callables : <EOL> val = self . callables [ key ] <EOL> elif self . inherits : <EOL> val = getattr ( self . inherits , key ) <EOL> else : <EOL> raise AttributeError ( <EOL> "<STR_LIT>" % <EOL> ( self . name , key ) ) <EOL> setattr ( self , key , val ) <EOL> return val <EOL> class TemplateNamespace ( Namespace ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , context , template = None , templateuri = None , <EOL> callables = None , inherits = None , <EOL> populate_self = True , calling_uri = None ) : <EOL> self . name = name <EOL> self . context = context <EOL> self . inherits = inherits <EOL> if callables is not None : <EOL> self . callables = dict ( [ ( c . __name__ , c ) for c in callables ] ) <EOL> if templateuri is not None : <EOL> self . template = _lookup_template ( context , templateuri , <EOL> calling_uri ) <EOL> self . _templateuri = self . template . module . _template_uri <EOL> elif template is not None : <EOL> self . template = template <EOL> self . _templateuri = template . module . _template_uri <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if populate_self : <EOL> lclcallable , lclcontext = _populate_self_namespace ( context , self . template , <EOL> self_ns = self ) <EOL> @ property <EOL> def module ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . template . module <EOL> @ property <EOL> def filename ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . template . filename <EOL> @ property <EOL> def uri ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . template . uri <EOL> def _get_star ( self ) : <EOL> if self . callables : <EOL> for key in self . callables : <EOL> yield ( key , self . callables [ key ] ) <EOL> def get ( key ) : <EOL> callable_ = self . template . _get_def_callable ( key ) <EOL> return compat . partial ( callable_ , self . context ) <EOL> for k in self . template . module . _exports : <EOL> yield ( k , get ( k ) ) <EOL> def __getattr__ ( self , key ) : <EOL> if key in self . callables : <EOL> val = self . callables [ key ] <EOL> elif self . template . has_def ( key ) : <EOL> callable_ = self . template . _get_def_callable ( key ) <EOL> val = compat . partial ( callable_ , self . context ) <EOL> elif self . inherits : <EOL> val = getattr ( self . inherits , key ) <EOL> else : <EOL> raise AttributeError ( <EOL> "<STR_LIT>" % <EOL> ( self . name , key ) ) <EOL> setattr ( self , key , val ) <EOL> return val <EOL> class ModuleNamespace ( Namespace ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , context , module , <EOL> callables = None , inherits = None , <EOL> populate_self = True , calling_uri = None ) : <EOL> self . name = name <EOL> self . context = context <EOL> self . inherits = inherits <EOL> if callables is not None : <EOL> self . callables = dict ( [ ( c . __name__ , c ) for c in callables ] ) <EOL> mod = __import__ ( module ) <EOL> for token in module . split ( '<STR_LIT:.>' ) [ <NUM_LIT:1> : ] : <EOL> mod = getattr ( mod , token ) <EOL> self . module = mod <EOL> @ property <EOL> def filename ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . module . __file__ <EOL> def _get_star ( self ) : <EOL> if self . callables : <EOL> for key in self . callables : <EOL> yield ( key , self . callables [ key ] ) <EOL> for key in dir ( self . module ) : <EOL> if key [ <NUM_LIT:0> ] != '<STR_LIT:_>' : <EOL> callable_ = getattr ( self . module , key ) <EOL> if compat . callable ( callable_ ) : <EOL> yield key , compat . partial ( callable_ , self . context ) <EOL> def __getattr__ ( self , key ) : <EOL> if key in self . callables : <EOL> val = self . callables [ key ] <EOL> elif hasattr ( self . module , key ) : <EOL> callable_ = getattr ( self . module , key ) <EOL> val = compat . partial ( callable_ , self . context ) <EOL> elif self . inherits : <EOL> val = getattr ( self . inherits , key ) <EOL> else : <EOL> raise AttributeError ( <EOL> "<STR_LIT>" % <EOL> ( self . name , key ) ) <EOL> setattr ( self , key , val ) <EOL> return val <EOL> def supports_caller ( func ) : <EOL> """<STR_LIT>""" <EOL> def wrap_stackframe ( context , * args , ** kwargs ) : <EOL> context . caller_stack . _push_frame ( ) <EOL> try : <EOL> return func ( context , * args , ** kwargs ) <EOL> finally : <EOL> context . caller_stack . _pop_frame ( ) <EOL> return wrap_stackframe <EOL> def capture ( context , callable_ , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not compat . callable ( callable_ ) : <EOL> raise exceptions . RuntimeException ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> context . _push_buffer ( ) <EOL> try : <EOL> callable_ ( * args , ** kwargs ) <EOL> finally : <EOL> buf = context . _pop_buffer ( ) <EOL> return buf . getvalue ( ) <EOL> def _decorate_toplevel ( fn ) : <EOL> def decorate_render ( render_fn ) : <EOL> def go ( context , * args , ** kw ) : <EOL> def y ( * args , ** kw ) : <EOL> return render_fn ( context , * args , ** kw ) <EOL> try : <EOL> y . __name__ = render_fn . __name__ [ <NUM_LIT:7> : ] <EOL> except TypeError : <EOL> pass <EOL> return fn ( y ) ( context , * args , ** kw ) <EOL> return go <EOL> return decorate_render <EOL> def _decorate_inline ( context , fn ) : <EOL> def decorate_render ( render_fn ) : <EOL> dec = fn ( render_fn ) <EOL> def go ( * args , ** kw ) : <EOL> return dec ( context , * args , ** kw ) <EOL> return go <EOL> return decorate_render <EOL> def _include_file ( context , uri , calling_uri , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> template = _lookup_template ( context , uri , calling_uri ) <EOL> ( callable_ , ctx ) = _populate_self_namespace ( <EOL> context . _clean_inheritance_tokens ( ) , <EOL> template ) <EOL> callable_ ( ctx , ** _kwargs_for_include ( callable_ , context . _data , ** kwargs ) ) <EOL> def _inherit_from ( context , uri , calling_uri ) : <EOL> """<STR_LIT>""" <EOL> if uri is None : <EOL> return None <EOL> template = _lookup_template ( context , uri , calling_uri ) <EOL> self_ns = context [ '<STR_LIT>' ] <EOL> ih = self_ns <EOL> while ih . inherits is not None : <EOL> ih = ih . inherits <EOL> lclcontext = context . _locals ( { '<STR_LIT>' : ih } ) <EOL> ih . inherits = TemplateNamespace ( "<STR_LIT>" % template . uri , <EOL> lclcontext , <EOL> template = template , <EOL> populate_self = False ) <EOL> context . _data [ '<STR_LIT>' ] = lclcontext . _data [ '<STR_LIT>' ] = ih . inherits <EOL> callable_ = getattr ( template . module , '<STR_LIT>' , None ) <EOL> if callable_ is not None : <EOL> ret = callable_ ( template , lclcontext ) <EOL> if ret : <EOL> return ret <EOL> gen_ns = getattr ( template . module , '<STR_LIT>' , None ) <EOL> if gen_ns is not None : <EOL> gen_ns ( context ) <EOL> return ( template . callable_ , lclcontext ) <EOL> def _lookup_template ( context , uri , relativeto ) : <EOL> lookup = context . _with_template . lookup <EOL> if lookup is None : <EOL> raise exceptions . TemplateLookupException ( <EOL> "<STR_LIT>" % <EOL> context . _with_template . uri ) <EOL> uri = lookup . adjust_uri ( uri , relativeto ) <EOL> try : <EOL> return lookup . get_template ( uri ) <EOL> except exceptions . TopLevelLookupException : <EOL> raise exceptions . TemplateLookupException ( str ( compat . exception_as ( ) ) ) <EOL> def _populate_self_namespace ( context , template , self_ns = None ) : <EOL> if self_ns is None : <EOL> self_ns = TemplateNamespace ( '<STR_LIT>' % template . uri , <EOL> context , template = template , <EOL> populate_self = False ) <EOL> context . _data [ '<STR_LIT>' ] = context . _data [ '<STR_LIT>' ] = self_ns <EOL> if hasattr ( template . module , '<STR_LIT>' ) : <EOL> ret = template . module . _mako_inherit ( template , context ) <EOL> if ret : <EOL> return ret <EOL> return ( template . callable_ , context ) <EOL> def _render ( template , callable_ , args , data , as_unicode = False ) : <EOL> """<STR_LIT>""" <EOL> if as_unicode : <EOL> buf = util . FastEncodingBuffer ( as_unicode = True ) <EOL> elif template . bytestring_passthrough : <EOL> buf = compat . StringIO ( ) <EOL> else : <EOL> buf = util . FastEncodingBuffer ( <EOL> as_unicode = as_unicode , <EOL> encoding = template . output_encoding , <EOL> errors = template . encoding_errors ) <EOL> context = Context ( buf , ** data ) <EOL> context . _outputting_as_unicode = as_unicode <EOL> context . _set_with_template ( template ) <EOL> _render_context ( template , callable_ , context , * args , <EOL> ** _kwargs_for_callable ( callable_ , data ) ) <EOL> return context . _pop_buffer ( ) . getvalue ( ) <EOL> def _kwargs_for_callable ( callable_ , data ) : <EOL> argspec = compat . inspect_func_args ( callable_ ) <EOL> if argspec [ <NUM_LIT:2> ] : <EOL> return data <EOL> namedargs = argspec [ <NUM_LIT:0> ] + [ v for v in argspec [ <NUM_LIT:1> : <NUM_LIT:3> ] if v is not None ] <EOL> kwargs = { } <EOL> for arg in namedargs : <EOL> if arg != '<STR_LIT>' and arg in data and arg not in kwargs : <EOL> kwargs [ arg ] = data [ arg ] <EOL> return kwargs <EOL> def _kwargs_for_include ( callable_ , data , ** kwargs ) : <EOL> argspec = compat . inspect_func_args ( callable_ ) <EOL> namedargs = argspec [ <NUM_LIT:0> ] + [ v for v in argspec [ <NUM_LIT:1> : <NUM_LIT:3> ] if v is not None ] <EOL> for arg in namedargs : <EOL> if arg != '<STR_LIT>' and arg in data and arg not in kwargs : <EOL> kwargs [ arg ] = data [ arg ] <EOL> return kwargs <EOL> def _render_context ( tmpl , callable_ , context , * args , ** kwargs ) : <EOL> import mako . template as template <EOL> if not isinstance ( tmpl , template . DefTemplate ) : <EOL> ( inherit , lclcontext ) = _populate_self_namespace ( context , tmpl ) <EOL> _exec_template ( inherit , lclcontext , args = args , kwargs = kwargs ) <EOL> else : <EOL> ( inherit , lclcontext ) = _populate_self_namespace ( context , tmpl . parent ) <EOL> _exec_template ( callable_ , context , args = args , kwargs = kwargs ) <EOL> def _exec_template ( callable_ , context , args = None , kwargs = None ) : <EOL> """<STR_LIT>""" <EOL> template = context . _with_template <EOL> if template is not None and ( template . format_exceptions or template . error_handler ) : <EOL> error = None <EOL> try : <EOL> callable_ ( context , * args , ** kwargs ) <EOL> except Exception : <EOL> _render_error ( template , context , compat . exception_as ( ) ) <EOL> except : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:0> ] <EOL> _render_error ( template , context , e ) <EOL> else : <EOL> callable_ ( context , * args , ** kwargs ) <EOL> def _render_error ( template , context , error ) : <EOL> if template . error_handler : <EOL> result = template . error_handler ( context , error ) <EOL> if not result : <EOL> raise error <EOL> else : <EOL> error_template = exceptions . html_error_template ( ) <EOL> if context . _outputting_as_unicode : <EOL> context . _buffer_stack [ : ] = [ util . FastEncodingBuffer ( as_unicode = True ) ] <EOL> else : <EOL> context . _buffer_stack [ : ] = [ util . FastEncodingBuffer ( <EOL> error_template . output_encoding , <EOL> error_template . encoding_errors ) ] <EOL> context . _set_with_template ( error_template ) <EOL> error_template . render_context ( context , error = error ) </s>
