<s> """<STR_LIT>""" <EOL> import unittest <EOL> from django import template <EOL> try : <EOL> from django . template . base import FilterExpression <EOL> except ImportError : <EOL> from django . template import FilterExpression <EOL> from django . template . loader import get_template <EOL> import six <EOL> from pyjade . runtime import iteration <EOL> register = template . Library ( ) <EOL> @ register . tag ( name = "<STR_LIT>" ) <EOL> def do_evaluate ( parser , token ) : <EOL> '''<STR_LIT>''' <EOL> code = token . contents <EOL> firstspace = code . find ( '<STR_LIT:U+0020>' ) <EOL> if firstspace >= <NUM_LIT:0> : <EOL> code = code [ firstspace + <NUM_LIT:1> : ] <EOL> return Evaluator ( code ) <EOL> class Evaluator ( template . Node ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , code ) : <EOL> self . code = code <EOL> def render ( self , context ) : <EOL> '''<STR_LIT>''' <EOL> modules = { <EOL> '<STR_LIT>' : __import__ ( '<STR_LIT>' ) <EOL> } <EOL> context [ '<STR_LIT:false>' ] = False <EOL> context [ '<STR_LIT:true>' ] = True <EOL> try : <EOL> return six . text_type ( eval ( '<STR_LIT>' % self . code , modules , context ) ) <EOL> except NameError : <EOL> return '<STR_LIT>' <EOL> @ register . tag ( name = "<STR_LIT>" ) <EOL> def do_set ( parser , token ) : <EOL> '''<STR_LIT>''' <EOL> code = token . contents <EOL> firstspace = code . find ( '<STR_LIT:U+0020>' ) <EOL> if firstspace >= <NUM_LIT:0> : <EOL> code = code [ firstspace + <NUM_LIT:1> : ] <EOL> return Setter ( code ) <EOL> class Setter ( template . Node ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , code ) : <EOL> self . code = code <EOL> def render ( self , context ) : <EOL> '''<STR_LIT>''' <EOL> modules = { <EOL> } <EOL> context [ '<STR_LIT:false>' ] = False <EOL> context [ '<STR_LIT:true>' ] = True <EOL> new_ctx = eval ( '<STR_LIT>' % self . code , modules , context ) <EOL> context . update ( new_ctx ) <EOL> return '<STR_LIT>' <EOL> register . filter ( '<STR_LIT>' , iteration ) <EOL> def _setup_macros_dict ( parser ) : <EOL> try : <EOL> parser . _macros <EOL> except AttributeError : <EOL> parser . _macros = { } <EOL> class DefineMacroNode ( template . Node ) : <EOL> def __init__ ( self , name , nodelist , args ) : <EOL> self . name = name <EOL> self . nodelist = nodelist <EOL> self . args = [ ] <EOL> self . kwargs = { } <EOL> for a in args : <EOL> a = a . rstrip ( '<STR_LIT:U+002C>' ) <EOL> if "<STR_LIT:=>" not in a : <EOL> self . args . append ( a ) <EOL> else : <EOL> name , value = a . split ( "<STR_LIT:=>" ) <EOL> self . kwargs [ name ] = value <EOL> def render ( self , context ) : <EOL> return '<STR_LIT>' <EOL> @ register . tag ( name = "<STR_LIT>" ) <EOL> def do_macro ( parser , token ) : <EOL> try : <EOL> args = token . split_contents ( ) <EOL> tag_name , macro_name , args = args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> ] , args [ <NUM_LIT:2> : ] <EOL> except IndexError : <EOL> m = ( "<STR_LIT>" <EOL> % token . contents . split ( ) [ <NUM_LIT:0> ] ) <EOL> raise template . TemplateSyntaxError ( m ) <EOL> nodelist = parser . parse ( ( '<STR_LIT>' , ) ) <EOL> parser . delete_first_token ( ) <EOL> _setup_macros_dict ( parser ) <EOL> parser . _macros [ macro_name ] = DefineMacroNode ( macro_name , nodelist , args ) <EOL> return parser . _macros [ macro_name ] <EOL> class LoadMacrosNode ( template . Node ) : <EOL> def render ( self , context ) : <EOL> return '<STR_LIT>' <EOL> @ register . tag ( name = "<STR_LIT>" ) <EOL> def do_loadmacros ( parser , token ) : <EOL> try : <EOL> tag_name , filename = token . split_contents ( ) <EOL> except IndexError : <EOL> m = ( "<STR_LIT>" <EOL> % token . contents . split ( ) [ <NUM_LIT:0> ] ) <EOL> raise template . TemplateSyntaxError ( m ) <EOL> if filename [ <NUM_LIT:0> ] in ( '<STR_LIT:">' , "<STR_LIT:'>" ) and filename [ - <NUM_LIT:1> ] == filename [ <NUM_LIT:0> ] : <EOL> filename = filename [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> t = get_template ( filename ) <EOL> macros = t . nodelist . get_nodes_by_type ( DefineMacroNode ) <EOL> _setup_macros_dict ( parser ) <EOL> for macro in macros : <EOL> parser . _macros [ macro . name ] = macro <EOL> return LoadMacrosNode ( ) <EOL> class UseMacroNode ( template . Node ) : <EOL> def __init__ ( self , macro , fe_args , fe_kwargs ) : <EOL> self . macro = macro <EOL> self . fe_args = fe_args <EOL> self . fe_kwargs = fe_kwargs <EOL> def render ( self , context ) : <EOL> for i , arg in enumerate ( self . macro . args ) : <EOL> try : <EOL> fe = self . fe_args [ i ] <EOL> context [ arg ] = fe . resolve ( context ) <EOL> except IndexError : <EOL> context [ arg ] = "<STR_LIT>" <EOL> for name , default in six . iteritems ( self . macro . kwargs ) : <EOL> if name in self . fe_kwargs : <EOL> context [ name ] = self . fe_kwargs [ name ] . resolve ( context ) <EOL> else : <EOL> context [ name ] = FilterExpression ( default , <EOL> self . macro . parser <EOL> ) . resolve ( context ) <EOL> return self . macro . nodelist . render ( context ) <EOL> @ register . tag ( name = "<STR_LIT>" ) <EOL> def do_usemacro ( parser , token ) : <EOL> try : <EOL> args = token . split_contents ( ) <EOL> tag_name , macro_name , values = args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> ] , args [ <NUM_LIT:2> : ] <EOL> except IndexError : <EOL> m = ( "<STR_LIT>" <EOL> % token . contents . split ( ) [ <NUM_LIT:0> ] ) <EOL> raise template . TemplateSyntaxError ( m ) <EOL> try : <EOL> macro = parser . _macros [ macro_name ] <EOL> except ( AttributeError , KeyError ) : <EOL> m = "<STR_LIT>" % macro_name <EOL> raise template . TemplateSyntaxError ( m ) <EOL> fe_kwargs = { } <EOL> fe_args = [ ] <EOL> for val in values : <EOL> val = val . rstrip ( '<STR_LIT:U+002C>' ) <EOL> if "<STR_LIT:=>" in val : <EOL> name , value = val . split ( "<STR_LIT:=>" ) <EOL> fe_kwargs [ name ] = FilterExpression ( value , parser ) <EOL> else : <EOL> fe_args . append ( FilterExpression ( val , parser ) ) <EOL> macro . parser = parser <EOL> return UseMacroNode ( macro , fe_args , fe_kwargs ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
