<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> import inspect <EOL> import logging <EOL> import re <EOL> from django . template . context import ( <EOL> BaseContext , Context , ContextPopException , RequestContext , <EOL> ) <EOL> from django . utils import six <EOL> from django . utils . deprecation import ( <EOL> DeprecationInstanceCheck , RemovedInDjango20Warning , <EOL> ) <EOL> from django . utils . encoding import ( <EOL> force_str , force_text , python_2_unicode_compatible , <EOL> ) <EOL> from django . utils . formats import localize <EOL> from django . utils . html import conditional_escape , escape <EOL> from django . utils . inspect import getargspec <EOL> from django . utils . safestring import ( <EOL> EscapeData , SafeData , mark_for_escaping , mark_safe , <EOL> ) <EOL> from django . utils . text import ( <EOL> get_text_list , smart_split , unescape_string_literal , <EOL> ) <EOL> from django . utils . timezone import template_localtime <EOL> from django . utils . translation import pgettext_lazy , ugettext_lazy <EOL> from . exceptions import TemplateSyntaxError <EOL> TOKEN_TEXT = <NUM_LIT:0> <EOL> TOKEN_VAR = <NUM_LIT:1> <EOL> TOKEN_BLOCK = <NUM_LIT:2> <EOL> TOKEN_COMMENT = <NUM_LIT:3> <EOL> TOKEN_MAPPING = { <EOL> TOKEN_TEXT : '<STR_LIT>' , <EOL> TOKEN_VAR : '<STR_LIT>' , <EOL> TOKEN_BLOCK : '<STR_LIT>' , <EOL> TOKEN_COMMENT : '<STR_LIT>' , <EOL> } <EOL> FILTER_SEPARATOR = '<STR_LIT:|>' <EOL> FILTER_ARGUMENT_SEPARATOR = '<STR_LIT::>' <EOL> VARIABLE_ATTRIBUTE_SEPARATOR = '<STR_LIT:.>' <EOL> BLOCK_TAG_START = '<STR_LIT>' <EOL> BLOCK_TAG_END = '<STR_LIT>' <EOL> VARIABLE_TAG_START = '<STR_LIT>' <EOL> VARIABLE_TAG_END = '<STR_LIT>' <EOL> COMMENT_TAG_START = '<STR_LIT>' <EOL> COMMENT_TAG_END = '<STR_LIT>' <EOL> TRANSLATOR_COMMENT_MARK = '<STR_LIT>' <EOL> SINGLE_BRACE_START = '<STR_LIT:{>' <EOL> SINGLE_BRACE_END = '<STR_LIT:}>' <EOL> UNKNOWN_SOURCE = '<STR_LIT>' <EOL> tag_re = ( re . compile ( '<STR_LIT>' % <EOL> ( re . escape ( BLOCK_TAG_START ) , re . escape ( BLOCK_TAG_END ) , <EOL> re . escape ( VARIABLE_TAG_START ) , re . escape ( VARIABLE_TAG_END ) , <EOL> re . escape ( COMMENT_TAG_START ) , re . escape ( COMMENT_TAG_END ) ) ) ) <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class TemplateEncodingError ( Exception ) : <EOL> pass <EOL> @ python_2_unicode_compatible <EOL> class VariableDoesNotExist ( Exception ) : <EOL> def __init__ ( self , msg , params = ( ) ) : <EOL> self . msg = msg <EOL> self . params = params <EOL> def __str__ ( self ) : <EOL> return self . msg % tuple ( force_text ( p , errors = '<STR_LIT:replace>' ) for p in self . params ) <EOL> class Origin ( object ) : <EOL> def __init__ ( self , name , template_name = None , loader = None ) : <EOL> self . name = name <EOL> self . template_name = template_name <EOL> self . loader = loader <EOL> def __str__ ( self ) : <EOL> return self . name <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , Origin ) : <EOL> return False <EOL> return ( <EOL> self . name == other . name and <EOL> self . loader == other . loader <EOL> ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> @ property <EOL> def loader_name ( self ) : <EOL> if self . loader : <EOL> return '<STR_LIT>' % ( <EOL> self . loader . __module__ , self . loader . __class__ . __name__ , <EOL> ) <EOL> class StringOrigin ( six . with_metaclass ( DeprecationInstanceCheck , Origin ) ) : <EOL> alternative = '<STR_LIT>' <EOL> deprecation_warning = RemovedInDjango20Warning <EOL> class Template ( object ) : <EOL> def __init__ ( self , template_string , origin = None , name = None , engine = None ) : <EOL> try : <EOL> template_string = force_text ( template_string ) <EOL> except UnicodeDecodeError : <EOL> raise TemplateEncodingError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if engine is None : <EOL> from . engine import Engine <EOL> engine = Engine . get_default ( ) <EOL> if origin is None : <EOL> origin = Origin ( UNKNOWN_SOURCE ) <EOL> self . name = name <EOL> self . origin = origin <EOL> self . engine = engine <EOL> self . source = template_string <EOL> self . nodelist = self . compile_nodelist ( ) <EOL> def __iter__ ( self ) : <EOL> for node in self . nodelist : <EOL> for subnode in node : <EOL> yield subnode <EOL> def _render ( self , context ) : <EOL> return self . nodelist . render ( context ) <EOL> def render ( self , context ) : <EOL> "<STR_LIT>" <EOL> context . render_context . push ( ) <EOL> try : <EOL> if context . template is None : <EOL> with context . bind_template ( self ) : <EOL> context . template_name = self . name <EOL> return self . _render ( context ) <EOL> else : <EOL> return self . _render ( context ) <EOL> finally : <EOL> context . render_context . pop ( ) <EOL> def compile_nodelist ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . engine . debug : <EOL> lexer = DebugLexer ( self . source ) <EOL> else : <EOL> lexer = Lexer ( self . source ) <EOL> tokens = lexer . tokenize ( ) <EOL> parser = Parser ( <EOL> tokens , self . engine . template_libraries , self . engine . template_builtins , <EOL> self . origin , <EOL> ) <EOL> try : <EOL> return parser . parse ( ) <EOL> except Exception as e : <EOL> if self . engine . debug : <EOL> e . template_debug = self . get_exception_info ( e , e . token ) <EOL> raise <EOL> def get_exception_info ( self , exception , token ) : <EOL> """<STR_LIT>""" <EOL> start , end = token . position <EOL> context_lines = <NUM_LIT:10> <EOL> line = <NUM_LIT:0> <EOL> upto = <NUM_LIT:0> <EOL> source_lines = [ ] <EOL> before = during = after = "<STR_LIT>" <EOL> for num , next in enumerate ( linebreak_iter ( self . source ) ) : <EOL> if start >= upto and end <= next : <EOL> line = num <EOL> before = escape ( self . source [ upto : start ] ) <EOL> during = escape ( self . source [ start : end ] ) <EOL> after = escape ( self . source [ end : next ] ) <EOL> source_lines . append ( ( num , escape ( self . source [ upto : next ] ) ) ) <EOL> upto = next <EOL> total = len ( source_lines ) <EOL> top = max ( <NUM_LIT:1> , line - context_lines ) <EOL> bottom = min ( total , line + <NUM_LIT:1> + context_lines ) <EOL> try : <EOL> message = force_text ( exception . args [ <NUM_LIT:0> ] ) <EOL> except ( IndexError , UnicodeDecodeError ) : <EOL> message = '<STR_LIT>' <EOL> return { <EOL> '<STR_LIT:message>' : message , <EOL> '<STR_LIT>' : source_lines [ top : bottom ] , <EOL> '<STR_LIT>' : before , <EOL> '<STR_LIT>' : during , <EOL> '<STR_LIT>' : after , <EOL> '<STR_LIT>' : top , <EOL> '<STR_LIT>' : bottom , <EOL> '<STR_LIT>' : total , <EOL> '<STR_LIT>' : line , <EOL> '<STR_LIT:name>' : self . origin . name , <EOL> '<STR_LIT:start>' : start , <EOL> '<STR_LIT:end>' : end , <EOL> } <EOL> def linebreak_iter ( template_source ) : <EOL> yield <NUM_LIT:0> <EOL> p = template_source . find ( '<STR_LIT:\n>' ) <EOL> while p >= <NUM_LIT:0> : <EOL> yield p + <NUM_LIT:1> <EOL> p = template_source . find ( '<STR_LIT:\n>' , p + <NUM_LIT:1> ) <EOL> yield len ( template_source ) + <NUM_LIT:1> <EOL> class Token ( object ) : <EOL> def __init__ ( self , token_type , contents , position = None , lineno = None ) : <EOL> """<STR_LIT>""" <EOL> self . token_type , self . contents = token_type , contents <EOL> self . lineno = lineno <EOL> self . position = position <EOL> def __str__ ( self ) : <EOL> token_name = TOKEN_MAPPING [ self . token_type ] <EOL> return ( '<STR_LIT>' % <EOL> ( token_name , self . contents [ : <NUM_LIT:20> ] . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) ) ) <EOL> def split_contents ( self ) : <EOL> split = [ ] <EOL> bits = iter ( smart_split ( self . contents ) ) <EOL> for bit in bits : <EOL> if bit . startswith ( ( '<STR_LIT>' , "<STR_LIT>" ) ) : <EOL> sentinel = bit [ <NUM_LIT:2> ] + '<STR_LIT:)>' <EOL> trans_bit = [ bit ] <EOL> while not bit . endswith ( sentinel ) : <EOL> bit = next ( bits ) <EOL> trans_bit . append ( bit ) <EOL> bit = '<STR_LIT:U+0020>' . join ( trans_bit ) <EOL> split . append ( bit ) <EOL> return split <EOL> class Lexer ( object ) : <EOL> def __init__ ( self , template_string ) : <EOL> self . template_string = template_string <EOL> self . verbatim = False <EOL> def tokenize ( self ) : <EOL> """<STR_LIT>""" <EOL> in_tag = False <EOL> lineno = <NUM_LIT:1> <EOL> result = [ ] <EOL> for bit in tag_re . split ( self . template_string ) : <EOL> if bit : <EOL> result . append ( self . create_token ( bit , None , lineno , in_tag ) ) <EOL> in_tag = not in_tag <EOL> lineno += bit . count ( '<STR_LIT:\n>' ) <EOL> return result <EOL> def create_token ( self , token_string , position , lineno , in_tag ) : <EOL> """<STR_LIT>""" <EOL> if in_tag and token_string . startswith ( BLOCK_TAG_START ) : <EOL> block_content = token_string [ <NUM_LIT:2> : - <NUM_LIT:2> ] . strip ( ) <EOL> if self . verbatim and block_content == self . verbatim : <EOL> self . verbatim = False <EOL> if in_tag and not self . verbatim : <EOL> if token_string . startswith ( VARIABLE_TAG_START ) : <EOL> token = Token ( TOKEN_VAR , token_string [ <NUM_LIT:2> : - <NUM_LIT:2> ] . strip ( ) , position , lineno ) <EOL> elif token_string . startswith ( BLOCK_TAG_START ) : <EOL> if block_content [ : <NUM_LIT:9> ] in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . verbatim = '<STR_LIT>' % block_content <EOL> token = Token ( TOKEN_BLOCK , block_content , position , lineno ) <EOL> elif token_string . startswith ( COMMENT_TAG_START ) : <EOL> content = '<STR_LIT>' <EOL> if token_string . find ( TRANSLATOR_COMMENT_MARK ) : <EOL> content = token_string [ <NUM_LIT:2> : - <NUM_LIT:2> ] . strip ( ) <EOL> token = Token ( TOKEN_COMMENT , content , position , lineno ) <EOL> else : <EOL> token = Token ( TOKEN_TEXT , token_string , position , lineno ) <EOL> return token <EOL> class DebugLexer ( Lexer ) : <EOL> def tokenize ( self ) : <EOL> """<STR_LIT>""" <EOL> lineno = <NUM_LIT:1> <EOL> result = [ ] <EOL> upto = <NUM_LIT:0> <EOL> for match in tag_re . finditer ( self . template_string ) : <EOL> start , end = match . span ( ) <EOL> if start > upto : <EOL> token_string = self . template_string [ upto : start ] <EOL> result . append ( self . create_token ( token_string , ( upto , start ) , lineno , in_tag = False ) ) <EOL> lineno += token_string . count ( '<STR_LIT:\n>' ) <EOL> upto = start <EOL> token_string = self . template_string [ start : end ] <EOL> result . append ( self . create_token ( token_string , ( start , end ) , lineno , in_tag = True ) ) <EOL> lineno += token_string . count ( '<STR_LIT:\n>' ) <EOL> upto = end <EOL> last_bit = self . template_string [ upto : ] <EOL> if last_bit : <EOL> result . append ( self . create_token ( last_bit , ( upto , upto + len ( last_bit ) ) , lineno , in_tag = False ) ) <EOL> return result <EOL> class Parser ( object ) : <EOL> def __init__ ( self , tokens , libraries = None , builtins = None , origin = None ) : <EOL> self . tokens = tokens <EOL> self . tags = { } <EOL> self . filters = { } <EOL> self . command_stack = [ ] <EOL> if libraries is None : <EOL> libraries = { } <EOL> if builtins is None : <EOL> builtins = [ ] <EOL> self . libraries = libraries <EOL> for builtin in builtins : <EOL> self . add_library ( builtin ) <EOL> self . origin = origin <EOL> def parse ( self , parse_until = None ) : <EOL> """<STR_LIT>""" <EOL> if parse_until is None : <EOL> parse_until = [ ] <EOL> nodelist = NodeList ( ) <EOL> while self . tokens : <EOL> token = self . next_token ( ) <EOL> if token . token_type == <NUM_LIT:0> : <EOL> self . extend_nodelist ( nodelist , TextNode ( token . contents ) , token ) <EOL> elif token . token_type == <NUM_LIT:1> : <EOL> if not token . contents : <EOL> raise self . error ( token , '<STR_LIT>' % token . lineno ) <EOL> try : <EOL> filter_expression = self . compile_filter ( token . contents ) <EOL> except TemplateSyntaxError as e : <EOL> raise self . error ( token , e ) <EOL> var_node = VariableNode ( filter_expression ) <EOL> self . extend_nodelist ( nodelist , var_node , token ) <EOL> elif token . token_type == <NUM_LIT:2> : <EOL> try : <EOL> command = token . contents . split ( ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> raise self . error ( token , '<STR_LIT>' % token . lineno ) <EOL> if command in parse_until : <EOL> self . prepend_token ( token ) <EOL> return nodelist <EOL> self . command_stack . append ( ( command , token ) ) <EOL> try : <EOL> compile_func = self . tags [ command ] <EOL> except KeyError : <EOL> self . invalid_block_tag ( token , command , parse_until ) <EOL> try : <EOL> compiled_result = compile_func ( self , token ) <EOL> except Exception as e : <EOL> raise self . error ( token , e ) <EOL> self . extend_nodelist ( nodelist , compiled_result , token ) <EOL> self . command_stack . pop ( ) <EOL> if parse_until : <EOL> self . unclosed_block_tag ( parse_until ) <EOL> return nodelist <EOL> def skip_past ( self , endtag ) : <EOL> while self . tokens : <EOL> token = self . next_token ( ) <EOL> if token . token_type == TOKEN_BLOCK and token . contents == endtag : <EOL> return <EOL> self . unclosed_block_tag ( [ endtag ] ) <EOL> def extend_nodelist ( self , nodelist , node , token ) : <EOL> if node . must_be_first and nodelist . contains_nontext : <EOL> raise self . error ( <EOL> token , '<STR_LIT>' % node , <EOL> ) <EOL> if isinstance ( nodelist , NodeList ) and not isinstance ( node , TextNode ) : <EOL> nodelist . contains_nontext = True <EOL> node . token = token <EOL> node . origin = self . origin <EOL> nodelist . append ( node ) <EOL> def error ( self , token , e ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( e , Exception ) : <EOL> e = TemplateSyntaxError ( e ) <EOL> if not hasattr ( e , '<STR_LIT>' ) : <EOL> e . token = token <EOL> return e <EOL> def invalid_block_tag ( self , token , command , parse_until = None ) : <EOL> if parse_until : <EOL> raise self . error ( <EOL> token , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> token . lineno , <EOL> command , <EOL> get_text_list ( [ "<STR_LIT>" % p for p in parse_until ] , '<STR_LIT>' ) , <EOL> ) , <EOL> ) <EOL> raise self . error ( <EOL> token , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( token . lineno , command ) <EOL> ) <EOL> def unclosed_block_tag ( self , parse_until ) : <EOL> command , token = self . command_stack . pop ( ) <EOL> msg = "<STR_LIT>" % ( <EOL> token . lineno , <EOL> command , <EOL> '<STR_LIT:U+002CU+0020>' . join ( parse_until ) , <EOL> ) <EOL> raise self . error ( token , msg ) <EOL> def next_token ( self ) : <EOL> return self . tokens . pop ( <NUM_LIT:0> ) <EOL> def prepend_token ( self , token ) : <EOL> self . tokens . insert ( <NUM_LIT:0> , token ) <EOL> def delete_first_token ( self ) : <EOL> del self . tokens [ <NUM_LIT:0> ] <EOL> def add_library ( self , lib ) : <EOL> self . tags . update ( lib . tags ) <EOL> self . filters . update ( lib . filters ) <EOL> def compile_filter ( self , token ) : <EOL> """<STR_LIT>""" <EOL> return FilterExpression ( token , self ) <EOL> def find_filter ( self , filter_name ) : <EOL> if filter_name in self . filters : <EOL> return self . filters [ filter_name ] <EOL> else : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % filter_name ) <EOL> constant_string = r"""<STR_LIT>""" % { <EOL> '<STR_LIT>' : r'<STR_LIT>' , <EOL> '<STR_LIT>' : r"<STR_LIT>" , <EOL> '<STR_LIT>' : re . escape ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : re . escape ( "<STR_LIT:)>" ) , <EOL> } <EOL> constant_string = constant_string . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) <EOL> filter_raw_string = r"""<STR_LIT>""" % { <EOL> '<STR_LIT>' : constant_string , <EOL> '<STR_LIT>' : r'<STR_LIT>' , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : re . escape ( FILTER_SEPARATOR ) , <EOL> '<STR_LIT>' : re . escape ( FILTER_ARGUMENT_SEPARATOR ) , <EOL> } <EOL> filter_re = re . compile ( filter_raw_string , re . UNICODE | re . VERBOSE ) <EOL> class FilterExpression ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , token , parser ) : <EOL> self . token = token <EOL> matches = filter_re . finditer ( token ) <EOL> var_obj = None <EOL> filters = [ ] <EOL> upto = <NUM_LIT:0> <EOL> for match in matches : <EOL> start = match . start ( ) <EOL> if upto != start : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( token [ : upto ] , token [ upto : start ] , <EOL> token [ start : ] ) ) <EOL> if var_obj is None : <EOL> var , constant = match . group ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if constant : <EOL> try : <EOL> var_obj = Variable ( constant ) . resolve ( { } ) <EOL> except VariableDoesNotExist : <EOL> var_obj = None <EOL> elif var is None : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" <EOL> "<STR_LIT>" % token ) <EOL> else : <EOL> var_obj = Variable ( var ) <EOL> else : <EOL> filter_name = match . group ( "<STR_LIT>" ) <EOL> args = [ ] <EOL> constant_arg , var_arg = match . group ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if constant_arg : <EOL> args . append ( ( False , Variable ( constant_arg ) . resolve ( { } ) ) ) <EOL> elif var_arg : <EOL> args . append ( ( True , Variable ( var_arg ) ) ) <EOL> filter_func = parser . find_filter ( filter_name ) <EOL> self . args_check ( filter_name , filter_func , args ) <EOL> filters . append ( ( filter_func , args ) ) <EOL> upto = match . end ( ) <EOL> if upto != len ( token ) : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( token [ upto : ] , token ) ) <EOL> self . filters = filters <EOL> self . var = var_obj <EOL> def resolve ( self , context , ignore_failures = False ) : <EOL> if isinstance ( self . var , Variable ) : <EOL> try : <EOL> obj = self . var . resolve ( context ) <EOL> except VariableDoesNotExist : <EOL> if ignore_failures : <EOL> obj = None <EOL> else : <EOL> string_if_invalid = context . template . engine . string_if_invalid <EOL> if string_if_invalid : <EOL> if '<STR_LIT:%s>' in string_if_invalid : <EOL> return string_if_invalid % self . var <EOL> else : <EOL> return string_if_invalid <EOL> else : <EOL> obj = string_if_invalid <EOL> else : <EOL> obj = self . var <EOL> for func , args in self . filters : <EOL> arg_vals = [ ] <EOL> for lookup , arg in args : <EOL> if not lookup : <EOL> arg_vals . append ( mark_safe ( arg ) ) <EOL> else : <EOL> arg_vals . append ( arg . resolve ( context ) ) <EOL> if getattr ( func , '<STR_LIT>' , False ) : <EOL> obj = template_localtime ( obj , context . use_tz ) <EOL> if getattr ( func , '<STR_LIT>' , False ) : <EOL> new_obj = func ( obj , autoescape = context . autoescape , * arg_vals ) <EOL> else : <EOL> new_obj = func ( obj , * arg_vals ) <EOL> if getattr ( func , '<STR_LIT>' , False ) and isinstance ( obj , SafeData ) : <EOL> obj = mark_safe ( new_obj ) <EOL> elif isinstance ( obj , EscapeData ) : <EOL> obj = mark_for_escaping ( new_obj ) <EOL> else : <EOL> obj = new_obj <EOL> return obj <EOL> def args_check ( name , func , provided ) : <EOL> provided = list ( provided ) <EOL> plen = len ( provided ) + <NUM_LIT:1> <EOL> func = getattr ( func , '<STR_LIT>' , func ) <EOL> args , _ , _ , defaults = getargspec ( func ) <EOL> alen = len ( args ) <EOL> dlen = len ( defaults or [ ] ) <EOL> if plen < ( alen - dlen ) or plen > alen : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % <EOL> ( name , alen - dlen , plen ) ) <EOL> return True <EOL> args_check = staticmethod ( args_check ) <EOL> def __str__ ( self ) : <EOL> return self . token <EOL> class Variable ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , var ) : <EOL> self . var = var <EOL> self . literal = None <EOL> self . lookups = None <EOL> self . translate = False <EOL> self . message_context = None <EOL> if not isinstance ( var , six . string_types ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" % type ( var ) ) <EOL> try : <EOL> self . literal = float ( var ) <EOL> if '<STR_LIT:.>' not in var and '<STR_LIT:e>' not in var . lower ( ) : <EOL> self . literal = int ( self . literal ) <EOL> if var . endswith ( '<STR_LIT:.>' ) : <EOL> raise ValueError <EOL> except ValueError : <EOL> if var . startswith ( '<STR_LIT>' ) and var . endswith ( '<STR_LIT:)>' ) : <EOL> self . translate = True <EOL> var = var [ <NUM_LIT:2> : - <NUM_LIT:1> ] <EOL> try : <EOL> self . literal = mark_safe ( unescape_string_literal ( var ) ) <EOL> except ValueError : <EOL> if var . find ( VARIABLE_ATTRIBUTE_SEPARATOR + '<STR_LIT:_>' ) > - <NUM_LIT:1> or var [ <NUM_LIT:0> ] == '<STR_LIT:_>' : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> var ) <EOL> self . lookups = tuple ( var . split ( VARIABLE_ATTRIBUTE_SEPARATOR ) ) <EOL> def resolve ( self , context ) : <EOL> """<STR_LIT>""" <EOL> if self . lookups is not None : <EOL> value = self . _resolve_lookup ( context ) <EOL> else : <EOL> value = self . literal <EOL> if self . translate : <EOL> is_safe = isinstance ( value , SafeData ) <EOL> msgid = value . replace ( '<STR_LIT:%>' , '<STR_LIT>' ) <EOL> msgid = mark_safe ( msgid ) if is_safe else msgid <EOL> if self . message_context : <EOL> return pgettext_lazy ( self . message_context , msgid ) <EOL> else : <EOL> return ugettext_lazy ( msgid ) <EOL> return value <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . var ) <EOL> def __str__ ( self ) : <EOL> return self . var <EOL> def _resolve_lookup ( self , context ) : <EOL> """<STR_LIT>""" <EOL> current = context <EOL> try : <EOL> for bit in self . lookups : <EOL> try : <EOL> current = current [ bit ] <EOL> except ( TypeError , AttributeError , KeyError , ValueError , IndexError ) : <EOL> try : <EOL> if isinstance ( current , BaseContext ) and getattr ( type ( current ) , bit ) : <EOL> raise AttributeError <EOL> current = getattr ( current , bit ) <EOL> except ( TypeError , AttributeError ) as e : <EOL> if ( isinstance ( e , AttributeError ) and <EOL> not isinstance ( current , BaseContext ) and bit in dir ( current ) ) : <EOL> raise <EOL> try : <EOL> current = current [ int ( bit ) ] <EOL> except ( IndexError , <EOL> ValueError , <EOL> KeyError , <EOL> TypeError ) : <EOL> raise VariableDoesNotExist ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> ( bit , current ) ) <EOL> if callable ( current ) : <EOL> if getattr ( current , '<STR_LIT>' , False ) : <EOL> pass <EOL> elif getattr ( current , '<STR_LIT>' , False ) : <EOL> current = context . template . engine . string_if_invalid <EOL> else : <EOL> try : <EOL> current = current ( ) <EOL> except TypeError : <EOL> try : <EOL> inspect . getcallargs ( current ) <EOL> except TypeError : <EOL> current = context . template . engine . string_if_invalid <EOL> else : <EOL> raise <EOL> except Exception as e : <EOL> template_name = getattr ( context , '<STR_LIT>' , None ) or '<STR_LIT>' <EOL> logger . debug ( <EOL> "<STR_LIT>" , <EOL> bit , <EOL> template_name , <EOL> exc_info = True , <EOL> ) <EOL> if getattr ( e , '<STR_LIT>' , False ) : <EOL> current = context . template . engine . string_if_invalid <EOL> else : <EOL> raise <EOL> return current <EOL> class Node ( object ) : <EOL> must_be_first = False <EOL> child_nodelists = ( '<STR_LIT>' , ) <EOL> token = None <EOL> def render ( self , context ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def render_annotated ( self , context ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . render ( context ) <EOL> except Exception as e : <EOL> if context . template . engine . debug and not hasattr ( e , '<STR_LIT>' ) : <EOL> e . template_debug = context . template . get_exception_info ( e , self . token ) <EOL> raise <EOL> def __iter__ ( self ) : <EOL> yield self <EOL> def get_nodes_by_type ( self , nodetype ) : <EOL> """<STR_LIT>""" <EOL> nodes = [ ] <EOL> if isinstance ( self , nodetype ) : <EOL> nodes . append ( self ) <EOL> for attr in self . child_nodelists : <EOL> nodelist = getattr ( self , attr , None ) <EOL> if nodelist : <EOL> nodes . extend ( nodelist . get_nodes_by_type ( nodetype ) ) <EOL> return nodes <EOL> class NodeList ( list ) : <EOL> contains_nontext = False <EOL> def render ( self , context ) : <EOL> bits = [ ] <EOL> for node in self : <EOL> if isinstance ( node , Node ) : <EOL> bit = node . render_annotated ( context ) <EOL> else : <EOL> bit = node <EOL> bits . append ( force_text ( bit ) ) <EOL> return mark_safe ( '<STR_LIT>' . join ( bits ) ) <EOL> def get_nodes_by_type ( self , nodetype ) : <EOL> "<STR_LIT>" <EOL> nodes = [ ] <EOL> for node in self : <EOL> nodes . extend ( node . get_nodes_by_type ( nodetype ) ) <EOL> return nodes <EOL> class TextNode ( Node ) : <EOL> def __init__ ( self , s ) : <EOL> self . s = s <EOL> def __repr__ ( self ) : <EOL> rep = "<STR_LIT>" % ( self . __class__ . __name__ , self . s [ : <NUM_LIT> ] ) <EOL> return force_str ( rep , '<STR_LIT:ascii>' , errors = '<STR_LIT:replace>' ) <EOL> def render ( self , context ) : <EOL> return self . s <EOL> def render_value_in_context ( value , context ) : <EOL> """<STR_LIT>""" <EOL> value = template_localtime ( value , use_tz = context . use_tz ) <EOL> value = localize ( value , use_l10n = context . use_l10n ) <EOL> value = force_text ( value ) <EOL> if ( ( context . autoescape and not isinstance ( value , SafeData ) ) or <EOL> isinstance ( value , EscapeData ) ) : <EOL> return conditional_escape ( value ) <EOL> else : <EOL> return value <EOL> class VariableNode ( Node ) : <EOL> def __init__ ( self , filter_expression ) : <EOL> self . filter_expression = filter_expression <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . filter_expression <EOL> def render ( self , context ) : <EOL> try : <EOL> output = self . filter_expression . resolve ( context ) <EOL> except UnicodeDecodeError : <EOL> return '<STR_LIT>' <EOL> return render_value_in_context ( output , context ) <EOL> kwarg_re = re . compile ( r"<STR_LIT>" ) <EOL> def token_kwargs ( bits , parser , support_legacy = False ) : <EOL> """<STR_LIT>""" <EOL> if not bits : <EOL> return { } <EOL> match = kwarg_re . match ( bits [ <NUM_LIT:0> ] ) <EOL> kwarg_format = match and match . group ( <NUM_LIT:1> ) <EOL> if not kwarg_format : <EOL> if not support_legacy : <EOL> return { } <EOL> if len ( bits ) < <NUM_LIT:3> or bits [ <NUM_LIT:1> ] != '<STR_LIT>' : <EOL> return { } <EOL> kwargs = { } <EOL> while bits : <EOL> if kwarg_format : <EOL> match = kwarg_re . match ( bits [ <NUM_LIT:0> ] ) <EOL> if not match or not match . group ( <NUM_LIT:1> ) : <EOL> return kwargs <EOL> key , value = match . groups ( ) <EOL> del bits [ : <NUM_LIT:1> ] <EOL> else : <EOL> if len ( bits ) < <NUM_LIT:3> or bits [ <NUM_LIT:1> ] != '<STR_LIT>' : <EOL> return kwargs <EOL> key , value = bits [ <NUM_LIT:2> ] , bits [ <NUM_LIT:0> ] <EOL> del bits [ : <NUM_LIT:3> ] <EOL> kwargs [ key ] = parser . compile_filter ( value ) <EOL> if bits and not kwarg_format : <EOL> if bits [ <NUM_LIT:0> ] != '<STR_LIT>' : <EOL> return kwargs <EOL> del bits [ : <NUM_LIT:1> ] <EOL> return kwargs </s>
