ó ØÕL]c@sƒdZddlmZmZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z ddlmZmZmZmZesÍejZejZnd„Zd„Zd Zd jd ƒejed ƒejejd d >ejdBƒZdefd„ƒYZdefd„ƒYZdefd„ƒYZdefd„ƒYZ dS(sÉ jinja2.bccache ~~~~~~~~~~~~~~ This module implements the bytecode cache system Jinja is optionally using. This is useful if you have very complex template situations and the compiliation of all those templates slow down your application too much. Situations where this is useful are often forking web applications that are initialized on the first request. :copyright: (c) 2010 by the Jinja Team. :license: BSD. iÿÿÿÿ(tpathtlistdirN(tsha1(topen_if_exists(tBytesIOtpickletPY2t text_typecCs<t|tƒr"tj||ƒn|jtj|ƒƒdS(N(t isinstancetfiletmarshaltdumptwritetdumps(tcodetf((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyt marshal_dump$scCs/t|tƒrtj|ƒStj|jƒƒS(N(RR R tloadtloadstread(R((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyt marshal_load*s itj2tasciiiiitBucketcBsDeZdZd„Zd„Zd„Zd„Zd„Zd„ZRS(suBuckets are used to store the bytecode for one template. It's created and initialized by the bytecode cache and passed to the loading functions. The buckets get an internal checksum from the cache assigned and use this to automatically reject outdated cache material. Individual bytecode cache subclasses don't have to care about cache invalidation. cCs)||_||_||_|jƒdS(N(t environmenttkeytchecksumtreset(tselfRRR((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyt__init__Es   cCs d|_dS(s)Resets the bucket (unloads the bytecode).N(tNoneR(R((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyRKscCsn|jttƒƒ}|tkr/|jƒdStj|ƒ}|j|kr[|jƒdSt|ƒ|_dS(s/Loads bytecode from a file or file like object.N( Rtlentbc_magicRRRRRR(RRtmagicR((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyt load_bytecodeOs   cCsU|jdkrtdƒ‚n|jtƒtj|j|dƒt|j|ƒdS(s;Dump the bytecode into the file or file like object passed.scan't write empty bucketiN( RRt TypeErrorR R RR RR(RR((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pytwrite_bytecode]s  cCs|jt|ƒƒdS(sLoad bytecode from a string.N(R"R(Rtstring((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pytbytecode_from_stringescCs tƒ}|j|ƒ|jƒS(sReturn the bytecode as string.(RR$tgetvalue(Rtout((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pytbytecode_to_stringis  ( t__name__t __module__t__doc__RRR"R$R&R)(((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyR<s     t BytecodeCachecBsPeZdZd„Zd„Zd„Zdd„Zd„Zd„Z d„Z RS( sßTo implement your own bytecode cache you have to subclass this class and override :meth:`load_bytecode` and :meth:`dump_bytecode`. Both of these methods are passed a :class:`~jinja2.bccache.Bucket`. A very basic bytecode cache that saves the bytecode on the file system:: from os import path class MyCache(BytecodeCache): def __init__(self, directory): self.directory = directory def load_bytecode(self, bucket): filename = path.join(self.directory, bucket.key) if path.exists(filename): with open(filename, 'rb') as f: bucket.load_bytecode(f) def dump_bytecode(self, bucket): filename = path.join(self.directory, bucket.key) with open(filename, 'wb') as f: bucket.write_bytecode(f) A more advanced version of a filesystem based bytecode cache is part of Jinja2. cCs tƒ‚dS(s¹Subclasses have to override this method to load bytecode into a bucket. If they are not able to find code in the cache for the bucket, it must not do anything. N(tNotImplementedError(Rtbucket((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyR"scCs tƒ‚dS(sÀSubclasses have to override this method to write the bytecode from a bucket back to the cache. If it unable to do so it must not fail silently but raise an exception. N(R.(RR/((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyt dump_bytecode”scCsdS(s»Clears the cache. This method is not used by Jinja2 but should be implemented to allow applications to clear the bytecode cache used by a particular environment. N((R((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pytclear›scCsft|jdƒƒ}|dk r\d|}t|tƒrL|jdƒ}n|j|ƒn|jƒS(s3Returns the unique hash key for this template name.sutf-8t|N(RtencodeRRRtupdatet hexdigest(Rtnametfilenamethash((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyt get_cache_key¡s  cCst|jdƒƒjƒS(s"Returns a checksum for the source.sutf-8(RR3R5(Rtsource((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pytget_source_checksum«scCsD|j||ƒ}|j|ƒ}t|||ƒ}|j|ƒ|S(swReturn a cache bucket for the given template. All arguments are mandatory but filename may be `None`. (R9R;RR"(RRR6R7R:RRR/((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyt get_bucket¯s  cCs|j|ƒdS(sPut the bucket into the cache.N(R0(RR/((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyt set_bucket¹sN( R*R+R,R"R0R1RR9R;R<R=(((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyR-ps     tFileSystemBytecodeCachecBsJeZdZddd„Zd„Zd„Zd„Zd„Zd„Z RS( sïA bytecode cache that stores bytecode on the filesystem. It accepts two arguments: The directory where the cache items are stored and a pattern string that is used to build the filename. If no directory is specified a default cache directory is selected. On Windows the user's temp directory is used, on UNIX systems a directory is created for the user in the system temp directory. The pattern can be used to have multiple separate caches operate on the same directory. The default pattern is ``'__jinja2_%s.cache'``. ``%s`` is replaced with the cache key. >>> bcc = FileSystemBytecodeCache('/tmp/jinja_cache', '%s.cache') This bytecode cache supports clearing of the cache using the clear method. s__jinja2_%s.cachecCs1|dkr|jƒ}n||_||_dS(N(Rt_get_default_cache_dirt directorytpattern(RR@RA((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyRÐs  cCstjƒ}tjdkr|Sttdƒs=tdƒ‚ndtjƒ}tjj||ƒ}ytj |dƒWn+t k r£}|j t j kr¤‚q¤nXtj |ƒjtjƒkrÑtdƒ‚ntjtj |ƒjƒdkrtdƒ‚n|S(NtntgetuidsJCannot determine safe temp directory. You need to explicitly provide one.s_jinja2-cache-%diÀsWSomeone else owns temp directory with your uid. You need to explicitly provide another.sLBad permission flags on temp directory, shoud be 0700. You need to fix this.(ttempfilet gettempdirtosR6thasattrt RuntimeErrorRCRtjointmkdirtOSErrorterrnotEEXISTtlstattst_uidtstattS_IMODEtst_mode(Rttmpdirtdirnamet actual_dirte((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyR?Ös"  !cCstj|j|j|jƒS(N(RRIR@RAR(RR/((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyt_get_cache_filenameôscCsJt|j|ƒdƒ}|dk rFz|j|ƒWd|jƒXndS(Ntrb(RRWRR"tclose(RR/R((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyR"÷s  cCs;t|j|ƒdƒ}z|j|ƒWd|jƒXdS(Ntwb(topenRWR$RY(RR/R((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyR0ÿscCsxddlm}tjt|jƒ|jdƒ}x?|D]7}y|tj|j|ƒƒWq9t k roq9Xq9WdS(Niÿÿÿÿ(tremovet*( RFR\tfnmatchtfilterRR@RARRIRK(RR\tfilesR7((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyR1s"  N( R*R+R,RRR?RWR"R0R1(((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyR>¾s    tMemcachedBytecodeCachecBs2eZdZdded„Zd„Zd„ZRS(suThis class implements a bytecode cache that uses a memcache cache for storing the information. It does not enforce a specific memcache library (tummy's memcache or cmemcache) but will accept any class that provides the minimal interface required. Libraries compatible with this class: - `werkzeug `_.contrib.cache - `python-memcached `_ - `cmemcache `_ (Unfortunately the django cache interface is not compatible because it does not support storing binary data, only unicode. You can however pass the underlying cache client to the bytecode cache which is available as `django.core.cache.cache._client`.) The minimal interface for the client passed to the constructor is this: .. class:: MinimalClientInterface .. method:: set(key, value[, timeout]) Stores the bytecode in the cache. `value` is a string and `timeout` the timeout of the key. If timeout is not provided a default timeout or no timeout should be assumed, if it's provided it's an integer with the number of seconds the cache item should exist. .. method:: get(key) Returns the value for the cache key. If the item does not exist in the cache the return value must be `None`. The other arguments to the constructor are the prefix for all keys that is added before the actual cache key and the timeout for the bytecode in the cache system. We recommend a high (or no) timeout. This bytecode cache does not support clearing of used items in the cache. The clear method is a no-operation function. .. versionadded:: 2.7 Added support for ignoring memcache errors through the `ignore_memcache_errors` parameter. sjinja2/bytecode/cCs(||_||_||_||_dS(N(tclienttprefixttimeouttignore_memcache_errors(RRbRcRdRe((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyRAs   cCsiy |jj|j|jƒ}Wn&tk rH|js?‚nd}nX|dk re|j|ƒndS(N(RbtgetRcRt ExceptionReRR&(RR/R((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyR"Hs     cCsy|j|j|jƒf}|jdk r>||jf7}ny|jj|ŒWn tk rt|jsu‚qunXdS(N( RcRR)RdRRbtsetRgRe(RR/targs((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyR0Rs  N(R*R+R,RtTrueRR"R0(((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyRas ,  (!R,RFRRRPtsysRLR RDR^thashlibRt jinja2.utilsRtjinja2._compatRRRRR RRRt bc_versionR3R t version_infoR tobjectRR-R>Ra(((s2/usr/lib/python2.7/site-packages/jinja2/bccache.pyts.       "    &4NU