f6dZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl mZddlmZe j"rddlZddlmZGdd ej,Zd Zd ej2ed zej2ej4dd zej4dzd zZGddZGddZGddeZGddeZy)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 ReZdZdedefdZ ddededejeddfdZ y) _MemcachedClientkeyreturncyN)selfr s W/var/lib/jenkins/workspace/mettalog/venv/lib/python3.12/site-packages/jinja2/bccache.pygetz_MemcachedClient.gets#Nvaluetimeoutcyr r)rr rrs rsetz_MemcachedClient.setsrr ) __name__ __module__ __qualname__strbytesrtOptionalintrrrrr r sC-3-5-FJ  #( 34::c?   rr sj2ceZdZdZdddededdfdZdd Zd ejddfd Z d eje ddfd Z d e ddfdZ de fdZy)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. environmentrr checksumr NcN||_||_||_|jyr )r$r r%reset)rr$r r%s r__init__zBucket.__init__7s!&   rcd|_y)z)Resets the bucket (unloads the bytecode).N)coders rr'z Bucket.reset=s *. rfch|jtt}|tk7r|jyt j |}|j |k7r|jy tj ||_y#tttf$r|jYywxYw)z/Loads bytecode from a file or file like object.N) readlenbc_magicr'pickleloadr%marshalr*EOFError ValueError TypeError)rr,magicr%s r load_bytecodezBucket.load_bytecodeAss8}% H  JJL ;;q> ==H $ JJL   QDI*i0  JJL  s/B $B10B1c|j td|jtt j |j |dtj |j|y)z;Dump the bytecode into the file or file like object passed.Nzcan't write empty bucketr )r*r6writer0r1dumpr%r3)rr,s rwrite_bytecodezBucket.write_bytecodeTsJ 99 67 7  DMM1a( TYY"rstringc8|jt|y)zLoad bytecode from bytes.N)r8r)rr=s rbytecode_from_stringzBucket.bytecode_from_string\s 76?+rcXt}|j||jS)zReturn the bytecode as bytes.)rr<getvalue)routs rbytecode_to_stringzBucket.bytecode_to_string`s#i C ||~rr N)rrr__doc__rr(r'rBinaryIOr8IOrr<r?rCrrrr#r#.s~Mst /qzzd&#U ##,5,T,Err#c eZdZdZdeddfdZdeddfdZddZ dded e je jedefd Z d edefd Z d dded e jed edef dZdeddfdZy) 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. bucketr Nct)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. NotImplementedErrorrrJs rr8zBytecodeCache.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. rLrNs r dump_bytecodezBytecodeCache.dump_bytecoderOrcy)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. Nrr+s rclearzBytecodeCache.clearsrnamefilenamect|jd}|"|jd|j|jS)z3Returns the unique hash key for this template name.utf-8|)rencodeupdate hexdigest)rrTrUhashs r get_cache_keyzBytecodeCache.get_cache_keysEDKK()   KK!H:--/ 0~~rsourcecRt|jdjS)z"Returns a checksum for the source.rW)rrYr[)rr^s rget_source_checksumz!BytecodeCache.get_source_checksumsFMM'*+5577rr$rc|j||}|j|}t|||}|j||S)zwReturn a cache bucket for the given template. All arguments are mandatory but filename may be `None`. )r]r`r#r8)rr$rTrUr^r r%rJs r get_bucketzBytecodeCache.get_bucketsG  x0++F3 S(3 6" rc&|j|y)zPut the bucket into the cache.N)rQrNs r set_bucketzBytecodeCache.set_buckets 6"rrDr )rrrrEr#r8rQrSrrrUnionr]r`rbrdrrrrIrIgs8$F$t$$F$t$ ?C    #$::aggcl#;    8#8#8"**S/      ##D#rrIceZdZdZ d dej ededdfdZdefdZde defd Z de ddfd Z de ddfd Z dd Z y)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 directorypatternr cD||j}||_||_yr )_get_default_cache_dirrhri)rrhris rr(z FileSystemBytecodeCache.__init__s&  335I" rcdd}tj}tjdk(r|St tds|dtj }tj j||} tj|tj tj|tjtj|}|j tj k7sOtj"|j$r0tj&|j$tjk7r|tj|}|j tj k7sOtj"|j$r0tj&|j$tjk7r||S#t$r)}|jtjk7rYd}~ld}~wwxYw#t$r(}|jtjk7rYd}~d}~wwxYw)Nctd)NzJCannot determine safe temp directory. You need to explicitly provide one.) RuntimeErrorrrr _unsafe_dirzCFileSystemBytecodeCache._get_default_cache_dir.._unsafe_dirs2 rntgetuidz_jinja2-cache-)r z te.NoReturn)tempfile gettempdirosrThasattrrqpathjoinmkdirstatS_IRWXUOSErrorerrnoEEXISTchmodlstatst_uidS_ISDIRst_modeS_IMODE)rrotmpdirdirname actual_direactual_dir_stats rrkz.FileSystemBytecodeCache._get_default_cache_dirs  $$& 77d?Mr8$ M"299;-0WW\\&'2   HHZ .  HHZ . hhz2O&&"))+5||O$;$;<<< 7 78DLLH ((:.  " "biik 1<< 7 78||O334 D M1 ww%,,&'  ww%,,&' s1=$G "B0H H)H  H IIIrJctjj|j|j|j fzSr )rtrvrwrhrir rNs r_get_cache_filenamez+FileSystemBytecodeCache._get_cache_filenames*ww||DNNDLLFJJ=,HIIrc|j|} t|d}|5|j |dddy#tttf$rYywxYw#1swYyxYw)Nrb)ropenFileNotFoundErrorIsADirectoryErrorPermissionErrorr8)rrJrUr,s rr8z%FileSystemBytecodeCache.load_bytecodesn++F3 Xt$A  $   # $ $ "#4oF     $ $s <AAAAc|j|}tjdtjj |tjj |dddfd } 5|jddd tjj|y#1swY+xYw#t$r |wxYw#t$r |Yyt$r |wxYw)Nwbz.tmpF)modedirprefixsuffixdeletecd tjjy#t$rYywxYwr )rtremoverTr{)r,sr remove_silentz.remove_silent$s,  !&&!  s # //rD) rrrNamedTemporaryFilertrvrbasenamer< BaseExceptionreplacerTr{)rrJrTrr,s @rrQz%FileSystemBytecodeCache.dump_bytecodes''/  ' '%77##D)     )%%a( )   JJqvvt $ ) )  O     O  O  s6.B80B,B8 C ,B51B88C  C0C0cddlm}tjtj|j |j dz}|D]3} |tjj|j |5y#t$rYBwxYw)Nr)r)*) rtrfnmatchfilterlistdirrhrirvrwr{)rrfilesrUs rrSzFileSystemBytecodeCache.clear=sk rzz$..94<<&;PQH rww||DNNH=>  s 0B B  B )Nz__jinja2_%s.cacherD)rrrrErrrr(rkr#rr8rQrSrrrrgrgs$ATC:= --^J&JSJ $F $t $$F$t$L rrgc jeZdZdZ d dddedej edefdZ d e d dfd Z d e d dfd Z y)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. Nclientr rrignore_memcache_errorsc<||_||_||_||_yr )rrrr)rrrrrs rr(zMemcachedBytecodeCache.__init__xs!   &<#rrJr c |jj|j|jz}|j |y#t $r|j sYywxYwr )rrrr r? Exceptionr)rrJr*s rr8z$MemcachedBytecodeCache.load_bytecodesX .;;??4;;#;rs  ??"(2;;  fll:q!"fllC$$Q'2-1A1A!1DDaHI 66rR#R#jLmL^M]Mr