<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import weakref <EOL> from types import ModuleType <EOL> from os import path <EOL> try : <EOL> from hashlib import sha1 <EOL> except ImportError : <EOL> from sha import new as sha1 <EOL> from jinja2 . exceptions import TemplateNotFound <EOL> from jinja2 . utils import LRUCache , open_if_exists , internalcode <EOL> def split_template_path ( template ) : <EOL> """<STR_LIT>""" <EOL> pieces = [ ] <EOL> for piece in template . split ( '<STR_LIT:/>' ) : <EOL> if path . sep in piece or ( path . altsep and path . altsep in piece ) or piece == path . pardir : <EOL> raise TemplateNotFound ( template ) <EOL> elif piece and piece != '<STR_LIT:.>' : <EOL> pieces . append ( piece ) <EOL> return pieces <EOL> class BaseLoader ( object ) : <EOL> """<STR_LIT>""" <EOL> has_source_access = True <EOL> def get_source ( self , environment , template ) : <EOL> """<STR_LIT>""" <EOL> if not self . has_source_access : <EOL> raise RuntimeError ( '<STR_LIT>' % <EOL> self . __class__ . __name__ ) <EOL> raise TemplateNotFound ( template ) <EOL> def list_templates ( self ) : <EOL> """<STR_LIT>""" <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> @ internalcode <EOL> def load ( self , environment , name , globals = None ) : <EOL> """<STR_LIT>""" <EOL> code = None <EOL> if globals is None : <EOL> globals = { } <EOL> source , filename , uptodate = self . get_source ( environment , name ) <EOL> bcc = environment . bytecode_cache <EOL> if bcc is not None : <EOL> bucket = bcc . get_bucket ( environment , name , filename , source ) <EOL> code = bucket . code <EOL> if code is None : <EOL> code = environment . compile ( source , name , filename ) <EOL> if bcc is not None and bucket . code is None : <EOL> bucket . code = code <EOL> bcc . set_bucket ( bucket ) <EOL> return environment . template_class . from_code ( environment , code , <EOL> globals , uptodate ) <EOL> class FileSystemLoader ( BaseLoader ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , searchpath , encoding = '<STR_LIT:utf-8>' ) : <EOL> if isinstance ( searchpath , basestring ) : <EOL> searchpath = [ searchpath ] <EOL> self . searchpath = list ( searchpath ) <EOL> self . encoding = encoding <EOL> def get_source ( self , environment , template ) : <EOL> pieces = split_template_path ( template ) <EOL> for searchpath in self . searchpath : <EOL> filename = path . join ( searchpath , * pieces ) <EOL> f = open_if_exists ( filename ) <EOL> if f is None : <EOL> continue <EOL> try : <EOL> contents = f . read ( ) . decode ( self . encoding ) <EOL> finally : <EOL> f . close ( ) <EOL> mtime = path . getmtime ( filename ) <EOL> def uptodate ( ) : <EOL> try : <EOL> return path . getmtime ( filename ) == mtime <EOL> except OSError : <EOL> return False <EOL> return contents , filename , uptodate <EOL> raise TemplateNotFound ( template ) <EOL> def list_templates ( self ) : <EOL> found = set ( ) <EOL> for searchpath in self . searchpath : <EOL> for dirpath , dirnames , filenames in os . walk ( searchpath ) : <EOL> for filename in filenames : <EOL> template = os . path . join ( dirpath , filename ) [ len ( searchpath ) : ] . strip ( os . path . sep ) . replace ( os . path . sep , '<STR_LIT:/>' ) <EOL> if template [ : <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> template = template [ <NUM_LIT:2> : ] <EOL> if template not in found : <EOL> found . add ( template ) <EOL> return sorted ( found ) <EOL> class PackageLoader ( BaseLoader ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , package_name , package_path = '<STR_LIT>' , <EOL> encoding = '<STR_LIT:utf-8>' ) : <EOL> from pkg_resources import DefaultProvider , ResourceManager , get_provider <EOL> provider = get_provider ( package_name ) <EOL> self . encoding = encoding <EOL> self . manager = ResourceManager ( ) <EOL> self . filesystem_bound = isinstance ( provider , DefaultProvider ) <EOL> self . provider = provider <EOL> self . package_path = package_path <EOL> def get_source ( self , environment , template ) : <EOL> pieces = split_template_path ( template ) <EOL> p = '<STR_LIT:/>' . join ( ( self . package_path , ) + tuple ( pieces ) ) <EOL> if not self . provider . has_resource ( p ) : <EOL> raise TemplateNotFound ( template ) <EOL> filename = uptodate = None <EOL> if self . filesystem_bound : <EOL> filename = self . provider . get_resource_filename ( self . manager , p ) <EOL> mtime = path . getmtime ( filename ) <EOL> def uptodate ( ) : <EOL> try : <EOL> return path . getmtime ( filename ) == mtime <EOL> except OSError : <EOL> return False <EOL> source = self . provider . get_resource_string ( self . manager , p ) <EOL> return source . decode ( self . encoding ) , filename , uptodate <EOL> def list_templates ( self ) : <EOL> path = self . package_path <EOL> if path [ : <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> path = path [ <NUM_LIT:2> : ] <EOL> elif path == '<STR_LIT:.>' : <EOL> path = '<STR_LIT>' <EOL> offset = len ( path ) <EOL> results = [ ] <EOL> def _walk ( path ) : <EOL> for filename in self . provider . resource_listdir ( path ) : <EOL> fullname = path + '<STR_LIT:/>' + filename <EOL> if self . provider . resource_isdir ( fullname ) : <EOL> _walk ( fullname ) <EOL> else : <EOL> results . append ( fullname [ offset : ] . lstrip ( '<STR_LIT:/>' ) ) <EOL> _walk ( path ) <EOL> results . sort ( ) <EOL> return results <EOL> class DictLoader ( BaseLoader ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , mapping ) : <EOL> self . mapping = mapping <EOL> def get_source ( self , environment , template ) : <EOL> if template in self . mapping : <EOL> source = self . mapping [ template ] <EOL> return source , None , lambda : source != self . mapping . get ( template ) <EOL> raise TemplateNotFound ( template ) <EOL> def list_templates ( self ) : <EOL> return sorted ( self . mapping ) <EOL> class FunctionLoader ( BaseLoader ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , load_func ) : <EOL> self . load_func = load_func <EOL> def get_source ( self , environment , template ) : <EOL> rv = self . load_func ( template ) <EOL> if rv is None : <EOL> raise TemplateNotFound ( template ) <EOL> elif isinstance ( rv , basestring ) : <EOL> return rv , None , None <EOL> return rv <EOL> class PrefixLoader ( BaseLoader ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , mapping , delimiter = '<STR_LIT:/>' ) : <EOL> self . mapping = mapping <EOL> self . delimiter = delimiter <EOL> def get_source ( self , environment , template ) : <EOL> try : <EOL> prefix , name = template . split ( self . delimiter , <NUM_LIT:1> ) <EOL> loader = self . mapping [ prefix ] <EOL> except ( ValueError , KeyError ) : <EOL> raise TemplateNotFound ( template ) <EOL> try : <EOL> return loader . get_source ( environment , name ) <EOL> except TemplateNotFound : <EOL> raise TemplateNotFound ( template ) <EOL> def list_templates ( self ) : <EOL> result = [ ] <EOL> for prefix , loader in self . mapping . iteritems ( ) : <EOL> for template in loader . list_templates ( ) : <EOL> result . append ( prefix + self . delimiter + template ) <EOL> return result <EOL> class ChoiceLoader ( BaseLoader ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , loaders ) : <EOL> self . loaders = loaders <EOL> def get_source ( self , environment , template ) : <EOL> for loader in self . loaders : <EOL> try : <EOL> return loader . get_source ( environment , template ) <EOL> except TemplateNotFound : <EOL> pass <EOL> raise TemplateNotFound ( template ) <EOL> def list_templates ( self ) : <EOL> found = set ( ) <EOL> for loader in self . loaders : <EOL> found . update ( loader . list_templates ( ) ) <EOL> return sorted ( found ) <EOL> class _TemplateModule ( ModuleType ) : <EOL> """<STR_LIT>""" <EOL> class ModuleLoader ( BaseLoader ) : <EOL> """<STR_LIT>""" <EOL> has_source_access = False <EOL> def __init__ ( self , path ) : <EOL> package_name = '<STR_LIT>' % id ( self ) <EOL> mod = _TemplateModule ( package_name ) <EOL> if isinstance ( path , basestring ) : <EOL> path = [ path ] <EOL> else : <EOL> path = list ( path ) <EOL> mod . __path__ = path <EOL> sys . modules [ package_name ] = weakref . proxy ( mod , <EOL> lambda x : sys . modules . pop ( package_name , None ) ) <EOL> self . module = mod <EOL> self . package_name = package_name <EOL> @ staticmethod <EOL> def get_template_key ( name ) : <EOL> return '<STR_LIT>' + sha1 ( name . encode ( '<STR_LIT:utf-8>' ) ) . hexdigest ( ) <EOL> @ staticmethod <EOL> def get_module_filename ( name ) : <EOL> return ModuleLoader . get_template_key ( name ) + '<STR_LIT>' <EOL> @ internalcode <EOL> def load ( self , environment , name , globals = None ) : <EOL> key = self . get_template_key ( name ) <EOL> module = '<STR_LIT>' % ( self . package_name , key ) <EOL> mod = getattr ( self . module , module , None ) <EOL> if mod is None : <EOL> try : <EOL> mod = __import__ ( module , None , None , [ '<STR_LIT:root>' ] ) <EOL> except ImportError : <EOL> raise TemplateNotFound ( name ) <EOL> sys . modules . pop ( module , None ) <EOL> return environment . template_class . from_module_dict ( <EOL> environment , mod . __dict__ , globals ) </s>
