a Ã`Igã@s2dZddlZddlZddlmZddlmZddlmZddl m Z ddl m Z dd l m Z dd l mZdd l mZdd l mZdd l mZddl mZddl mZddl mZddl mZddl mZddl mZddl mZddl mZddlmZddlmZddlmZddlmZddl m!Z!ddl"m#Z#ddl"m$Z$dZ%e &d¡Z'Gdd „d e(ƒZ)Gd!d"„d"e e)e*ƒƒZ+e#d#d$„ƒZ,d%d&„Z-d'd(„Z.Gd)d*„d*e+ƒZ/Gd+d,„d,e+ƒZ0Gd-d.„d.e+ƒZ1Gd/d0„d0e+ƒZ2Gd1d2„d2e+ƒZ3Gd3d4„d4e+ƒZ4e%d5fd6d7„Z5Gd8d9„d9e*ƒZ6d:d;„Z7e/Z8e0Z9e1Z:e2Z;e3Z.gettextr)rWrr+rVr,Ú_make_new_gettext“s rXcst‡fdd„ƒ}|S)Ncs4| d|¡| ˆ|||¡}|jjr,t|ƒ}||S)NÚnum)Ú setdefaultrOrSrTr)rQZ __singularZ__pluralZ__numrUr*rVr+r,r¢s  z$_make_new_ngettext..ngettextr)rWrr+rVr,Ú_make_new_ngettext¡sr[c@steZdZdZdhZdd„Zddd„Zddd „Zdd d „Zd d „Z e fdd„Z dd„Z e fdd„Zdd„Zdd„ZdS)ÚInternationalizationExtensionz-This extension adds gettext support to Jinja.ZtranscCs:t ||¡t|jd<|j|j|j|j|j|j dddS)NrF)Zinstall_gettext_translationsZinstall_null_translationsZinstall_gettext_callablesZuninstall_gettext_translationsZextract_translationsÚnewstyle_gettext) r/r5rRÚglobalsÚextendÚ_installÚ _install_nullÚ_install_callablesÚ _uninstallÚ_extractr3r+r+r,r5ºs  úz&InternationalizationExtension.__init__NcCsFt|ddƒ}|dur|j}t|ddƒ}|dur4|j}| |||¡dS)NZugettextZ ungettext)Úgetattrrrrb)r4Ú translationsÚnewstylerrr+r+r,r`Æs  z&InternationalizationExtension._installcCs| dd„dd„|¡dS)NcSs|Sr1r+)Úxr+r+r,ÚÑóz=InternationalizationExtension._install_null..cSs|dkr|fp|fdS)Nrrr+)ÚsÚpÚnr+r+r,riÑrj)rb)r4rgr+r+r,raÏsÿz+InternationalizationExtension._install_nullcCs>|dur||j_|jjr(t|ƒ}t|ƒ}|jjj||ddS©N)rr)r2r]rXr[r^r9)r4rrrgr+r+r,rbÔs z0InternationalizationExtension._install_callablescCsdD]}|jj |d¡qdSrn)r2r^Úpop)r4rfÚkeyr+r+r,rcÜsz(InternationalizationExtension._uninstallcCs t|tƒr|j |¡}t||ƒSr1)Ú isinstancerr2rBÚextract_from_ast)r4r;Úgettext_functionsr+r+r,rdàs  z&InternationalizationExtension._extractcCst|jƒj}d}d}d}i}d}|jjjdkr4|r@|j d¡|j d¡rPq4|j d¡}|j|vr~|jd|j|jt d|jjjd krªt|jƒ|  ¡||j<} n8|durÊ|jd vrÊ|jd k}q nt   |jd ¡||j<} |dur t | t jƒr$t   d d ¡}|||j<t  t   d d¡| ¡}n| }|jdk}q |j d¡d} d} tƒ} | |d¡\} }| r”|  | ¡|dur”t   | dd ¡}| ddk}|jj d¡r8d} t|jƒ|jjjdkr|j d¡}|j|vrò|jd|j|jt d||j}|jdk}|j d¡| |d¡\}} t|jƒ|  |¡n t|jƒ| D] } | |vrFt   | d ¡|| <qF| std}n|durŠ| d|¡|dur |jjd}|rÀ| |¡}| rÀ| | ¡} | || ||t| ƒ|oÚ| ¡}| |¡|durú||gS|SdS)zParse a translatable tag.FNZ block_endZcommaÚcolonr'z'translatable variable %r defined twice.)ÚexcZassign)ÚtrimmedZ notrimmedrvÚloadZ_transÚstorerYTrúname:pluralizez%unknown variable %r for pluralizationzpluralize without variablesúext.i18n.trimmed)Únextr>rDÚcurrentr"ÚexpectZskip_ifÚvalueÚfailrZparse_expressionrÚNamerqrFZAssignrLÚ _parse_blockr9Útestr2ÚpoliciesÚ_trim_whitespaceÚ _make_nodeÚboolZ set_lineno)r4rArDÚnum_called_numÚ plural_exprZplural_expr_assignmentrUrvr'ÚvarÚpluralZ have_pluralÚ referencedZsingular_namesÚsingularZ plural_namesÚnoder+r+r,rBås®     ý     ÿ        ý             ú  z#InternationalizationExtension.parsecCs| d| ¡¡S)Nú )ÚsubÚstrip)r4ÚstringÚ_ws_rer+r+r,r„Xsz.InternationalizationExtension._trim_whitespacecCsg}g}|jjjdkr:| |jjj dd¡¡t|jƒq|jjjdkr†t|jƒ|j d¡j}| |¡| d|¡|j d¡q|jjjdkrÚt|jƒ|jj d ¡r°qøn|jj d ¡rÎ|rÄqø|  d ¡|  d ¡q|jj rî|  d ¡qt dƒ‚q|t |ƒfS)z1Parse until the next block tag with a given name.Údataú%ú%%Zvariable_beginr'z%%(%s)sZ variable_endZ block_beginz name:endtransryz:a translatable section can have only one pluralize sectionz;control structures in translatable sections are not allowedzunclosed translation blockzinternal parser error) r>r|r"Úappendr~Úreplacer{r}r‚rZeosÚ RuntimeErrorr)r4rAZallow_pluralizer‹Úbufr'r+r+r,r[s8    ÿÿ  z*InternationalizationExtension._parse_blockc Cs|s(|jjs(| dd¡}|r(| dd¡}|durXt dd¡}t |t |¡ggdd¡}n0t dd¡} t | t |¡t |¡|ggdd¡}|jjrÆt|ƒD]*\} } |r®| dkr®q˜|j  t  | | ¡¡q˜n.t  |¡}|rôt  |t  dd „| ¡Dƒ¡¡}t |g¡S) z/Generates a useful node from the data provided.r•r”NrrwrrYcSs"g|]\}}t t |¡|¡‘qSr+)rZPairÚConst)Ú.0rpr~r+r+r,Ú ¬sÿz.)r2r]r—rr€rFršrrHr–ZKeywordZMarkSafeIfAutoescapeZModÚDictÚitemsÚOutput) r4rŒrŠrUrˆZvars_referencedr‡rrrrpr~r+r+r,r…}s>     û   þÿþ z(InternationalizationExtension._make_node)N)N)N)r%r$r-r.rMr5r`rarbrcÚGETTEXT_FUNCTIONSrdrBr’r„rr…r+r+r+r,r\®s    s "r\c@s"eZdZdZedgƒZdd„ZdS)ÚExprStmtExtensionzrAdds a `do` tag to Jinja that works like the print statement just that it doesn't print the return value. ÚdocCs"tjt|jƒjd}| ¡|_|S)NrC)rZExprStmtr{r>rDZ parse_tupler)r4rArr+r+r,rB¼s zExprStmtExtension.parseN©r%r$r-r.rLrMrBr+r+r+r,r¡µs r¡c@s$eZdZdZeddgƒZdd„ZdS)ÚLoopControlExtensionz/Adds break and continue to the template engine.ÚbreakÚcontinuecCs0t|jƒ}|jdkr"tj|jdStj|jdS)Nr¥rC)r{r>r~rZBreakrDZContinue)r4rAÚtokenr+r+r,rBÇs  zLoopControlExtension.parseNr£r+r+r+r,r¤Âs r¤c@s eZdZdS)Ú WithExtensionN©r%r$r-r+r+r+r,r¨Îsr¨c@s eZdZdS)ÚAutoEscapeExtensionNr©r+r+r+r,rªÒsrªc@s&eZdZdZdhZdd„Zdd„ZdS)ÚDebugExtensiona·A ``{% debug %}`` tag that dumps the available variables, filters, and tests. .. code-block:: html+jinja
{% debug %}
.. code-block:: text {'context': {'cycler': , ..., 'namespace': }, 'filters': ['abs', 'attr', 'batch', 'capitalize', 'center', 'count', 'd', ..., 'urlencode', 'urlize', 'wordcount', 'wordwrap', 'xmlattr'], 'tests': ['!=', '<', '<=', '==', '>', '>=', 'callable', 'defined', ..., 'odd', 'sameas', 'sequence', 'string', 'undefined', 'upper']} .. versionadded:: 2.11.0 ÚdebugcCs6|j d¡j}tƒ}|jd|g|d}tj|g|dS)Nz name:debugÚ_renderrC)r>r}rDrrKrrŸ)r4rArDÚcontextÚresultr+r+r,rBíszDebugExtension.parsecCsZ| ¡t|jj ¡ƒt|jj ¡ƒdœ}tdd…dkrHtj|dddStj|ddSdS)N)r®ÚfiltersÚtestsé)éér³T)ÚdepthZcompact)rµ) Zget_allÚsortedr2r°Úkeysr±rÚpprintZpformat)r4r®r¯r+r+r,r­ósýzDebugExtension._renderN)r%r$r-r.rMrBr­r+r+r+r,r«Ösr«Tccsþ| tj¡D]ì}t|jtjƒr |jj|vr,q g}|jD]4}t|tjƒr`t|j t ƒr`|  |j ¡q6|  d¡q6|j D]}|  d¡qr|j dur–|  d¡|jdurª|  d¡|sÈtdd„|Dƒƒ}|sæq nt|ƒdkrÞ|d}nt|ƒ}|j|jj|fVq dS)aîExtract localizable strings from the given template node. Per default this function returns matches in babel style that means non string parameters as well as keyword arguments are returned as `None`. This allows Babel to figure out what you really meant if you are using gettext functions that allow keyword arguments for placeholder expansion. If you don't want that behavior set the `babel_style` parameter to `False` which causes only strings to be returned and parameters are always stored in tuples. As a consequence invalid gettext calls (calls without a single string parameter or string parameters after non-string parameters) are skipped. This example explains the behavior: >>> 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. Ncss|]}|dur|VqdSr1r+)r›rhr+r+r,Ú :rjz#extract_from_ast..rr)Zfind_allrrFrqrr€r'rGršr~rr–rHrIrJÚtupleÚlenrD)rrsZ babel_styleÚstringsÚargrr+r+r,rrs2# ÿ þ          rrc@s(eZdZdZdd„Zdd„Zdd„ZdS) Ú_CommentFinderzÒ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)Nr)ÚtokensÚ comment_tagsÚoffsetÚ last_lineno)r4r¿rÀr+r+r,r5Lsz_CommentFinder.__init__c CsŽz€t|j|j|…ƒD]^\}}}|dvrz| dd¡\}}WntyRYqYn0||jvr| ¡gW||_SqgW||_S||_0dS)N)ÚcommentZ linecommentr)Úreversedr¿rÁÚsplitÚ ValueErrorrÀÚrstrip)r4rÁrÚ token_typeZ token_valueÚprefixrÃr+r+r,Úfind_backwardsRs"ÿ   ýþz_CommentFinder.find_backwardscCsd|jr|j|krgSt|j|jd…ƒD]*\}\}}}||kr(| |j|¡Sq(| t|jƒ¡Sr1)rÀrÂÚ enumerater¿rÁrÊr»)r4rDÚidxZ token_linenorr+r+r,Ú find_commentsbs "z_CommentFinder.find_commentsN)r%r$r-r.r5rÊrÍr+r+r+r,r¾Esr¾ccs tƒ}| dd¡ d¡D] }| ¡}|s*q| t|ƒ¡qt|vrL| t¡ddd„}||ddƒ}t| d t¡| d t ¡| d t ¡| d t ¡| d t ¡| dt ¡| d¡p¬t| d¡p¸t||dtƒ||dtƒt||dtƒt|ƒddd}||dƒrüd|jd<||dƒrd|_| ¡ | dd¡¡} z"| | ¡} t| | | ¡¡ƒ} Wntyd|s^‚YdS0t| |ƒ} t| |ƒD] \} }}| |||   | ¡fVqzdS)aBabel 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 preceding 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) Ú extensionsÚú,FcSs| |t|ƒ¡ ¡dvS)N)Ú1ZonZyesÚtrue)ÚgetÚstrÚlower)ÚoptionsrpÚdefaultr+r+r,Úgetboolszbabel_extract..getboolÚsilentTZblock_start_stringZblock_end_stringZvariable_start_stringZvariable_end_stringZcomment_start_stringZcomment_end_stringZline_statement_prefixZline_comment_prefixZ trim_blocksZ lstrip_blocksZkeep_trailing_newliner)Z cache_sizeZ auto_reloadrvrzr]Úencodingzutf-8N)F)!rLrÓrÅrÚaddrr\rr r rrr r rrrrrr Ú frozensetrƒr]ÚreadÚdecoderBÚlistZlexr=rr¾rrrÍ)ZfileobjÚkeywordsrÀrÖrÎÚ extensionrØrÙr2r;rr¿ÚfinderrDrWÚmessager+r+r,Ú babel_extractksT              ñ     rä)>r.r¸ÚreÚsysrZ markupsaferrÏrZ_compatrrrÚdefaultsr r r r r rrrrrrrr2rÚ exceptionsrrrZruntimerZutilsrrr Úcompiler’r"r r6r/rRrXr[r\r¡r¤r¨rªr«rrr¾räZi18nr¢Z loopcontrolsZwith_rTr¬r+r+r+r,Úsh                           \     +D&P