o ^a@s8dZddlZddlZddlZddlmZddlmZddl m Z ddl m Z ddl m Z dd l mZdd l mZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZedZejdej dej!fdZ"ej#rddl$Z$ddl%Z&ddl'm(Z(Gddde&j)Z*gdZ+gdZ,ded efd!d"Z-d#ej.ej!d e/fd$d%Z0d#ej.ej!d e/fd&d'Z1d#ej.ej!d e/fd(d)Z2  *  dQd+d,d-ej3e/d.ej4e/ej d/gej5e/ffd0ej3ej4e/ej!fd1e6d2ej3ej7e/ej!fd3ej3ej8e/ej!fd d/fd4d5Z9Gd6d7d7Z:d8e"d e"fd9d:Z;ej8jGd>d?d?Z?Gd@dAdAe?Z@GdBdCdCZAGdDdEdEZBdeBfdFej3dGdHejCeBd ejCeBfdIdJZDGdKdLdLeBZEGdMdNdNeBZFGdOdPdPeBZGeB`HeE`HeF`HeG`HdS)Rz;The runtime functions and state used by compiled templates.N)abc)chain)escape)Markup)soft_str auto_aiter) auto_await)TemplateNotFound)TemplateRuntimeError)UndefinedError) EvalContext)_PassArg)concat) internalcode)missing) Namespace)object_type_repr)pass_eval_contextVF.)bound) Environmentc @s0eZdZ d dejedddedefddZdS) LoopRenderFuncrreciterloop_render_funcdepthreturncCsdSN)selfrrrr r 0/usr/lib/python3/dist-packages/jinja2/runtime.py__call__#szLoopRenderFunc.__call__N)r) __name__ __module__ __qualname__tIterablerintstrr#r r r r"r"sr) LoopContextTemplateReferenceMacrorr rrr markup_joinstr_joinidentityr r Undefinedr)AsyncLoopContextr r xrcC|S)zLReturns its argument. Useful for certain things in the environment. r )r3r r r"r0Esr0seqcCsLg}tt|}|D]}||t|dr!tdt||Sq t|S)z?Concatenation that escapes if necessary and converts to string.__html__)maprappendhasattrrjoinrr)r5bufiteratorargr r r"r.Ls   r.cCsttt|S)z3Simple args to string conversion and concatenation.)rr8r*)r5r r r"r/Wsr/cCs ddl}|jdtddt|S)NrzWThis template must be recompiled with at least Jinja 3.0, or it will fail in Jinja 3.1. stacklevel)warningswarnDeprecationWarningr/)r5rBr r r" unicode_join\srEF environmentr template_nameblocksContextvarssharedglobalslocalsc Csp|duri}|r |}n t|pdfi|}|r.|rt|}|D] \}} | tur-| ||<q!|j|||||dS)z%Internal helper for context creation.Nr )rL)dictitemsr context_class) rFrGrHrJrKrLrMparentkeyvaluer r r" new_contexths  rTc@s<eZdZdZd ddZdedejfd d Zdefd d Z dS)r,zThe `self` in templates.contextrIrNcCs ||_dSr)_TemplateReference__context)r!rUr r r"__init__s zTemplateReference.__init__namecCs|jj|}t||j|dSNr)rVrHBlockReference)r!rXrHr r r" __getitem__s zTemplateReference.__getitem__cCsdt|jd|jjdS)N< >)typer$rVrXr!r r r"__repr__szTemplateReference.__repr__)rUrIrN) r$r%r&__doc__rWr*r'Anyr[rar r r r"r,s  r, dict_methodcs.tdddtjffdd }tt|S)Nr!rIrcs |Sr)get_allr`rdr r"f_alls z_dict_method_all..f_all) functoolswrapsr'rccastr)rdrgr rfr"_dict_method_alls rkc@seZdZUdZdZejeed<d/ddZ d0dd d ej e ej fd ej e d ej e ejdgeje ffd ej eje ej ff ddZd e dejdgeje fdejdfddZd0de dej dej fddZde dejej dffddZde dej fddZdej e ej ffddZdej e ej ffdd Zed!ejd"ej d#ej dejej dffd$d%Zd0d&ej ej e ej fddfd'd(ZeejZeejZeej Z d e defd)d*Z!de dej fd+d,Z"de fd-d.Z#dS)1rIaThe template context holds the variables of a template. It stores the values passed to the template and also the names the template exports. Creating instances is neither supported nor useful as it's created automatically at various stages of the template evaluation and should not be created by hand. The context is immutable. Modifications on :attr:`parent` **must not** happen and modifications on :attr:`vars` are allowed from generated template code only. Template filters and global functions marked as :func:`pass_context` get the active context passed as first argument and are allowed to access the context read-only. The template context supports read only dict operations (`get`, `keys`, `values`, `items`, `iterkeys`, `itervalues`, `iteritems`, `__getitem__`, `__contains__`). Additionally there is a :meth:`resolve` method that doesn't fail with a `KeyError` but returns an :class:`Undefined` object for missing variables. F_legacy_resolve_moderNcCsJd|jvr d|_dSd|jvs|jr#ddl}|jdtddd|_dSdS) Nresolve_or_missingFresolverzOverriding 'resolve' is deprecated and will not have the expected behavior in Jinja 3.1. Override 'resolve_or_missing' instead r?r@T)__dict__rlrBrCrD)clsrBr r r"__init_subclass__s   zContext.__init_subclass__rFrrQrXrHrLcCs^||_i|_||_t|j||_t|_||_|durtnt||_dd| D|_ dS)NcSsi|]\}}||gqSr r .0kvr r r" z$Context.__init__..) rQrJrFreval_ctxset exported_varsrX globals_keysrOrH)r!rFrQrXrHrLr r r"rWszContext.__init__currentrZr1cCs^z|j|}||d}||Wnty'|jjd|dddYSwt||||S)zRender a parent block.r there is no parent block called .superrX)rHindex LookupErrorrF undefinedrZ)r!rXr|rHrr r r"rs     z Context.superrRdefaultcCs"z||WSty|YSw)zLook up a variable by name, or return a default if the key is not found. :param key: The variable name to look up. :param default: The value to return if the key is not found. )KeyError)r!rRrr r r"gets   z Context.getr1cCs`|jr||jvr |j|S||jvr|j|S|jj|dS||}|tur.|jj|dS|S)aNLook up a variable by name, or return an :class:`Undefined` object if the key is not found. If you need to add custom behavior, override :meth:`resolve_or_missing`, not this method. The various lookup functions use that method, not this one. :param key: The variable name to look up. r)rlrJrQrFrrmrr!rRrvr r r"rns     zContext.resolvecCsN|jr||}t|trtS|S||jvr|j|S||jvr%|j|StS)aNLook up a variable by name, or return a ``missing`` sentinel if the key is not found. Override this method to add custom lookup behavior. :meth:`resolve`, :meth:`get`, and :meth:`__getitem__` use this method. Don't call this method directly. :param key: The variable name to look up. )rlrn isinstancer1rrJrQrr r r"rms      zContext.resolve_or_missingcsfddjDS)z+Get a new dict with the exported variables.csi|]}|j|qSr )rJ)rsrtr`r r"rv*rwz(Context.get_exported..)rzr`r r`r" get_exported(szContext.get_exportedcCs,|js|jS|js |jSt|jfi|jS)zReturn the complete context as dict including the exported variables. For optimizations reasons this might not return an actual copy so be careful with using it. )rJrQrNr`r r r"re,s zContext.get_all _Context__objargskwargscOs d}t|drt|jdur|j}t|}|tjur;|dr)||d}|dr5||d}|f|}n|tjurG|jf|}n |tj urR|j f|}| dd| ddz||i|WSt yu|j dYSw)zCall the callable with the arguments and keyword arguments provided but inject the active context or environment as first argument if the callable has :func:`pass_context` or :func:`pass_environment`. Tr#N _loop_vars _block_varszGvalue was undefined because a callable raised a StopIteration exception) r:rfrom_objr#rUrderived eval_contextrxrFpop StopIterationr)_Context__selfrrr__traceback_hide__pass_argr r r"call7s4            z Context.callrMcCsDt|j|ji|dd|}|j|_|jdd|jD|S)zInternal helper function to create a derived context. This is used in situations where the system needs a new context in the same template that is independent. TNcss |] \}}|t|fVqdSr)listrrr r r" msz"Context.derived..)rTrFrXrerxrHupdaterO)r!rMrUr r r"rds zContext.derivedcCs||jvp ||jvSr)rJrQr!rXr r r" __contains__tszContext.__contains__cCs||}|tur t||S)zpLook up a variable by name with ``[]`` syntax, or raise a ``KeyError`` if the key is not found. )rmrr)r!rRitemr r r"r[ws zContext.__getitem__cCs$dt|jd|d|jdS)Nr\r]z of r^)r_r$rerXr`r r r"ras$zContext.__repr__)rNr)$r$r%r&rbrlr'ClassVarbool__annotations__rqDictr*rcOptionalCallableIteratorMutableMappingrWUnionrrrnrmrrerrrrkrNkeysvaluesrOrr[rar r r r"rIsZ       &,   c @seZdZdZdedddejejdgejefde ddf d d Z e dej d fd d Z edefddZedefddZdS)rZz"One block on a template reference.rXrUrIstackrrNcC||_||_||_||_dSr)rX_context_stack_depth)r!rXrUrrr r r"rW zBlockReference.__init__r}cCsJ|jdt|jkr|jjjd|jdddSt|j|j|j|jdS)zSuper the block.rr~rrr)rlenrrrFrrXrZr`r r r"rs zBlockReference.supercs>tdd|j|j|j2IdH}|jjjrt|S|S)Ncg|z3dHW}|q6Srr rsr3r r r" z.BlockReference._async_call..)rrrrrx autoescaperr!rr r r" _async_calls  zBlockReference._async_callcCs>|jjjr |St|j|j|j}|jjjrt |S|Sr) rrFis_asyncrrrrrxrrrr r r"r#s  zBlockReference.__call__)r$r%r&rbr*r'Listrrr)rWpropertyrrrrr#r r r r"rZs&   rZc @seZdZUdZdZdZejee d<e Z ej e d<e Z ej e d<e Zej e d<e Zej e d<  d9d ejed ejd d ejddeddf ddZed ejedejefddZedefddZdefddZedefddZedefddZedefddZedefdd Zedefd!d"Zdej fd#d$Z edefd%d&Z!edej"ej d ffd'd(Z#edej"ej d ffd)d*Z$d+edefd,d-Z%d.ej defd/d0Z&d:d1d2Z'dej(ej dffd3d4Z)e*d ejede+fd5d6Z,de+fd7d8Z-dS);r+zeA wrapper iterable for dynamic ``for`` loops, with information about the loop and iteration. N_length_after_current_before_last_changed_valueriterablerr1recurserdepth0rcCs(||_|||_||_||_||_dS)a@ :param iterable: Iterable to wrap. :param undefined: :class:`Undefined` class to use for next and previous items. :param recurse: The function to render the loop body when the loop is marked recursive. :param depth0: Incremented when looping recursively. N) _iterable _to_iterator _iterator _undefined_recurser)r!rrrrr r r"rWs   zLoopContext.__init__cCt|Sr)iterrr r r"rszLoopContext._to_iteratorcCsl|jdur|jSz t|j|_W|jSty5t|j}|||_t||j|jt u|_Y|jSw)zLength of the iterable. If the iterable is a generator or otherwise does not have a size, it is eagerly evaluated to get a size. N) rrr TypeErrorrrrrrrr!rr r r"lengths   zLoopContext.lengthcCs|jSr)rr`r r r"__len__szLoopContext.__len__cC |jdS)zBHow many levels deep a recursive loop currently is, starting at 1.r)rr`r r r"r zLoopContext.depthcCr)z-Current iteration of the loop, starting at 1.rindex0r`r r r"rrzLoopContext.indexcC |j|jS)zrNumber of iterations from the end of the loop, ending at 0. Requires calculating :attr:`length`. rrr`r r r" revindex0 zLoopContext.revindex0cCr)zrNumber of iterations from the end of the loop, ending at 1. Requires calculating :attr:`length`. rrr`r r r"revindexrzLoopContext.revindexcCs |jdkS)z0Whether this is the first iteration of the loop.rrr`r r r"firstrzLoopContext.firstcCs$|jtur|jSt|jt|_|jS)a Return the next element in the iterable, or :data:`missing` if the iterable is exhausted. Only peeks one item ahead, caching the result in :attr:`_last` for use in subsequent checks. The cache is reset when :meth:`__next__` is called. )rrnextrr`r r r" _peek_nexts zLoopContext._peek_nextcCs |tuS)zWhether this is the last iteration of the loop. Causes the iterable to advance early. See :func:`itertools.groupby` for issues this can cause. The :func:`groupby` filter avoids that issue. rrr`r r r"lasts zLoopContext.lastcCs|jr|dS|jS)zZThe item in the previous iteration. Undefined during the first iteration. zthere is no previous item)rrrr`r r r"previtem)s zLoopContext.previtemcCs|}|tur |dS|S)a The item in the next iteration. Undefined during the last iteration. Causes the iterable to advance early. See :func:`itertools.groupby` for issues this can cause. The :func:`jinja-filters.groupby` filter avoids that issue. there is no next itemrrrrr r r"nextitem3s  zLoopContext.nextitemrcGs|std||jt|S)zReturn a value from the given args, cycling through based on the current :attr:`index0`. :param args: One or more values to cycle through. zno items for cycling given)rrr)r!rr r r"cycleCszLoopContext.cyclerScGs|j|kr ||_dSdS)zReturn ``True`` if previously called with a different value (including when called for the first time). :param value: One or more values to compare to the last call. TF)r)r!rSr r r"changedNs zLoopContext.changedcCr4rr r`r r r"__iter__ZzLoopContext.__iter__cCsF|jtur |j}t|_nt|j}|jd7_|j|_||_||fSNr)rrrrrrrrr r r"__next__]s  zLoopContext.__next__cCs&|jdur td|j||j|jdS)zWhen iterating over nested data, render the body of the loop recursively with the given inner iterable data. The loop must have the ``recursive`` marker for this to work. NzCThe loop must have the 'recursive' marker to be called recursively.)r)rrrrr r r"r#is zLoopContext.__call__cCs"dt|jd|jd|jdS)Nr\r]/r^)r_r$rrr`r r r"raws"zLoopContext.__repr__rY)rr+).r$r%r&rbrrr'rr)rrrrcrrrr(rTyperW staticmethodrrrrrrrrrrrrrrrrrrrTuplerrr*r#rar r r r"r+sd         r+c@seZdZUejejed<edejej e ej e fdeje fddZ e defddZe defdd Ze defd d Zdejfd d Ze defddZe dejejdffddZdddZdejejdffddZdS)r2rrrcCrrrrr r r"r~szAsyncLoopContext._to_iteratorcsz|jdur |jSz t|j|_W|jSty<dd|j2IdH}|||_t||j|jtu|_Y|jSw)Ncrrr rr r r"rrz+AsyncLoopContext.length..) rrrrrrrrrrr r r"rs   zAsyncLoopContext.lengthc|jIdH|jSrrr`r r r"rzAsyncLoopContext.revindex0crrrr`r r r"rrzAsyncLoopContext.revindexcsJ|jtur |jSz |jIdH|_W|jSty$t|_Y|jSwr)rrr __anext__StopAsyncIterationr`r r r"rs  zAsyncLoopContext._peek_nextcs|IdHtuSrrr`r r r"rrzAsyncLoopContext.lastr1cs&|IdH}|tur|dS|S)Nrrrr r r"rs  zAsyncLoopContext.nextitemcCr4rr r`r r r" __aiter__rzAsyncLoopContext.__aiter__csN|jtur |j}t|_n|jIdH}|jd7_|j|_||_||fSr)rrrrrrrrr r r"rs zAsyncLoopContext.__anext__N)rr2)r$r%r&r' AsyncIteratorrcrrrr(r AsyncIterablerrr)rrrrrrrrrrr r r r"r2{s*     r2c@seZdZdZ ddddejdefdedejed ed ed ed ej efd dZ e e dej dej defddZdejej dedefddZdejej dedefddZdefddZdS)r-zWraps a macro function.NrFrfunc.rX arguments catch_kwargs catch_varargscallerdefault_autoescapec Csl||_||_t||_||_||_||_||_||_d|v|_ |dur1t |j r.| d}n|j }||_ dS)Nr) _environment_funcr_argument_countrXrrrrexplicit_callercallabler_default_autoescape) r!rFrrXrrrrrr r r"rWs      zMacro.__init__rrrc Os|rt|dtr|dj}|dd}n|j}t|d|j}t|}d}||jkrW|jt|dD] }z||}Wn t yIt }Ynw|dkrPd}| |q5n|j }|j rv|sv|dd} | durq|jjddd} | | |jr| |n|rd|vrtd|jd td|jd tt||jr| ||jdnt||jkrtd|jd t|jd |||S) NrrFrTzNo caller definedrzmacro zX was invoked with two values for the special caller argument. This is most likely a bug.z takes no keyword argument z takes not more than z argument(s))rrrrrrrrrrrr9rrrrrrrXrrr_invoke) r!rrrroff found_callerrXrSrr r r"r#sV           zMacro.__call__rcs"|j|IdH}|rt|S|Sr)rrr!rrrr r r" _async_invoke0s zMacro._async_invokecCs.|jjr |||S|j|}|rt|}|Sr)rrrrrrr r r"r8s   z Macro._invokecCs0|jdurdnt|j}dt|jd|dS)N anonymousr\r]r^)rXreprr_r$rr r r"raCszMacro.__repr__r)r$r%r&rbr'rr*rrrrWrrrcr#rrrar r r r"r-s4   L r-c @seZdZdZdZdedefdeje dej deje dej e ddf d d Z ede fd d Zed ej dej ddfddZede dej fddZeZZZZeZZZZeZZeZZeZZ eZ!Z"eZ#Z$eZ%Z&Z'Z(eZ)Z*Z+eZ,Z-dej de.fddZ/dej de.fddZ0de1fddZ2de fddZ3de1fddZ4dej5ej fdd Z6dej7ej fd!d"Z8de.fd#d$Z9de fd%d&Z:dS)'r1ahThe default undefined type. This undefined type can be printed and iterated over, but every other access will raise an :exc:`UndefinedError`: >>> foo = Undefined(name='foo') >>> str(foo) '' >>> not foo True >>> foo + 42 Traceback (most recent call last): ... jinja2.exceptions.UndefinedError: 'foo' is undefined _undefined_hint_undefined_obj_undefined_name_undefined_exceptionNhintobjrXexcrcCrrr)r!r rrXrr r r"rW^rzUndefined.__init__cCsZ|jr|jS|jtur|jdSt|jts"t|jd|jSt|jd|jS)zXBuild a message about the undefined value based on how it was accessed. z is undefinedz has no element z has no attribute )r r rr rr*rr`r r r"_undefined_messagejs     zUndefined._undefined_messagerr te.NoReturncOs ||j)zeRaise an :exc:`UndefinedError` when operations are performed on the undefined value. )r r)r!rrr r r"_fail_with_undefined_errors z$Undefined._fail_with_undefined_errorcCs |dddkr t||S)Nr?__)AttributeErrorrrr r r" __getattr__szUndefined.__getattr__othercCst|t|uSr)r_r!rr r r"__eq__szUndefined.__eq__cCs || Sr)rrr r r"__ne__ zUndefined.__ne__cCs tt|Sr)idr_r`r r r"__hash__rzUndefined.__hash__cCdS)Nr7r r`r r r"__str__rzUndefined.__str__cCrrYr r`r r r"rrzUndefined.__len__ccsdEdHdSNr r r`r r r"rszUndefined.__iter__cCsdD]}dVqdSrr r!_r r r"rszUndefined.__aiter__cCr)NFr r`r r r"__bool__rzUndefined.__bool__cCr)Nr1r r`r r r"rarzUndefined.__repr__);r$r%r&rb __slots__rr r'rr*rcrr rWrrrrr__add____radd____sub____rsub____mul____rmul____div____rdiv__ __truediv__ __rtruediv__ __floordiv__ __rfloordiv____mod____rmod____pos____neg__r#r[__lt____le____gt____ge____int__ __float__ __complex____pow____rpow__rrrr)rrrrrrrr"rar r r r"r1Hsb     r1loggerzlogging.Loggerbasecs\durddl}|t|tjdtddffdd Gfddd|}|S) akGiven a logger object this returns a new undefined class that will log certain failures. It will log iterations and printing. If no logger is given a default logger is created. Example:: logger = logging.getLogger(__name__) LoggingUndefined = make_logging_undefined( logger=logger, base=Undefined ) .. versionadded:: 2.8 :param logger: the logger to use. If not provided, a default logger is created. :param base: the base class to add logging functionality to. This defaults to :class:`Undefined`. Nrundefrcsd|jdS)NzTemplate variable warning: %s)warningr)r?)r=r r" _log_messagesz,make_logging_undefined.._log_messagecsxeZdZdZdejdejddffdd Zdeffdd Zdej ejffd d Z de ffd d Z Z S)z0make_logging_undefined..LoggingUndefinedr rrrrc sDz tj|i|WdS|jy!}zd||d}~ww)NzTemplate variable error: %s)rrr error)r!rre) __class__r=r r"rs zKmake_logging_undefined..LoggingUndefined._fail_with_undefined_errorc|tSr)rrr`rDrAr r"r z8make_logging_undefined..LoggingUndefined.__str__crEr)rrr`rFr r"rrGz9make_logging_undefined..LoggingUndefined.__iter__crEr)rr"r`rFr r"r"rGz9make_logging_undefined..LoggingUndefined.__bool__)r$r%r&r#r'rcrr*rrrrr" __classcell__r rAr=)rDr"LoggingUndefineds rJ)logging getLoggerr$ addHandler StreamHandlersysstderrr1)r=r>rKrJr rIr"make_logging_undefineds rQc@s8eZdZdZdZdefddZdeddfddZeZd S) ChainableUndefinedaAn undefined that is chainable, where both ``__getattr__`` and ``__getitem__`` return itself rather than raising an :exc:`UndefinedError`. >>> foo = ChainableUndefined(name='foo') >>> str(foo.bar['baz']) '' >>> foo.bar['baz'] + 42 Traceback (most recent call last): ... jinja2.exceptions.UndefinedError: 'foo' is undefined .. versionadded:: 2.11.0 r rcCrr)r*r`r r r"r6szChainableUndefined.__html__r!cCr4rr r r r r"r rzChainableUndefined.__getattr__N) r$r%r&rbr#r*r6rr[r r r r"rRs rRc@s"eZdZdZdZdefddZdS)DebugUndefinedaAn undefined that returns the debug info when printed. >>> foo = DebugUndefined(name='foo') >>> str(foo) '{{ foo }}' >>> not foo True >>> foo + 42 Traceback (most recent call last): ... jinja2.exceptions.UndefinedError: 'foo' is undefined r rcCsL|jr d|j}n|jtur|j}n dt|jd|jd}d|dS)Nzundefined value printed: zno such element: []z{{ z }})r r rr r)r!messager r r"rs  zDebugUndefined.__str__N)r$r%r&rbr#r*rr r r r"rSs rSc@s:eZdZdZdZejZZZ ejZ Z Z Z ejZdS)StrictUndefinedasAn undefined that barks on print and iteration as well as boolean tests and all kinds of comparisons. In other words: you can do nothing with it except checking if it's defined using the `defined` test. >>> foo = StrictUndefined(name='foo') >>> str(foo) Traceback (most recent call last): ... jinja2.exceptions.UndefinedError: 'foo' is undefined >>> not foo Traceback (most recent call last): ... jinja2.exceptions.UndefinedError: 'foo' is undefined >>> foo + 42 Traceback (most recent call last): ... jinja2.exceptions.UndefinedError: 'foo' is undefined r N)r$r%r&rbr#r1rrrrrrr"rrr r r r"rW/s  rW)NFNN)IrbrhrOtypingr' collectionsr itertoolsr markupsaferrr async_utilsr r exceptionsr r r nodesrutilsrrrrrrrTypeVarrrrcr TYPE_CHECKINGrKtyping_extensionsterFrProtocolrexportedasync_exportedr0r(r*r.r/rErrrrrMappingrTr,rkregisterrIrZr+r2r-r1rrQrRrSrWr#r r r r"s                       k0FGq <