U a1@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)keyreturncCsdSN)selfrr r OC:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-8gbwd15f\jinja2\bccache.pygetsz_MemcachedClient.getN)rvaluetimeoutr cCsdSr r )r rrrr r r setsz_MemcachedClient.set)N) __name__ __module__ __qualname__strbytesrtOptionalintrr 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||_||_||_|dSr )rrrreset)r rrrr r r __init__5szBucket.__init__r cCs d|_dS)z)Resets the bucket (unloads the bytecode).N)coder r r r r ;sz Bucket.reset)fr c Cs||tt}|tkr"|dSt|}|j|krB|dSzt||_Wn$t t t fk rv|YdSXdS)z/Loads bytecode from a file or file like object.N) readlenbc_magicr pickleloadrmarshalr#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)r0r)r r4r r r bytecode_from_stringZszBucket.bytecode_from_stringcCst}|||S)zReturn the bytecode as bytes.)rr3getvalue)r outr r r bytecode_to_string^s zBucket.bytecode_to_string)rrr__doc__rr!r rBinaryIOr0r3rr5r8r 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. Nbucketr 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 r=r r r r0szBytecodeCache.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. Nr>r@r r r dump_bytecodeszBytecodeCache.dump_bytecoder"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 cCs4t|d}|dk r,|d|d|S)z3Returns the unique hash key for this template name.utf-8N|)rencodeupdate hexdigest)r rCrDhashr r r get_cache_keyszBytecodeCache.get_cache_key)sourcer cCst|dS)z"Returns a checksum for the source.rE)rrGrI)r rLr r r get_source_checksumsz!BytecodeCache.get_source_checksumr)rrCrDrLr cCs0|||}||}t|||}|||S)zwReturn a cache bucket for the given template. All arguments are mandatory but filename may be `None`. )rKrMrr0)r rrCrDrLrrr=r r r get_buckets    zBytecodeCache.get_bucketcCs||dS)zPut the bucket into the cache.N)rAr@r r r set_bucketszBytecodeCache.set_bucket)N)rrrr9rr0rArBrrrUnionrKrMrNrOr r r r r;es$  r;c@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|}||_||_dSr )_get_default_cache_dirrSrT)r rSrTr r r r!sz FileSystemBytecodeCache.__init__r"c Cs^dddd}t}tjdkr$|Sttds4|dt}tj||}zt|t j Wn0t k r}z|j t j krW5d}~XYnXzNt|t j t|}|jtkst |jrt |jt j kr|Wn4t k r}z|j t j krW5d}~XYnXt|}|jtksTt |jrTt |jt j krZ||S)Nz te.NoReturnr"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-)tempfile gettempdirosrChasattrrYpathjoinmkdirstatS_IRWXUOSErrorerrnoEEXISTchmodlstatst_uidS_ISDIRst_modeS_IMODE)r rWZtmpdirdirnameZ actual_direZactual_dir_statr r r rUsF          z.FileSystemBytecodeCache._get_default_cache_dirr<cCstj|j|j|jfSr )r\r^r_rSrTrr@r r r _get_cache_filenamesz+FileSystemBytecodeCache._get_cache_filenamec Cs:||}tj|r6t|d}||W5QRXdS)Nrb)rnr\r^existsopenr0)r r=rDr%r r r r0s   z%FileSystemBytecodeCache.load_bytecodec Cs*t||d}||W5QRXdS)Nwb)rqrnr3)r r=r%r r r rA sz%FileSystemBytecodeCache.dump_bytecodec Csbddlm}tt|j|jd}|D]2}z|tj|j|Wq*t k rZYq*Xq*dS)Nr)remove)*) r\rsfnmatchfilterlistdirrSrTr^r_rc)r rsfilesrDr r r rBs zFileSystemBytecodeCache.clear)NrR)rrrr9rrrr!rUrrnr0rArBr r r r rQs /rQc@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/NTrclientprefixrignore_memcache_errorscCs||_||_||_||_dSr r{)r r|r}rr~r r r r!LszMemcachedBytecodeCache.__init__r<cCsDz|j|j|j}Wntk r4|js0Yn X||dSr )r|rr}r Exceptionr~r5)r r=r#r r r r0Xs z$MemcachedBytecodeCache.load_bytecodecCsf|j|j}|}z0|jdk r4|j|||jn|j||Wntk r`|js\YnXdSr )r}rr8rr|rrr~)r r=rrr r r rAas  z$MemcachedBytecodeCache.dump_bytecode)rzNT) rrrr9rrrrboolr!rr0rAr r r r rys/  ry) r9rdrur+r\r)rasysrZtypingrhashlibriortypesr TYPE_CHECKINGZtyping_extensionsterrProtocolrZ bc_versiondumps version_infor(rr;rQryr r r r s6     9Ue