<s> import imp <EOL> import re <EOL> from inspect import getargspec <EOL> from django . conf import settings <EOL> from django . template . context import Context , RequestContext , ContextPopException <EOL> from django . utils . importlib import import_module <EOL> from django . utils . itercompat import is_iterable <EOL> from django . utils . functional import curry , Promise <EOL> from django . utils . text import smart_split , unescape_string_literal , get_text_list <EOL> from django . utils . encoding import smart_unicode , force_unicode , smart_str <EOL> from django . utils . translation import ugettext_lazy <EOL> from django . utils . safestring import SafeData , EscapeData , mark_safe , mark_for_escaping <EOL> from django . utils . formats import localize <EOL> from django . utils . html import escape <EOL> from django . utils . module_loading import module_has_submodule <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> 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> ALLOWED_VARIABLE_CHARS = '<STR_LIT>' <EOL> UNKNOWN_SOURCE = '<STR_LIT>' <EOL> tag_re = re . compile ( '<STR_LIT>' % ( 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> libraries = { } <EOL> builtins = [ ] <EOL> invalid_var_format_string = None <EOL> class TemplateSyntaxError ( Exception ) : <EOL> pass <EOL> class TemplateDoesNotExist ( Exception ) : <EOL> pass <EOL> class TemplateEncodingError ( Exception ) : <EOL> pass <EOL> class VariableDoesNotExist ( Exception ) : <EOL> def __init__ ( self , msg , params = ( ) ) : <EOL> self . msg = msg <EOL> self . params = params <EOL> def __str__ ( self ) : <EOL> return unicode ( self ) . encode ( '<STR_LIT:utf-8>' ) <EOL> def __unicode__ ( self ) : <EOL> return self . msg % tuple ( [ force_unicode ( p , errors = '<STR_LIT:replace>' ) for p in self . params ] ) <EOL> class InvalidTemplateLibrary ( Exception ) : <EOL> pass <EOL> class Origin ( object ) : <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> def reload ( self ) : <EOL> raise NotImplementedError <EOL> def __str__ ( self ) : <EOL> return self . name <EOL> class StringOrigin ( Origin ) : <EOL> def __init__ ( self , source ) : <EOL> super ( StringOrigin , self ) . __init__ ( UNKNOWN_SOURCE ) <EOL> self . source = source <EOL> def reload ( self ) : <EOL> return self . source <EOL> class Template ( object ) : <EOL> def __init__ ( self , template_string , origin = None , name = '<STR_LIT>' ) : <EOL> try : <EOL> template_string = smart_unicode ( template_string ) <EOL> except UnicodeDecodeError : <EOL> raise TemplateEncodingError ( "<STR_LIT>" ) <EOL> if settings . TEMPLATE_DEBUG and origin is None : <EOL> origin = StringOrigin ( template_string ) <EOL> self . nodelist = compile_string ( template_string , origin ) <EOL> self . name = name <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> return self . _render ( context ) <EOL> finally : <EOL> context . render_context . pop ( ) <EOL> def compile_string ( template_string , origin ) : <EOL> "<STR_LIT>" <EOL> if settings . TEMPLATE_DEBUG : <EOL> from debug import DebugLexer , DebugParser <EOL> lexer_class , parser_class = DebugLexer , DebugParser <EOL> else : <EOL> lexer_class , parser_class = Lexer , Parser <EOL> lexer = lexer_class ( template_string , origin ) <EOL> parser = parser_class ( lexer . tokenize ( ) ) <EOL> return parser . parse ( ) <EOL> class Token ( object ) : <EOL> def __init__ ( self , token_type , contents ) : <EOL> self . token_type , self . contents = token_type , contents <EOL> self . lineno = None <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( { TOKEN_TEXT : '<STR_LIT>' , TOKEN_VAR : '<STR_LIT>' , TOKEN_BLOCK : '<STR_LIT>' , TOKEN_COMMENT : '<STR_LIT>' } [ self . token_type ] , <EOL> 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>' ) or bit . startswith ( "<STR_LIT>" ) : <EOL> sentinal = bit [ <NUM_LIT:2> ] + '<STR_LIT:)>' <EOL> trans_bit = [ bit ] <EOL> while not bit . endswith ( sentinal ) : <EOL> bit = bits . next ( ) <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 , origin ) : <EOL> self . template_string = template_string <EOL> self . origin = origin <EOL> self . lineno = <NUM_LIT:1> <EOL> def tokenize ( self ) : <EOL> "<STR_LIT>" <EOL> in_tag = False <EOL> result = [ ] <EOL> for bit in tag_re . split ( self . template_string ) : <EOL> if bit : <EOL> result . append ( self . create_token ( bit , in_tag ) ) <EOL> in_tag = not in_tag <EOL> return result <EOL> def create_token ( self , token_string , in_tag ) : <EOL> """<STR_LIT>""" <EOL> if in_tag : <EOL> if token_string . startswith ( VARIABLE_TAG_START ) : <EOL> token = Token ( TOKEN_VAR , token_string [ len ( VARIABLE_TAG_START ) : - len ( VARIABLE_TAG_END ) ] . strip ( ) ) <EOL> elif token_string . startswith ( BLOCK_TAG_START ) : <EOL> token = Token ( TOKEN_BLOCK , token_string [ len ( BLOCK_TAG_START ) : - len ( BLOCK_TAG_END ) ] . strip ( ) ) <EOL> elif token_string . startswith ( COMMENT_TAG_START ) : <EOL> content = '<STR_LIT>' <EOL> if token_string . find ( TRANSLATOR_COMMENT_MARK ) : <EOL> content = token_string [ len ( COMMENT_TAG_START ) : - len ( COMMENT_TAG_END ) ] . strip ( ) <EOL> token = Token ( TOKEN_COMMENT , content ) <EOL> else : <EOL> token = Token ( TOKEN_TEXT , token_string ) <EOL> token . lineno = self . lineno <EOL> self . lineno += token_string . count ( '<STR_LIT:\n>' ) <EOL> return token <EOL> class Parser ( object ) : <EOL> def __init__ ( self , tokens ) : <EOL> self . tokens = tokens <EOL> self . tags = { } <EOL> self . filters = { } <EOL> for lib in builtins : <EOL> self . add_library ( lib ) <EOL> def parse ( self , parse_until = None ) : <EOL> if parse_until is None : parse_until = [ ] <EOL> nodelist = self . create_nodelist ( ) <EOL> while self . tokens : <EOL> token = self . next_token ( ) <EOL> if token . token_type == TOKEN_TEXT : <EOL> self . extend_nodelist ( nodelist , TextNode ( token . contents ) , token ) <EOL> elif token . token_type == TOKEN_VAR : <EOL> if not token . contents : <EOL> self . empty_variable ( token ) <EOL> filter_expression = self . compile_filter ( token . contents ) <EOL> var_node = self . create_variable_node ( filter_expression ) <EOL> self . extend_nodelist ( nodelist , var_node , token ) <EOL> elif token . token_type == TOKEN_BLOCK : <EOL> if token . contents in parse_until : <EOL> self . prepend_token ( token ) <EOL> return nodelist <EOL> try : <EOL> command = token . contents . split ( ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> self . empty_block_tag ( token ) <EOL> self . enter_command ( 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 TemplateSyntaxError , e : <EOL> if not self . compile_function_error ( token , e ) : <EOL> raise <EOL> self . extend_nodelist ( nodelist , compiled_result , token ) <EOL> self . exit_command ( ) <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 create_variable_node ( self , filter_expression ) : <EOL> return VariableNode ( filter_expression ) <EOL> def create_nodelist ( self ) : <EOL> return NodeList ( ) <EOL> def extend_nodelist ( self , nodelist , node , token ) : <EOL> if node . must_be_first and nodelist : <EOL> try : <EOL> if nodelist . contains_nontext : <EOL> raise AttributeError <EOL> except AttributeError : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % node ) <EOL> if isinstance ( nodelist , NodeList ) and not isinstance ( node , TextNode ) : <EOL> nodelist . contains_nontext = True <EOL> nodelist . append ( node ) <EOL> def enter_command ( self , command , token ) : <EOL> pass <EOL> def exit_command ( self ) : <EOL> pass <EOL> def error ( self , token , msg ) : <EOL> return TemplateSyntaxError ( msg ) <EOL> def empty_variable ( self , token ) : <EOL> raise self . error ( token , "<STR_LIT>" ) <EOL> def empty_block_tag ( self , token ) : <EOL> raise self . error ( token , "<STR_LIT>" ) <EOL> def invalid_block_tag ( self , token , command , parse_until = None ) : <EOL> if parse_until : <EOL> raise self . error ( token , "<STR_LIT>" % ( command , get_text_list ( [ "<STR_LIT>" % p for p in parse_until ] ) ) ) <EOL> raise self . error ( token , "<STR_LIT>" % command ) <EOL> def unclosed_block_tag ( self , parse_until ) : <EOL> raise self . error ( None , "<STR_LIT>" % '<STR_LIT:U+002CU+0020>' . join ( parse_until ) ) <EOL> def compile_function_error ( self , token , e ) : <EOL> pass <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> class TokenParser ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , subject ) : <EOL> self . subject = subject <EOL> self . pointer = <NUM_LIT:0> <EOL> self . backout = [ ] <EOL> self . tagname = self . tag ( ) <EOL> def top ( self ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( ) <EOL> def more ( self ) : <EOL> "<STR_LIT>" <EOL> return self . pointer < len ( self . subject ) <EOL> def back ( self ) : <EOL> "<STR_LIT>" <EOL> if not len ( self . backout ) : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" ) <EOL> self . pointer = self . backout . pop ( ) <EOL> def tag ( self ) : <EOL> "<STR_LIT>" <EOL> subject = self . subject <EOL> i = self . pointer <EOL> if i >= len ( subject ) : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % subject ) <EOL> p = i <EOL> while i < len ( subject ) and subject [ i ] not in ( '<STR_LIT:U+0020>' , '<STR_LIT:\t>' ) : <EOL> i += <NUM_LIT:1> <EOL> s = subject [ p : i ] <EOL> while i < len ( subject ) and subject [ i ] in ( '<STR_LIT:U+0020>' , '<STR_LIT:\t>' ) : <EOL> i += <NUM_LIT:1> <EOL> self . backout . append ( self . pointer ) <EOL> self . pointer = i <EOL> return s <EOL> def value ( self ) : <EOL> "<STR_LIT>" <EOL> subject = self . subject <EOL> i = self . pointer <EOL> def next_space_index ( subject , i ) : <EOL> "<STR_LIT>" <EOL> while i < len ( subject ) and subject [ i ] not in ( '<STR_LIT:U+0020>' , '<STR_LIT:\t>' ) : <EOL> if subject [ i ] in ( '<STR_LIT:">' , "<STR_LIT:'>" ) : <EOL> c = subject [ i ] <EOL> i += <NUM_LIT:1> <EOL> while i < len ( subject ) and subject [ i ] != c : <EOL> i += <NUM_LIT:1> <EOL> if i >= len ( subject ) : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % ( i , subject ) ) <EOL> i += <NUM_LIT:1> <EOL> return i <EOL> if i >= len ( subject ) : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % subject ) <EOL> if subject [ i ] in ( '<STR_LIT:">' , "<STR_LIT:'>" ) : <EOL> p = i <EOL> i += <NUM_LIT:1> <EOL> while i < len ( subject ) and subject [ i ] != subject [ p ] : <EOL> i += <NUM_LIT:1> <EOL> if i >= len ( subject ) : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % ( i , subject ) ) <EOL> i += <NUM_LIT:1> <EOL> i = next_space_index ( subject , i ) <EOL> res = subject [ p : i ] <EOL> while i < len ( subject ) and subject [ i ] in ( '<STR_LIT:U+0020>' , '<STR_LIT:\t>' ) : <EOL> i += <NUM_LIT:1> <EOL> self . backout . append ( self . pointer ) <EOL> self . pointer = i <EOL> return res <EOL> else : <EOL> p = i <EOL> i = next_space_index ( subject , i ) <EOL> s = subject [ p : i ] <EOL> while i < len ( subject ) and subject [ i ] in ( '<STR_LIT:U+0020>' , '<STR_LIT:\t>' ) : <EOL> i += <NUM_LIT:1> <EOL> self . backout . append ( self . pointer ) <EOL> self . pointer = i <EOL> return s <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> r"""<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>" % ( token [ : upto ] , token [ upto : start ] , 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>" % 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>" % ( 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> if settings . TEMPLATE_STRING_IF_INVALID : <EOL> global invalid_var_format_string <EOL> if invalid_var_format_string is None : <EOL> invalid_var_format_string = '<STR_LIT:%s>' in settings . TEMPLATE_STRING_IF_INVALID <EOL> if invalid_var_format_string : <EOL> return settings . TEMPLATE_STRING_IF_INVALID % self . var <EOL> return settings . TEMPLATE_STRING_IF_INVALID <EOL> else : <EOL> obj = settings . TEMPLATE_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> 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 ) <EOL> func = getattr ( func , '<STR_LIT>' , func ) <EOL> args , varargs , varkw , defaults = getargspec ( func ) <EOL> args . pop ( <NUM_LIT:0> ) <EOL> if defaults : <EOL> nondefs = args [ : - len ( defaults ) ] <EOL> else : <EOL> nondefs = args <EOL> try : <EOL> for arg in nondefs : <EOL> provided . pop ( <NUM_LIT:0> ) <EOL> except IndexError : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % ( name , len ( nondefs ) , plen ) ) <EOL> defaults = defaults and list ( defaults ) or [ ] <EOL> try : <EOL> for parg in provided : <EOL> defaults . pop ( <NUM_LIT:0> ) <EOL> except IndexError : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % ( name , len ( nondefs ) , plen ) ) <EOL> return True <EOL> args_check = staticmethod ( args_check ) <EOL> def __str__ ( self ) : <EOL> return self . token <EOL> def resolve_variable ( path , context ) : <EOL> """<STR_LIT>""" <EOL> return Variable ( path ) . resolve ( context ) <EOL> class Variable ( object ) : <EOL> r"""<STR_LIT>""" <EOL> def __init__ ( self , var ) : <EOL> self . var = var <EOL> self . literal = None <EOL> self . lookups = None <EOL> self . translate = False <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>" % 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> return ugettext_lazy ( value ) <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 ) : <EOL> try : <EOL> current = getattr ( current , bit ) <EOL> except ( TypeError , AttributeError ) : <EOL> try : <EOL> current = current [ int ( bit ) ] <EOL> except ( IndexError , <EOL> ValueError , <EOL> KeyError , <EOL> TypeError , <EOL> ) : <EOL> raise VariableDoesNotExist ( "<STR_LIT>" , ( bit , current ) ) <EOL> if callable ( current ) : <EOL> if getattr ( current , '<STR_LIT>' , False ) : <EOL> current = settings . TEMPLATE_STRING_IF_INVALID <EOL> else : <EOL> try : <EOL> current = current ( ) <EOL> except TypeError : <EOL> current = settings . TEMPLATE_STRING_IF_INVALID <EOL> except Exception , e : <EOL> if getattr ( e , '<STR_LIT>' , False ) : <EOL> current = settings . TEMPLATE_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> def render ( self , context ) : <EOL> "<STR_LIT>" <EOL> pass <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> bits . append ( self . render_node ( node , context ) ) <EOL> else : <EOL> bits . append ( node ) <EOL> return mark_safe ( '<STR_LIT>' . join ( [ force_unicode ( b ) for b in 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> def render_node ( self , node , context ) : <EOL> return node . render ( context ) <EOL> class TextNode ( Node ) : <EOL> def __init__ ( self , s ) : <EOL> self . s = s <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % smart_str ( self . s [ : <NUM_LIT> ] , '<STR_LIT:ascii>' , <EOL> 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 = localize ( value , use_l10n = context . use_l10n ) <EOL> value = force_unicode ( value ) <EOL> if ( context . autoescape and not isinstance ( value , SafeData ) ) or isinstance ( value , EscapeData ) : <EOL> return 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> def generic_tag_compiler ( params , defaults , name , node_class , parser , token ) : <EOL> "<STR_LIT>" <EOL> bits = token . split_contents ( ) [ <NUM_LIT:1> : ] <EOL> bmax = len ( params ) <EOL> def_len = defaults and len ( defaults ) or <NUM_LIT:0> <EOL> bmin = bmax - def_len <EOL> if ( len ( bits ) < bmin or len ( bits ) > bmax ) : <EOL> if bmin == bmax : <EOL> message = "<STR_LIT>" % ( name , bmin ) <EOL> else : <EOL> message = "<STR_LIT>" % ( name , bmin , bmax ) <EOL> raise TemplateSyntaxError ( message ) <EOL> return node_class ( bits ) <EOL> class Library ( object ) : <EOL> def __init__ ( self ) : <EOL> self . filters = { } <EOL> self . tags = { } <EOL> def tag ( self , name = None , compile_function = None ) : <EOL> if name == None and compile_function == None : <EOL> return self . tag_function <EOL> elif name != None and compile_function == None : <EOL> if ( callable ( name ) ) : <EOL> return self . tag_function ( name ) <EOL> else : <EOL> def dec ( func ) : <EOL> return self . tag ( name , func ) <EOL> return dec <EOL> elif name != None and compile_function != None : <EOL> self . tags [ name ] = compile_function <EOL> return compile_function <EOL> else : <EOL> raise InvalidTemplateLibrary ( "<STR_LIT>" , ( name , compile_function ) ) <EOL> def tag_function ( self , func ) : <EOL> self . tags [ getattr ( func , "<STR_LIT>" , func ) . __name__ ] = func <EOL> return func <EOL> def filter ( self , name = None , filter_func = None ) : <EOL> if name == None and filter_func == None : <EOL> return self . filter_function <EOL> elif filter_func == None : <EOL> if ( callable ( name ) ) : <EOL> return self . filter_function ( name ) <EOL> else : <EOL> def dec ( func ) : <EOL> return self . filter ( name , func ) <EOL> return dec <EOL> elif name != None and filter_func != None : <EOL> self . filters [ name ] = filter_func <EOL> return filter_func <EOL> else : <EOL> raise InvalidTemplateLibrary ( "<STR_LIT>" , ( name , filter_func ) ) <EOL> def filter_function ( self , func ) : <EOL> self . filters [ getattr ( func , "<STR_LIT>" , func ) . __name__ ] = func <EOL> return func <EOL> def simple_tag ( self , func = None , takes_context = None ) : <EOL> def dec ( func ) : <EOL> params , xx , xxx , defaults = getargspec ( func ) <EOL> if takes_context : <EOL> if params [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> params = params [ <NUM_LIT:1> : ] <EOL> else : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" ) <EOL> class SimpleNode ( Node ) : <EOL> def __init__ ( self , vars_to_resolve ) : <EOL> self . vars_to_resolve = map ( Variable , vars_to_resolve ) <EOL> def render ( self , context ) : <EOL> resolved_vars = [ var . resolve ( context ) for var in self . vars_to_resolve ] <EOL> if takes_context : <EOL> func_args = [ context ] + resolved_vars <EOL> else : <EOL> func_args = resolved_vars <EOL> return func ( * func_args ) <EOL> compile_func = curry ( generic_tag_compiler , params , defaults , getattr ( func , "<STR_LIT>" , func ) . __name__ , SimpleNode ) <EOL> compile_func . __doc__ = func . __doc__ <EOL> self . tag ( getattr ( func , "<STR_LIT>" , func ) . __name__ , compile_func ) <EOL> return func <EOL> if func is None : <EOL> return dec <EOL> elif callable ( func ) : <EOL> return dec ( func ) <EOL> else : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" ) <EOL> def inclusion_tag ( self , file_name , context_class = Context , takes_context = False ) : <EOL> def dec ( func ) : <EOL> params , xx , xxx , defaults = getargspec ( func ) <EOL> if takes_context : <EOL> if params [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> params = params [ <NUM_LIT:1> : ] <EOL> else : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" ) <EOL> class InclusionNode ( Node ) : <EOL> def __init__ ( self , vars_to_resolve ) : <EOL> self . vars_to_resolve = map ( Variable , vars_to_resolve ) <EOL> def render ( self , context ) : <EOL> resolved_vars = [ var . resolve ( context ) for var in self . vars_to_resolve ] <EOL> if takes_context : <EOL> args = [ context ] + resolved_vars <EOL> else : <EOL> args = resolved_vars <EOL> dict = func ( * args ) <EOL> if not getattr ( self , '<STR_LIT>' , False ) : <EOL> from django . template . loader import get_template , select_template <EOL> if not isinstance ( file_name , basestring ) and is_iterable ( file_name ) : <EOL> t = select_template ( file_name ) <EOL> else : <EOL> t = get_template ( file_name ) <EOL> self . nodelist = t . nodelist <EOL> new_context = context_class ( dict , autoescape = context . autoescape ) <EOL> csrf_token = context . get ( '<STR_LIT>' , None ) <EOL> if csrf_token is not None : <EOL> new_context [ '<STR_LIT>' ] = csrf_token <EOL> return self . nodelist . render ( new_context ) <EOL> compile_func = curry ( generic_tag_compiler , params , defaults , getattr ( func , "<STR_LIT>" , func ) . __name__ , InclusionNode ) <EOL> compile_func . __doc__ = func . __doc__ <EOL> self . tag ( getattr ( func , "<STR_LIT>" , func ) . __name__ , compile_func ) <EOL> return func <EOL> return dec <EOL> def import_library ( taglib_module ) : <EOL> """<STR_LIT>""" <EOL> app_path , taglib = taglib_module . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> app_module = import_module ( app_path ) <EOL> try : <EOL> mod = import_module ( taglib_module ) <EOL> except ImportError , e : <EOL> if not module_has_submodule ( app_module , taglib ) : <EOL> return None <EOL> else : <EOL> raise InvalidTemplateLibrary ( "<STR_LIT>" % ( taglib_module , e ) ) <EOL> try : <EOL> return mod . register <EOL> except AttributeError : <EOL> raise InvalidTemplateLibrary ( "<STR_LIT>" % taglib_module ) <EOL> templatetags_modules = [ ] <EOL> def get_templatetags_modules ( ) : <EOL> """<STR_LIT>""" <EOL> global templatetags_modules <EOL> if not templatetags_modules : <EOL> _templatetags_modules = [ ] <EOL> for app_module in [ '<STR_LIT>' ] + list ( settings . INSTALLED_APPS ) : <EOL> try : <EOL> templatetag_module = '<STR_LIT>' % app_module <EOL> import_module ( templatetag_module ) <EOL> _templatetags_modules . append ( templatetag_module ) <EOL> except ImportError : <EOL> continue <EOL> templatetags_modules = _templatetags_modules <EOL> return templatetags_modules <EOL> def get_library ( library_name ) : <EOL> """<STR_LIT>""" <EOL> lib = libraries . get ( library_name , None ) <EOL> if not lib : <EOL> templatetags_modules = get_templatetags_modules ( ) <EOL> tried_modules = [ ] <EOL> for module in templatetags_modules : <EOL> taglib_module = '<STR_LIT>' % ( module , library_name ) <EOL> tried_modules . append ( taglib_module ) <EOL> lib = import_library ( taglib_module ) <EOL> if lib : <EOL> libraries [ library_name ] = lib <EOL> break <EOL> if not lib : <EOL> raise InvalidTemplateLibrary ( "<STR_LIT>" % ( library_name , '<STR_LIT:U+002C>' . join ( tried_modules ) ) ) <EOL> return lib <EOL> def add_to_builtins ( module ) : <EOL> builtins . append ( import_library ( module ) ) <EOL> add_to_builtins ( '<STR_LIT>' ) <EOL> add_to_builtins ( '<STR_LIT>' ) </s>
