U G\1@sdZddlmZmZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z ddlmZmZmZmZesejZejZnddZd d Zd Zd d eedeejdd>ejdBZGdddeZGdddeZGdddeZGdddeZ dS)a 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) 2017 by the Jinja Team. :license: BSD. )pathlistdirN)sha1)open_if_exists)BytesIOpicklePY2 text_typecCs,t|trt||n|t|dSN) isinstancefilemarshaldumpwritedumps)codefr0/usr/lib/python3/dist-packages/jinja2/bccache.py marshal_dump$s rcCs"t|trt|St|Sr )r r r loadloadsread)rrrr marshal_load*s  rZj2asciic@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)BucketauBuckets 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||_||_||_|dSr ) environmentkeychecksumreset)selfr r!r"rrr__init__EszBucket.__init__cCs d|_dS)z)Resets the bucket (unloads the bytecode).N)rr$rrrr#Ksz Bucket.resetc Csz|tt}|tkr"|dSt|}|j|krB|dSzt||_Wn$t t t fk rt|YdSXdS)z/Loads bytecode from a file or file like object.N) rlenbc_magicr#rrr"rrEOFError ValueError TypeError)r$rmagicr"rrr load_bytecodeOs  zBucket.load_bytecodecCs<|jdkrtd|tt|j|dt|j|dS)z;Dump the bytecode into the file or file like object passed.Nzcan't write empty bucketr)rr+rr(rrr"r)r$rrrrwrite_bytecodebs   zBucket.write_bytecodecCs|t|dS)zLoad bytecode from a string.N)r-r)r$stringrrrbytecode_from_stringjszBucket.bytecode_from_stringcCst}|||S)zReturn the bytecode as string.)rr.getvalue)r$outrrrbytecode_to_stringns zBucket.bytecode_to_stringN) __name__ __module__ __qualname____doc__r%r#r-r.r0r3rrrrr<src@sJeZdZdZddZddZddZdd d Zd d Zd dZ ddZ dS) BytecodeCacheaTo 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 tdS)zSubclasses 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. NNotImplementedErrorr$bucketrrrr-szBytecodeCache.load_bytecodecCs tdS)zSubclasses 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. Nr9r;rrr dump_bytecodeszBytecodeCache.dump_bytecodecCsdS)zClears 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. Nrr&rrrclearszBytecodeCache.clearNcCsDt|d}|dk rrGrIrJrKrrrrr8us  r8c@sBeZdZdZdddZddZdd Zd d Zd d ZddZ dS)FileSystemBytecodeCacheaA 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. N__jinja2_%s.cachecCs |dkr|}||_||_dSr )_get_default_cache_dir directorypattern)r$rOrPrrrr%sz FileSystemBytecodeCache.__init__c CsTdd}t}tjdkr|Sttds.|dt}tj||}zt|t j Wn0t k r}z|j t j krzW5d}~XYnXzNt|t j t|}|jtkst |jrt |jt j kr|Wn2t k r }z|j t j krW5d}~XYnXt|}|jtksJt |jrJt |jt j krP||S)NcSs tddS)NzJCannot determine safe temp directory. You need to explicitly provide one.) RuntimeErrorrrrr _unsafe_dirszCFileSystemBytecodeCache._get_default_cache_dir.._unsafe_dirntgetuidz_jinja2-cache-%d)tempfileZ gettempdirosrDhasattrrTrjoinmkdirstatS_IRWXUOSErrorerrnoZEEXISTchmodlstatst_uidS_ISDIRst_modeS_IMODE)r$rRZtmpdirdirnameZ actual_direZactual_dir_statrrrrNsB          z.FileSystemBytecodeCache._get_default_cache_dircCst|j|j|jSr )rrXrOrPr!r;rrr_get_cache_filenamesz+FileSystemBytecodeCache._get_cache_filenamecCs6t||d}|dk r2z||W5|XdS)Nrb)rrfcloser-r$r<rrrrr-s z%FileSystemBytecodeCache.load_bytecodecCs.t||d}z||W5|XdS)Nwb)openrfrhr.rirrrr=sz%FileSystemBytecodeCache.dump_bytecodec Cs^ddlm}tt|j|jd}|D]0}z|t|j|Wq(t k rVYq(Xq(dS)Nr)remove*) rVrlfnmatchfilterrrOrPrrXr\)r$rlfilesrErrrr>s zFileSystemBytecodeCache.clear)NrM) r4r5r6r7r%rNrfr-r=r>rrrrrLs )rLc@s*eZdZdZd ddZddZd d ZdS) MemcachedBytecodeCacheavThis 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. jinja2/bytecode/NTcCs||_||_||_||_dSr )clientprefixtimeoutignore_memcache_errors)r$rsrtrurvrrrr%QszMemcachedBytecodeCache.__init__cCsPz|j|j|j}Wn tk r8|js0d}YnX|dk rL||dSr )rsgetrtr! Exceptionrvr0)r$r<rrrrr-Xs z$MemcachedBytecodeCache.load_bytecodecCs\|j|j|f}|jdk r*||jf7}z|jj|Wntk rV|jsRYnXdSr )rtr!r3rurssetrxrv)r$r<argsrrrr=bs  z$MemcachedBytecodeCache.dump_bytecode)rrNT)r4r5r6r7r%r-r=rrrrrq#s -  rq)!r7rVrrsysrZr]r rUrnZhashlibrZ jinja2.utilsrZjinja2._compatrrrr rrrrZ bc_versionrAr version_infor(objectrr8rLrqrrrrs4   9N`