Rc @sKdZddlZddlZddlmZddlmZmZmZm Z m Z ej dZ ej ddj eejd5dj eejd6fZej dZej dZej dZdZdZedd7idd6ZeZdj ZdZdZdZdZdZdZ d Z!e"d!Z#d"d#Z$d$Z%e"d%Z&de"d&Z(d'e)d(d)d*Z*d+d,Z+d-e,fd.YZ-y!dd/lm.Z.e.j/e-Wne0k rnXed0e,fd1YZ1d2e,fd3YZ2dd4l3m4Z4mZm5Z5dS(8s jinja2.utils ~~~~~~~~~~~~ Utility functions. :copyright: (c) 2010 by the Jinja Team. :license: BSD, see LICENSE for more details. iN(tdeque(t text_typet string_typestimplements_iteratort allocate_lockt url_quotes(\s+)s4^(?P(?:%s)*)(?P.*?)(?P(?:%s)*)$t|t(ts s>s&^\S+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+$s(|<[^>]*>)s &([^;]+);t4abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZt 0123456789t MissingTypecCsdS(Ntmissing((tx((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyt st__repr__ucCs t|_|S(s+This decorator can be used to mark a function or method context callable. A context callable is passed the active :class:`Context` as first argument when called from the template. This is useful if a function wants to get access to the context or functions provided on the context object. For example a function that returns a sorted list of template variables the current template exports could look like this:: @contextfunction def get_exported_names(context): return sorted(context.exported_vars) (tTruetcontextfunction(tf((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyR(s cCs t|_|S(sGThis decorator can be used to mark a function or method as an eval context callable. This is similar to the :func:`contextfunction` but instead of passing the context, an evaluation context object is passed. For more information about the eval context, see :ref:`eval-context`. .. versionadded:: 2.4 (Rtevalcontextfunction(R((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyR8s cCs t|_|S(sThis decorator can be used to mark a function or method as environment callable. This decorator works exactly like the :func:`contextfunction` decorator just that the first argument is the active :class:`Environment` and not context. (Rtenvironmentfunction(R((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyREs cCstj|j|S(s%Marks the function as internally used(t internal_codetaddt__code__(R((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyt internalcodeOscCsddlm}t||S(sCheck 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 i(t Undefined(tjinja2.runtimeRt isinstance(tobjR((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyt is_undefinedUs cCsx|D]}qWdS(s4Consumes an iterable without doing anything with it.N((titerabletevent((s0/usr/lib/python2.7/site-packages/jinja2/utils.pytconsumees cCs8ddlm}ddlm}|j|jdS(s"Jinja2 keeps internal caches for environments and lexers. These are used so that Jinja2 doesn't have to recreate environments and lexers all the time. Normally you don't have to care about that but if you are messuring memory consumption you may want to clean the caches. i(t_spontaneous_environments(t _lexer_cacheN(tjinja2.environmentR%t jinja2.lexerR&tclear(R%R&((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyt clear_cachesks cCsyd|kr*|jdd\}}nEd|kre|jd}dj|d }|d}n t|Stt|dd|g|SWn#ttfk r|sqnXdS(sImports 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 t:iR iN(tsplittjoint __import__tgetattrtNonet ImportErrortAttributeError(t import_nametsilenttmoduleR titems((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyt import_stringws     #trbcCsLyt||SWn4tk rG}|jtjtjfkrHqHnXdS(sZReturns a file descriptor for the filename if that file exists, otherwise `None`. N(topentIOErrorterrnotENOENTtEISDIR(tfilenametmodete((s0/usr/lib/python2.7/site-packages/jinja2/utils.pytopen_if_existss cCsc|dkrdS|tkr dS|jjdkrA|jj}n|jjd|jj}d|S( sReturns 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`). R0tEllipsist __builtin__tbuiltinsR s %s objectN(s __builtin__RD(R0RBt __class__t __module__t__name__(R tname((s0/usr/lib/python2.7/site-packages/jinja2/utils.pytobject_type_reprs  cCsSy$ddlm}||d|SWn(tk rNddlm}||SXdS(sYPrettyprint an object. Either use the `pretty` library or the builtin `pprint`. i(tprettytverbose(tpformatN(RJR1tpprintRL(R RKRJRL((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyRLs  c Cs|d}tjtt|}|r3dp6d}xt|D]\}}tj|}|rF|j\} } } | jds d| kr&| jd r&| jd r&t | dkr&| dt t kr&| j d s | j d s | j d r&d | ||| f} n| jdsD| jdr`d | ||| f} nd| kr| jd rd| krt j| rd| | f} n| | | |kr| | | ||%ss%sR+s%su(t_word_split_reR,Rtescapet enumeratet_punctuation_retmatchtgroupst startswithROt_letterst_digitstendswitht_simple_email_reR-( ttextttrim_url_limittnofollowttrim_urltwordst nofollow_attrtitwordRVtleadtmiddlettrail((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyturlizes8  iiidcCsddlm}ddlm}m}|j}g}x}t|D]o} t} d} } d} d}g}xt t|||D]\}} x,tr||} | |kr| }PqqW| r| j } t } n||dd| kr|} | d7} | d7} n||d d | krK|} } | d 7} t} n|j | qWd j |}|jdr|d d }n|jd s|d 7}n|j |qEW|sd j |Stdj d|DS(s,Generate some lorem impsum for the template.i(tLOREM_IPSUM_WORDS(tchoicet randrangeiiiiR i iR u u u css|]}dt|VqdS(u

%s

N(RS(t.0R((s0/usr/lib/python2.7/site-packages/jinja2/utils.pys sN(tjinja2.constantsRitrandomRjRkR,trangeRR0RTt capitalizetFalsetappendR-R[tMarkup(tnthtmltmintmaxRiRjRkRatresultt_tnext_capitalizedt last_commat last_fullstopRdtlasttptidx((s0/usr/lib/python2.7/site-packages/jinja2/utils.pytgenerate_lorem_ipsumsJ  (            sutf-8cCsOt|tst|}nt|tr?|j|}ntt|S(s&URL escapes a single bytestring or unicode string with the given charset if applicable to URL safe quoting under all rules that need to be considered under all supported Python versions. If non strings are provided they are converted to their unicode representation first. (RRRtencodeR(R tcharset((s0/usr/lib/python2.7/site-packages/jinja2/utils.pytunicode_urlencodes tLRUCachecBseZdZdZdZdZdZdZdZddZ ddZ d Z d Z d Zd Zd ZdZdZdZdZdZdZdZdZeZdZeZRS(s"A simple LRU Cache implementation.cCs,||_i|_t|_|jdS(N(tcapacityt_mappingRt_queuet _postinit(tselfR((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyt__init__-s   cCsL|jj|_|jj|_|jj|_t|_|jj |_ dS(N( Rtpopleftt_poplefttpopt_poptremovet_removeRt_wlockRrt_append(R((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyR3s  cCs"i|jd6|jd6|jd6S(NRRR(RRR(R((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyt __getstate__;s  cCs|jj||jdS(N(t__dict__tupdateR(Rtd((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyt __setstate__BscCs |jfS(N(R(R((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyt__getnewargs__FscCs;|j|j}|jj|jt|j|_|S(s&Return a shallow copy of the instance.(RERRRRR(Rtrv((s0/usr/lib/python2.7/site-packages/jinja2/utils.pytcopyIscCs%y ||SWntk r |SXdS(s/Return an item from the cache dict or `default`N(tKeyError(Rtkeytdefault((s0/usr/lib/python2.7/site-packages/jinja2/utils.pytgetPs  cCsQ|jjz/y ||SWntk r:|||<|SXWd|jjXdS(svSet `default` if the key is not in the cache otherwise leave unchanged. Return the value of this key. N(RtacquireRtrelease(RRR((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyt setdefaultWs     cCs@|jjz|jj|jjWd|jjXdS(sClear the cache.N(RRRR)RR(R((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyR)es   cCs ||jkS(s$Check if a key exists in this cache.(R(RR((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyt __contains__nscCs t|jS(s%Return the current size of the cache.(ROR(R((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyt__len__rscCsd|jj|jfS(Ns<%s %r>(RERGR(R((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyRvs cCs|jjz]|j|}|jd|krey|j|Wntk rTnX|j|n|SWd|jjXdS(sGet 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. iN(RRRRRt ValueErrorRR(RRR((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyt __getitem__|s   cCs|jjzh||jkr/|j|n+t|j|jkrZ|j|j=n|j|||j|nXWd|jjXdS(s]Remove an item from the cache dict. Raise a `KeyError` if it does not exist. N(RRRRRR(RR((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyt __delitem__s   cCs=gt|jD]}||j|f^q}|j|S(sReturn a list of items.(tlistRRtreverse(RRRx((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyR6s/ cCst|jS(sIterate over all items.(titerR6(R((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyt iteritemsscCs!g|jD]}|d^q S(sReturn a list of all values.i(R6(RR((s0/usr/lib/python2.7/site-packages/jinja2/utils.pytvaluesscCst|jS(sIterate over all values.(RR(R((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyt itervaluescCs t|S(s7Return a list of all keys ordered by most recent usage.(R(R((s0/usr/lib/python2.7/site-packages/jinja2/utils.pytkeysscCstt|jS(s[Iterate over all keys in the cache dict, ordered by the most recent usage. (treversedttupleR(R((s0/usr/lib/python2.7/site-packages/jinja2/utils.pytiterkeysscCstt|jS(sVIterate over the values in the cache dict, oldest items coming first. (RRR(R((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyt __reversed__sN(RGRFt__doc__RRRRRRR0RRR)RRRRRRR6RRRRRt__iter__Rt__copy__(((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyR&s2                     (tMutableMappingtCyclercBs8eZdZdZdZedZdZRS(sA cycle helper for templates.cGs,|stdn||_|jdS(Ns$at least one item has to be provided(t RuntimeErrorR6treset(RR6((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyRs cCs d|_dS(sResets the cycle.iN(tpos(R((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyRscCs|j|jS(sReturns the current item.(R6R(R((s0/usr/lib/python2.7/site-packages/jinja2/utils.pytcurrentscCs*|j}|jdt|j|_|S(s#Goes one item ahead and returns it.i(RRROR6(RR((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyt__next__s (RGRFRRRtpropertyRR(((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyRs   tJoinercBs#eZdZddZdZRS(sA joining helper for templates.u, cCs||_t|_dS(N(tsepRqtused(RR((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyRs cCs|jst|_dS|jS(Nu(RRR(R((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyt__call__s  (RGRFRRR(((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyRs (RsRSt soft_unicode(RRs<(R R R R s s>((6RtreR;t collectionsRtjinja2._compatRRRRRtcompileRRR-tmapRSRUR\t _striptags_ret _entity_reRYRZttypeRtsetRtconcatRRRRR!R$R*RqR7RARIRLR0RhRRRtobjectRRtregisterR1RRt markupsafeRsR(((s0/usr/lib/python2.7/site-packages/jinja2/utils.pyt sR  ("         -1