<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> import os <EOL> import sys <EOL> import re <EOL> from datetime import datetime <EOL> from itertools import groupby , cycle as itertools_cycle <EOL> from django . conf import settings <EOL> from django . template . base import ( Node , NodeList , Template , Context , Library , <EOL> TemplateSyntaxError , VariableDoesNotExist , InvalidTemplateLibrary , <EOL> BLOCK_TAG_START , BLOCK_TAG_END , VARIABLE_TAG_START , VARIABLE_TAG_END , <EOL> SINGLE_BRACE_START , SINGLE_BRACE_END , COMMENT_TAG_START , COMMENT_TAG_END , <EOL> VARIABLE_ATTRIBUTE_SEPARATOR , get_library , token_kwargs , kwarg_re ) <EOL> from django . template . smartif import IfParser , Literal <EOL> from django . template . defaultfilters import date <EOL> from django . utils . encoding import smart_text <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . html import format_html <EOL> from django . utils import six <EOL> from django . utils import timezone <EOL> register = Library ( ) <EOL> class AutoEscapeControlNode ( Node ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , setting , nodelist ) : <EOL> self . setting , self . nodelist = setting , nodelist <EOL> def render ( self , context ) : <EOL> old_setting = context . autoescape <EOL> context . autoescape = self . setting <EOL> output = self . nodelist . render ( context ) <EOL> context . autoescape = old_setting <EOL> if self . setting : <EOL> return mark_safe ( output ) <EOL> else : <EOL> return output <EOL> class CommentNode ( Node ) : <EOL> def render ( self , context ) : <EOL> return '<STR_LIT>' <EOL> class CsrfTokenNode ( Node ) : <EOL> def render ( self , context ) : <EOL> csrf_token = context . get ( '<STR_LIT>' , None ) <EOL> if csrf_token : <EOL> if csrf_token == '<STR_LIT>' : <EOL> return format_html ( "<STR_LIT>" ) <EOL> else : <EOL> return format_html ( "<STR_LIT>" , csrf_token ) <EOL> else : <EOL> from django . conf import settings <EOL> if settings . DEBUG : <EOL> import warnings <EOL> warnings . warn ( "<STR_LIT>" ) <EOL> return '<STR_LIT>' <EOL> class CycleNode ( Node ) : <EOL> def __init__ ( self , cyclevars , variable_name = None , silent = False ) : <EOL> self . cyclevars = cyclevars <EOL> self . variable_name = variable_name <EOL> self . silent = silent <EOL> def render ( self , context ) : <EOL> if self not in context . render_context : <EOL> context . render_context [ self ] = itertools_cycle ( self . cyclevars ) <EOL> cycle_iter = context . render_context [ self ] <EOL> value = next ( cycle_iter ) . resolve ( context ) <EOL> if self . variable_name : <EOL> context [ self . variable_name ] = value <EOL> if self . silent : <EOL> return '<STR_LIT>' <EOL> return value <EOL> class DebugNode ( Node ) : <EOL> def render ( self , context ) : <EOL> from pprint import pformat <EOL> output = [ pformat ( val ) for val in context ] <EOL> output . append ( '<STR_LIT>' ) <EOL> output . append ( pformat ( sys . modules ) ) <EOL> return '<STR_LIT>' . join ( output ) <EOL> class FilterNode ( Node ) : <EOL> def __init__ ( self , filter_expr , nodelist ) : <EOL> self . filter_expr , self . nodelist = filter_expr , nodelist <EOL> def render ( self , context ) : <EOL> output = self . nodelist . render ( context ) <EOL> context . update ( { '<STR_LIT>' : output } ) <EOL> filtered = self . filter_expr . resolve ( context ) <EOL> context . pop ( ) <EOL> return filtered <EOL> class FirstOfNode ( Node ) : <EOL> def __init__ ( self , vars ) : <EOL> self . vars = vars <EOL> def render ( self , context ) : <EOL> for var in self . vars : <EOL> value = var . resolve ( context , True ) <EOL> if value : <EOL> return smart_text ( value ) <EOL> return '<STR_LIT>' <EOL> class ForNode ( Node ) : <EOL> child_nodelists = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , loopvars , sequence , is_reversed , nodelist_loop , nodelist_empty = None ) : <EOL> self . loopvars , self . sequence = loopvars , sequence <EOL> self . is_reversed = is_reversed <EOL> self . nodelist_loop = nodelist_loop <EOL> if nodelist_empty is None : <EOL> self . nodelist_empty = NodeList ( ) <EOL> else : <EOL> self . nodelist_empty = nodelist_empty <EOL> def __repr__ ( self ) : <EOL> reversed_text = self . is_reversed and '<STR_LIT>' or '<STR_LIT>' <EOL> return "<STR_LIT>" % ( '<STR_LIT:U+002CU+0020>' . join ( self . loopvars ) , self . sequence , len ( self . nodelist_loop ) , <EOL> reversed_text ) <EOL> def __iter__ ( self ) : <EOL> for node in self . nodelist_loop : <EOL> yield node <EOL> for node in self . nodelist_empty : <EOL> yield node <EOL> def render ( self , context ) : <EOL> if '<STR_LIT>' in context : <EOL> parentloop = context [ '<STR_LIT>' ] <EOL> else : <EOL> parentloop = { } <EOL> context . push ( ) <EOL> try : <EOL> values = self . sequence . resolve ( context , True ) <EOL> except VariableDoesNotExist : <EOL> values = [ ] <EOL> if values is None : <EOL> values = [ ] <EOL> if not hasattr ( values , '<STR_LIT>' ) : <EOL> values = list ( values ) <EOL> len_values = len ( values ) <EOL> if len_values < <NUM_LIT:1> : <EOL> context . pop ( ) <EOL> return self . nodelist_empty . render ( context ) <EOL> nodelist = NodeList ( ) <EOL> if self . is_reversed : <EOL> values = reversed ( values ) <EOL> unpack = len ( self . loopvars ) > <NUM_LIT:1> <EOL> loop_dict = context [ '<STR_LIT>' ] = { '<STR_LIT>' : parentloop } <EOL> for i , item in enumerate ( values ) : <EOL> loop_dict [ '<STR_LIT>' ] = i <EOL> loop_dict [ '<STR_LIT>' ] = i + <NUM_LIT:1> <EOL> loop_dict [ '<STR_LIT>' ] = len_values - i <EOL> loop_dict [ '<STR_LIT>' ] = len_values - i - <NUM_LIT:1> <EOL> loop_dict [ '<STR_LIT>' ] = ( i == <NUM_LIT:0> ) <EOL> loop_dict [ '<STR_LIT>' ] = ( i == len_values - <NUM_LIT:1> ) <EOL> pop_context = False <EOL> if unpack : <EOL> try : <EOL> unpacked_vars = dict ( zip ( self . loopvars , item ) ) <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> pop_context = True <EOL> context . update ( unpacked_vars ) <EOL> else : <EOL> context [ self . loopvars [ <NUM_LIT:0> ] ] = item <EOL> if settings . TEMPLATE_DEBUG : <EOL> for node in self . nodelist_loop : <EOL> try : <EOL> nodelist . append ( node . render ( context ) ) <EOL> except Exception as e : <EOL> if not hasattr ( e , '<STR_LIT>' ) : <EOL> e . django_template_source = node . source <EOL> raise <EOL> else : <EOL> for node in self . nodelist_loop : <EOL> nodelist . append ( node . render ( context ) ) <EOL> if pop_context : <EOL> context . pop ( ) <EOL> context . pop ( ) <EOL> return nodelist . render ( context ) <EOL> class IfChangedNode ( Node ) : <EOL> child_nodelists = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , nodelist_true , nodelist_false , * varlist ) : <EOL> self . nodelist_true , self . nodelist_false = nodelist_true , nodelist_false <EOL> self . _last_seen = None <EOL> self . _varlist = varlist <EOL> self . _id = str ( id ( self ) ) <EOL> def render ( self , context ) : <EOL> if '<STR_LIT>' in context and self . _id not in context [ '<STR_LIT>' ] : <EOL> self . _last_seen = None <EOL> context [ '<STR_LIT>' ] [ self . _id ] = <NUM_LIT:1> <EOL> try : <EOL> if self . _varlist : <EOL> compare_to = [ var . resolve ( context , True ) for var in self . _varlist ] <EOL> else : <EOL> compare_to = self . nodelist_true . render ( context ) <EOL> except VariableDoesNotExist : <EOL> compare_to = None <EOL> if compare_to != self . _last_seen : <EOL> self . _last_seen = compare_to <EOL> content = self . nodelist_true . render ( context ) <EOL> return content <EOL> elif self . nodelist_false : <EOL> return self . nodelist_false . render ( context ) <EOL> return '<STR_LIT>' <EOL> class IfEqualNode ( Node ) : <EOL> child_nodelists = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , var1 , var2 , nodelist_true , nodelist_false , negate ) : <EOL> self . var1 , self . var2 = var1 , var2 <EOL> self . nodelist_true , self . nodelist_false = nodelist_true , nodelist_false <EOL> self . negate = negate <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" <EOL> def render ( self , context ) : <EOL> val1 = self . var1 . resolve ( context , True ) <EOL> val2 = self . var2 . resolve ( context , True ) <EOL> if ( self . negate and val1 != val2 ) or ( not self . negate and val1 == val2 ) : <EOL> return self . nodelist_true . render ( context ) <EOL> return self . nodelist_false . render ( context ) <EOL> class IfNode ( Node ) : <EOL> def __init__ ( self , conditions_nodelists ) : <EOL> self . conditions_nodelists = conditions_nodelists <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" <EOL> def __iter__ ( self ) : <EOL> for _ , nodelist in self . conditions_nodelists : <EOL> for node in nodelist : <EOL> yield node <EOL> @ property <EOL> def nodelist ( self ) : <EOL> return NodeList ( node for _ , nodelist in self . conditions_nodelists for node in nodelist ) <EOL> def render ( self , context ) : <EOL> for condition , nodelist in self . conditions_nodelists : <EOL> if condition is not None : <EOL> try : <EOL> match = condition . eval ( context ) <EOL> except VariableDoesNotExist : <EOL> match = None <EOL> else : <EOL> match = True <EOL> if match : <EOL> return nodelist . render ( context ) <EOL> return '<STR_LIT>' <EOL> class RegroupNode ( Node ) : <EOL> def __init__ ( self , target , expression , var_name ) : <EOL> self . target , self . expression = target , expression <EOL> self . var_name = var_name <EOL> def resolve_expression ( self , obj , context ) : <EOL> context [ self . var_name ] = obj <EOL> return self . expression . resolve ( context , True ) <EOL> def render ( self , context ) : <EOL> obj_list = self . target . resolve ( context , True ) <EOL> if obj_list == None : <EOL> context [ self . var_name ] = [ ] <EOL> return '<STR_LIT>' <EOL> context [ self . var_name ] = [ <EOL> { '<STR_LIT>' : key , '<STR_LIT:list>' : list ( val ) } <EOL> for key , val in <EOL> groupby ( obj_list , lambda obj : self . resolve_expression ( obj , context ) ) <EOL> ] <EOL> return '<STR_LIT>' <EOL> def include_is_allowed ( filepath ) : <EOL> filepath = os . path . abspath ( filepath ) <EOL> for root in settings . ALLOWED_INCLUDE_ROOTS : <EOL> if filepath . startswith ( root ) : <EOL> return True <EOL> return False <EOL> class SsiNode ( Node ) : <EOL> def __init__ ( self , filepath , parsed ) : <EOL> self . filepath = filepath <EOL> self . parsed = parsed <EOL> def render ( self , context ) : <EOL> filepath = self . filepath . resolve ( context ) <EOL> if not include_is_allowed ( filepath ) : <EOL> if settings . DEBUG : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return '<STR_LIT>' <EOL> try : <EOL> with open ( filepath , '<STR_LIT:r>' ) as fp : <EOL> output = fp . read ( ) <EOL> except IOError : <EOL> output = '<STR_LIT>' <EOL> if self . parsed : <EOL> try : <EOL> t = Template ( output , name = filepath ) <EOL> return t . render ( context ) <EOL> except TemplateSyntaxError as e : <EOL> if settings . DEBUG : <EOL> return "<STR_LIT>" % e <EOL> else : <EOL> return '<STR_LIT>' <EOL> return output <EOL> class LoadNode ( Node ) : <EOL> def render ( self , context ) : <EOL> return '<STR_LIT>' <EOL> class NowNode ( Node ) : <EOL> def __init__ ( self , format_string ) : <EOL> self . format_string = format_string <EOL> def render ( self , context ) : <EOL> tzinfo = timezone . get_current_timezone ( ) if settings . USE_TZ else None <EOL> return date ( datetime . now ( tz = tzinfo ) , self . format_string ) <EOL> class SpacelessNode ( Node ) : <EOL> def __init__ ( self , nodelist ) : <EOL> self . nodelist = nodelist <EOL> def render ( self , context ) : <EOL> from django . utils . html import strip_spaces_between_tags <EOL> return strip_spaces_between_tags ( self . nodelist . render ( context ) . strip ( ) ) <EOL> class TemplateTagNode ( Node ) : <EOL> mapping = { '<STR_LIT>' : BLOCK_TAG_START , <EOL> '<STR_LIT>' : BLOCK_TAG_END , <EOL> '<STR_LIT>' : VARIABLE_TAG_START , <EOL> '<STR_LIT>' : VARIABLE_TAG_END , <EOL> '<STR_LIT>' : SINGLE_BRACE_START , <EOL> '<STR_LIT>' : SINGLE_BRACE_END , <EOL> '<STR_LIT>' : COMMENT_TAG_START , <EOL> '<STR_LIT>' : COMMENT_TAG_END , <EOL> } <EOL> def __init__ ( self , tagtype ) : <EOL> self . tagtype = tagtype <EOL> def render ( self , context ) : <EOL> return self . mapping . get ( self . tagtype , '<STR_LIT>' ) <EOL> class URLNode ( Node ) : <EOL> def __init__ ( self , view_name , args , kwargs , asvar ) : <EOL> self . view_name = view_name <EOL> self . args = args <EOL> self . kwargs = kwargs <EOL> self . asvar = asvar <EOL> def render ( self , context ) : <EOL> from django . core . urlresolvers import reverse , NoReverseMatch <EOL> args = [ arg . resolve ( context ) for arg in self . args ] <EOL> kwargs = dict ( [ ( smart_text ( k , '<STR_LIT:ascii>' ) , v . resolve ( context ) ) <EOL> for k , v in self . kwargs . items ( ) ] ) <EOL> view_name = self . view_name . resolve ( context ) <EOL> if not view_name : <EOL> raise NoReverseMatch ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> url = '<STR_LIT>' <EOL> try : <EOL> url = reverse ( view_name , args = args , kwargs = kwargs , current_app = context . current_app ) <EOL> except NoReverseMatch as e : <EOL> if settings . SETTINGS_MODULE : <EOL> project_name = settings . SETTINGS_MODULE . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> try : <EOL> url = reverse ( project_name + '<STR_LIT:.>' + view_name , <EOL> args = args , kwargs = kwargs , <EOL> current_app = context . current_app ) <EOL> except NoReverseMatch : <EOL> if self . asvar is None : <EOL> raise e <EOL> else : <EOL> if self . asvar is None : <EOL> raise e <EOL> if self . asvar : <EOL> context [ self . asvar ] = url <EOL> return '<STR_LIT>' <EOL> else : <EOL> return url <EOL> class VerbatimNode ( Node ) : <EOL> def __init__ ( self , content ) : <EOL> self . content = content <EOL> def render ( self , context ) : <EOL> return self . content <EOL> class WidthRatioNode ( Node ) : <EOL> def __init__ ( self , val_expr , max_expr , max_width ) : <EOL> self . val_expr = val_expr <EOL> self . max_expr = max_expr <EOL> self . max_width = max_width <EOL> def render ( self , context ) : <EOL> try : <EOL> value = self . val_expr . resolve ( context ) <EOL> max_value = self . max_expr . resolve ( context ) <EOL> max_width = int ( self . max_width . resolve ( context ) ) <EOL> except VariableDoesNotExist : <EOL> return '<STR_LIT>' <EOL> except ( ValueError , TypeError ) : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" ) <EOL> try : <EOL> value = float ( value ) <EOL> max_value = float ( max_value ) <EOL> ratio = ( value / max_value ) * max_width <EOL> except ZeroDivisionError : <EOL> return '<STR_LIT:0>' <EOL> except ( ValueError , TypeError ) : <EOL> return '<STR_LIT>' <EOL> return str ( int ( round ( ratio ) ) ) <EOL> class WithNode ( Node ) : <EOL> def __init__ ( self , var , name , nodelist , extra_context = None ) : <EOL> self . nodelist = nodelist <EOL> self . extra_context = extra_context or { } <EOL> if name : <EOL> self . extra_context [ name ] = var <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" <EOL> def render ( self , context ) : <EOL> values = dict ( [ ( key , val . resolve ( context ) ) for key , val in <EOL> six . iteritems ( self . extra_context ) ] ) <EOL> context . update ( values ) <EOL> output = self . nodelist . render ( context ) <EOL> context . pop ( ) <EOL> return output <EOL> @ register . tag <EOL> def autoescape ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> args = token . contents . split ( ) <EOL> if len ( args ) != <NUM_LIT:2> : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" ) <EOL> arg = args [ <NUM_LIT:1> ] <EOL> if arg not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" ) <EOL> nodelist = parser . parse ( ( '<STR_LIT>' , ) ) <EOL> parser . delete_first_token ( ) <EOL> return AutoEscapeControlNode ( ( arg == '<STR_LIT>' ) , nodelist ) <EOL> @ register . tag <EOL> def comment ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> parser . skip_past ( '<STR_LIT>' ) <EOL> return CommentNode ( ) <EOL> @ register . tag <EOL> def cycle ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> args = token . split_contents ( ) <EOL> if len ( args ) < <NUM_LIT:2> : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" ) <EOL> if '<STR_LIT:U+002C>' in args [ <NUM_LIT:1> ] : <EOL> args [ <NUM_LIT:1> : <NUM_LIT:2> ] = [ '<STR_LIT>' % arg for arg in args [ <NUM_LIT:1> ] . split ( "<STR_LIT:U+002C>" ) ] <EOL> if len ( args ) == <NUM_LIT:2> : <EOL> name = args [ <NUM_LIT:1> ] <EOL> if not hasattr ( parser , '<STR_LIT>' ) : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % name ) <EOL> if not name in parser . _namedCycleNodes : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % name ) <EOL> return parser . _namedCycleNodes [ name ] <EOL> as_form = False <EOL> if len ( args ) > <NUM_LIT:4> : <EOL> if args [ - <NUM_LIT:3> ] == "<STR_LIT>" : <EOL> if args [ - <NUM_LIT:1> ] != "<STR_LIT>" : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % args [ - <NUM_LIT:1> ] ) <EOL> as_form = True <EOL> silent = True <EOL> args = args [ : - <NUM_LIT:1> ] <EOL> elif args [ - <NUM_LIT:2> ] == "<STR_LIT>" : <EOL> as_form = True <EOL> silent = False <EOL> if as_form : <EOL> name = args [ - <NUM_LIT:1> ] <EOL> values = [ parser . compile_filter ( arg ) for arg in args [ <NUM_LIT:1> : - <NUM_LIT:2> ] ] <EOL> node = CycleNode ( values , name , silent = silent ) <EOL> if not hasattr ( parser , '<STR_LIT>' ) : <EOL> parser . _namedCycleNodes = { } <EOL> parser . _namedCycleNodes [ name ] = node <EOL> else : <EOL> values = [ parser . compile_filter ( arg ) for arg in args [ <NUM_LIT:1> : ] ] <EOL> node = CycleNode ( values ) <EOL> return node <EOL> @ register . tag <EOL> def csrf_token ( parser , token ) : <EOL> return CsrfTokenNode ( ) <EOL> @ register . tag <EOL> def debug ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> return DebugNode ( ) <EOL> @ register . tag ( '<STR_LIT>' ) <EOL> def do_filter ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> _ , rest = token . contents . split ( None , <NUM_LIT:1> ) <EOL> filter_expr = parser . compile_filter ( "<STR_LIT>" % ( rest ) ) <EOL> for func , unused in filter_expr . filters : <EOL> if getattr ( func , '<STR_LIT>' , func ) . __name__ in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise TemplateSyntaxError ( '<STR_LIT>' % func . __name__ ) <EOL> nodelist = parser . parse ( ( '<STR_LIT>' , ) ) <EOL> parser . delete_first_token ( ) <EOL> return FilterNode ( filter_expr , nodelist ) <EOL> @ register . tag <EOL> def firstof ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) [ <NUM_LIT:1> : ] <EOL> if len ( bits ) < <NUM_LIT:1> : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" ) <EOL> return FirstOfNode ( [ parser . compile_filter ( bit ) for bit in bits ] ) <EOL> @ register . tag ( '<STR_LIT>' ) <EOL> def do_for ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . contents . split ( ) <EOL> if len ( bits ) < <NUM_LIT:4> : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" <EOL> "<STR_LIT>" % token . contents ) <EOL> is_reversed = bits [ - <NUM_LIT:1> ] == '<STR_LIT>' <EOL> in_index = is_reversed and - <NUM_LIT:3> or - <NUM_LIT:2> <EOL> if bits [ in_index ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" <EOL> "<STR_LIT>" % token . contents ) <EOL> loopvars = re . split ( r'<STR_LIT>' , '<STR_LIT:U+0020>' . join ( bits [ <NUM_LIT:1> : in_index ] ) ) <EOL> for var in loopvars : <EOL> if not var or '<STR_LIT:U+0020>' in var : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" <EOL> "<STR_LIT>" % token . contents ) <EOL> sequence = parser . compile_filter ( bits [ in_index + <NUM_LIT:1> ] ) <EOL> nodelist_loop = parser . parse ( ( '<STR_LIT>' , '<STR_LIT>' , ) ) <EOL> token = parser . next_token ( ) <EOL> if token . contents == '<STR_LIT>' : <EOL> nodelist_empty = parser . parse ( ( '<STR_LIT>' , ) ) <EOL> parser . delete_first_token ( ) <EOL> else : <EOL> nodelist_empty = None <EOL> return ForNode ( loopvars , sequence , is_reversed , nodelist_loop , nodelist_empty ) <EOL> def do_ifequal ( parser , token , negate ) : <EOL> bits = list ( token . split_contents ( ) ) <EOL> if len ( bits ) != <NUM_LIT:3> : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % bits [ <NUM_LIT:0> ] ) <EOL> end_tag = '<STR_LIT:end>' + bits [ <NUM_LIT:0> ] <EOL> nodelist_true = parser . parse ( ( '<STR_LIT>' , end_tag ) ) <EOL> token = parser . next_token ( ) <EOL> if token . contents == '<STR_LIT>' : <EOL> nodelist_false = parser . parse ( ( end_tag , ) ) <EOL> parser . delete_first_token ( ) <EOL> else : <EOL> nodelist_false = NodeList ( ) <EOL> val1 = parser . compile_filter ( bits [ <NUM_LIT:1> ] ) <EOL> val2 = parser . compile_filter ( bits [ <NUM_LIT:2> ] ) <EOL> return IfEqualNode ( val1 , val2 , nodelist_true , nodelist_false , negate ) <EOL> @ register . tag <EOL> def ifequal ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> return do_ifequal ( parser , token , False ) <EOL> @ register . tag <EOL> def ifnotequal ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> return do_ifequal ( parser , token , True ) <EOL> class TemplateLiteral ( Literal ) : <EOL> def __init__ ( self , value , text ) : <EOL> self . value = value <EOL> self . text = text <EOL> def display ( self ) : <EOL> return self . text <EOL> def eval ( self , context ) : <EOL> return self . value . resolve ( context , ignore_failures = True ) <EOL> class TemplateIfParser ( IfParser ) : <EOL> error_class = TemplateSyntaxError <EOL> def __init__ ( self , parser , * args , ** kwargs ) : <EOL> self . template_parser = parser <EOL> super ( TemplateIfParser , self ) . __init__ ( * args , ** kwargs ) <EOL> def create_var ( self , value ) : <EOL> return TemplateLiteral ( self . template_parser . compile_filter ( value ) , value ) <EOL> @ register . tag ( '<STR_LIT>' ) <EOL> def do_if ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) [ <NUM_LIT:1> : ] <EOL> condition = TemplateIfParser ( parser , bits ) . parse ( ) <EOL> nodelist = parser . parse ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> conditions_nodelists = [ ( condition , nodelist ) ] <EOL> token = parser . next_token ( ) <EOL> while token . contents . startswith ( '<STR_LIT>' ) : <EOL> bits = token . split_contents ( ) [ <NUM_LIT:1> : ] <EOL> condition = TemplateIfParser ( parser , bits ) . parse ( ) <EOL> nodelist = parser . parse ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> conditions_nodelists . append ( ( condition , nodelist ) ) <EOL> token = parser . next_token ( ) <EOL> if token . contents == '<STR_LIT>' : <EOL> nodelist = parser . parse ( ( '<STR_LIT>' , ) ) <EOL> conditions_nodelists . append ( ( None , nodelist ) ) <EOL> token = parser . next_token ( ) <EOL> assert token . contents == '<STR_LIT>' <EOL> return IfNode ( conditions_nodelists ) <EOL> @ register . tag <EOL> def ifchanged ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . contents . split ( ) <EOL> nodelist_true = parser . parse ( ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> token = parser . next_token ( ) <EOL> if token . contents == '<STR_LIT>' : <EOL> nodelist_false = parser . parse ( ( '<STR_LIT>' , ) ) <EOL> parser . delete_first_token ( ) <EOL> else : <EOL> nodelist_false = NodeList ( ) <EOL> values = [ parser . compile_filter ( bit ) for bit in bits [ <NUM_LIT:1> : ] ] <EOL> return IfChangedNode ( nodelist_true , nodelist_false , * values ) <EOL> @ register . tag <EOL> def ssi ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> parsed = False <EOL> if len ( bits ) not in ( <NUM_LIT:2> , <NUM_LIT:3> ) : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if len ( bits ) == <NUM_LIT:3> : <EOL> if bits [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> parsed = True <EOL> else : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" <EOL> "<STR_LIT>" % bits [ <NUM_LIT:0> ] ) <EOL> filepath = parser . compile_filter ( bits [ <NUM_LIT:1> ] ) <EOL> return SsiNode ( filepath , parsed ) <EOL> @ register . tag <EOL> def load ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . contents . split ( ) <EOL> if len ( bits ) >= <NUM_LIT:4> and bits [ - <NUM_LIT:2> ] == "<STR_LIT>" : <EOL> try : <EOL> taglib = bits [ - <NUM_LIT:1> ] <EOL> lib = get_library ( taglib ) <EOL> except InvalidTemplateLibrary as e : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % <EOL> ( taglib , e ) ) <EOL> else : <EOL> temp_lib = Library ( ) <EOL> for name in bits [ <NUM_LIT:1> : - <NUM_LIT:2> ] : <EOL> if name in lib . tags : <EOL> temp_lib . tags [ name ] = lib . tags [ name ] <EOL> if name in lib . filters : <EOL> temp_lib . filters [ name ] = lib . filters [ name ] <EOL> elif name in lib . filters : <EOL> temp_lib . filters [ name ] = lib . filters [ name ] <EOL> else : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % <EOL> ( name , taglib ) ) <EOL> parser . add_library ( temp_lib ) <EOL> else : <EOL> for taglib in bits [ <NUM_LIT:1> : ] : <EOL> try : <EOL> lib = get_library ( taglib ) <EOL> parser . add_library ( lib ) <EOL> except InvalidTemplateLibrary as e : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % <EOL> ( taglib , e ) ) <EOL> return LoadNode ( ) <EOL> @ register . tag <EOL> def now ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> if len ( bits ) != <NUM_LIT:2> : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" ) <EOL> format_string = bits [ <NUM_LIT:1> ] [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> return NowNode ( format_string ) <EOL> @ register . tag <EOL> def regroup ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> firstbits = token . contents . split ( None , <NUM_LIT:3> ) <EOL> if len ( firstbits ) != <NUM_LIT:4> : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" ) <EOL> target = parser . compile_filter ( firstbits [ <NUM_LIT:1> ] ) <EOL> if firstbits [ <NUM_LIT:2> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" ) <EOL> lastbits_reversed = firstbits [ <NUM_LIT:3> ] [ : : - <NUM_LIT:1> ] . split ( None , <NUM_LIT:2> ) <EOL> if lastbits_reversed [ <NUM_LIT:1> ] [ : : - <NUM_LIT:1> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> var_name = lastbits_reversed [ <NUM_LIT:0> ] [ : : - <NUM_LIT:1> ] <EOL> expression = parser . compile_filter ( var_name + <EOL> VARIABLE_ATTRIBUTE_SEPARATOR + <EOL> lastbits_reversed [ <NUM_LIT:2> ] [ : : - <NUM_LIT:1> ] ) <EOL> return RegroupNode ( target , expression , var_name ) <EOL> @ register . tag <EOL> def spaceless ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> nodelist = parser . parse ( ( '<STR_LIT>' , ) ) <EOL> parser . delete_first_token ( ) <EOL> return SpacelessNode ( nodelist ) <EOL> @ register . tag <EOL> def templatetag ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . contents . split ( ) <EOL> if len ( bits ) != <NUM_LIT:2> : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" ) <EOL> tag = bits [ <NUM_LIT:1> ] <EOL> if tag not in TemplateTagNode . mapping : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( tag , list ( TemplateTagNode . mapping ) ) ) <EOL> return TemplateTagNode ( tag ) <EOL> @ register . tag <EOL> def url ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> if len ( bits ) < <NUM_LIT:2> : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" <EOL> "<STR_LIT>" % bits [ <NUM_LIT:0> ] ) <EOL> try : <EOL> viewname = parser . compile_filter ( bits [ <NUM_LIT:1> ] ) <EOL> except TemplateSyntaxError as exc : <EOL> exc . args = ( exc . args [ <NUM_LIT:0> ] + "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> raise <EOL> args = [ ] <EOL> kwargs = { } <EOL> asvar = None <EOL> bits = bits [ <NUM_LIT:2> : ] <EOL> if len ( bits ) >= <NUM_LIT:2> and bits [ - <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> asvar = bits [ - <NUM_LIT:1> ] <EOL> bits = bits [ : - <NUM_LIT:2> ] <EOL> if len ( bits ) : <EOL> for bit in bits : <EOL> match = kwarg_re . match ( bit ) <EOL> if not match : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" ) <EOL> name , value = match . groups ( ) <EOL> if name : <EOL> kwargs [ name ] = parser . compile_filter ( value ) <EOL> else : <EOL> args . append ( parser . compile_filter ( value ) ) <EOL> return URLNode ( viewname , args , kwargs , asvar ) <EOL> @ register . tag <EOL> def verbatim ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> nodelist = parser . parse ( ( '<STR_LIT>' , ) ) <EOL> parser . delete_first_token ( ) <EOL> return VerbatimNode ( nodelist . render ( Context ( ) ) ) <EOL> @ register . tag <EOL> def widthratio ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . contents . split ( ) <EOL> if len ( bits ) != <NUM_LIT:4> : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" ) <EOL> tag , this_value_expr , max_value_expr , max_width = bits <EOL> return WidthRatioNode ( parser . compile_filter ( this_value_expr ) , <EOL> parser . compile_filter ( max_value_expr ) , <EOL> parser . compile_filter ( max_width ) ) <EOL> @ register . tag ( '<STR_LIT>' ) <EOL> def do_with ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> remaining_bits = bits [ <NUM_LIT:1> : ] <EOL> extra_context = token_kwargs ( remaining_bits , parser , support_legacy = True ) <EOL> if not extra_context : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" <EOL> "<STR_LIT>" % bits [ <NUM_LIT:0> ] ) <EOL> if remaining_bits : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % <EOL> ( bits [ <NUM_LIT:0> ] , remaining_bits [ <NUM_LIT:0> ] ) ) <EOL> nodelist = parser . parse ( ( '<STR_LIT>' , ) ) <EOL> parser . delete_first_token ( ) <EOL> return WithNode ( None , None , nodelist , extra_context = extra_context ) </s>
