lfk/dZddlZddlZddlZddlZddlZddlZddlmZddlm Z ddlm Z ddl m Z ddl m Z dd l mZdd l mZdd l mZdd lmZd ZdejedzejejddzejdzdzZGddeZGddeZGddeZGddeZdS)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)listdir)path)BytesIO) marshal_dump) marshal_load)pickle) text_type)open_if_existssj2c6eZdZdZdZdZdZdZdZdZ 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. cX||_||_||_|dSN) environmentkeychecksumreset)selfrrrs n/builddir/build/BUILD/imunify360-venv-2.3.5/opt/imunify360/venv/lib/python3.11/site-packages/jinja2/bccache.py__init__zBucket.__init__.s)&   cd|_dS)z)Resets the bucket (unloads the bytecode).N)coders rrz Bucket.reset4s  rc|tt}|tkr|dSt j|}|j|kr|dS t||_dS#tttf$r|YdSwxYw)z/Loads bytecode from a file or file like object.N) readlenbc_magicrr loadrr rEOFError ValueError TypeError)rfmagicrs r load_bytecodezBucket.load_bytecode8ss8}}%% H   JJLLL F;q>> =H $ $ JJLLL F $QDIII*i0    JJLLL FF s?B+CCc|jtd|tt j|j|dt|j|dS)z;Dump the bytecode into the file or file like object passed.Nzcan't write empty bucketr)rr&writer"r dumprr)rr's rwrite_bytecodezBucket.write_bytecodeKsY 9 677 7  DM1a(((TY"""""rcJ|t|dS)zLoad bytecode from a string.N)r)r)rstrings rbytecode_from_stringzBucket.bytecode_from_stringSs" 76??+++++rcpt}|||S)zReturn the bytecode as string.)rr-getvalue)routs rbytecode_to_stringzBucket.bytecode_to_stringWs-ii C   ||~~rN) __name__ __module__ __qualname____doc__rrr)r-r0r4rrrr%sx &###,,,rrc>eZdZdZdZdZdZd dZdZdZ d Z 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 Jinja. ct)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. NotImplementedErrorrbuckets rr)zBytecodeCache.load_bytecode{ "###rct)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. r=r?s r dump_bytecodezBytecodeCache.dump_bytecoderArcdS)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. Nr9rs rclearzBytecodeCache.clearsrNct|d}|Dd|z}t|tr|d}|||S)z3Returns the unique hash key for this template name.utf-8N|)rencode isinstancer update hexdigest)rnamefilenamehashs r get_cache_keyzBytecodeCache.get_cache_keysoDKK(())  X~H(I.. 4#??733 KK ! ! !~~rcjt|dS)z"Returns a checksum for the source.rG)rrIrL)rsources rget_source_checksumz!BytecodeCache.get_source_checksums(FMM'**++55777rc|||}||}t|||}|||S)zwReturn a cache bucket for the given template. All arguments are mandatory but filename may be `None`. )rPrSrr))rrrMrNrRrrr@s r get_bucketzBytecodeCache.get_bucketsV  x00++F33 S(33 6""" rc0||dS)zPut the bucket into the cache.N)rCr?s r set_bucketzBytecodeCache.set_buckets 6"""""rr) r5r6r7r8r)rCrErPrSrUrWr9rrr;r;^s8$$$$$$       888#####rr;c8eZdZdZd dZdZdZdZdZd Z 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.cachecN||}||_||_dSr)_get_default_cache_dir directorypattern)rr]r^s rrz FileSystemBytecodeCache.__init__s+  3355I" rc d}tj}tjdkr|St tds |dtjz}tj||} tj|tj n-#t$r }|j tj krYd}~nd}~wwxYw tj|tj tj|}|jtjks@tj|jr'tj|jtj kr |n-#t$r }|j tj krYd}~nd}~wwxYwtj|}|jtjks@tj|jr'tj|jtj kr ||S)Nc td)NzJCannot determine safe temp directory. You need to explicitly provide one.) RuntimeErrorr9rr _unsafe_dirzCFileSystemBytecodeCache._get_default_cache_dir.._unsafe_dirs2 rntgetuidz_jinja2-cache-%d)tempfile gettempdirosrMhasattrrdrjoinmkdirstatS_IRWXUOSErrorerrnoEEXISTchmodlstatst_uidS_ISDIRst_modeS_IMODE)rrbtmpdirdirname actual_direactual_dir_stats rr\z.FileSystemBytecodeCache._get_default_cache_dirs    $&& 7d??Mr8$$  KMMM$ry{{2W\\&'22   HZ . . . .   w%,&&'&&&&   HZ . . . hz22O&")++55|O$;<<6< 788DLHH    w%,&&'&&&& (:..  "bikk 1 1< 788 2|O344 DD KMMMs1?B C )CC  BE'' F1F  FcPtj|j|j|jzSr)rrir]r^rr?s r_get_cache_filenamez+FileSystemBytecodeCache._get_cache_filenamesy )BCCCrct||d}|E |||dS#|wxYwdS)Nrb)r r|r)closerr@r's rr)z%FileSystemBytecodeCache.load_bytecodesg 433F;;T B B = $$Q'''  =s AA(ct||d} |||dS#|wxYw)Nwb)openr|r-rrs rrCz%FileSystemBytecodeCache.dump_bytecodes[ ))&114 8 8   ! !! $ $ $ GGIIIIIAGGIIIIs AA&cddlm}tjt |j|jdz}|D]6} |tj|j|'#t$rY3wxYwdS)Nr)remove*) rgrfnmatchfilterrr]r^rrirm)rrfilesrNs rrEzFileSystemBytecodeCache.clears wt~66 s8JKK  H ty::;;;;      s#A A,+A,)NrZ) r5r6r7r8rr\r|r)rCrEr9rrrYrYs" ---^DDD     rrYc,eZdZdZ ddZdZdZdS) 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 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/NTc>||_||_||_||_dSr)clientprefixtimeoutignore_memcache_errors)rrrrrs rrzMemcachedBytecodeCache.__init__@s'   &<###rc |j|j|jz}n#t$r |jsd}YnwxYw|||dSdSr)rgetrr Exceptionrr0)rr@rs rr)z$MemcachedBytecodeCache.load_bytecodeLs ;??4;#;<rs  !!!!!!!!!!!!!!!!!!   fl:q!!"flC$Q'2-1A!1DDaHHI 66666V666rK#K#K#K#K#FK#K#K#\dddddmdddNKKKKK]KKKKKr