File manager - Edit - /opt/imh-python/lib/python2.7/site-packages/jinja2/bccache.pyc
Back
� �t�^c @ s| d Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d Z d e j e d � e j e j d d >e j d Bd � Z d e f d � � YZ d e f d � � YZ d e f d � � YZ d e f d � � YZ d S( s 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. i����N( t sha1( t listdir( t pathi ( t BytesIO( t marshal_dump( t marshal_load( t pickle( t text_type( t open_if_existsi t j2i i i t Bucketc B sD e Z d Z d � Z d � Z d � Z d � Z d � Z d � Z RS( su Buckets 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. c C s) | | _ | | _ | | _ | j � d S( N( t environmentt keyt checksumt reset( t selfR R R ( ( s= /opt/imh-python/lib/python2.7/site-packages/jinja2/bccache.pyt __init__. s c C s d | _ d S( s) Resets the bucket (unloads the bytecode).N( t Nonet code( R ( ( s= /opt/imh-python/lib/python2.7/site-packages/jinja2/bccache.pyR 4 s c C s� | j t t � � } | t k r/ | j � d St j | � } | j | k r[ | j � d Sy t | � | _ Wn% t t t f k r� | j � d SXd S( s/ Loads bytecode from a file or file like object.N( t readt lent bc_magicR R t loadR R R t EOFErrort ValueErrort TypeError( R t ft magicR ( ( s= /opt/imh-python/lib/python2.7/site-packages/jinja2/bccache.pyt load_bytecode8 s c C sU | j d k r t d � � n | j t � t j | j | d � t | j | � d S( s; Dump the bytecode into the file or file like object passed.s can't write empty bucketi N( R R R t writeR R t dumpR R ( R R ( ( s= /opt/imh-python/lib/python2.7/site-packages/jinja2/bccache.pyt write_bytecodeK s c C s | j t | � � d S( s Load bytecode from a string.N( R R ( R t string( ( s= /opt/imh-python/lib/python2.7/site-packages/jinja2/bccache.pyt bytecode_from_stringS s c C s t � } | j | � | j � S( s Return the bytecode as string.( R R t getvalue( R t out( ( s= /opt/imh-python/lib/python2.7/site-packages/jinja2/bccache.pyt bytecode_to_stringW s ( t __name__t __module__t __doc__R R R R R! R$ ( ( ( s= /opt/imh-python/lib/python2.7/site-packages/jinja2/bccache.pyR % s t BytecodeCachec B sP e Z d Z d � Z d � Z d � Z d d � Z d � Z d � Z d � Z RS( s� 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. c C s t � � d S( s� 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( t NotImplementedError( R t bucket( ( s= /opt/imh-python/lib/python2.7/site-packages/jinja2/bccache.pyR { s c C s t � � d S( s� 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. N( R) ( R R* ( ( s= /opt/imh-python/lib/python2.7/site-packages/jinja2/bccache.pyt dump_bytecode� s c C s d S( s� 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. N( ( R ( ( s= /opt/imh-python/lib/python2.7/site-packages/jinja2/bccache.pyt clear� s c C sf t | j d � � } | d k r\ d | } t | t � rL | j d � } n | j | � n | j � S( s3 Returns the unique hash key for this template name.s utf-8t |N( R t encodeR t isinstanceR t updatet hexdigest( R t namet filenamet hash( ( s= /opt/imh-python/lib/python2.7/site-packages/jinja2/bccache.pyt get_cache_key� s c C s t | j d � � j � S( s"