zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
U
Þ=®dí6ã@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl    Z
ddl m Z ddl mZddlmZe
jrœddlZddlmZGdd    „d    ejƒZd
Zd e ed ¡e ejdd >ejdBd ¡ZGdd„dƒZGdd„dƒZGdd„deƒZGdd„deƒZdS)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@s8eZdZeedœdd„Zdeeejeddœdd„Z    dS)    Ú_MemcachedClient)ÚkeyÚreturncCsdS©N©)Úselfrr r úEd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\jinja2/bccache.pyÚgetsz_MemcachedClient.getN)rÚvalueÚtimeoutr    cCsdSr
r )r rrrr r r Úsetsz_MemcachedClient.set)N)
Ú__name__Ú
__module__Ú __qualname__ÚstrÚbytesrÚtÚOptionalÚintrr r r r rsrésj2ééc@sxeZdZdZdeeddœdd„Zddœdd    „Zejdd
œd d „Z    ej
e dd
œd d„Z e ddœdd„Z e dœdd„ZdS)Ú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)Ú environmentrÚchecksumr    cCs||_||_||_| ¡dSr
)rrrÚreset)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
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) ÚreadÚlenÚbc_magicr ÚpickleÚloadrÚmarshalr#ÚEOFErrorÚ
ValueErrorÚ    TypeError)r r%Úmagicrr r r Ú load_bytecode?s
 
zBucket.load_bytecodecCs>|jdkrtdƒ‚| t¡t |j|d¡t |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.)rr3Úgetvalue)r Úoutr r r Úbytecode_to_string^s
zBucket.bytecode_to_string)rrrÚ__doc__rr!r rÚBinaryIOr0ÚIOrr3r5r8r r r r r,src@sšeZdZdZeddœdd„Zeddœdd„Zddœd    d
„Zdee    j
e    j eed œd d „Z eedœdd„Z dee    j
eeedœdd„Zeddœdd„ZdS)Ú BytecodeCacheaÞTo 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
tƒ‚dS)z¹Subclasses 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 r>r r r r0‚szBytecodeCache.load_bytecodecCs
tƒ‚dS)zÀSubclasses 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?rAr r r Ú dump_bytecode‰szBytecodeCache.dump_bytecoder"cCsdS)zºClears 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 ÚclearszBytecodeCache.clear)ÚnameÚfilenamer    cCs2t| d¡ƒ}|dk    r*| d|› ¡¡| ¡S)z3Returns the unique hash key for this template name.úutf-8Nú|)rÚencodeÚupdateÚ    hexdigest)r rDrEÚhashr r r Ú get_cache_key–szBytecodeCache.get_cache_key)Úsourcer    cCst| d¡ƒ ¡S)z"Returns a checksum for the source.rF)rrHrJ)r rMr r r Úget_source_checksum¡sz!BytecodeCache.get_source_checksumr)rrDrErMr    cCs0| ||¡}| |¡}t|||ƒ}| |¡|S)zwReturn a cache bucket for the given template.  All arguments are
        mandatory but filename may be `None`.
        )rLrNrr0)r rrDrErMrrr>r r r Ú
get_bucket¥s
 
 
 
zBytecodeCache.get_bucketcCs| |¡dS)zPut the bucket into the cache.N)rBrAr r r Ú
set_bucketµszBytecodeCache.set_bucket)N)rrrr9rr0rBrCrrrÚUnionrLrNrOrPr r r r r<es$ÿþ ú r<c@sveZdZdZdejeeddœdd„Zedœdd    „Ze    ed
œd d „Z
e    dd
œd d„Z e    dd
œdd„Z ddœdd„Z dS)ÚFileSystemBytecodeCacheaïA 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)Ú    directoryÚpatternr    cCs |dkr| ¡}||_||_dSr
)Ú_get_default_cache_dirrTrU)r rTrUr r r r!Ìsz FileSystemBytecodeCache.__init__r"c
Cs^ddœdd„}t ¡}tjdkr$|Sttdƒs4|ƒdt ¡›}tj ||¡}zt |t    j
¡Wn0t k
r’}z|j t j kr‚‚W5d}~XYnXzNt |t    j
¡t |¡}|jt ¡ksÚt     |j¡rÚt     |j¡t    j
krà|ƒWn4t k
r}z|j t j kr‚W5d}~XYnXt |¡}|jt ¡ksTt     |j¡rTt     |j¡t    j
krZ|ƒ|S)Nz te.NoReturnr"cSs tdƒ‚dS)NzJCannot determine safe temp directory.  You need to explicitly provide one.)Ú RuntimeErrorr r r r Ú _unsafe_dirÕsÿzCFileSystemBytecodeCache._get_default_cache_dir.<locals>._unsafe_dirÚntÚgetuidz_jinja2-cache-)ÚtempfileÚ
gettempdirÚosrDÚhasattrrZÚpathÚjoinÚmkdirÚstatÚS_IRWXUÚOSErrorÚerrnoÚEEXISTÚchmodÚlstatÚst_uidÚS_ISDIRÚst_modeÚS_IMODE)r rXZtmpdirÚdirnameZ
actual_dirÚeZactual_dir_statr r r rVÔsF
 
 
 ÿ
þý
 
 ÿ
þýz.FileSystemBytecodeCache._get_default_cache_dirr=cCstj |j|j|jf¡Sr
)r]r_r`rTrUrrAr r r Ú_get_cache_filenamesz+FileSystemBytecodeCache._get_cache_filenamec
CsT| |¡}zt|dƒ}Wntttfk
r4YdSX|| |¡W5QRXdS)NÚrb)roÚopenÚFileNotFoundErrorÚIsADirectoryErrorÚPermissionErrorr0)r r>rEr%r r r r0s
z%FileSystemBytecodeCache.load_bytecodec    sÈ| |¡}tjdtj |¡tj |¡ddd‰ddœ‡fdd„ }zˆ| ˆ¡W5QRXWntk
rz|ƒ‚YnXzt     ˆj
|¡Wn4t k
r¨|ƒYntk
rÂ|ƒ‚YnXdS)NÚwbz.tmpF)ÚmodeÚdirÚprefixÚsuffixÚdeleter"cs*zt ˆj¡Wntk
r$YnXdSr
)r]ÚremoverDrdr ©r%r r Ú remove_silent"sz<FileSystemBytecodeCache.dump_bytecode.<locals>.remove_silent) ror[ÚNamedTemporaryFiler]r_rmÚbasenamer3Ú BaseExceptionÚreplacerDrd)r r>rDr}r r|r rBs,
 
 
û
z%FileSystemBytecodeCache.dump_bytecodec    Csbddlm}t t |j¡|jd¡}|D]2}z|tj |j|¡ƒWq*t    k
rZYq*Xq*dS)Nr)r{)Ú*)
r]r{ÚfnmatchÚfilterÚlistdirrTrUr_r`rd)r r{ÚfilesrEr r r rC;s zFileSystemBytecodeCache.clear)NrS)rrrr9rrrr!rVrror0rBrCr r r r rRºsÿþ /&rRc@sLeZdZdZddeejeedœdd„Z    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 <https://github.com/pallets/cachelib>`_
    -   `python-memcached <https://pypi.org/project/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©ÚclientrxrÚignore_memcache_errorscCs||_||_||_||_dSr
r‰)r rŠrxrr‹r r r r!vszMemcachedBytecodeCache.__init__r=cCsDz|j |j|j¡}Wntk
r4|js0‚Yn X| |¡dSr
)rŠrrxrÚ    Exceptionr‹r5)r r>r#r r r r0‚s z$MemcachedBytecodeCache.load_bytecodecCsf|j|j}| ¡}z0|jdk    r4|j |||j¡n|j ||¡Wntk
r`|js\‚YnXdSr
)rxrr8rrŠrrŒr‹)r r>rrr r r rB‹s 
z$MemcachedBytecodeCache.dump_bytecode)rˆNT) rrrr9rrrrÚboolr!rr0rBr r r r r‡Is/ûû     r‡) r9rerƒr+r]r)rbÚsysr[ÚtypingrÚhashlibrÚiorÚtypesrÚ TYPE_CHECKINGZtyping_extensionsÚterrÚProtocolrZ
bc_versionÚdumpsÚ version_infor(rr<rRr‡r r r r Ú<module>s8    
ÿþÿ9U