o gi @sUddlZddlZddlZddlZddlZddlZddlmZzddl m Z Wne y6ddlm Z Ynwddl m Z ddl mZddlmZddlmZddlmZddlZejr`ddlZejd ejd ejfd Zed d dddiZejed<e Z!ej"eed<dj#Z$dedefddZ%dedefddZ&dedefddZ'Gdddej(Z)dedefddZ*dedefd d!Z+dedefd"d#Z,dedefd$d%Z-d&ejde.fd'd(Z/d)ej0ejddfd*d+Z1dnd,d-Z2dod/e3d0e.dejfd1d2Z4dpd4e3d5e3dej5ej6fd6d7Z7d&ejde3fd8d9Z8d&ejde3fd:d;Z9e:d    dqd>e3d?ej5e?d@ej5e3dAej5e3dBej5ej0e3de3f dCdDZ@ HdrdIe?dJe.dKe?dLe?de3f dMdNZAdsd&ejdPe3dQe.de3fdRdSZBdsd&ejdPe3dQe.de3fdTdUZCejDjEGdVdWdWZF X F .dtdYejGe3dZejGe3d[e.d\e.dejej5e3ge.ff d]d^ZH dud&ejd_ej5ejd e3fd`ejdejIfdadbZJGdcddddZKGdedfdfZLGdgdhdhZMGdidjdjejIZIdkejde3fdldmZNdS)vN)abc)deque)choice) randrange)Lock)CodeType)quote_from_bytesF.)bound MissingType__repr__cCsdS)Nmissingr xr r ./usr/lib/python3/dist-packages/jinja2/utils.pysrr internal_codefreturncC tj|_|S)aPass the :class:`~jinja2.runtime.Context` as the first argument to the decorated function when called while rendering a template. Can be used on functions, filters, and tests. If only ``Context.eval_context`` is needed, use :func:`pass_eval_context`. If only ``Context.environment`` is needed, use :func:`pass_environment`. .. versionadded:: 3.0.0 Replaces ``contextfunction`` and ``contextfilter``. )_PassArgcontextjinja_pass_argrr r r pass_context! rcCr)aPass the :class:`~jinja2.nodes.EvalContext` as the first argument to the decorated function when called while rendering a template. See :ref:`eval-context`. Can be used on functions, filters, and tests. If only ``EvalContext.environment`` is needed, use :func:`pass_environment`. .. versionadded:: 3.0.0 Replaces ``evalcontextfunction`` and ``evalcontextfilter``. )r eval_contextrrr r rpass_eval_context2rrcCr)aPass the :class:`~jinja2.Environment` as the first argument to the decorated function when called while rendering a template. Can be used on functions, filters, and tests. .. versionadded:: 3.0.0 Replaces ``environmentfunction`` and ``environmentfilter``. )r environmentrrr r rpass_environmentCs r!c@s@eZdZeZeZeZede de j dfddZ dS)robjrcCst|dr|jSdD]3}|dd}|d|dfD] }t||ddurcCr<)zPass the environment as the first argument to the decorated function. .. deprecated:: 3.0 Will be removed in Jinja 3.1. Use :func:`~jinja2.pass_environment` instead. zb'environmentfunction' is renamed to 'pass_environment', the old name will be removed in Jinja 3.1.r&r')r,r-r.r!rr r renvironmentfunctions r?cCst|j|S)z%Marks the function as internally used)radd__code__rr r r internalcodes rBr"cCsddlm}t||S)aCheck if the object passed is undefined. This does nothing more than performing an instance check against :class:`Undefined` but looks nicer. This can be used for custom filters or tests that want to react to undefined variables. For example a custom default filter can look like this:: def default(var, default=''): if is_undefined(var): return default return var ) Undefined)runtimerD isinstance)r"rDr r r is_undefineds rGiterablecCs|D]}qdS)z4Consumes an iterable without doing anything with it.Nr )rHr#r r rconsumesrIcCs,ddlm}ddlm}||dS)a Jinja keeps internal caches for environments and lexers. These are used so that Jinja doesn't have to recreate environments and lexers all the time. Normally you don't have to care about that but if you are measuring memory consumption you may want to clean the caches. rC)get_spontaneous_environment) _lexer_cacheN)r rJlexerrK cache_clearclear)rJrKr r r clear_cachess   rOF import_namesilentc Csvz+d|vr|dd\}}nd|vr|d\}}}nt|WStt|dd|g|WSttfy:|s7YdSw)aImports an object based on a string. This is useful if you want to use import paths as endpoints or something similar. An import path can be specified either in dotted notation (``xml.sax.saxutils.escape``) or with a colon as object delimiter (``xml.sax.saxutils:escape``). If the `silent` is True the return value will be `None` if the import fails. :return: imported object :rC.N)split rpartition __import__r+ ImportErrorAttributeError)rPrQmoduler"r#r r r import_strings  rZrbfilenamemodecCstj|sdSt||S)z\Returns a file descriptor for the filename if that file exists, otherwise ``None``. N)ospathisfileopen)r\r]r r ropen_if_existss  rbcCsJ|durdS|tur dSt|}|jdkr|jdS|jd|jdS)zReturns the name of the object's type. For some recognized singletons the name of the object is returned instead. (For example for `None` and `Ellipsis`). NNoneEllipsisbuiltinsz objectrS)rdtyper5r4)r"r/r r robject_type_reprs  rgcCsddlm}||S)z.Format an object using :func:`pprint.pformat`.r)pformat)pprintrh)r"rhr r rrhs rha@ ^ ( (https?://|www\.) # scheme or www (([\w%-]+\.)+)? # subdomain ( [a-z]{2,63} # basic tld | xn--[\w%]{2,59} # idna tld ) | ([\w%-]{2,63}\.)+ # basic domain (com|net|int|edu|gov|org|info|mil) # basic tld | (https?://) # scheme ( (([\d]{1,3})(\.[\d]{1,3}){3}) # IPv4 | (\[([\da-f]{0,4}:){2}([\da-f]{0,4}:?){1,6}]) # IPv6 ) ) (?::[\d]{1,5})? # port (?:[/?#]\S*)? # path, query, and fragment $ z^\S+@\w[\w.-]*\.\w+$texttrim_url_limitreltarget extra_schemesc sdurdtdtffdd }n dtdtfdd}tdtt|}|r/dt|d nd }|r.trim_urlcSs|SNr rr r rrqRsz(\s+)z rel=""rz target="z ^([(<]|<)+))>rS, >z([)>.,\n]|>)+$))(rt)@zwww.rRzz

N)rr.0rr r r s z'generate_lorem_ipsum..) constantsrrTrrrr capitalizeappendrrrMarkup)rrrrrrresultr#next_capitalized last_comma last_fullstoprlastpidxp_strr r rgenerate_lorem_ipsumsN        rutf-8charsetfor_qscCsPt|tst|tst|}||}|rdnd}t||}|r&|dd}|S)a5Quote a string for use in a URL using the given charset. :param obj: String or bytes to quote. Other types are converted to string then encoded to bytes using the given charset. :param charset: Encode text to bytes using this charset. :param for_qs: Quote "/" and use "+" for spaces. /z%20+)rFbytesr}encoderr*)r"rrsafervr r r url_quotes      rcCs&ddl}|jdtddt|||dS)Nrz_'unicode_urlencode' has been renamed to 'url_quote'. The old name will be removed in Jinja 3.1.r&r')rr)r,r-r.r)r"rrr,r r runicode_urlencodesrc@seZdZdZdeddfddZd1ddZdeje ej ffd d Z d eje ej fddfd d Z dej fddZd2ddZd3dej dej dej fddZd3dej dej dej fddZd1ddZdej defddZdefddZde fddZdej dej fd d!Zdej d"ej ddfd#d$Zdej ddfd%d&Zdejej ej ej ffd'd(Zdejej fd)d*Zdejej fd+d,Zdejej fd-d.Zdejej fd/d0Z eZ!dS)4LRUCachez"A simple LRU Cache implementation.capacityrNcCs ||_i|_t|_|dSrr)r_mappingr_queue _postinit)selfrr r r__init__s zLRUCache.__init__cCs4|jj|_|jj|_|jj|_t|_|jj |_ dSrr) rpopleft_popleftpop_popremove_remover_wlockr_appendrr r rrs   zLRUCache._postinitcCs|j|j|jdS)Nrrrrrr r r __getstate__ szLRUCache.__getstate__dcCs|j||dSrr)__dict__updater)rrr r r __setstate__s  zLRUCache.__setstate__cCs|jfSrr)rrr r r__getnewargs__szLRUCache.__getnewargs__cCs,||j}|j|j|j|j|S)z&Return a shallow copy of the instance.) __class__rrrrextendrrr r rcopys z LRUCache.copykeydefaultcCs"z||WSty|YSw)z/Return an item from the cache dict or `default`KeyErrorrrrr r rget s   z LRUCache.getcCs*z||WSty|||<|YSw)zvSet `default` if the key is not in the cache otherwise leave unchanged. Return the value of this key. rrr r r setdefault's   zLRUCache.setdefaultcCs@|j|j|jWddS1swYdS)zClear the cache.N)rrrNrrr r rrN1s  "zLRUCache.clearcCs ||jvS)z$Check if a key exists in this cache.rrrr r r __contains__7 zLRUCache.__contains__cCs t|jS)z%Return the current size of the cache.)rorrr r r__len__;rzLRUCache.__len__cCsdt|jd|jdS)Nrzrru)rfr4rrr r rr ?szLRUCache.__repr__c Csr|j,|j|}|jd|kr&z||Wn ty Ynw|||WdS1s2wYdS)zGet an item from the cache. Moves the item up so that it has the highest priority then. Raise a `KeyError` if it does not exist. rN)rrrr ValueErrorr)rrrr r r __getitem__Bs   $zLRUCache.__getitem__valuecCsr|j,||jvr||nt|j|jkr|j|=||||j|<WddS1s2wYdS)zhSets the value for an item. Moves the item up so that it has the highest priority then. N)rrrrorrr)rrrr r r __setitem__Xs     "zLRUCache.__setitem__c Csf|j&|j|=z||Wn tyYn wWddSWddS1s,wYdS)z]Remove an item from the cache dict. Raise a `KeyError` if it does not exist. N)rrrrrr r r __delitem__es "zLRUCache.__delitem__cs$fddtjD}||S)zReturn a list of items.csg|] }|j|fqSr r)rrrr r ssz"LRUCache.items..)listrreverse)rrr rritemsqszLRUCache.itemscCsdd|DS)zReturn a list of all values.cSsg|]}|dqS)rCr rr r rrysz#LRUCache.values..)rrr r rvalueswszLRUCache.valuescCst|S)z7Return a list of all keys ordered by most recent usage.)rrr r rkeys{sz LRUCache.keyscCtt|jSrr)reversedtuplerrr r r__iter__zLRUCache.__iter__cCr)zTIterate over the keys in the cache dict, oldest items coming first. )iterrrrr r r __reversed__szLRUCache.__reversed__rN)rrrr)"r4r5r6__doc__intrrr:Mappingr}AnyrrTuplerrrrrNboolrrr rrrIterablerrrIteratorrr__copy__r r r rrs.    " rrhtmxmlenabled_extensionsdisabled_extensionsdefault_for_stringrcsJtdd|Dtdd|Ddtjtdtffdd }|S)acIntelligently sets the initial value of autoescaping based on the filename of the template. This is the recommended way to configure autoescaping if you do not want to write a custom function yourself. If you want to enable it for all templates created from strings or for all templates with `.html` and `.xml` extensions:: from jinja2 import Environment, select_autoescape env = Environment(autoescape=select_autoescape( enabled_extensions=('html', 'xml'), default_for_string=True, )) Example configuration to turn it on at all times except if the template ends with `.txt`:: from jinja2 import Environment, select_autoescape env = Environment(autoescape=select_autoescape( disabled_extensions=('txt',), default_for_string=True, default=True, )) The `enabled_extensions` is an iterable of all the extensions that autoescaping should be enabled for. Likewise `disabled_extensions` is a list of all templates it should be disabled for. If a template is loaded from a string then the default from `default_for_string` is used. If nothing matches then the initial value of autoescaping is set to the value of `default`. For security reasons this function operates case insensitive. .. versionadded:: 2.9 cs$|] }d|dVqdSrSNlstriplowerrr r rr"z$select_autoescape..csrrrrr r rrr template_namercs4|durS|}|rdS|rdSS)NTF)rr)rrrdisabled_patternsenabled_patternsr r autoescapes  z%select_autoescape..autoescape)rr:r;r}r)r r rrrr rrselect_autoescapes(" rdumpskwargscKsD|durtj}t||fi|dddddddd S) aSerialize an object to a string of JSON with :func:`json.dumps`, then replace HTML-unsafe characters with Unicode escapes and mark the result safe with :class:`~markupsafe.Markup`. This is available in templates as the ``|tojson`` filter. The following characters are escaped: ``<``, ``>``, ``&``, ``'``. The returned string is safe to render in HTML documents and ``