f|>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__r6setr7Setpriorityrr;rCOptionalrHUnionIterablerKrNodeListrOr&rSrTExprKeywordrZr[rrrr1r17s$ 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   rr1 __contextrUrVrcL|j|jdg|i|S)Nr)callresolve)rjrUrVs r_gettext_aliasrns* 9>>)++I6 H H HHrfuncc jtdtdtdtjdtffd }|S)Nrj__string variablesrct|j|}|jjr t|}||zSr)rleval_ctx autoescaper)rjrqrrrBros rrz"_make_new_gettext..gettexts7 ^^D( +    ( (BI~rrr r%r]Any)rors` r_make_new_gettextrxs<7c# Nrc tdtdtdtdtdtj dtf fd }|S)Nrj __singular__plural__numrrrc|jd||j|||}|jjr t |}||zS)Nnum setdefaultrlrtrur)rjrzr{r|rrrBros rr!z$_make_new_ngettext..ngettextsK UE* ^^D*h >    ( (BI~rrr r%r&r]rw)ror!s` r_make_new_ngettextrsX      UU    Orc vtdtdtdtdtjdtf fd }|S)Nrj __string_ctxrqrrrc|jd||j||}|jjr t |}||zS)Nr)r)rjrrqrrrBros rr+z$_make_new_pgettext..pgettextsK Y 5 ^^D, 9    ( (BI~rrv)ror+s` r_make_new_pgettextrsH  *- 9< KL55     Orctdtdtdtdtdtdtj dtffd }|S) Nrjrrzr{r|rrrc|jd||jd||j||||}|jjr t |}||zS)Nr)r~r)rjrrzr{r|rrrBros rr-z&_make_new_npgettext..npgettexts_ Y 5UE* ^^D, He L    ( (BI~rr)ror-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.transr9rNct||t|jd<|j |j |j |j|j|jdy)Nr.F)install_gettext_translationsinstall_null_translationsinstall_gettext_callablesuninstall_gettext_translationsextract_translationsnewstyle_gettext) superr;rnglobalsextend_install _install_null_install_callables _uninstall_extract)rr9r?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._installs{ , 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-prc|dk(r|S|S)Nrr)rrrrs rr-z>InternationalizationExtension._install_null..npgettext1sFq))r)rr!rr+r-) rNullTranslationshasattrr+r%r-r&rr!)rrrrr+r-s rrz+InternationalizationExtension._install_null s//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-)r9rrxrrrrrA)rrr!rr+r-s rrz0InternationalizationExtension._install_callables<s  08D   -    , ,'0G)(3H#-h7$/ :    ''hY ( rc^dD](}|jjj|d*yr)r9rpop)rrkeys rrz(InternationalizationExtension._uninstallTs)CC    $ $ ( (d 3DrrDgettext_functions.cpt|tr|jj|}t ||Sr) isinstancer%r9rOextract_from_ast)rrDrs rrz&InternationalizationExtension._extractXs2 fc "%%++F3F(9::rrLrc  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_endcommacolonrEztranslatable variable z defined twice.)excassign)trimmed notrimmedrload_transstorer~Trzname:pluralizezunknown variable z for pluralizationzpluralize without variablesext.i18n.trimmed)nextrIrPnext_ifvaluecurrenttypeexpectskip_iffailrparse_expressionrNamerrZAssignr` _parse_blockrAtestr9policies_trim_whitespace _make_nodebool set_lineno)rrLrPr) context_token plural_exprplural_expr_assignmentnum_called_numrrrtokenvarr have_plural referencedsingular_namesr plural_namesrEnodes rrOz#InternationalizationExtension.parsecsCfmm$++ --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_pluralizecRg}g} |jjjdk(rV|j|jjjj ddt |jn|jjjdk(r|t |j|jjdj}|j||jd|d|jjdn|jjjd k(rt |j|jjjdk(r |jjjnd }|d k(rn{|d k(r|rns|jd n|dk(r|jd|jd|dn3|jjr|jdn td|t|fS)z1Parse until the next block tag with a given name.data%%%variable_beginrEz%(z)s variable_end block_beginNendtrans pluralizez:a translatable section can have only one pluralize sectionrz6trans blocks can't be nested; did you mean `endtrans`?zBcontrol structures in translatable sections are not allowed; saw ``zunclosed translation blockzinternal parser error) rIrrappendrreplacerrreos RuntimeErrorr )rrLrrbufrE block_names rrz*InternationalizationExtension._parse_blocks }}$$))V3 6==0066>>sDIJV]]#&&++/??V]]#}}++F399!!$' RvR=) $$^4&&++}<V]]#}},,11V;MM))// +;.&KKT 7*KKP &ntj| } |rqtj| tj|jD cgc].\} } tj tj| | 0c} } } tj"| gScc} } w) z/Generates a useful node from the data provided.rrrNrrrrr~)r9rrrConstinsertrrZritemsrVrriMarkSafeIfAutoescapeModDictPairOutput)rrrr)rrrrrr 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#<= 0--d3DyyJJ/8oo.? *U"JJu{{3'7?||TF## s:3Gr)NNN)rrrN)%r"r#r$r\r7rr;r]rcrrrCallabler%r&rrr/rdrTemplateSequenceIteratorTuplerrfrgrOrPatternrrrrhrr __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! "-'^<$<$ 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. dorLrrctjt|jj}|j |_|S)NrR)rExprStmtrrIrP parse_tupler)rrLrs rrOzExprStmtExtension.parseUs4~~T&--%8%?%?@&&(  rN)r"r#r$r\r7rr rOrrrrrNs' 6DHrrcneZdZdZddhZdddej ejejffdZ y) LoopControlExtensionz/Adds break and continue to the template engine.breakcontinuerLrrct|j}|jdk(r tj|j Stj |j S)NrrR)rrIrrBreakrPContinue)rrLrs rrOzLoopControlExtension.parse`sBV]]# ;;' !;;ell3 3~~U\\22rN) r"r#r$r\r7r]rdrrrrOrrrrr[s89 Z D3H3enn1L)M3rrcJeZdZdZdhZdddej 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 debugrLrrc|jjdj}tj}|j d|g|}tj |g|S)Nz name:debug_renderrR)rIrrPrContextReferencer[r)rrLrPr)results rrOzDebugExtension.parse~sV%%l3::((*!!)gYv!F||VHV44rr)c|jt|jjj t|jj j d}t j|ddS)N)r)filterstestsT)depthcompact)get_allsortedr9rkeysrpprintpformat)rr)rs rrzDebugExtension._rendersb(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)r5r6offset last_lineno)rr5r6s rr;z_CommentFinder.__init__s" ( rr8c( t|j|j|D]I\}}}|dvs |jdd\}}||j vs1|j gc||_Sg||_S#t$rY`wxYw#||_wxYw)N)comment linecommentr)reversedr5r8split ValueErrorr6rstrip)rr8r. token_type token_valueprefixr;s rfind_backwardsz_CommentFinder.find_backwardss !.6 DKK&1/*:{!;;!*5*;*;D!*D!2!22 ' 011!DK/ DK &! ! !DKs:-BA9BB/B9 BBBB BrPc*|jr|j|kDrgSt|j|jdD].\}\}}}||kDs|j |j|zcS|j t |jSr)r6r9 enumerater5r8rDr/)rrPidx token_linenor.s r find_commentsz_CommentFinder.find_commentss  D$4$4v$=I)24;;t{{}3M)N %C%,1f$**4;;+<==*O""3t{{#344r) r"r#r$r\r]rrr&r%r;rgrDrIrrrr4r4s{ jjc3!78HI SV !S!QVVC[! 5C5AFF3K5rr4fileobjkeywordsr6optionsc#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,NFrLrdefaultrcX|j|t|jdvS)N>1onyestrue)getr%lower)rLrrQs rgetboolzbabel_extract..getbool8s&{{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)$rWr>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.rrreaddecoderOlistlexrHr r4rrI)rJrKr6rLrNextension_namerYrZr9rDrr5finderrPrors r babel_extractr|sJ35J!++lB7==cB'--/ 48 =01 C%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. /ZZS2U$IU$p   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