U az} @sPUdZddlZddlZddlZddlZddlmZddlm Z ddlm Z ddl m Z ddl mZdd l mZdd lmZdd lmZdd lmZdd lmZddlmZejrddlZddlmZddlmZddlmZGdddejZ Gddde Z!ej"e e!fZ#dZ$ej%e&dfe'd<e(dZ)GdddZ*eeej+ej+ej"ej+efdddZ,ej-e&ge&fej-de&fdd d!Z.ej-e&e&e/ge&fej-de&fdd"d#Z0ej-e&e&ge&fej-de&fdd$d%Z1ej-e&e&e&e/ge&fej-de&fdd&d'Z2Gd(d)d)e*Z3Gd*d+d+e*Z4Gd,d-d-e*Z5Gd.d/d/e*Z6Gd0d1d1e*Z7Gd2d3d3e*Z8e$d4fe j9ej:e&e;ejGd8d9d9Z?ej@ej:e&ej:e&ejAe&ej+fejrrr preprocesscszExtension.preprocessrr)streamrcCs|S)a 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`. r)rr@rrr filter_streamlszExtension.filter_streamrparserrcCs tdS)aIf 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)NotImplementedError)rrCrrrparsevszExtension.parse)r=linenorcCstj|j||dS)zReturn 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) rF)rExtensionAttributer))rr=rFrrrattr~s zExtension.attr)r=argskwargsdyn_args dyn_kwargsrFrcCs8|dkr g}|dkrg}tj|j||d|||||dS)zsCall a method of the extension. This is a shortcut for :meth:`attr` + :class:`jinja2.nodes.Call`. NrG)rCallrI)rr=rJrKrLrMrFrrr call_methods  zExtension.call_method)N)N)NNNNN) rrr__doc__r3ClassVarr __annotations__r,setr-Setpriorityrr2r;Optionalr?UnionIterablerArNodeListrEr!rHrIExprKeywordrNrOrrrrr(7sL   $    r() __contextrJrKrcOs|j|df||S)Nr)callresolve)r]rJrKrrr_gettext_aliassr`)funcrcs"ttttjtdfdd }|S)N)r]__string variablesrcs$||}|jjrt|}||Sr)r^eval_ctx autoescaper)r]rbrcr:rarrrs z"_make_new_gettext..gettextr r r r3Any)rarrrfr_make_new_gettexts rics&ttttttjtdfdd }|S)N)r] __singular__plural__numrcrcs4|d|||||}|jjr,t|}||S)Nnum setdefaultr^rdrer)r]rjrkrlrcr:rfrrrs  z$_make_new_ngettext..ngettextr r r r!r3rh)rarrrfr_make_new_ngettextsrqcs$tttttjtdfdd }|S)N)r] __string_ctxrbrcrcs2|d||||}|jjr*t|}||S)Nr#rn)r]rrrbrcr:rfrrr$s  z$_make_new_pgettext..pgettextrg)rar$rrfr_make_new_pgettexts rsc s(tttttttjtdfdd }|S)N)r]rrrjrkrlrcrcsB|d||d||||||}|jjr:t|}||S)Nr#rmrn)r]rrrjrkrlrcr:rfrrr%s  z&_make_new_npgettext..npgettextrp)rar%rrfr_make_new_npgettextsrtc seZdZdZdhZeddfdd Zd%deje ddd d Z d&eje dd d d Z d'ej e ge fej e e ege feje ejej e e ge fejej e e e ege fddddZdddddZefeje ejfeje ejejee ejeje ejeje dfffdddZdejejejejfdddZefe eje e dddZde ejeje e fdd d!Ze eje ej e ej!fejej!e e ej"d"d#d$Z#Z$S)(InternationalizationExtensionz-This extension adds gettext support to Jinja.ZtransNr/cs:t|t|jd<|j|j|j|j|j|j dddS)Nr&F)Zinstall_gettext_translationsZinstall_null_translationsZinstall_gettext_callablesZuninstall_gettext_translationsZextract_translationsnewstyle_gettext) superr2r`globalsextend_install _install_null_install_callables _uninstall_extractr1r7rrr2s  z&InternationalizationExtension.__init___SupportedTranslations) translationsnewstylercCsdt|dd}|dkr|j}t|dd}|dkr4|j}t|dd}t|dd}|j|||||ddS)NZugettextZ ungettextr$r%)rr$r%)getattrrrr|)rrrrrr$r%rrrrzs    z&InternationalizationExtension._install)rrcCsxddl}|}t|dr"|j}ntttddd}t|drF|j}ntttttddd }|j|j|j|||d dS) Nrr$)csrcSs|Srr)rrrrrr$*sz=InternationalizationExtension._install_null..pgettextr%)rrprrcSs|dkr |S|S)Nrr)rrrrrrrr%1sz>InternationalizationExtension._install_null..npgettext)rrrr$r%) rNullTranslationshasattrr$r r%r!r|r)rrrrr$r%rrrr{ s  z+InternationalizationExtension._install_null)rrrr$r%rcCsb|dk r||j_|jjrHt|}t|}|dk r8t|}|dk rHt|}|jjj||||ddSN)rrr$r%)r0rvrirqrsrtrxr9)rrrrr$r%rrrr|<sz0InternationalizationExtension._install_callables)rrcCsdD]}|jj|dqdSr)r0rxpop)rrkeyrrrr}Tsz(InternationalizationExtension._uninstall.)r<gettext_functionsrcCs t|tr|j|}t||Sr) isinstancer r0rEextract_from_ast)rr<rrrrr~Xs  z&InternationalizationExtension._extractrrBcCs t|jj}d}d}d}i}d}|jjjdkr8|r@|jd|jdrPq8|jd}|j|kr|jd|jd|jt d |jjjd krt|j| ||j<} n8|dkr|jd kr|jd k}q nt |jd ||j<} |dkr t | t jr(t dd }|||j<t t dd| }n| }|jdk}q |jdd} d} t} ||d\} }| r| | |dkrt | dd }| ddk}|jjdr@d} t|j|jjjdkr|jd}|j|kr|jd|jd|jt d ||j}|jdk}|jd||d\}} t|j| |n t|j| D] }||krNt |d ||<qN| s|d}n|dkr|d||dkr|jjd}|r||}| r|| } ||| ||t| |o| }|||dk r||gS|SdS)zParse a translatable tag.FNZ block_endcommacolonr=ztranslatable variable z defined twice.)excZassign)trimmedZ notrimmedrloadZ_transstorermTrname:pluralizezunknown variable z for pluralizationzpluralize without variablesext.i18n.trimmed)nextr@rFcurrenttypeexpectZskip_ifvaluefailrZparse_expressionrNamerrNZAssignrS _parse_blockr9testr0policies_trim_whitespace _make_nodeboolZ set_lineno)rrCrFnum_called_num plural_exprZplural_expr_assignmentrcrtokenvarrZ have_plural referencedZsingular_namesrZ plural_namesr=noderrrrEcs                                   z#InternationalizationExtension.parse)string_ws_rercCs|d|S)N )substrip)rrrrrrrsz.InternationalizationExtension._trim_whitespace)rCallow_pluralizercCsg}g}|jjjdkr:||jjjddt|jq|jjjdkrt|j|jdj}|||d|d|jdq|jjjd krt|j|jjd rqn|jjd r|rq| d | d q|jj r| dqt dq|t |fS)z1Parse until the next block tag with a given name.data%%%Zvariable_beginr=z%(z)sZ variable_endZ block_beginz name:endtransrz:a translatable section can have only one pluralize sectionz;control structures in translatable sections are not allowedzunclosed translation blockzinternal parser error) r@rrappendrreplacerrrrZeos RuntimeErrorr )rrCrrbufr=rrrrs8      z*InternationalizationExtension._parse_block)rrrcrvars_referencedrrc Cs|jj}|s,|s,|dd}|r,|dd}|dkr\tdd}t|t|ggdd} n0tdd} t| t|t||ggdd} |r|D]*\} } |r| dkrq| j t | | qn.t | } |rt | t dd |D} t| gS) z/Generates a useful node from the data provided.rrNrrrrmcSs"g|]\}}tt||qSr)rZPairConst).0rrrrr 6sz.)r0rvrrrrNritemsrKrr\ZMarkSafeIfAutoescapeZModDictOutput) rrrrcrrrrrrrrrrrrrs@         z(InternationalizationExtension._make_node)N)N)NNN)%rrrrPr-rr2r3rVrrzr{Callabler r!r|r}r'rWrTemplateSequenceIteratorTupler~rYrZrErPatternrrrr[rr __classcell__rrrrrusX     . $s ' ruc@s(eZdZdZdhZdejdddZdS)ExprStmtExtensionzrAdds a `do` tag to Jinja that works like the print statement just that it doesn't print the return value. dorrBcCs"tjt|jjd}||_|S)NrG)rExprStmtrr@rFZ parse_tupler)rrCrrrrrEFs zExprStmtExtension.parseN)rrrrPr-rrrErrrrr?src@s6eZdZdZddhZdejejej fdddZ dS) LoopControlExtensionz/Adds break and continue to the template engine.breakcontinuerrBcCs0t|j}|jdkr"tj|jdStj|jdS)NrrG)rr@rrBreakrFContinue)rrCrrrrrEQs  zLoopControlExtension.parseN) rrrrPr-r3rWrrrrErrrrrLsrcs$eZdZeddfdd ZZS) WithExtensionNr/cs t|tjdtdddS)NzZThe 'with' extension is deprecated and will be removed in Jinja 3.1. This is built in now. stacklevelrwr2warningswarnDeprecationWarningr1rrrr2Ys  zWithExtension.__init__rrrrr2rrrrrrXsrcs$eZdZeddfdd ZZS)AutoEscapeExtensionNr/cs t|tjdtdddS)Nz`The 'autoescape' extension is deprecated and will be removed in Jinja 3.1. This is built in now.rrrr1rrrr2ds  zAutoEscapeExtension.__init__rrrrrrcsrc@s8eZdZdZdhZdejdddZee ddd Z d S) DebugExtensionaA ``{% 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 debugrrBcCs8|jdj}t}|jd|g|d}tj|g|dS)Nz name:debug_renderrG)r@rrFrZContextReferencerOr)rrCrFr#resultrrrrEszDebugExtension.parse)r#rcCs8|t|jjt|jjd}tj|dddS)N)r#filterstestsrT)depthcompact)get_allsortedr0rkeysrpprintpformat)rr#rrrrrs zDebugExtension._renderN) rrrrPr-rrrEr r rrrrrrnsrT)astr babel_stylerccs|tjD]}t|jtjr |jj|kr,q g}|jD]4}t|tjr`t|j t r`| |j q6| dq6|j D]}| dqr|j dk r| d|jdk r| d|stdd|D}|sq nt|dkr|d}nt|}|j|jj|fVq dS)aExtract 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, or a tuple of strings 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|]}|dk r|VqdSrr)rxrrr sz#extract_from_ast..rr)Zfind_allrrNrrrr=rJrrr rrKrLrMtuplelenrF)rrrrstringsargr&outrrrrs2+            rc@sfeZdZdZejejeeefejeddddZ eej edddZ eej ed d d Z dS) _CommentFinderzHelper 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. N)tokens comment_tagsrcCs||_||_d|_d|_dS)Nr)rroffset last_lineno)rrrrrrr2sz_CommentFinder.__init__)rrc Cszzt|j|j|D]\\}}}|dkrz|dd\}}Wntk rTYqYnX||jkr|gW SqgWS||_XdS)N)commentZ linecommentr)rreversedrsplit ValueErrorrrstrip)rrr& token_typeZ token_valueprefixrrrrfind_backwardss  z_CommentFinder.find_backwards)rFrcCsd|jr|j|krgSt|j|jdD]*\}\}}}||kr(||j|Sq(|t|jSr)rr enumeraterrrr)rrFidxZ token_linenor&rrr find_commentss "z_CommentFinder.find_comments) rrrrPr3rrr!r r2rZrrrrrrrs r)fileobjkeywordsroptionsrccsi}|dddD]}|}|s(qd|t|<qt|krFd|t<dtjttftttddd}||d d }t |d t j |d t j |d t j |dt j|dt j|dt j|dpt j|dpt j||dt j||dt jt j||dt jt|ddd}||dr&d |jd<||dr8d |_||dd} z"|| } t||| } Wn t k r|sYdSXt!| |} t"| |D] \} }}| ||| #| fVqdS)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,NF)rrdefaultrcSs||t|dkS)N>yes1trueon)getr lower)rrrrrrgetbool?szbabel_extract..getboolsilentTZblock_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_reloadrrrvencodingzutf-8)F)$rrrr rur3Mappingr rrrZBLOCK_START_STRINGZBLOCK_END_STRINGZVARIABLE_START_STRINGZVARIABLE_END_STRINGZCOMMENT_START_STRINGZCOMMENT_END_STRINGZLINE_STATEMENT_PREFIXZLINE_COMMENT_PREFIXZ TRIM_BLOCKSZ LSTRIP_BLOCKSZNEWLINE_SEQUENCEZKEEP_TRAILING_NEWLINErrrvreaddecoderElistlexr?rrrr)rrrrrZextension_namer r r0r<rrfinderrFrarrrr babel_extract sT%                r)JrPrretypingr3rZ markupsaferrrrr0r exceptionsrrZruntimer r r utilsr r TYPE_CHECKINGZtyping_extensionstelexerrrrCrProtocolrr"rWrr'rr rRcompilerr(rhr`rrir!rqrsrtrurrrrrrrrrrVrrBinaryIOrrZrZi18nrZ loopcontrolsZwith_rerrrrrs               m&*( K    *. O)0 ]