>Mc@sdZddlZddlZddlZddlmZddlZddlZddl m Z ddl m Z ddl mZmZmZmZmZddd d d d d gZejdZejdejZdefdYZdefdYZdefdYZdZdefdYZedZ edZ!d e"fdYZ#d efdYZ$e%dZ&dZ'dZ(d efdYZ)dZ*d efd!YZ+d"efd#YZ,d$efd%YZ-d&efd'YZ.e.Z/[.ee%d(ed)Z0ejd*Z1d+d,d-d.d/d0gZ2ejd1Z3ejd2Z4d3Z5d4Z6ed(ed5Z7fd6Z8d7Z9d8Z:d9Z;d:Z<d;Z=d<Z>d=Z?d>Z@d?ZAed@ZBeCdAkreBndS(Bs A small templating language This implements a small templating language. This language implements if/elif/else, for/continue/break, expressions, and blocks of Python code. The syntax is:: {{any expression (function calls etc)}} {{any expression | filter}} {{for x in y}}...{{endfor}} {{if x}}x{{elif y}}y{{else}}z{{endif}} {{py:x=1}} {{py: def foo(bar): return 'baz' }} {{default var = default_value}} {{# comment}} You use this with the ``Template`` class or the ``sub`` shortcut. The ``Template`` class takes the template string and the name of the template (for errors) and a default namespace. Then (like ``string.Template``) you can call the ``tmpl.substitute(**kw)`` method to make a substitution (or ``tmpl.substitute(a_dict)``). ``sub(content, **kw)`` substitutes the template immediately. You can use ``__name='tmpl.html'`` to set the name of the template. If there are syntax errors ``TemplateError`` will be raised. iN(tquote(tStringIO(tlooper(tbytest basestring_tnextt is_unicodet coerce_textt TemplateErrortTemplatetsubt HTMLTemplatetsub_htmlthtmltbunchs\s+in\s+s^[a-z_][a-z0-9_]*$cBs#eZdZddZdZRS(s.Exception raised while parsing a template cCs&tj||||_||_dS(N(t Exceptiont__init__tpositiontname(tselftmessageRR((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyR5s cCs`dj|j}|jr?d||jd|jdf}n|jr\|d|j7}n|S(Nt s%s at line %s column %siis in %s(tjointargsRR(Rtmsg((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyt__str__:s ! N(t__name__t __module__t__doc__tNoneRR(((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyR1s t_TemplateContinuecBseZRS((RR(((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRDst_TemplateBreakcBseZRS((RR(((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRHscCsFtjjtjj|j|}|jj|d|jd|jS(Nt namespacet get_template( tostpathRtdirnameRt __class__t from_filenameR R!(Rt from_templateR#((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pytget_file_templateLs$ cBseZidd6dd6ed6ZdZdZddddddddZdddedZ e e Z d Z d Z d Z d Zd ZdZdZdZdZdZdZdZRS(s{{t start_bracess}}t end_bracesRtutf8ic Cs||_|dkr2|jd|jdf}n7|jjj|_|d|jd<|d|jd<||_t||_|dkrO|dk rOytj |} Wnt k rqOX| j } | j } d| kr| d}|j ds|j dr5|d }q5nd | kr/| d }nd }| rO|d | 7}qOn||_t|d |d |d|j|_|dkri}n||_||_|dk r||_ndS(NR)R*iit__file__s.pycs.pyoiRss:%sRt line_offsett delimeters(tcontentRtdefault_namespaceR%tcopyR.Rt_unicodetsyst _getframet ValueErrort f_globalstf_linenotendswithRtparset_parsedR R!tdefault_inherit( RR/RR t stacklevelR!R;R-R.tcallertglobalstlineno((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyR^sB            $     c Csbt|d}|j}|j|r=|j|}n|d|d|d|d|d|S(NtrbR/RR R;R!(topentreadtclosetdecode(tclstfilenameR tencodingR;R!tftc((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyR&s  cCs*d|jjtt|d|jfS(Ns<%s %s name=%r>i(R%RthextidR(R((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyt__repr__s cOs|rv|rtdnt|dkr<tdnt|ddsitd|dfn|d}n|}|j|d<|jr|j|jn|j|\}}}|s|j}n|r|j||||}n|S(Ns3You can only give positional *or* keyword argumentsis)You can only give one positional argumentititemssqIf you pass in a single argument, you must pass in a dictionary-like object (with a .items() method); you gave %rt__template_name__( t TypeErrortlenthasattrRR tupdatet _interpretR;t_interpret_inherit(RRtkwtnstresulttdefstinherit((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyt substitutes,      cCskt}g}i}|j|j|d|d|d|krO|jd}nd}dj|||fS(NtoutRXt __inherit__t(tTruet_interpret_codesR:tpopRR(RRVt__traceback_hide__tpartsRXRY((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRSs c Cst}|js-tdddd|jn|j||}t|j}x*|jD]\}} t||| q[W||_|j }||d<|j |S(Ns:You cannot use inheritance without passing in get_templateRRR( R^R!RRRtTemplateObjectt iteritemstsetattrtbodyR1RZ( RRfRXtinherit_templateRVRattempltself_Rtvalue((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRTs    cCsPt}xC|D];}t|tr2|j|q |j||||q WdS(N(R^t isinstanceRtappendt_interpret_code(RtcodesRVR[RXRatitem((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyR_s  c Cst}|d|d}}|dkrA|j|d||n@|dkrYtn(|dkrqtn|dkr|d|d|d }} } |j| ||} |j|| | |||n|d kr|d} |j| |||n|d kr|djd } |j| d||} x3| dD]'} |j| ||}|| } qCW|j|j | |n|d kr|d|d}} ||kr|j| ||}|||Rs%s=%r(RdtsortR%RR(RtktvRM((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRLs +  (RRRRRRRL(((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRs     cBs,eZdZdZdZdZRS(cCs ||_dS(N(Rj(RRj((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRscCs|jS(N(Rj(R((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRscCs|jS(N(Rj(R((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyt__html__scCsd|jj|jfS(Ns<%s %r>(R%RRj(R((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRLs(RRRRRRL(((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyR s   cCs| r t|dr |jS|dkr0dSt|tsNt|}ntjdkrt|trt j |j dd}|j d}nt j |d}tjdkrt |r|j dd}qn|S(NRR]t3tlatin1itasciitxmlcharrefreplace(RQRRRkRRR3tversionRtcgitescapeRDRR(Rjtforce((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyt html_quotes   cCs4t|}t|r*|jd}nt|S(NR+(RRRt url_quote(R((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyturls  cKst|j}|jg}xe|D]]\}}|dkrGq)n|jdrc|d }n|jdt|t|fq)Wtdj|S(Nt_is%s="%s"R( tlistRdRRR8RlRR R(RURbRRj((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pytattrs   'c BsHeZejjZejededede de dZ RS(R RRRcCsWt|dr$|j}t}nt}tj|||}|rOt|S|SdS(NR(RQRtFalseR^R RR(RRjRyRtplain((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRs   ( RRR R0R1RRRR RRRR(((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyR s  cKs.|jd}t|d|}|j|S(NRR(RR RZ(R/RURR((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyR sRcBsDeZddZdZdZdZddZdZRS(cCsC||_||_||_||_||_||_||_dS(N(t _templatet _func_namet_func_signaturet_bodyt_nst_post _bound_self(Rttemplatet func_nametfunc_signatureRfRVRyt bound_self((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRs      cCs#d|j|j|jj|jfS(Ns"(RRRRR(R((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRLs cCs|S(N((R((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyR scOs|j||}|jj}|j||jdk rM|j|d8ssMissing argument: %s( RRdRORR`ttupleRRR|RR( RRRURtsig_argstvar_argstvar_kwtdefaultstextra_kwRRjt value_expr((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRsF     !      N( RRRRRLRRRR(((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRs    RccBseZdZdZRS(cCs||_t||_dS(N(t_TemplateObject__nametTemplateObjectGetterR(RR((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRHs cCsd|jj|jfS(Ns<%s %s>(R%RR(R((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRLLs(RRRRL(((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRcFs RcBs#eZdZdZdZRS(cCs ||_dS(N(t#_TemplateObjectGetter__template_obj(Rt template_obj((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRRscCst|j|tS(N(RRtEmpty(RR((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRUscCsd|jj|jfS(Ns<%s around %r>(R%RR(R((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRLXs(RRRRRL(((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRPs  t_EmptycBsVeZdZdZdZdZdZdZej dkrTeZ nRS(cOs|S(N((RRRU((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyR]scCsdS(NR]((R((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyR`scCsdS(NR((R((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRLcscCsdS(Nu((R((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyt __unicode__fscCs tdS(N((titer(R((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyt__iter__iscCstS(N(R(R((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyt__bool__lsR( RRRRRLRRRR3Rt __nonzero__(((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyR\s      ic Cs|d kr)tjdtjdf}nt}g}d}d }tjdtj|dtj|df} x+| j|D]} | jd} t || j |} | |dkr|rt d|dd| d|n:| |dkr$| r$t d |dd| d|n| |dkrf||| j !} | r]|j | nt}n&|j ||| j !|ft}| j }| }qW|rt d |dd|d|n||} | r|j | n|rt|}n|S( sY Lex a string into chunks: >>> lex('hey') ['hey'] >>> lex('hey {{you}}') ['hey ', ('you', (1, 7))] >>> lex('hey {{') Traceback (most recent call last): ... TemplateError: No }} to finish last expression at line 1 column 7 >>> lex('hey }}') Traceback (most recent call last): ... TemplateError: }} outside expression at line 1 column 7 >>> lex('hey {{ {{') Traceback (most recent call last): ... TemplateError: {{ inside expression at line 1 column 10 R)R*iis%s|%ss%s inside expressionRRs%s outside expressionsNo %s to finish last expressionN(ii(RR R0RtretcompileRtfinditertgroupt find_positionRRRRlR^ttrim_lex(tsRttrim_whitespaceR-R.tin_exprtchunkstlasttlast_posttoken_retmatchRuRyR((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pytlexzsL         s.^(?:if |elif |for |def |inherit |default |py:)RtendiftendfortenddefRqRrs \n\r?[\t ]*$s ^[\t ]*\nc Csrd}xett|D]Q}||}t||trBqn|d}tj| rn|tkrnqn|s}d}n||d}|dt|krd}n||d}t|t st|t rqn| ptj|}|dkr|j rt }n|dk rH|d|krH|j rHd}n|r| st j|s|t|dkr|j r|r|dkr|j s|dkrd||d>> tokens = lex('{{if x}}\nx\n{{endif}}\ny', trim_whitespace=False) >>> tokens [('if x', (1, 3)), '\nx\n', ('endif', (3, 3)), '\ny'] >>> trim_lex(tokens) [('if x', (1, 3)), 'x\n', ('endif', (3, 3)), 'y'] iR]iiRN(RtrangeRPRkRt statement_retsearchtsingle_statementsttrail_whitespace_retstripR^tlead_whitespace_reRR( ttokenst last_trimtitcurrentRotprevt next_chunktprev_oktm((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRsP      ) # #cCs2|| j}t||t|ddfS(s/Given a string and index, return (line, column)ii(t splitlinesRP(tstringtindexR-tleading((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRscCs|dkr)tjdtjdf}nt|d|d|d|}g}x,|r{t||\}}|j|qPW|S(s Parses a string into a kind of AST >>> parse('{{x}}') [('expr', (1, 3), 'x')] >>> parse('foo') ['foo'] >>> parse('{{if x}}test{{endif}}') [('cond', (1, 3), ('if', (1, 3), 'x', ['test']))] >>> parse('series->{{for x in y}}x={{x}}{{endfor}}') ['series->', ('for', (1, 11), ('x',), 'y', ['x=', ('expr', (1, 27), 'x')])] >>> parse('{{for x, y in z:}}{{continue}}{{endfor}}') [('for', (1, 3), ('x', 'y'), 'z', [('continue', (1, 21))])] >>> parse('{{py:x=1}}') [('py', (1, 3), 'x=1')] >>> parse('{{if x}}a{{elif y}}b{{else}}c{{endif}}') [('cond', (1, 3), ('if', (1, 3), 'x', ['a']), ('elif', (1, 12), 'y', ['b']), ('else', (1, 23), None, ['c']))] Some exceptions:: >>> parse('{{continue}}') Traceback (most recent call last): ... TemplateError: continue outside of for loop at line 1 column 3 >>> parse('{{if x}}foo') Traceback (most recent call last): ... TemplateError: No {{endif}} at line 1 column 3 >>> parse('{{else}}') Traceback (most recent call last): ... TemplateError: else outside of an if block at line 1 column 3 >>> parse('{{if x}}{{for x in y}}{{endif}}{{endfor}}') Traceback (most recent call last): ... TemplateError: Unexpected endif at line 1 column 25 >>> parse('{{if}}{{endif}}') Traceback (most recent call last): ... TemplateError: if with no expression at line 1 column 3 >>> parse('{{for x y}}{{endfor}}') Traceback (most recent call last): ... TemplateError: Bad for (no "in") in 'x y' at line 1 column 3 >>> parse('{{py:x=1\ny=2}}') Traceback (most recent call last): ... TemplateError: Multi-line py blocks must start with a newline at line 1 column 3 R)R*RR-R.N(RR R0Rt parse_exprRl(RRR-R.RRWR((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyR9s2   cCst|dtr%|d|dfS|d\}}|j}|jdr|djd}|jds|jdr|jd}d|kr|jdd}|jdd }n|d7}n'd|krtd d |d |nd ||f|dfS|d$krUd|krAtdd |d |n||f|dfS|jdrtt|||S|jds|dkrtd|jdd |d |n|d%krtd|d |d |n|d&krtd|d |d |n|jdr-t |||S|jdrLt |||S|jdrkt |||S|jd rt |||S|jd!rd"||ddf|dfSd#||ddf|dfS('Niispy:is s s s R]s.Multi-line py blocks must start with a newlineRRRpRqRrRsscontinue outside of for loopsif selif Rs%s outside of an if blocktiftelifs%s with no expressionRRRs Unexpected %ssfor sdefault sinherit sdef t#RzRu(scontinuesbreak(RRsfor(RRR( RkRRt startswithtlstriptreplaceRt parse_condRt parse_fort parse_defaultt parse_inheritt parse_def(RRtcontextRuRy((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyR<sb           cCs|dd}g}|d }x|sBtdd|d|nt|dtr|dddkrd|ft||dfSt|||\}}|j|q!dS( NiiRsMissing {{endif}}RRRRt(sif(RRkRtparse_one_condRl(RRRRtpiecesR((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyR qs cCsf|d|d\}}}g}|jdr=|d }n|jdrkd||dj|f}nO|jdrd ||d j|f}n!|d krd |d|f}nx|std d |d|nt|dtr:|dddks0|ddjds0|ddd kr:||fSt|||\}}|j|qdS(Niit:isif Riselif RiRs No {{endif}}RRR( R8R R RRRkRRRl(RRRtfirstRyR/RR((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRs,   c Cs|d\}}|d}d|}g}|jdrF|d }n|dj}tj|}|std|d|d |n||j }d |krtd |d|d |ntg||j jd D]}|jr|j^q}||j} x|s=td d|d |nt |dtr|dddkrd||| |f|dfSt |||\} }|j | qdS(NiiRsRiisBad for (no "in") in %rRRt(s=You cannot have () in the variable section of a for loop (%r)t,s No {{endfor}}R(sfor( R8Rtin_reRRRRRRRkRRl( RRRRRyR/RRRRuR((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRs>      !cCs|d\}}|jddd}|jdd}t|dkritd|d|d|n|dj}d|krtdd|d|ntj|std |d|d|n|dj}d |||f|dfS( Niit=s:Expression must be {{default var=value}}; no = found in %rRRRs'{{default x, y = ...}} is not supporteds-Not a valid variable name for {{default}}: %rRw(RRRPRRtvar_reR(RRRRRyRbRRu((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRs$  cCs=|d\}}|jddd}d||f|dfS(NiiRY(RR(RRRRRyRu((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRsc Csy|d\}}|d}|jddd}|jdrL|d }nd|krs|}d ddif}nb|jdstd|d|d |n4|d }|jdd\}}t|||}|d}g}x|s td d|d |nt|dtrM|ddd krMd ||||f|dfSt|||\} }|j| qdS(NiiRiRt)s*Function definition doesn't end with ): %sRRRxsMissing {{enddef}}R((sdef( RRR8Rtparse_signatureRkRRRl( RRRRRRtsigtsig_textR/R((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRs4      cstjt|jg}d}d}i}tfd}x/d}|\} } | tjkrmPn| tjkr| dks| dkr| }|\} } n| tjkrt d|d|d|n| } |\} } | tjks| tjkri| dkri|dkr.| }n"|dkrC| }n |j | | tjkrEPqEqEn|dk rt d|d|d|n| tjkrE| dkrEd} d} d }d}}g}x|t \} } }}|dkr|}n|}| tjkr;|r;t d|d|d|n| r| tjksl| tjkr| dkrt |||}||| <|j | Pn|j | | f|r| tjkr| | kr|d 7}q|r| tjkr| | kr|d 8}|skd} } qkq| r| tjkr| dkr| } d }idd 6dd 6dd 6| } qqqEqE||||fS(Ncsyy+t\}}\}}\}}}Wntk rHtjdfSX|rk||||f||ffS||fSdS(NR](Rt StopIterationttokenizet ENDMARKER(Ryttok_typet tok_stringtsrowtscolterowtecoltline(R(sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyt get_tokens+ t*s**sInvalid signature: (%s)RRRRiiRt[t{Rt]t}(RR,R-( R!tgenerate_tokensRtreadlineRRR"tOPtNAMERRlR^tisolate_expression(RRRyRtvar_argRRR*t var_arg_typeR#R$tvar_namet nest_typet unnest_typet nest_countt start_postend_posRbRRt default_expr((RsA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyRs~ ' *           *  ! ! "+c Cs|\}}|d8}|\}}|d8}|jt}||krV||||!S|||g}|j||d|!|t|kr|j||| ndj|S(NiR](RR^textendRPRlR( RR;R<R%R&R'R(tlinesRb((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyR4Gs     sn%prog [OPTIONS] TEMPLATE arg=value Use py:arg=value to set a Python value; otherwise all values are strings. c Csddl}ddl}ddl}ddl}|dkrL|jd}n|jd}|jdt|dt }|j dddd d d d d |j dddddd d|j dddddd d|j |\}}t |dkrdGH|j dn|d}|d}i} |jrK| j|jnx}|D]u} d| kr}d| GH|j dn| jdd\} } | jdr| d } t| } n| | | R@Rtwb(R3toptparset pkg_resourcesR"Rtargvtget_distributiont OptionParserRt_fill_command_usaget add_optiont parse_argsRPtexitRJRRtenvironRR RtstdinRBRARCRGR R RZRCtwritetstdout(RR3RMRNR"tdisttparsertoptionst template_nameRRjRttemplate_contentRHt TemplateClassRRW((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pyt fill_command^sv                         t__main__(DRRR3RturllibRRR"R!t cStringIORttempita._looperRttempita.compat3RRRRRt__all__RRtIRRRRRR(RR RR RRRR R^RRRR R RRcRRRRRRRRRRR9RR RRRRRRR4RRR`R(((sA/opt/alt/python27/lib/python2.7/site-packages/tempita/__init__.pytsn     (  $  %    M   > ; = 5   #    L  @