U  e?J@sbdZddlZddlmZddlmZddlmZddlmZGdddZGd d d eZGd d d eZ Gd ddeZ GdddeZ GdddeZ GdddeZ GdddeZGdddeedZGdddeZGdddeZGdddeZGd d!d!eZGd"d#d#eZGd$d%d%eZGd&d'd'eZGd(d)d)eZGd*d+d+eZdS),z5defines the parse tree components for Mako templates.N)ast) exceptions)filters)utilc@s4eZdZdZddZeddZddZdd Zd S) Nodez(base class for a Node in the parse tree.cCs||_||_||_||_dSNsourcelinenoposfilename)selfr r r r r.traversevisit)getattr __class____name__)r rrmethodrrrr's zNode.accept_visitorN) r __module__ __qualname____doc__rpropertyrrrrrrrrs  rcs0eZdZdZfddZddZddZZS) TemplateNodez?a 'container' node that stores the overall collection of nodes.cs"tddd|g|_i|_dS)Nr)superrnodespage_attributes)r r rrrr4szTemplateNode.__init__cCs|jSrr&rrrrr9szTemplateNode.get_childrencCsdt|j|jfS)NzTemplateNode(%s, %r))rsorted_dict_reprr'r&rrrr__repr__<s zTemplateNode.__repr__)rrr r!rrr+ __classcell__rrr(rr#0s r#csLeZdZdZdZfddZddZddZd d Zd d Z d dZ Z S) ControlLinezdefines a control line, a line-oriented python line or end tag. e.g.:: % if foo: (markup) % endif Fc shtjf|||_||_||_|dk|_g|_|jrDg|_g|_n t j |f|j }|j |_|j |_dS)N)forifwhiletrywith)r%rtextkeywordisendZ is_primaryr&_declared_identifiers_undeclared_identifiersrZPythonFragmentrdeclared_identifiersundeclared_identifiers)r r4r5r3kwargscoder(rrrQs zControlLine.__init__cCs|jSrr)rrrrr`szControlLine.get_childrencCs|jSr)r6rrrrr8csz ControlLine.declared_identifierscCs|jSr)r7rrrrr9fsz"ControlLine.undeclared_identifierscCs*ddhddhdhd}|||jtkS)zRreturn true if the given keyword is a ternary keyword for this ControlLineelseelifexceptfinally)r/r1r.)getr4set)r r4Zcasesrrr is_ternaryis zControlLine.is_ternarycCsd|j|j|j|j|jffS)NzControlLine(%r, %r, %r, %r))r4r3r5r r rrrrr+us  zControlLine.__repr__) rrr r!Zhas_loop_contextrrr8r9rBr+r,rrr(rr-Cs   r-cs(eZdZdZfddZddZZS)Textz#defines plain text in the template.c stjf|||_dSr)r%rcontent)r rDr:r(rrrsz Text.__init__cCsd|j|j|jffS)Nz Text(%r, %r))rDr r rrrrr+sz Text.__repr__rrr r!rr+r,rrr(rrC~s rCcs8eZdZdZfddZddZddZdd ZZS) Codezdefines a Python code block, either inline or module level. e.g.:: inline: <% x = 12 %> module level: <%! import logger %> c s0tjf|||_||_tj|f|j|_dSr)r%rr3ismoduler PythonCoderr;)r r3rGr:r(rrrsz Code.__init__cCs|jjSr)r;r8rrrrr8szCode.declared_identifierscCs|jjSr)r;r9rrrrr9szCode.undeclared_identifierscCsd|j|j|j|jffS)NzCode(%r, %r, %r))r3rGr r rrrrr+s  z Code.__repr__ rrr r!rr8r9r+r,rrr(rrFs  rFcs(eZdZdZfddZddZZS)Commentz6defines a comment line. # this is a comment c stjf|||_dSr)r%rr3)r r3r:r(rrrszComment.__init__cCsd|j|j|jffS)NzComment(%r, %r))r3r r rrrrr+szComment.__repr__rErrr(rrJs rJcs8eZdZdZfddZddZddZdd ZZS) Expressionz/defines an inline expression. ${x+y} c sBtjf|||_||_tj|f|j|_tj|f|j|_ dSr) r%rr3escapesr ArgumentListr escapes_coderHr;)r r3rLr:r(rrrs zExpression.__init__cCsgSrrrrrrr8szExpression.declared_identifierscCs$|jj|jjtj|jjSr)r;r9unionrN differencerDEFAULT_ESCAPESr8rrrrr9sz!Expression.undeclared_identifierscCsd|j|jj|j|jffS)NzExpression(%r, %r, %r))r3rNargsr r rrrrr+s  zExpression.__repr__rIrrr(rrKs  rKcs,eZdZdZiZfddZddZZS)_TagMetazImetaclass to allow Tag to produce a subclass according to its keywordcs0t|dddk r||j|j<t|||dS)N __keyword__)r _classmaprTr%r)clsclsnamebasesZdict_r(rrrs z_TagMeta.__init__c Ksd|kr*|d\}}tjt|||f|Sztj|}Wn<tk rttjd||d|d|d|ddYnXtj|||f|S)N:zNo such tag: '%s'r r r r r) splittype__call__CallNamespaceTagrSrUKeyErrorrCompileException)rVr4 attributesr:nsdefnamerrrr\s* z_TagMeta.__call__)rrr r!rUrr\r,rrr(rrSs rScsTeZdZdZdZfddZddZddZd d Zd d Z d dZ ddZ Z S)Tagzabstract base class for tags. e.g.:: <%sometag/> <%someothertag> stuff Nc sttjf||_|_||fdd|D}t|rdtjdddd|Dfj d_ g_ dS)aVconstruct a new Tag instance. this constructor not called directly, and is only called by subclasses. :param keyword: the tag keyword :param attributes: raw dictionary of attribute key/value pairs :param expressions: a set of identifiers that are legal attributes, which can also contain embedded expressions :param nonexpressions: a set of identifiers that are legal attributes, which cannot contain embedded expressions :param \**kwargs: other arguments passed to the Node superclass (lineno, pos) csg|]}|jkr|qSr)parsed_attributes).0rrrr ,s z Tag.__init__..zMissing attribute(s): %s,css|]}t|VqdSr)repr)remrrr 1szTag.__init__..N) r%rr4r`_parse_attributeslenrr_joinrparentr&)r r4r` expressionsnonexpressionsrequiredr:missingr(rrr s z Tag.__init__cCs |jdkSr)rorrrris_root9sz Tag.is_rootcCs|jSrr)rrrrr<szTag.get_childrenc Cs6t}i|_|jD]}||krg}tdtj|j|D]j}tdtj|}|rtj | d f|j }| |j}|d| dq>|r>|t|q>d|ptd|j|<q||krtd|j|rtjd||jff|j t|j||j|<qtjd |j|ff|j q||_dS) Nz (\${.+?})z ^\${(.+?)}$z(%s)z + r$z\${.+?}z>Attribute '%s' in tag '%s' does not allow embedded expressionsz$Invalid attribute for tag '%s': '%s')rArdr`recompileSrZmatchrrHgrouprstriprrOr9appendrirnsearchrr_r4!expression_undeclared_identifiers) r rprqr9keyexprxrjr;rrrrl?sP   zTag._parse_attributescCsgSrrrrrrr8hszTag.declared_identifierscCs|jSr)r~rrrrr9kszTag.undeclared_identifierscCs*d|jj|jt|j|j|jf|jfS)Nz%s(%r, %s, %r, %r)) rrr4rr*r`r r r&rrrrr+ns  z Tag.__repr__) rrr r!rTrrtrrlr8r9r+r,rrr(rrcs  -)rc) metaclasscs4eZdZdZfddZddZfddZZS) IncludeTagincludec s:tj||dddf|tjd|ddf|j|_dS)N)fileimportrRrrz __DUMMY(%s)rRr$)r%rrrHr@r page_argsr r4r`r:r(rrr{szIncludeTag.__init__cCsgSrrrrrrr8szIncludeTag.declared_identifierscs*|jjdh|jj}|tS)NZ__DUMMY)rr9rPr8rOr%)r Z identifiersr(rrr9s z!IncludeTag.undeclared_identifiersrrr rTrr8r9r,rrr(rrxs rcs(eZdZdZfddZddZZS) NamespaceTag namespacec svtj||dddf||ddttt||_d|krTd|krTtjd |j d|krrd |krrtjd |j dS) Nr)name inheritablermodulerrz __anon_%sr?'name' and/or 'import' attributes are required for <%namespace>rr4<%namespace> may only have one of 'file' or 'module')r)r) r%rr@hexabsidrrr_rrr(rrrs, zNamespaceTag.__init__cCsgSrrrrrrr8sz!NamespaceTag.declared_identifiersrrr rTrr8r,rrr(rrs rcs(eZdZdZfddZddZZS)TextTagr3c s6tj||dddf|tj|ddf|j|_dS)Nrfilterr$)r%rrrMr@r filter_argsrr(rrrs  zTextTag.__init__cCs|jjtj|jSrrr9rPrrQkeysrOr~rrrrr9s zTextTag.undeclared_identifiers)rrr rTrr9r,rrr(rrs rcsLeZdZdZfddZdZdZeddZddZ d d Z d d Z Z S) DefTagdefc sddgdd|D}tj|||ddf||d}td|rPtjd|jtjd |d f|j|_ |j j |_ | d d |_ tj| dd f|j|_dS)NbufferedcachedcSsg|]}|dr|qSZcache_ startswithrecrrrrgs z#DefTag.__init__..rr decoratorrr^[\w_]+$Missing parenthesis in %defzdef z:passrr$r)r)r%rrvryrr_rrZ FunctionDecl function_declfuncnamerr@rrMrr r4r`r:rprr(rrrs>     zDefTag.__init__FcCs|jjSr)rrrrrrrszDefTag.funcnamecKs|jjf|Sr)rget_argument_expressionsr kwrrrrszDefTag.get_argument_expressionscCs|jjSr)r allargnamesrrrrr8szDefTag.declared_identifierscCs\g}|jjD]}|ttj|f|jj7}q t||j j t j |j |jjSr)rdefaultslistrrHrr9rArOrrPrrQrr~r)r resrrrrr9s& zDefTag.undeclared_identifiers) rrr rTr is_anonymousis_blockr"rrr8r9r,rrr(rrs  rcsTeZdZdZfddZdZeddZeddZd d Z d d Z d dZ Z S)BlockTagblockc sdddgdd|D}tj|||ddf||d}|rXtd |sXtjd|j|sv|ddrvtjd|jtj |dd f|j|_ ||_ |d d |_ tj |dd f|j|_dS)NrrrRcSsg|]}|dr|qSrrrrrrrgs z%BlockTag.__init__..rrrr,%block may not specify an argument signature#Only named %blocks may specify argsr$rr)r)r)r%rr@rvryrr_rr FunctionArgs body_declrrrMrrr(rrrsJ   zBlockTag.__init__TcCs |jdkSrrrrrrr$szBlockTag.is_anonymouscCs|jpd|jfS)Nz __M_anon_%d)rr rrrrr(szBlockTag.funcnamecKs|jjf|Sr)rrrrrrr,sz!BlockTag.get_argument_expressionscCs|jjSrrrrrrrr8/szBlockTag.declared_identifierscCs|jjtj|jSrrrrrrr92s zBlockTag.undeclared_identifiers) rrr rTrrr"rrrr8r9r,rrr(rrs !  rcs0eZdZdZfddZddZddZZS)CallTagcallc sTtj||dddf||d|_tj|jf|j|_tj|ddf|j|_ dS)NrR)rrr$) r%r expressionrrHrr;rr@rrr(rrr=s   zCallTag.__init__cCs|jj|jjSrr;r8rOrrrrrrr8GszCallTag.declared_identifierscCs|jj|jjSrr;r9rPr8rrrrr9JszCallTag.undeclared_identifiersrrrr(rr:s rcs,eZdZfddZddZddZZS)r]c stj|d||t|dddf|d||ddd|jDf|_tj |jf|j |_ tj | dd f|j |_dS) NrY)rRrz %s.%s(%s)rhcss&|]\}}|dkrd||fVqdS)rRz%s=%sNr)rekvrrrrk^sz,CallNamespaceTag.__init__..rRr$)r%rtuplerrnrditemsrrrHrr;rr@r)r rrbr`r:r(rrrQs,     zCallNamespaceTag.__init__cCs|jj|jjSrrrrrrr8jsz%CallNamespaceTag.declared_identifierscCs|jj|jjSrrrrrrr9msz'CallNamespaceTag.undeclared_identifiers)rrr rr8r9r,rrr(rr]Ps r]cs eZdZdZfddZZS) InheritTaginheritc stj||dddf|dS)Nrr)r%rrr(rrrvszInheritTag.__init__)rrr rTrr,rrr(rrssrcs(eZdZdZfddZddZZS)PageTagpagec sjddddgdd|D}tj|||ddf|tj|ddf|j|_tj|ddf|j|_dS) NrrRZexpression_filterZ enable_loopcSsg|]}|dr|qSrrrrrrrgs z$PageTag.__init__..rr$) r%rrrr@rrrMr)r r4r`r:rpr(rrrs$   zPageTag.__init__cCs|jjSrrrrrrr8szPageTag.declared_identifiersrrrr(rr|s r)r!rvmakorrrrrr#r-rCrFrJrKr[rSrcrrrrrrr]rrrrrrs.    ; %!{?<#