ó ÈÏRc@södZddlmZddlmZmZmZmZmZm Z m Z m Z m Z m Z mZmZddlmZddlmZddlmZmZddlmZmZmZddlmZmZmZmZd"Z d e!fd „ƒYZ"dee"e#ƒfd„ƒYZ$ed„ƒZ%d„Z&d„Z'de$fd„ƒYZ(de$fd„ƒYZ)de$fd„ƒYZ*de$fd„ƒYZ+de$fd„ƒYZ,e e-d„Z.de#fd„ƒYZ/d „Z0e(Z1e)Z2e*Z3e+Z4e,Z5d!S(#s  jinja2.ext ~~~~~~~~~~ Jinja extensions allow to add custom tags similar to the way django custom tags work. By default two example extensions exist: an i18n and a cache extension. :copyright: (c) 2010 by the Jinja Team. :license: BSD. iÿÿÿÿ(tnodes( tBLOCK_START_STRINGtBLOCK_END_STRINGtVARIABLE_START_STRINGtVARIABLE_END_STRINGtCOMMENT_START_STRINGtCOMMENT_END_STRINGtLINE_STATEMENT_PREFIXtLINE_COMMENT_PREFIXt TRIM_BLOCKStNEWLINE_SEQUENCEtKEEP_TRAILING_NEWLINEt LSTRIP_BLOCKS(t Environment(tconcat(tTemplateAssertionErrortTemplateSyntaxError(tcontextfunctiont import_stringtMarkup(tnexttwith_metaclasst string_typest iteritemst_tgettexttngettexttExtensionRegistrycBseZdZd„ZRS(s)Gives the extension an unique identifier.cCs3tj||||ƒ}|jd|j|_|S(Nt.(ttypet__new__t __module__t__name__t identifier(tclstnametbasestdtrv((s./usr/lib/python2.7/site-packages/jinja2/ext.pyR#s(R Rt__doc__R(((s./usr/lib/python2.7/site-packages/jinja2/ext.pyR st ExtensioncBsqeZdZeƒZdZd„Zd„Zd d„Z d„Z d„Z d d„Z d d d d d d„Z RS( sgExtensions can be used to add extra functionality to the Jinja template system at the parser level. Custom extensions are bound to an environment but may not store environment specific data on `self`. The reason for this is that an extension can be bound to another environment (for overlays) by creating a copy and reassigning the `environment` attribute. As extensions are created by the environment they cannot accept any arguments for configuration. One may want to work around that by using a factory function, but that is not possible as extensions are identified by their import name. The correct way to configure the extension is storing the configuration values on the environment. Because this way the environment ends up acting as central configuration storage the attributes may clash which is why extensions have to ensure that the names they choose for configuration are not too generic. ``prefix`` for example is a terrible name, ``fragment_cache_prefix`` on the other hand is a good name as includes the name of the extension (fragment cache). idcCs ||_dS(N(t environment(tselfR)((s./usr/lib/python2.7/site-packages/jinja2/ext.pyt__init__FscCs2tj|jƒ}|jj|jƒ||_|S(s=Create a copy of this extension bound to another environment.(tobjectRt __class__t__dict__tupdateR)(R*R)R&((s./usr/lib/python2.7/site-packages/jinja2/ext.pytbindIs cCs|S(sÁThis method is called before the actual lexing and can be used to preprocess the source. The `filename` is optional. The return value must be the preprocessed source. ((R*tsourceR#tfilename((s./usr/lib/python2.7/site-packages/jinja2/ext.pyt preprocessPscCs|S(s¹It's passed a :class:`~jinja2.lexer.TokenStream` that can be used to filter tokens returned. This method has to return an iterable of :class:`~jinja2.lexer.Token`\s, but it doesn't have to return a :class:`~jinja2.lexer.TokenStream`. In the `ext` folder of the Jinja2 source distribution there is a file called `inlinegettext.py` which implements a filter that utilizes this method. ((R*tstream((s./usr/lib/python2.7/site-packages/jinja2/ext.pyt filter_streamWs cCs tƒ‚dS(sIf any of the :attr:`tags` matched this method is called with the parser as first argument. The token the parser stream is pointing at is the name token that matched. This method has to return one or a list of multiple nodes. N(tNotImplementedError(R*tparser((s./usr/lib/python2.7/site-packages/jinja2/ext.pytparsecscCstj|j|d|ƒS(sÓReturn an attribute node for the current extension. This is useful to pass constants on extensions to generated template code. :: self.attr('_my_attribute', lineno=lineno) tlineno(RtExtensionAttributeR!(R*R#R9((s./usr/lib/python2.7/site-packages/jinja2/ext.pytattrkscCsX|dkrg}n|dkr*g}ntj|j|d|ƒ||||d|ƒS(ssCall a method of the extension. This is a shortcut for :meth:`attr` + :class:`jinja2.nodes.Call`. R9N(tNoneRtCallR;(R*R#targstkwargstdyn_argst dyn_kwargsR9((s./usr/lib/python2.7/site-packages/jinja2/ext.pyt call_methodus     N(R RR'tsetttagstpriorityR+R0R<R3R5R8R;RB(((s./usr/lib/python2.7/site-packages/jinja2/ext.pyR()s      cOs|j|jdƒ||ŽS(NR(tcalltresolve(t __contextR>R?((s./usr/lib/python2.7/site-packages/jinja2/ext.pyt_gettext_alias‚scst‡fd†ƒ}|S(Ncs5|jˆ|ƒ}|jjr-t|ƒ}n||S(N(RFteval_ctxt autoescapeR(RHt__stringt variablesR&(tfunc(s./usr/lib/python2.7/site-packages/jinja2/ext.pyRˆs (R(RNR((RNs./usr/lib/python2.7/site-packages/jinja2/ext.pyt_make_new_gettext‡scst‡fd†ƒ}|S(NcsK|jd|ƒ|jˆ|||ƒ}|jjrCt|ƒ}n||S(Ntnum(t setdefaultRFRJRKR(RHt __singulart__pluralt__numRMR&(RN(s./usr/lib/python2.7/site-packages/jinja2/ext.pyR’s  (R(RNR((RNs./usr/lib/python2.7/site-packages/jinja2/ext.pyt_make_new_ngettext‘stInternationalizationExtensioncBszeZdZedgƒZd„Zd d„Zd d„Zd d„Z d„Z e d„Z d„Z d „Zd „ZRS( s.This extension adds gettext support to Jinja2.ttransc Cs^tj||ƒt|jd<|jd|jd|jd|jd|jd|j dt ƒdS(NRtinstall_gettext_translationstinstall_null_translationstinstall_gettext_callablestuninstall_gettext_translationstextract_translationstnewstyle_gettext( R(R+RItglobalstextendt_installt _install_nullt_install_callablest _uninstallt_extracttFalse(R*R)((s./usr/lib/python2.7/site-packages/jinja2/ext.pyR+§s       cCskt|ddƒ}|dkr*|j}nt|ddƒ}|dkrT|j}n|j|||ƒdS(Ntugettextt ungettext(tgetattrR<RRRb(R*t translationstnewstyleRR((s./usr/lib/python2.7/site-packages/jinja2/ext.pyR`³s    cCs|jd„d„|ƒdS(NcSs|S(N((tx((s./usr/lib/python2.7/site-packages/jinja2/ext.pyt¾scSs |dkr|fp|fdS(Nii((tstptn((s./usr/lib/python2.7/site-packages/jinja2/ext.pyRl¿s(Rb(R*Rj((s./usr/lib/python2.7/site-packages/jinja2/ext.pyRa¼scCsb|dk r||j_n|jjrBt|ƒ}t|ƒ}n|jjjd|d|ƒdS(NRR(R<R)R]RORUR^R/(R*RRRj((s./usr/lib/python2.7/site-packages/jinja2/ext.pyRbÃs   cCs+x$dD]}|jjj|dƒqWdS(NRR(sgettextsngettext(R)R^tpopR<(R*Ritkey((s./usr/lib/python2.7/site-packages/jinja2/ext.pyRcÎs cCs1t|tƒr$|jj|ƒ}nt||ƒS(N(t isinstanceRR)R8textract_from_ast(R*R1tgettext_functions((s./usr/lib/python2.7/site-packages/jinja2/ext.pyRdÒscCsÂt|jƒj}t}d}d}i}x^|jjjdkrŠ|r[|jjdƒn|jjdƒrqPn|jjdƒ}|j |krµ|j d|j |jdt ƒn|jjjdkrñt|jƒ|j ƒ||j <}n t j|j dƒ||j <}|dkr-t|t jƒrot jd dƒ}|||j >> from jinja2 import Environment >>> env = Environment() >>> node = env.parse('{{ (_("foo"), _(), ngettext("foo", "bar", 42)) }}') >>> list(extract_from_ast(node)) [(1, '_', 'foo'), (1, '_', ()), (1, 'ngettext', ('foo', 'bar', None))] >>> list(extract_from_ast(node, babel_style=False)) [(1, '_', ('foo',)), (1, 'ngettext', ('foo', 'bar'))] For every string found this function yields a ``(lineno, function, message)`` tuple, where: * ``lineno`` is the number of the line on which the string was found, * ``function`` is the name of the ``gettext`` function used (if the string was extracted from embedded Python code), and * ``message`` is the string itself (a ``unicode`` object, or a tuple of ``unicode`` objects for functions with multiple string arguments). This extraction function operates on the AST and is because of that unable to extract any comments. For comment support you have to use the babel extraction interface or extract comments yourself. css!|]}|dk r|VqdS(N(R<(t.0Rk((s./usr/lib/python2.7/site-packages/jinja2/ext.pys ùsiiN(tfind_allRR=RrR•RƒR#R>R¡R€RR›R<R?R@RAttupletlenR9(R•Rtt babel_styletstringstarg((s./usr/lib/python2.7/site-packages/jinja2/ext.pyRsÀs0$   t_CommentFindercBs)eZdZd„Zd„Zd„ZRS(sÒHelper class to find comments in a token stream. Can only find comments for gettext calls forwards. Once the comment from line 4 is found, a comment for line 1 will not return a usable value. cCs(||_||_d|_d|_dS(Ni(ttokenst comment_tagstoffsett last_lineno(R*RËRÌ((s./usr/lib/python2.7/site-packages/jinja2/ext.pyR+ s   cCs¤z“xˆt|j|j|!ƒD]m\}}}|dkry|jddƒ\}}Wntk rjqnX||jkrŠ|jƒgSqqWgSWd||_XdS(Ntcommentt linecommenti(scommentRÐ(treversedRËRÍtsplitR<t ValueErrorRÌtrstrip(R*RÍRt token_typet token_valuetprefixRÏ((s./usr/lib/python2.7/site-packages/jinja2/ext.pytfind_backwardss&  cCsƒ|j s|j|krgSxMt|j|jƒD]5\}\}}}||kr4|j|j|ƒSq4W|jt|jƒƒS(N(RÌRÎt enumerateRËRÍRØRÆ(R*R9tidxt token_linenoR((s./usr/lib/python2.7/site-packages/jinja2/ext.pyt find_comments s , (R RR'R+RØRÜ(((s./usr/lib/python2.7/site-packages/jinja2/ext.pyRÊs  ccs@tƒ}xN|jddƒjdƒD]1}|jƒ}|sCq%n|jt|ƒƒq%Wt|krv|jtƒntd„}||dtƒ}t |jdt ƒ|jdt ƒ|jdt ƒ|jd t ƒ|jd tƒ|jd tƒ|jd ƒpt|jd ƒpt||dtƒ||dtƒt||dtƒt|ƒdddtƒ }||dƒr{t|_n|jƒj|jddƒƒ} y1|j| ƒ} t|j|j| ƒƒƒ} Wn tk rï} |së‚ndSXt| |ƒ} x:t | |ƒD])\}}}|||| j!|ƒfVqWdS(sBabel extraction method for Jinja templates. .. versionchanged:: 2.3 Basic support for translation comments was added. If `comment_tags` is now set to a list of keywords for extraction, the extractor will try to find the best preceeding comment that begins with one of the keywords. For best results, make sure to not have more than one gettext call in one line of code and the matching comment in the same line or the line before. .. versionchanged:: 2.5.1 The `newstyle_gettext` flag can be set to `True` to enable newstyle gettext calls. .. versionchanged:: 2.7 A `silent` option can now be provided. If set to `False` template syntax errors are propagated instead of being ignored. :param fileobj: the file-like object the messages should be extracted from :param keywords: a list of keywords (i.e. function names) that should be recognized as translation functions :param comment_tags: a list of translator tags to search for and include in the results. :param options: a dictionary of additional options (optional) :return: an iterator over ``(lineno, funcname, message, comments)`` tuples. (comments will be empty currently) t extensionstt,cSs"|j|t|ƒƒjƒdkS(Nt1tontyesttrue(RàRásyesRã(tgettstrtlower(RÂRqtdefault((s./usr/lib/python2.7/site-packages/jinja2/ext.pytgetboolNstsilenttblock_start_stringtblock_end_stringtvariable_start_stringtvariable_end_stringtcomment_start_stringtcomment_end_stringtline_statement_prefixtline_comment_prefixt trim_blockst lstrip_blockstkeep_trailing_newlinet cache_sizeit auto_reloadR]tencodingsutf-8N("RCRäRÒtstriptaddRRVReR†R RRRRRRRRR R R R t frozensetR]treadtdecodeR8RºtlexR3RRÊRsRÜ(tfileobjtkeywordsRÌRÂRÝt extensionRèRéR)R1R•RËtetfinderR9RNtmessage((s./usr/lib/python2.7/site-packages/jinja2/ext.pyt babel_extract)sN "      !"N(Rsgettextsngettext(6R'tjinja2Rtjinja2.defaultsRRRRRRRRR R R R tjinja2.environmentR tjinja2.runtimeRtjinja2.exceptionsRRt jinja2.utilsRRRtjinja2._compatRRRRRªRRR,R(RIRORURVR«R¯RµRÀR†RsRÊRti18nR¬t loopcontrolstwith_RK(((s./usr/lib/python2.7/site-packages/jinja2/ext.pyt s6R" Y ê   C% O