Rc@sdZddlmZddlmZmZmZmZmZm Z m Z m Z m Z m Z mZmZddlmZddlmZddlmZmZddlmZmZmZddlmZmZmZmZd"Z d e!fd YZ"dee"e#fdYZ$edZ%dZ&dZ'de$fdYZ(de$fdYZ)de$fdYZ*de$fdYZ+de$fdYZ,e e-dZ.de#fdYZ/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_tgettexttngettexttExtensionRegistrycBseZdZdZRS(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 ExtensioncBsqeZdZeZdZdZdZd dZ dZ dZ d dZ d d d d d dZ 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(sThis 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(sIt'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 tdS(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(sReturn 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_aliasscstfd}|S(Ncs5|j|}|jjr-t|}n||S(N(RFteval_ctxt autoescapeR(RHt__stringt variablesR&(tfunc(s./usr/lib/python2.7/site-packages/jinja2/ext.pyRs (R(RNR((RNs./usr/lib/python2.7/site-packages/jinja2/ext.pyt_make_new_gettextscstfd}|S(NcsK|jd||j|||}|jjrCt|}n||S(Ntnum(t setdefaultRFRJRKR(RHt __singulart__pluralt__numRMR&(RN(s./usr/lib/python2.7/site-packages/jinja2/ext.pyRs  (R(RNR((RNs./usr/lib/python2.7/site-packages/jinja2/ext.pyt_make_new_ngettextstInternationalizationExtensioncBszeZdZedgZdZd dZd dZd dZ dZ e dZ dZ 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|jdd|dS(NcSs|S(N((tx((s./usr/lib/python2.7/site-packages/jinja2/ext.pytscSs |dkr|fp|fdS(Nii((tstptn((s./usr/lib/python2.7/site-packages/jinja2/ext.pyRls(Rb(R*Rj((s./usr/lib/python2.7/site-packages/jinja2/ext.pyRascCsb|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.pyRbs   cCs+x$dD]}|jjj|dqWdS(NRR(sgettextsngettext(R)R^tpopR<(R*Ritkey((s./usr/lib/python2.7/site-packages/jinja2/ext.pyRcs cCs1t|tr$|jj|}nt||S(N(t isinstanceRR)R8textract_from_ast(R*R1tgettext_functions((s./usr/lib/python2.7/site-packages/jinja2/ext.pyRdscCst|jj}t}d}d}i}x^|jjjdkr|r[|jjdn|jjdrqPn|jjd}|j |kr|j d|j |jdt n|jjjdkrt|j|j ||j <}n t j|j d||j <}|dkr-t|t jrot 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=RrRRR#R>RRRRR<R?R@RAttupletlenR9(RRtt babel_styletstringstarg((s./usr/lib/python2.7/site-packages/jinja2/ext.pyRss0$   t_CommentFindercBs)eZdZdZdZdZRS(sHelper 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*RR((s./usr/lib/python2.7/site-packages/jinja2/ext.pyR+ s   cCszxt|j|j|!D]m\}}}|dkry|jdd\}}Wntk rjqnX||jkr|jgSqqWgSWd||_XdS(Ntcommentt linecommenti(scommentR(treversedRRtsplitR<t ValueErrorRtrstrip(R*RRt token_typet token_valuetprefixR((s./usr/lib/python2.7/site-packages/jinja2/ext.pytfind_backwardss&  cCs|j s|j|krgSxMt|j|jD]5\}\}}}||kr4|j|j|Sq4W|jt|jS(N(RRt enumerateRRRR(R*R9tidxt token_linenoR((s./usr/lib/python2.7/site-packages/jinja2/ext.pyt find_comments s , (R RR'R+RR(((s./usr/lib/python2.7/site-packages/jinja2/ext.pyRs  ccs@t}xN|jddjdD]1}|j}|sCq%n|jt|q%Wt|krv|jtntd}||dt}t |jdt |jdt |jdt |jd t |jd t|jd t|jd pt|jd pt||dt||dtt||dtt|dddt }||dr{t|_n|jj|jdd} y1|j| } t|j|j| } Wn tk r} |sndSXt| |} 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|jdkS(Nt1tontyesttrue(RRsyesR(tgettstrtlower(RRqtdefault((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("RCRRtstriptaddRRVReRR RRRRRRRRR R R R t frozensetR]treadtdecodeR8RtlexR3RRRsR(tfileobjtkeywordsRRRt extensionRRR)R1RRtetfinderR9RNtmessage((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._compatRRRRRRRR,R(RIRORURVRRRRRRsRRti18nRt loopcontrolstwith_RK(((s./usr/lib/python2.7/site-packages/jinja2/ext.pyt s6R" Y    C% O