o 3a @sndZddlZddlZddlZddlmZddlmZddlm Z ddl m Z m Z ddl mZddlmZmZdd lmZmZmZdd lmZdd lmZmZd d lmZdZdZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(dZ)ede e e e!e e"e e#e e$e e%fZ*e+dZ,GdddeZ-Gdd d e.Z/Gd!d"d"Z0Gd#d$d$Z1d%d&Z2Gd'd(d(Z3Gd)d*d*Z4Gd+d,d,e4Z5Gd-d.d.Z6d/d0d1e d2e d3d4Z7e78d5d6Z7d7e7d8d9e ee ed:Z9ee9ej:Z;Gd;d<d<Zd>Z=Gd?d@d@Z>GdAdBdBe?Z@GdCdDdDe>ZAdEdFZBGdGdHdHe>ZCedIZDdMdKdLZEdS)Na This is the Django template system. How it works: The Lexer.tokenize() method converts a template string (i.e., a string containing markup with custom template tags) to tokens, which can be either plain text (TokenType.TEXT), variables (TokenType.VAR), or block statements (TokenType.BLOCK). The Parser() class takes a list of tokens in its constructor, and its parse() method returns a compiled template -- which is, under the hood, a list of Node objects. Each Node is responsible for creating some sort of output -- e.g. simple text (TextNode), variable values in a given context (VariableNode), results of basic logic (IfNode), results of looping (ForNode), or anything else. The core Node types are TextNode, VariableNode, IfNode and ForNode, but plugin modules can define their own custom node types. Each Node has a render() method, which takes a Context and returns a string of the rendered node. For example, the render() method of a Variable Node returns the variable's value as a string. The render() method of a ForNode returns the rendered output of whatever was inside the loop, recursively. The Template class is a convenient wrapper that takes care of template compilation and rendering. Usage: The only thing you should ever use directly in this file is the Template class. Create a compiled template object with a template_string, then call render() with a context. In the compilation stage, the TemplateSyntaxError exception will be raised if the template doesn't have proper syntax. Sample code: >>> from django import template >>> s = '{% if test %}

{{ varvalue }}

{% endif %}' >>> t = template.Template(s) (t is now a compiled template, and its render() method can be called multiple times with multiple contexts) >>> c = template.Context({'test':True, 'varvalue': 'Hello'}) >>> t.render(c) '

Hello

' >>> c = template.Context({'test':False, 'varvalue': 'Hello'}) >>> t.render(c) '' N)Enum) BaseContext)localize)conditional_escapeescape)_lazy_re_compile)SafeData mark_safe) get_text_list smart_splitunescape_string_literal)template_localtime) gettext_lazy pgettext_lazy)TemplateSyntaxError|:.z{%z%}z{{z}}z{#z#} Translators{}zz(%s.*?%s|%s.*?%s|%s.*?%s)zdjango.templatec@seZdZdZdZdZdZdS) TokenTyperrN)__name__ __module__ __qualname__TEXTVARBLOCKCOMMENTr"r"6/usr/lib/python3/dist-packages/django/template/base.pyrcs rc@seZdZdddZddZdS)VariableDoesNotExistr"cCs||_||_dSNmsgparams)selfr'r(r"r"r#__init__l zVariableDoesNotExist.__init__cCs |j|jSr%r&r)r"r"r#__str__p zVariableDoesNotExist.__str__N)r")rrrr*r-r"r"r"r#r$js  r$c@s2eZdZd ddZddZddZedd ZdS) OriginNcCs||_||_||_dSr%)name template_nameloader)r)r0r1r2r"r"r#r*us zOrigin.__init__cC|jSr%)r0r,r"r"r#r-zzOrigin.__str__cCs"t|to|j|jko|j|jkSr%) isinstancer/r0r2)r)otherr"r"r#__eq__}s   z Origin.__eq__cCs |jrd|jj|jjjfSdS)Nz%s.%s)r2r __class__rr,r"r"r# loader_names zOrigin.loader_nameNN)rrrr*r-r7propertyr9r"r"r"r#r/ts  r/c@s>eZdZdddZddZddZdd Zd d Zd d ZdS)TemplateNcCsV|durddlm}|}|durtt}||_||_||_t||_| |_ dS)Nr)Engine) enginer= get_defaultr/UNKNOWN_SOURCEr0originstrsourcecompile_nodelistnodelist)r)template_stringrAr0r>r=r"r"r#r*s  zTemplate.__init__ccs|jD]}|EdHqdSr%rE)r)noder"r"r#__iter__s  zTemplate.__iter__cCs |j|Sr%)rErenderr)contextr"r"r#_renderr.zTemplate._renderc Cs|j|@|jdur2|||j|_||WdWdS1s,wYn ||WdSWddS1sIwYdS)z)Display stage -- can be called many timesN)render_context push_statetemplate bind_templater0r1rMrKr"r"r#rJs  "zTemplate.renderc Cs||jjr t|j}nt|j}|}t||jj|jj|j }z| WSt y=}z |jjr8| ||j |_d}~ww)z Parse and compile the template source into a nodelist. If debug is True and an exception occurs during parsing, the exception is annotated with contextual line information where it occurred in the template source. N)r>debug DebugLexerrCLexertokenizeParsertemplate_librariestemplate_builtinsrAparse Exceptionget_exception_infotokentemplate_debug)r)lexertokensparserer"r"r#rDs    zTemplate.compile_nodelistc Cs(|j\}}d}d}d}g}d} } } tt|jD]9\} } ||krC|| krC| }t|j||} t|j||} t|j|| } || t|j|| f| }qt|}td||}t||d|}z t |j d}Wn t t fy~d}Ynw||||| | | |||||j j||d S)a: Return a dictionary containing contextual line information of where the exception occurred in the template. The following information is provided: message The message of the exception raised. source_lines The lines before, after, and including the line the exception occurred on. line The line number the exception occurred on. before, during, after The line the exception occurred on split into three parts: 1. The content before the token that raised the error. 2. The token that raised the error. 3. The content after the token that raised the error. total The number of lines in source_lines. top The line number where source_lines starts. bottom The line number where source_lines ends. start The start position of the token in the template source. end The end position of the token in the template source. rrz!(Could not get exception message)) message source_linesbeforeduringaftertopbottomtotalliner0startend)position enumeratelinebreak_iterrCrappendlenmaxminrBargs IndexErrorUnicodeDecodeErrorrAr0)r) exceptionr\rmrn context_linesrluptorerfrgrhnumnextrkrirjrdr"r"r#r[sF %  zTemplate.get_exception_infoNNN) rrrr*rIrMrJrDr[r"r"r"r#r<s  r<ccsNdV|d}|dkr|dV|d|d}|dks t|dVdS)Nr r)findrs)template_sourcepr"r"r#rqs  rqc@s&eZdZdddZddZddZdS) TokenNcCs|||_|_||_||_dS)a7 A token representing a string from the template. token_type A TokenType, either .TEXT, .VAR, .BLOCK, or .COMMENT. contents The token source string. position An optional tuple containing the start and end index of the token in the template source. This is used for traceback information when debug is on. lineno The line number the token appears on in the template source. This is used for traceback information and gettext files. N) token_typecontentslinenoro)r)rrrorr"r"r#r* s zToken.__init__cCs*|jj}d||jddddfS)Nz<%s token: "%s...">rrc)rr0 capitalizerreplace)r) token_namer"r"r#r-7s z Token.__str__cCsrg}t|j}|D]-}|dr1|dd}|g}||s,t|}||||rd|}||q |S)N)z_("z_('r) )r r startswithendswithr}rrjoin)r)splitbitsbitsentinel trans_bitr"r"r#split_contents<s       zToken.split_contentsr:)rrrr*r-rr"r"r"r#rs  rc@$eZdZddZddZddZdS)rTcCs||_d|_dS)NF)rFverbatim)r)rFr"r"r#r*Mr+zLexer.__init__c CsPd}d}g}t|jD]}|r|||d||| }||d7}q |S)zG Return a list of tokens from a given template_string. FrNr)tag_rerrFrr create_tokencount)r)in_tagrresultrr"r"r#rUQszLexer.tokenizecCs|r|tr|dd}|jr||jkrd|_|rj|jsj|tr2ttj|dd||S|trL|dddvrDd||_ttj|||S|t rhd}| t r`|dd}ttj |||SdSttj |||S) z Convert the given token string into a new Token object and return it. If in_tag is True, we are processing something that matched a tag, otherwise it should be treated as a literal string. rFN )rz verbatim zend%src)rBLOCK_TAG_STARTstriprVARIABLE_TAG_STARTrrrr COMMENT_TAG_STARTrTRANSLATOR_COMMENT_MARKr!r)r) token_stringrorr block_contentcontentr"r"r#r_s$      zLexer.create_tokenN)rrrr*rUrr"r"r"r#rTLs rTc@seZdZddZdS)rSc Csd}g}d}t|jD]F}|\}}||kr4|j||}||j|||f|dd||d7}|j||}||j|||f|dd||d7}|}q |j|d}|rn||j|||t|f|dd|S)z Split a template string into tokens and annotates each token with its start and end position in the source. This is slower than the default lexer so only use it when debug is True. rrF)rrTN)rfinditerrFspanrrrrrs) r)rrr{matchrmrnrlast_bitr"r"r#rU~s" $zDebugLexer.tokenizeN)rrrrUr"r"r"r#rS}s rSc@szeZdZdddZdddZddZdd Zd d Zdd d ZddZ ddZ ddZ ddZ ddZ ddZddZdS)rVNcCs\tt||_i|_i|_g|_|duri}|durg}||_|D]}||q!||_dSr%) listreversedr_tagsfilters command_stack libraries add_libraryrA)r)r_rbuiltinsrAbuiltinr"r"r#r*s  zParser.__init__c Cs|durg}t}|jr|}|jjdkr!||t|j|n|jjdkrZ|js3||d|j z| |j}Wnt yM}z|||d}~wwt |}||||nq|jjdkrz |j d}Wntyy||d|j w||vr|||S|j||fz|j|}Wnty||||Ynwz|||} Wnty}z|||d}~ww||| ||j|js |r|||S)ax Iterate through the parser tokens and compiles each one into a node. If parse_until is provided, parsing will stop once one of the specified tokens has been reached. This is formatted as a list of tokens, e.g. ['elif', 'else', 'endif']. If no matching token is reached, raise an exception with the unclosed block tag details. NrrzEmpty variable tag on line %drzEmpty block tag on line %d)NodeListr_ next_tokenrvalueextend_nodelistTextNodererrorrcompile_filterr VariableNoderrw prepend_tokenrrrrKeyErrorinvalid_block_tagrZpopunclosed_block_tag) r) parse_untilrEr\filter_expressionravar_nodecommand compile_funccompiled_resultr"r"r#rYsZ          , z Parser.parsecCs>|jr|}|jtjkr|j|krdS|js||gdSr%)r_rrrr rr)r)endtagr\r"r"r# skip_pasts zParser.skip_pastcCsR|jr|jr||d|t|trt|tsd|_||_|j|_||dS)Nz)%r must be the first tag in the template.T) must_be_firstcontains_nontextrr5rrr\rArr)r)rErHr\r"r"r#rs zParser.extend_nodelistcCs&t|ts t|}t|ds||_|S)a1 Return an exception annotated with the originating token. Since the parser can be called recursively, check if a token is already set. This ensures the innermost token is highlighted if an exception occurs, e.g. a compile error within the body of an if statement. r\)r5rZrhasattrr\)r)r\rar"r"r#rs  z Parser.errorc CsB|r||d|j|tdd|Ddf||d|j|f)Nz]Invalid block tag on line %d: '%s', expected %s. Did you forget to register or load this tag?cSsg|]}d|qS)z'%s'r").0rr"r"r# sz,Parser.invalid_block_tag..orzPInvalid block tag on line %d: '%s'. Did you forget to register or load this tag?)rrr )r)r\rrr"r"r#rs  zParser.invalid_block_tagcCs0|j\}}d|j|d|f}|||)Nz6Unclosed tag on line %d: '%s'. Looking for one of: %s.z, )rrrrr)r)rrr\r'r"r"r#rs zParser.unclosed_block_tagcCs |jSr%)r_rr,r"r"r#r" zParser.next_tokencCs|j|dSr%)r_rrr)r\r"r"r#r%szParser.prepend_tokencCs |jd=dS)N)r_r,r"r"r#delete_first_token(r.zParser.delete_first_tokencCs |j|j|j|jdSr%)rupdater)r)libr"r"r#r+szParser.add_librarycCs t||S)z9 Convenient wrapper for FilterExpression )FilterExpressionrr"r"r#r/s zParser.compile_filtercCs ||jvr |j|Std|)NzInvalid filter: '%s')rr)r) filter_namer"r"r# find_filter5s   zParser.find_filterr~r%)rrrr*rYrrrrrrrrrrrr"r"r"r#rVs  <   rVzf (?:%(i18n_open)s%(strdq)s%(i18n_close)s| %(i18n_open)s%(strsq)s%(i18n_close)s| %(strdq)s| %(strsq)s) z"[^"\\]*(?:\\.[^"\\]*)*"z'[^'\\]*(?:\\.[^'\\]*)*'_(r)strdqstrsq i18n_open i18n_closerrca ^(?P%(constant)s)| ^(?P[%(var_chars)s]+|%(num)s)| (?:\s*%(filter_sep)s\s* (?P\w+) (?:%(arg_sep)s (?: (?P%(constant)s)| (?P[%(var_chars)s]+|%(num)s) ) )? )z[-+\.]?\d[\d\.e]*z\w\.)constantr| var_chars filter_separg_sepc@s:eZdZdZddZd ddZddZeeZd d Zd S) ra Parse a variable token and its optional filters (all as a single string), and return a list of tuples of the filter name and arguments. Sample:: >>> token = 'variable|default:"Default value"|date:"Y-m-d"' >>> p = Parser('') >>> fe = FilterExpression(token, p) >>> len(fe.filters) 2 >>> fe.var c Csz||_t|}d}g}d}|D]}|}||kr/td|d||||||df|durb|d|d} } | rSz t| i}WnVtyRd}YnLw| dur]td|t| }n<|d} g} |d|d} }| r| d t| ifn |r| d t|f| | }| | || ||| f| }q|t |krtd ||d|f||_ ||_dS) Nrz)Could not parse some characters: %s|%s|%svarrz'Could not find variable at start of %s.r constant_argvar_argFTz-Could not parse the remainder: '%s' from '%s')r\ filter_rerrmrVariableresolver$rrr args_checkrnrsrr)r)r\r`matchesvar_objrr{rrmrrrrvrr filter_funcr"r"r#r*psV        zFilterExpression.__init__Fc Cs,t|jtr5z|j|}Wn)ty4|rd}n|jjj}|r0d|vr,||jYS|YS|}Ynw|j}|jD]X\}}g}|D]\}} |sQ| t | qC| | |qCt |ddrft ||j }t |ddry||g|Rd|ji} n||g|R} t |ddrt|trt | }q;| }q;|S)Nz%sexpects_localtimeFneeds_autoescape autoescapeis_safe)r5rrrr$rPr>string_if_invalidrrrr getattrr use_tzrr) r)rLignore_failuresobjrfuncrvarg_valslookupargnew_objr"r"r#rs<         zFilterExpression.resolvec Csxt|}t|d}t|}t|\}}}}}}}t|}t|p#g}|||ks/||kr:td||||fdS)Nrz%%s requires %d arguments, %d providedT)rrsinspectunwrapgetfullargspecr) r0rprovidedplenrv_defaultsalendlenr"r"r#rs    zFilterExpression.args_checkcCr3r%)r\r,r"r"r#r-r4zFilterExpression.__str__NF) rrr__doc__r*rr staticmethodr-r"r"r"r#rbs ,% rc@s8eZdZdZddZddZddZdd Zd d Zd S) ra' A template variable, resolvable against a given context. The variable may be a hard-coded string (if it begins and ends with single or double quote marks):: >>> c = {'article': {'section':'News'}} >>> Variable('article.section').resolve(c) 'News' >>> Variable('article').resolve(c) {'section': 'News'} >>> class AClass: pass >>> c = AClass() >>> c.article = AClass() >>> c.article.section = 'News' (The example assumes VARIABLE_ATTRIBUTE_SEPARATOR is '.') c Cs||_d|_d|_d|_d|_t|tstdt|z!d|vs'd| vr6t ||_| dr3t WdSt ||_WdSt y|drW| drWd|_|dd }z tt||_WYdSt y|td d ksx|d d kr~td |t|t|_YYdSww) NFz+Variable must be a string or number, got %srrarrTrrrrz=Variables and attributes may not begin with underscores: '%s')rliterallookups translatemessage_contextr5rB TypeErrortypelowerfloatr ValueErrorintrr r rVARIABLE_ATTRIBUTE_SEPARATORrtupler)r)rr"r"r#r*s<       zVariable.__init__cCsf|jdur ||}n|j}|jr1t|t}|dd}|r"t|n|}|jr-t |j|St |S|S)z.Resolve this variable against a given context.N%z%%) r_resolve_lookuprrr5rrr rrr)r)rLrrmsgidr"r"r#rs     zVariable.resolvecCsd|jj|jfS)N<%s: %r>)r8rrr,r"r"r#__repr__*szVariable.__repr__cCr3r%)rr,r"r"r#r--r4zVariable.__str__cCs|}z|jD]}z||}WnVtttttfydzt|tr)tt ||r)tt||}Wn2ttfyat|tsD|t |vrDz|t |}Wnttttfy^t d||fwYnwYnwt |rt|ddrpqt|ddr||jjj}qz|}Wqtyt|}z|Wty|jjj}YnwYqwqW|Sty}z%t|ddpd}tjd||d d t|d dr|jjj}nWYd}~|Sd}~ww) a Perform resolution of a real variable (i.e. not a literal) against the given context. As indicated by the method's name, this method is an implementation detail and shouldn't be called by external code. Use Variable.resolve() instead. z Failed lookup for key [%s] in %rdo_not_call_in_templatesF alters_datar1Nunknownz9Exception while resolving variable '%s' in template '%s'.T)exc_infosilent_variable_failure)rr AttributeErrorrr rwr5rrr dirrr$callablerPr>rr signaturebindrZloggerrR)r)rLcurrentrrrar1r"r"r#r0s~            6   zVariable._resolve_lookupN) rrrrr*rrr-rr"r"r"r#rs. rc@s8eZdZdZdZdZddZddZdd Zd d Z dS) NodeFrGNcCsdS)z7 Return the node rendered as a string. Nr"rKr"r"r#rJ{sz Node.renderc CsPz||WSty'}z|jjjr"t|ds"|jj||j|_ d}~ww)a' Render the node. If debug is True and an exception occurs during rendering, the exception is annotated with contextual line information where it occurred in the template. For internal usage this method is preferred over using the render method directly. r]N) rJrZrPr>rRrrNr[r\r])r)rLrar"r"r#render_annotateds zNode.render_annotatedccs |VdSr%r"r,r"r"r#rIs z Node.__iter__cCsHg}t||r |||jD]}t||d}|r!|||q|S)zj Return a list of all nodes (within this node and its nodelist) of the given type N)r5rrchild_nodelistsrextendget_nodes_by_type)r)nodetypenodesattrrEr"r"r#r&s    zNode.get_nodes_by_type) rrrrr$r\rJr#rIr&r"r"r"r#r"ts r"c@s eZdZdZddZddZdS)rFcCsDg}|D]}t|tr||}n|}|t|qtd|SNrc)r5r"r#rrrBr r)r)rLrrHrr"r"r#rJs  zNodeList.rendercCs"g}|D] }|||q|S)z,Return a list of all nodes of the given type)r%r&)r)r'r(rHr"r"r#r&szNodeList.get_nodes_by_typeN)rrrrrJr&r"r"r"r#rs rc@r)rcC ||_dSr%s)r)r-r"r"r#r*rzTextNode.__init__cCsd|jj|jddfS)Nr)r8rr-r,r"r"r#rszTextNode.__repr__cCr3r%r,rKr"r"r#rJr4zTextNode.renderNrrrr*rrJr"r"r"r#r rcCsHt||jd}t||jd}|jr tt|tst|}t|St|S)z Convert any value to a string to become part of a rendered template. This means escaping, if required, and conversion to a string. If value is a string, it's expected to already be translated. )r)use_l10n) r rrr1r issubclassr rBr)rrLr"r"r#render_value_in_contextsr3c@r)rcCr+r%r)r)rr"r"r#r*rzVariableNode.__init__cCs d|jS)Nzr4r,r"r"r#rrzVariableNode.__repr__cCs0z|j|}Wn tyYdSwt||Sr*)rrrxr3)r)rLoutputr"r"r#rJs   zVariableNode.renderNr/r"r"r"r#rr0rz(?:(\w+)=)?(.+)FcCs|siSt|d}|o|d}|s%|siSt|dks#|ddkr%iSi}|r||rFt|d}|r8|ds:|S|\}}|dd=nt|dksR|ddkrT|S|d|d}}|dd=||||<|rz|sz|ddkru|S|dd=|s)|S)aX Parse token keyword arguments and return a dictionary of the arguments retrieved from the ``bits`` token list. `bits` is a list containing the remainder of the token (split by spaces) that is to be checked for arguments. Valid arguments are removed from this list. `support_legacy` - if True, the legacy format ``1 as foo`` is accepted. Otherwise, only the standard ``foo=1`` format is allowed. There is no requirement for all remaining token ``bits`` to be keyword arguments, so return the dictionary as soon as an invalid argument format is reached. rrrasNrand)kwarg_rerrsgroupsr)rr`support_legacyr kwarg_formatkwargskeyrr"r"r# token_kwargss8       r>r)Frrloggingreenumrdjango.template.contextrdjango.utils.formatsrdjango.utils.htmlrrdjango.utils.regex_helperrdjango.utils.safestringrr django.utils.textr r r django.utils.timezoner django.utils.translationrr exceptionsrFILTER_SEPARATORFILTER_ARGUMENT_SEPARATORrr BLOCK_TAG_ENDrVARIABLE_TAG_ENDrCOMMENT_TAG_ENDrSINGLE_BRACE_STARTSINGLE_BRACE_ENDr@r getLoggerr rrZr$r/r<rqrrTrSrVconstant_stringrfilter_raw_stringVERBOSErrrr"rrrr3rr8r>r"r"r"r#s4          -1)   u-