dbfdg 3 sݨ\kl@sdZddlZddlmZddlmZddlmZmZddl m Z m Z m Z m Z mZmZmZmZmZddlmZmZmZddlmZmZmZmZmZmZmZmZd d d d d dddddddddgZ eZ!ddZ"e#Z$e#Z%ddZ&ddZ'd7ddZ(Gdd d e#Z)ddZ*Gd d!d!e+Z,e fd"d#Z-Gd$d%d%ee,Z.ydd&l/m0Z0e0j1e.Wne2k rfYnXGd'd(d(e#Z3Gd)d*d*e#Z4Gd+d d e4Z5eGd,d-d-e#Z6Gd.d d e#Z7eGd/d0d0e#Z8d8d1d2Z9eGd3d4d4e8Z:eGd5d6d6e8Z;e8`%sr$cCsLg}tt|}x4|D],}|j|t|drtdjt||SqWt|S)z@Concatenation that escapes if necessary and converts to unicode.Z__html__)rrappendhasattrrjoinrr )seqZbufiteratorargr!r!r#r+s    cCsttt|S)z4Simple args to unicode conversion and concatenation.)r rr)r)r!r!r#r6sc Csl|dkr i}|r|}nt|pff|}|r\|r6t|}x$t|D]\}} | tk r@| ||<q@W|j||||S)z(Internal helper to for context creation.N)dictrr Z context_class) environmentZ template_nameblocksvarsZsharedglobalslocalsparentkeyvaluer!r!r# new_context;s  r5c@s(eZdZdZddZddZddZdS) rzThe `self` in templates.cCs ||_dS)N)_TemplateReference__context)selfcontextr!r!r#__init__SszTemplateReference.__init__cCs|jj|}t||j|dS)Nr)r6r.BlockReference)r7namer.r!r!r# __getitem__Vs zTemplateReference.__getitem__cCsd|jj|jjfS)Nz<%s %r>) __class____name__r6r;)r7r!r!r#__repr__ZszTemplateReference.__repr__N)r> __module__ __qualname____doc__r9r<r?r!r!r!r#rPscCs t|d|S)N__func__)getattr)r"r!r!r# _get_funcasrEc@seZdZddZdS) ContextMetac Csvtj||||}|fkr|St|j}ttj}t|j}ttj}||k r\||kr\d|_n||krr||krrd|_|S)NT)type__new__rEresolveContextresolve_or_missing_legacy_resolve_mode_fast_resolve_mode) clsr;basesdrvrIZdefault_resolverKZdefault_resolve_or_missingr!r!r#rHgs    zContextMeta.__new__N)r>r@rArHr!r!r!r#rFesrFcCs,||jkr|j|S||jkr(|j|S|S)N)r/r2)r8r3r r!r!r#rK}s     rKc@seZdZdZdZdZddZddZd$dd Zd d Z d d Z ddZ ddZ e ddZd%ddZddZedZedZedZeredZedZedZ[ddZd d!Zd"d#ZdS)&rJaThe 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:`contextfunction`\s 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. FcCs\||_i|_||_t|j||_t|_||_tddt |D|_ |j rXt t ||_ dS)Ncss|]\}}||gfVqdS)Nr!).0kvr!r!r# sz#Context.__init__..)r2r/r-reval_ctxset exported_varsr;r,rr.rMrrK)r7r-r2r;r.r!r!r#r9szContext.__init__c CsXy$|j|}|j|d}||Wn$tk rH|jjd|ddSXt||||S)zRender a parent block.z#there is no parent block called %r.super)r;)r.index LookupErrorr- undefinedr:)r7r;Zcurrentr.r[r!r!r#rZs   z Context.superNc Cs"y||Stk r|SXdS)zfReturns an item from the template context, if it doesn't exist `default` is returned. N)KeyError)r7r3defaultr!r!r#getsz Context.getcCs6|jrt||}n |j|}|tkr2|jj|dS|S)zLooks up a variable like `__getitem__` or `get` but returns an :class:`Undefined` object with the name of the name looked up. )r;)rLrKr r-r])r7r3rQr!r!r#rIs   zContext.resolvecCs,|jr"|j|}t|trt}|St||S)zxResolves a variable like :meth:`resolve` but returns the special `missing` value if it cannot be found. )rLrI isinstance Undefinedr rK)r7r3rQr!r!r#rKs   zContext.resolve_or_missingcstfddjDS)z+Get a new dict with the exported variables.c3s|]}|j|fVqdS)N)r/)rRrS)r7r!r#rUsz'Context.get_exported..)r,rX)r7r!)r7r# get_exportedszContext.get_exportedcCs(|js |jS|js|jSt|jf|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. )r/r2r,)r7r!r!r#get_alls zContext.get_allc Ost|dr.|j}xdD]}t||r|}PqWt|trt|ddrP|f|}n2t|ddrj|jf|}nt|ddr|jf|}y |||Stk r|jjdSXdS) zCall the callable with the arguments and keyword arguments provided but inject the active context or environment as first argument if the callable is a :func:`contextfunction` or :func:`environmentfunction`. __call__contextfunctionr environmentfunctionrzGvalue was undefined because a callable raised a StopIteration exceptionN)rfr rg) r'rerarrDrVr- StopIterationr])Z_Context__selfZ _Context__objargskwargsfnZfn_typer!r!r#calls&         z Context.callcCsDt|j|ji|jdd|}|j|_|jjddt|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|fVqdS)N)list)rRrSrTr!r!r#rUsz"Context.derived..)r5r-r;rdrVr.updater)r7r1r8r!r!r#derived s  zContext.derivedcs$fdd}ttj|_|_|S)Ncst|jS)N)rDrd)r7)methr!r#r$szContext._all..)rDr,rBr>)rpproxyr!)rpr#_alls z Context._allkeysvaluesitemsiterkeys itervaluesrcCs||jkp||jkS)N)r/r2)r7r;r!r!r# __contains__(szContext.__contains__cCs|j|}|tkrt||S)zTLookup a variable or raise `KeyError` if the variable is undefined. )rKr r^)r7r3itemr!r!r#r<+s zContext.__getitem__cCsd|jjt|j|jfS)Nz <%s %s of %r>)r=r>reprrdr;)r7r!r!r#r?4s zContext.__repr__)N)N)r>r@rArBrLrMr9rZr`rIrKrcrdr rlrorrrsrtrurrvrwrrxr<r?r!r!r!r#rJs0    "  rJ)Mappingc@s0eZdZdZddZeddZeddZdS) r:z"One block on a template reference.cCs||_||_||_||_dS)N)r;_context_stack_depth)r7r;r8stackdepthr!r!r#r9GszBlockReference.__init__cCsF|jdt|jkr,|jjjd|jddSt|j|j|j|jdS)zSuper the block.rYz#there is no parent block called %r.rZ)r;)r~lenr}r|r-r]r;r:)r7r!r!r#rZMs zBlockReference.supercCs,t|j|j|j}|jjjr(t|}|S)N)r r}r~r|rV autoescaper)r7rQr!r!r#reWs zBlockReference.__call__N) r>r@rArBr9propertyrZr rer!r!r!r#r:Ds r:c@seZdZdZeZeZeZdZ dddZ ddZ dd Z e d d Ze d d Ze d d Ze dd Ze dd Ze dd Ze ddZe ddZddZeddZeZ[ddZdS)LoopContextBasez%A loop context for dynamic iteration.NrcCs"||_||_d|_||_t|_dS)NrY) _undefined_recurseindex0depth0r _last_checked_value)r7r]recurserr!r!r#r9gs zLoopContextBase.__init__cGs|s td||jt|S)z7Cycles among the arguments with the current loop index.zno items for cycling given) TypeErrorrr)r7rir!r!r#cyclenszLoopContextBase.cyclecGs|j|kr||_dSdS)z9Checks whether the value has changed since the last call.TF)r)r7r4r!r!r#changedts zLoopContextBase.changedcCs |jdkS)Nr)r)r"r!r!r#r${szLoopContextBase.cCs |jtkS)N)_after_last_iteration)r"r!r!r#r$|scCs |jdS)NrY)r)r"r!r!r#r$}scCs |j|jS)N)lengthr)r"r!r!r#r$~scCs |j|jS)N)rr[)r"r!r!r#r$scCs |jdS)NrY)r)r"r!r!r#r$scCs|jtkr|jdS|jS)Nzthere is no previous item)_before_first_iterationr)r7r!r!r#previtems  zLoopContextBase.previtemcCs|jtkr|jdS|jS)Nzthere is no next item)rrr)r7r!r!r#nextitems  zLoopContextBase.nextitemcCs|jS)N)r)r7r!r!r#__len__szLoopContextBase.__len__cCs(|jdkrtd|j||j|jdS)NzMTried to call non recursive loop. Maybe you forgot the 'recursive' modifier.rY)rrr)r7iterabler!r!r#loops zLoopContextBase.loopcCsd|jj|j|jfS)Nz <%s %r/%r>)r=r>r[r)r7r!r!r#r?szLoopContextBase.__repr__)Nr)r>r@rArBrr_currentrr_lengthr9rrrfirstZlastr[ZrevindexZ revindex0rrrrr rrer?r!r!r!r#r_s*          rc@s2eZdZd ddZeddZddZd d ZdS) rNrc CsVtj||||t||_yt||_Wnttfk rFd|_YnX|j|_ dS)N) rr9iter _iteratorrrrAttributeError _safe_nextr)r7rr]rrr!r!r#r9s  zLoopContext.__init__cCs<|jdkr6t|j}t||_|jd}t|||_|jS)N)rtuplerrrr)r7rZiterations_doner!r!r#rs     zLoopContext.lengthcCst|S)N)LoopContextIterator)r7r!r!r#__iter__szLoopContext.__iter__c Cs$y t|jStk rtSXdS)N)nextrrhr)r7r!r!r#rs zLoopContext._safe_next)Nr)r>r@rAr9rrrrr!r!r!r#rs  c@s,eZdZdZd ZddZddZddZd S) rz The iterator for a loop context.r8cCs ||_dS)N)r8)r7r8r!r!r#r9szLoopContextIterator.__init__cCs|S)Nr!)r7r!r!r#rszLoopContextIterator.__iter__cCsH|j}|jd7_|jtkr$t|j|_|j|_|j|_|j|fS)NrY)r8rrrrhrrr)r7Zctxr!r!r#__next__s  zLoopContextIterator.__next__N)r8)r>r@rArB __slots__r9rrr!r!r!r#rs rc@s:eZdZdZd ddZeeddZddZd d Z dS) rzWraps a macro function.Nc CsV||_||_t||_||_||_||_||_||_d|k|_ |dkrL|j }||_ dS)Ncaller) _environment_funcr_argument_countr; arguments catch_kwargs catch_varargsrexplicit_callerr_default_autoescape) r7r-funcr;rrrrZdefault_autoescaper!r!r#r9s  zMacro.__init__c Os|r*t|dtr*|dj}|dd}n|j}t|d|j}t|}d}||jkrxht|jt|dD]F\}}y|j |} Wnt k rt } YnX|dkrd}|j | qpWn|j }|jr| r|j dd} | dkr|jjddd} |j | |jr|j |n8|rHd|kr.td|jtd |jtt|f|jrf|j ||jdn(t||jkrtd |jt|jf|j||S) NrrYFrTzNo caller defined)r;zamacro %r was invoked with two values for the special caller argument. This is most likely a bug.z%macro %r takes no keyword argument %rz+macro %r takes not more than %d argument(s))rarrrrmrr enumeraterpopr^r r&rrrr]rrr;rrr_invoke) r7rirjrrZoffZ found_calleridxr;r4rr!r!r#resJ          zMacro.__call__cCs|j|}|rt|}|S)z=This method is being swapped out by the async implementation.)rr)r7rrrQr!r!r#rAs z Macro._invokecCs$d|jj|jdkrdpt|jfS)Nz<%s %s>Z anonymous)r=r>r;rz)r7r!r!r#r?HszMacro.__repr__)N) r>r@rArBr9r r rerr?r!r!r!r#rs  Kc@seZdZdZdZdedefddZed d Z ed d Z e Z Z Z ZZZZZZZZZZZZZZZZZZZ Z!Z"Z#Z$Z%d dZ&ddZ'ddZ(ddZ)ddZ*ddZ+ddZ,e,Z-ddZ.dS)rbazThe default undefined type. This undefined type can be printed and iterated over, but every other access will raise an :exc:`jinja2.exceptions.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_exceptionNcCs||_||_||_||_dS)N)rrrr)r7hintobjr;excr!r!r#r9aszUndefined.__init__cOsl|jdkrX|jtkr d|j}q^t|jtsBdt|j|jf}q^dt|j|jf}n|j}|j|dS)z{Regular callback function for undefined objects that raises an `jinja2.exceptions.UndefinedError` on call. Nz%r is undefinedz%s has no element %rz%r has no attribute %r)rrr rrarr r)r7rirjrr!r!r#_fail_with_undefined_errorgs      z$Undefined._fail_with_undefined_errorcCs |dddkrt||jS)Nr__)rr)r7r;r!r!r# __getattr__}szUndefined.__getattr__cCst|t|kS)N)rG)r7otherr!r!r#__eq__szUndefined.__eq__cCs |j| S)N)r)r7rr!r!r#__ne__szUndefined.__ne__cCs tt|S)N)idrG)r7r!r!r#__hash__szUndefined.__hash__cCsdS)Nr%r!)r7r!r!r#__str__szUndefined.__str__cCsdS)Nrr!)r7r!r!r#rszUndefined.__len__ccsdS)Nr!)r7r!r!r#rszUndefined.__iter__cCsdS)NFr!)r7r!r!r# __nonzero__szUndefined.__nonzero__cCsdS)Nrbr!)r7r!r!r#r?szUndefined.__repr__)rrrr)/r>r@rArBrr rr9r rr__add____radd____mul____rmul__Z__div__Z__rdiv__ __truediv__ __rtruediv__ __floordiv__ __rfloordiv____mod____rmod____pos____neg__rer<__lt____le____gt____ge____int__ __float__ __complex____pow____rpow____sub____rsub__rrrrrrr__bool__r?r!r!r!r#rbOs   lrbcs`dkr,ddl}|jtj|jtjdkr8tfddGfddd}|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`. Nrcsn|jdkrX|jtkr d|j}q^t|jtsBdt|j|jf}q^dt|j|jf}n|j}jd|dS)Nz%s is undefinedz%s has no element %sz%s has no attribute %szTemplate variable warning: %s)rrr rrarr Zwarning)Zundefr)loggerr!r# _log_messages      z,make_logging_undefined.._log_messagecsfeZdZfddZfddZfddZerTfddZfd d Znfd d Z d S)z0make_logging_undefined..LoggingUndefinedcsPyj|f||S|jk rJ}zjdt||WYdd}~XnXdS)NzTemplate variable error: %s)rrerrorstr)r7rirje)baserr!r#rs zKmake_logging_undefined..LoggingUndefined._fail_with_undefined_errorcsj|}||S)N)r)r7rQ)rrr!r#rs z8make_logging_undefined..LoggingUndefined.__str__csj|}||S)N)r)r7rQ)rrr!r#rs z9make_logging_undefined..LoggingUndefined.__iter__csj|}||S)N)r)r7rQ)rrr!r#rs z.LoggingUndefined.__nonzero__csj|}||S)N) __unicode__)r7rQ)rrr!r#rs z.LoggingUndefined.__unicode__csj|}||S)N)r)r7rQ)rrr!r#rs z9make_logging_undefined..LoggingUndefined.__bool__N) r>r@rArrrrrrrr!)rrrr!r#LoggingUndefinedsr)loggingZ getLoggerr>Z addHandlerZ StreamHandlersysstderrrb)rrrrr!)rrrr#make_logging_undefineds  #rc@seZdZdZfZddZdS)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 cCs<|jdkr2|jtkrd|jSdt|j|jfSd|jS)Nz{{ %s }}z{{ no such element: %s[%r] }}z!{{ undefined value printed: %s }})rrr rr )r7r!r!r#rs    zDebugUndefined.__str__N)r>r@rArBrrr!r!r!r#rs rc@s6eZdZdZfZejZZZ Z Z Z Z ZdS)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 N)r>r@rArBrrbrrrrrrrrrr!r!r!r#rsr)NNNN)NN)=rBr itertoolsrtypesrZ jinja2.nodesrrZ jinja2.utilsrrrr r r r r rZjinja2.exceptionsrrrZjinja2._compatrrrrrrrr__all__rr objectrrrrr5rrErGrFrKrJ collectionsr{register ImportErrorr:rrrrrbrrrrr!r!r!r# sX  ,(   9 F' m U Q