B g[a~1@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl mZddlmZe jrddlZddlmZGdd d ejZd Zd eed eejdd >ejdBd ZGdddZGdddZGdddeZGdddeZdS)a The optional bytecode cache system. This is useful if you have very complex template situations and the compilation of all those templates slows down your application too much. Situations where this is useful are often forking web applications that are initialized on the first request. N)sha1)BytesIO)CodeType) Environmentc@s8eZdZeedddZdeeejeddddZ dS) _MemcachedClient)keyreturncCsdS)N)selfrr r ?/opt/alt/python37/lib/python3.7/site-packages/jinja2/bccache.pygetsz_MemcachedClient.getN)rvaluetimeoutr cCsdS)Nr )r rrrr r r setsz_MemcachedClient.set)N) __name__ __module__ __qualname__strbytesr tOptionalintrr r r r rsrsj2c@steZdZdZdeeddddZdddd Zejdd d d Z ejdd d dZ e ddddZ e dddZ 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. rN) environmentrchecksumr cCs||_||_||_|dS)N)rrrreset)r rrrr r r __init__5szBucket.__init__)r cCs d|_dS)z)Resets the bucket (unloads the bytecode).N)code)r r r r r;sz Bucket.reset)fr c Csz|tt}|tkr"|dSt|}|j|krB|dSyt||_Wn"t t t fk rt|dSXdS)z/Loads bytecode from a file or file like object.N) readlenbc_magicrpickleloadrmarshalr!EOFError ValueError TypeError)r r"magicrr r r load_bytecode?s  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)r!r+writer%r&dumprr()r r"r r r write_bytecodeRs   zBucket.write_bytecode)stringr cCs|t|dS)zLoad bytecode from bytes.N)r-r)r r1r r r bytecode_from_stringZszBucket.bytecode_from_stringcCst}|||S)zReturn the bytecode as bytes.)rr0getvalue)r outr r r bytecode_to_string^s zBucket.bytecode_to_string)rrr__doc__rr rrZBinaryIOr-r0rr2r5r r r r r,src@seZdZdZeddddZeddddZddd d Zdee j e j eed d d Z eedddZ dee j eeedddZeddddZdS) 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 Jinja. N)bucketr 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. N)NotImplementedError)r r8r r r r-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. N)r9)r r8r r r dump_bytecodeszBytecodeCache.dump_bytecode)r cCsdS)zClears the cache. This method is not used by Jinja but should be implemented to allow applications to clear the bytecode cache used by a particular environment. Nr )r r r r clearszBytecodeCache.clear)namefilenamer cCs2t|d}|dk r*|d||S)z3Returns the unique hash key for this template name.zutf-8N|)rencodeupdate hexdigest)r r<r=hashr r r get_cache_keyszBytecodeCache.get_cache_key)sourcer cCst|dS)z"Returns a checksum for the source.zutf-8)rr?rA)r rDr r r get_source_checksumsz!BytecodeCache.get_source_checksumr)rr<r=rDr cCs0|||}||}t|||}|||S)zwReturn a cache bucket for the given template. All arguments are mandatory but filename may be `None`. )rCrErr-)r rr<r=rDrrr8r r r get_buckets    zBytecodeCache.get_bucketcCs||dS)zPut the bucket into the cache.N)r:)r r8r r r set_bucketszBytecodeCache.set_bucket)N)rrrr6rr-r:r;rrrZUnionrCrErFrGr r r r r7es  r7c@sveZdZdZdejeeddddZeddd Ze ed d d Z e dd d dZ e dd ddZ ddddZ 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.cache) directorypatternr cCs |dkr|}||_||_dS)N)_get_default_cache_dirrJrK)r rJrKr r r r sz FileSystemBytecodeCache.__init__)r c Cs^dddd}t}tjdkr$|Sttds4|dt}tj||}yt|t j Wn0t k r}z|j t j krWdd}~XYnXyNt|t j t|}|jtkst |jrt |jt j kr|Wn4t k r}z|j t j krWdd}~XYnXt|}|jtksTt |jrTt |jt j krZ||S)Nz te.NoReturn)r cSs tddS)NzJCannot determine safe temp directory. You need to explicitly provide one.) RuntimeErrorr r r r _unsafe_dirszCFileSystemBytecodeCache._get_default_cache_dir.._unsafe_dirntgetuidz_jinja2-cache-)tempfileZ gettempdirosr<hasattrrPpathjoinmkdirstatS_IRWXUOSErrorerrnoZEEXISTchmodlstatst_uidS_ISDIRst_modeS_IMODE)r rNZtmpdirdirnameZ actual_direZactual_dir_statr r r rLs:       z.FileSystemBytecodeCache._get_default_cache_dir)r8r cCstj|j|j|jfS)N)rRrTrUrJrKr)r r8r r r _get_cache_filenamesz+FileSystemBytecodeCache._get_cache_filenamec Cs:||}tj|r6t|d}||WdQRXdS)Nrb)rcrRrTexistsopenr-)r r8r=r"r r r r-s   z%FileSystemBytecodeCache.load_bytecodec Cs*t||d}||WdQRXdS)Nwb)rfrcr0)r r8r"r r r r: sz%FileSystemBytecodeCache.dump_bytecodec Csfddlm}tt|j|jd}x:|D]2}y|tj|j|Wq,t k r\Yq,Xq,WdS)Nr)remove)*) rRrhfnmatchfilterlistdirrJrKrTrUrY)r rhfilesr=r r r r;s  zFileSystemBytecodeCache.clear)NrI)rrrr6rrrr rLrrcr-r:r;r r r r rHs /rHc@sLeZdZdZddeejeedddZ e dd d d Z e dd d d Z dS)MemcachedBytecodeCachea'This 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: - `cachelib `_ - `python-memcached `_ (Unfortunately the django cache interface is not compatible because it does not support storing binary data, only text. 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/NTr)clientprefixrignore_memcache_errorscCs||_||_||_||_dS)N)rprqrrr)r rprqrrrr r r r LszMemcachedBytecodeCache.__init__)r8r cCsDy|j|j|j}Wntk r4|js0Yn X||dS)N)rpr rqr Exceptionrrr2)r r8r!r r r r-Xs z$MemcachedBytecodeCache.load_bytecodecCsf|j|j}|}y0|jdk r4|j|||jn|j||Wntk r`|js\YnXdS)N)rqrr5rrprrsrr)r r8rrr r r r:as  z$MemcachedBytecodeCache.dump_bytecode)roNT) rrrr6rrrrboolr rr-r:r r r r rns + rn) r6rZrjr(rRr&rWsysrQtypingrZhashlibriortypesrZ TYPE_CHECKINGZtyping_extensionsZterrZProtocolrZ bc_versiondumps version_infor%rr7rHrnr r r r s,    09Ue