P}UdZddlZddlZddlZddlmZddlmZddlm Z ddl m Z ddl m Z dd l mZdd lmZdd lmZdd lmZdd lmZddlmZej,rGddlZddlmZddlmZddlmZGddej<ZGddeZ ejBee fZ"dZ#ejHe%dfe&d<ejNdZ(GddZ)ededejTdejTdejBejTeffd Z+d!ejXe%ge%fdejXde%ffd"Z-d!ejXe%e%e.ge%fdejXde%ffd#Z/d!ejXe%e%ge%fdejXde%ffd$Z0d!ejXe%e%e%e.ge%fdejXde%ffd%Z1Gd&d'e)Z2Gd(d)e)Z3Gd*d+e)Z4Gd,d-e)Z5e#d.fd/e jld0ejne%d1e8dejrejHe.e%ejBejte%ejHejte%dffffd2Z;Gd3d4ZFzz IId6I *       r)rNr)NNNNN) r#r$r%__doc__tClassVarr&__annotations__r7setr8Setpriorityrr<rDOptionalrIUnionIterablerLrNodeListrPr'rTrUExprKeywordr[r\rrrr2r26s$ 3<uD!%%*H'K'D'  OS!"C<=JJsO #  7 33 4$H$QVVEJJ=O1O)P$48 N N!"C N  ! ! N0448+/-1"&  jj +,  166%--01  **UZZ(  JJuzz*   3   rr2 __contextrVrWrcL|j|jdg|i|S)Nr)callresolve)rkrVrWs r_gettext_aliasros* 9>>)++I6 H H HHrfuncc jtdtdtdtjdtffd }|S)Nrk__string variablesrct|j|}|jjr t|}||zSr)rmeval_ctx autoescaper)rkrrrsrCrps rrz"_make_new_gettext..gettexts7 ^^D( +    ( (BI~rrr r&r^Any)rprs` r_make_new_gettextrys<7c# Nrc tdtdtdtdtdtj dtf fd }|S)Nrk __singular__plural__numrsrc|jd||j|||}|jjr t |}||zS)Nnum setdefaultrmrurvr)rkr{r|r}rsrCrps rr"z$_make_new_ngettext..ngettextsK UE* ^^D*h >    ( (BI~rrr r&r'r^rx)rpr"s` r_make_new_ngettextrsX      UU    Orc vtdtdtdtdtjdtf fd }|S)Nrk __string_ctxrrrsrc|jd||j||}|jjr t |}||zS)Nr*r)rkrrrrsrCrps rr,z$_make_new_pgettext..pgettextsK Y 5 ^^D, 9    ( (BI~rrw)rpr,s` r_make_new_pgettextrsH  *- 9< KL55     Orctdtdtdtdtdtdtj dtffd }|S) Nrkrr{r|r}rsrc|jd||jd||j||||}|jjr t |}||zS)Nr*rr)rkrr{r|r}rsrCrps rr.z&_make_new_npgettext..npgettexts_ Y 5UE* ^^D, He L    ( (BI~rr)rpr.s` r_make_new_npgettextrse    UU  $ rcfeZdZdZdhZdeddffd Z d&ddd eje ddfd Z d&d eje ddfd Z d'd eje ge fd eje e ege fd eje dejeje e ge fdejeje e e ege fddf dZd(dZefdej$e ej(fdej*e dej,ej.ee ej$eje ej.eje dffffdZdddej$ej2ej4ej2ffdZefde dej:e de fdZddde dej.ej4e e ffdZde deje d eje d!ej@e ejBfd"ejejBd#e d$e dejDfd%Z#xZ$S))InternationalizationExtensionz-This extension adds gettext support to Jinja.transr:rNct||t|jd<|j |j |j |j|j|jdy)Nr/F)install_gettext_translationsinstall_null_translationsinstall_gettext_callablesuninstall_gettext_translationsextract_translationsnewstyle_gettext) superr<roglobalsextend_install _install_null_install_callables _uninstall_extract)rr:r@s rr<z&InternationalizationExtension.__init__s] %#1 C )-&*&8&8&*&=&=+/??!%"  r translations_SupportedTranslationsnewstylect|dd}| |j}t|dd}| |j}t|dd}t|dd}|j|||||y)Nugettext ungettextr,r.)rr,r.)getattrrr"r)rrrrr"r,r.s rrz&InternationalizationExtension._install s{ , D9 ?"**G<d;  #,,H<T:L+t<   X8y  rc Tddl}|j}t|dr |j}ndtdtdtfd}t|dr |j }n"dtdtdtd t dtf d }|j|j|j||| y) Nrr,csrc|Srr)rrs rr,z=InternationalizationExtension._install_null..pgettext)srr.pr c|dk(r|S|S)Nrr)rrrr s rr.z>InternationalizationExtension._install_null..npgettext0sFq))r)rr"rr,r.) rNullTranslationshasattrr,r&r.r'rr")rrrrr,r.s rrz+InternationalizationExtension._install_nulls//1 < ,#,,H C C C  < -$..I *S *S *S *S *S *  ((!**  rrr"r,r.c|||j_|jjr0t|}t|}| t |}| t |}|jj j||||yN)rr"r,r.)r:rryrrrrrB)rrr"rr,r.s rrz0InternationalizationExtension._install_callables;s  08D   -    , ,'0G)(3H#-h7$/ :    ''hY ( rc^dD](}|jjj|d*yr)r:rpop)rrkeys rrz(InternationalizationExtension._uninstallSs-C 4C    $ $ ( (d 3 4rrEgettext_functions.cpt|tr|jj|}t ||Sr) isinstancer&r:rPextract_from_ast)rrErs rrz&InternationalizationExtension._extractWs2 fc "%%++F3F(9::rrMrc  t|jj}d}|jjd}| |j}d}d}d}i}d} |jj j dk7r|r|jjd|jjdrn|jjd} | j|vr0|jd| jd | jt |jj j d k(r5t|j|jx|| j<} nP| | jd vr| jd k(} 0tj| jdx|| j<} |{t| tjrPtjdd}||| j<tj tjdd| }n| }| jdk(}|jj j dk7r|jjdd} d} t#}|j%|d\}}|r4|j'||!tj|dd}|ddk(}|jj j)drd} t|j|jj j dk7rw|jjd} | j|vr0|jd| jd| jt || j}| jdk(}|jjd|j%|d\}} t|j|j'|nt|j|D] }||vstj|d||<"| sd}n||jd|| |j*j,d} | r$|j/|}| r|j/| } |j1|| |||t3||xr| }|j5||||gS|S)zParse a translatable tag.NstringF block_endcommacolonrFztranslatable variable z defined twice.)excassign)trimmed notrimmedrload_transstorerTrname:pluralizezunknown variable z for pluralizationzpluralize without variablesext.i18n.trimmed)nextrJrQnext_ifvaluecurrenttypeexpectskip_iffailrparse_expressionrNamerr[Assignra _parse_blockrBtestr:policies_trim_whitespace _make_nodebool set_lineno)rrMrQr* context_token plural_exprplural_expr_assignmentnum_called_numrsrtokenvarr have_plural referencedsingular_namesr plural_namesrFnodes rrPz#InternationalizationExtension.parsebsHfmm$++ --h7  $#))G /3 ;?-/ mm##((K7 $$W-}}$$W-MM((0E{{i' ,U[[O?KLL.}}$$))X5V]]#/5/F/F/HH %++&U[[4L%L++2/4zz%++v/NN %++&"c5::."'**Xv">K-8Iekk*-2\\ 8W5s.*#&K!&!5Gmm##((K7J  [) U $(#4#4VT#B     n -"#jj):FC !/!2e!; == % %&6 7K  }}$$))[8 ,,V4;;i/KK+EKK?:LM 2  ( 4 !&!5 MM  -#'#4#4VU#C L&     l +   ;D9$"'**T6": $ ;K  KK5v > ?&&//0BCG ,,X6H..v6         *{   ! -*D1 1Krr_ws_recB|jd|jS)N )substrip)rrrs rrz.InternationalizationExtension._trim_whitespaceszz#v||~..rallow_pluralizecg}g} |jjjdk(rV|j|jjjj ddt |jn{|jjjdk(r|t |j|jjdj}|j||jd|d|jjdn|jjjd k(rt |j|jjjd rn|jjjd r|rnX|jd |jd n3|jjr|jdn td|t|fS)z1Parse until the next block tag with a given name.data%%%variable_beginrFz%(z)s variable_end 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) rJrrappendrreplacerrrreos RuntimeErrorr )rrMrrbufrFs rrz*InternationalizationExtension._parse_blocks }}$$))V3 6==0066>>sDIJV]]#&&++/??V]]#}}++F399!!$' RvR=) $$^4&&++}<V]]#==((--o>]]**//0@A&KKT Q"" 89"#:;;586#;&&rrrr*rsrvars_referencedrc|jj}|s(|s&|jdd}|r|jdd}d} tj|g} |*| j dtj|d| } |+d| } | j tj||ftjtj| d| gdd} |rP|jD]<\} } |r| d k(r| jjtj| | >ntj| } |rqtj| tj|jD cgc].\} } tj tj| | 0c} } } tj"| gScc} } w) z/Generates a useful node from the data provided.rrrNrrr rr)r:rrrConstinsertrr[ritemsrWrrjMarkSafeIfAutoescapeModDictPairOutput)rrrr*rsrrrr func_name func_argsrrrs rrz(InternationalizationExtension._make_nodes##44 x''c2Hc2 ).X)>(?     Q G 4 5I;I  "I;I   ekk&1;? @zz%**Y7BdS 'oo/ > U"cUl ""5==e#<=  >--d3DyyJJ/8oo.? *U"JJu{{3'7?||TF## s:3Gr)NNN)rrrN)%r#r$r%r]r8rr<r^rdrrrCallabler&r'rrr0rerTemplateSequenceIteratorTuplerrgrhrPrPatternrrrrirr __classcell__)r@s@rrrs7 9D  K  D  TX 4 @A 4@P  $ ajj&6 $ @&*<@GK  SE3J' **c3_c12 **T"  **QZZc C89  ::ajj#sC)=s)BCD    04.? ;U^^+, ;::c? ;  S!''!**S/1771::c?C;O3P"PQQR  ;xHxQVVEJJ=O1O)PxtFL/s/AIIcN/PS/#'#'15#' c! "#'J<$<$ 3<$C <$ 66#uzz/* <$ ZZ + <$<$<$ <$rrc:eZdZdZdhZdddej fdZy)ExprStmtExtensionzrAdds a `do` tag to Jinja that works like the print statement just that it doesn't print the return value. dorMrrctjt|jj}|j |_|S)NrS)rExprStmtrrJrQ parse_tupler)rrMrs rrPzExprStmtExtension.parseJs4~~T&--%8%?%?@&&(  rN)r#r$r%r]r8rr rPrrrrrCs' 6DHrrcneZdZdZddhZdddej ejejffdZ y) LoopControlExtensionz/Adds break and continue to the template engine.breakcontinuerMrrct|j}|jdk(r tj|j Stj |j S)Nr rS)rrJrrBreakrQContinue)rrMrs rrPzLoopControlExtension.parseUsBV]]# ;;' !;;ell3 3~~U\\22rN) r#r$r%r]r8r^rerrrrPrrrr r Ps89 Z D3H3enn1L)M3rr cJeZdZdZdhZdddej fdZdede fdZ y ) 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 debugrMrrc|jjdj}tj}|j d|g|}tj |g|S)Nz name:debug_renderrS)rJrrQrContextReferencer\r)rrMrQr*results rrPzDebugExtension.parsessV%%l3::((*!!)gYv!F||VHV44rr*c|jt|jjj t|jj j d}t j|ddS)N)r*filterstestsT)depthcompact)get_allsortedr:rkeysrpprintpformat)rr*rs rrzDebugExtension._renderysb(d..66;;=>D,,22779: ~~fAt<>> 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. Nc3&K|] }|| ywrr).0xs r z#extract_from_ast..s>3..C!/sFFceZdZdZdej ej eeefdej eddfdZ dedejefdZ d edejefd Z y) _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. tokens comment_tagsrNc<||_||_d|_d|_y)Nr)r3r4offset last_lineno)rr3r4s rr<z_CommentFinder.__init__s" ( rr6c( t|j|j|D]I\}}}|dvs |jdd\}}||j vs1|j gc||_Sg||_S#t$rY`wxYw#||_wxYw)N)comment linecommentr)reversedr3r6split ValueErrorr4rstrip)rr6r/ token_type token_valueprefixr9s rfind_backwardsz_CommentFinder.find_backwardss !.6 DKK&1/ 2*:{!;;!*5*;*;D!*D!2!22 ' 011!DK 2 DK &! ! !DKs:-BA9BB/B9 BBBB BrQc*|jr|j|kDrgSt|j|jdD].\}\}}}||kDs|j |j|zcS|j t |jSr)r4r7 enumerater3r6rBr-)rrQidx token_linenor/s r find_commentsz_CommentFinder.find_commentss  D$4$4v$=I)24;;t{{}3M)N > %C%,1f$**4;;+<== >""3t{{#344r) r#r$r%r]r^rrr'r&r<rhrBrGrrrr2r2s{ jjc3!78HI SV !S!QVVC[! 5C5AFF3K5rr2fileobjkeywordsr4optionsc#Ki}|jddjdD]#}|j}|sd|t|<%t|vr d|t<ddt j ttfdtdtd tfd }||d d }t|jd tj|jdtj|jdtj|jdtj|jdtj|jdtj |jdxstj"|jdxstj$||dtj&||dtj(tj*||dtj,t/|dd}||drd |j0d<||drd |_|j5j7|jdd} |j9| } t;|j=|j?| } tC| |} tE| |D]\} }}| ||| jG| fy#t@$r|sYywxYww) 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,NFrJrdefaultrcX|j|t|jdvS)N>1onyestrue)getr&lower)rJrrOs rgetboolzbabel_extract..getbool-s&{{3G -3359SSSrsilentTblock_start_stringblock_end_stringvariable_start_stringvariable_end_stringcomment_start_stringcomment_end_stringline_statement_prefixline_comment_prefix trim_blocks lstrip_blockskeep_trailing_newliner) cache_size auto_reloadrrrencodingzutf-8)F)$rUr<rr rr^Mappingr&rrrBLOCK_START_STRINGBLOCK_END_STRINGVARIABLE_START_STRINGVARIABLE_END_STRINGCOMMENT_START_STRINGCOMMENT_END_STRINGLINE_STATEMENT_PREFIXLINE_COMMENT_PREFIX TRIM_BLOCKS LSTRIP_BLOCKSNEWLINE_SEQUENCEKEEP_TRAILING_NEWLINEr,rrreaddecoderPlistlexrIr r2rrG)rHrIr4rJrLextension_namerWrXr:rErr3finderrQrprs r babel_extractrzsJ35J!++lB7==cB9'--/ 48 =01 9%J648 01T38,T3TTRVTWh -F ((*E*EF &(A(AB +X-K-KL )8+G+GH *H,I,IJ ((*E*EF +,N0N0N )*Jh.J.J(<(<=(*@*@A!!0(2P2PQ jK$w "37 /0w*+'+ $ \\^ " "7;;z7#C DF  (kook&<&rsr8 $.+ ??""R[[1WW%79M%MN (17738$ K j j ZII uuI01IWWQUUI II AJJucz2 qzz#s(7K QZZc3(<=!**SRUXBV&QZZc C8QZZS=Q" **c3S)3. /ZZS2K$IK$\   39 3%=Y%=T*;L/ L/zz#L/L/ZZGGCaggajjoqwwqzz#7K/LLM MN L/^%5%5PYB ZZYBjjoYB**S/YBVVCJ  YB ZZGG S!''!**S/1771::c?C3G+HHI166RU;V YBz%# r