<s> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import re <EOL> import logging <EOL> from floyd . helpers . dateformat import * <EOL> try : <EOL> import memcache <EOL> except ImportError : <EOL> memcache = { } <EOL> try : <EOL> import jinja2 <EOL> except ImportError : <EOL> logging . info ( '<STR_LIT>' ) <EOL> logging . info ( sys . path ) <EOL> raise NotFound ( '<STR_LIT>' ) <EOL> _jinja_env = None <EOL> _jinja_loaders = { } <EOL> class JinjaEnviroment ( jinja2 . Environment ) : <EOL> def join_path ( self , template , parent = None ) : <EOL> if '<STR_LIT::>' in template : <EOL> sep , template = template . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> return "<STR_LIT>" % ( sep , parent , template ) <EOL> elif '<STR_LIT:/>' in parent : <EOL> return os . path . normpath ( os . path . join ( parent , '<STR_LIT:..>' , template ) ) <EOL> elif parent . endswith ( '<STR_LIT>' ) : <EOL> return template <EOL> return "<STR_LIT>" % ( parent , template ) <EOL> class MemcacheBytecodeCache ( jinja2 . BytecodeCache ) : <EOL> def __init__ ( self , client = None , prefix = '<STR_LIT>' , timeout = None ) : <EOL> self . client = memcache <EOL> self . prefix = prefix <EOL> self . timeout = timeout <EOL> def load_bytecode ( self , bucket ) : <EOL> code = self . client . get ( self . prefix + bucket . key ) <EOL> if code is not None : <EOL> bucket . bytecode_from_string ( code ) <EOL> def dump_bytecode ( self , bucket ) : <EOL> args = ( self . prefix + bucket . key , bucket . bytecode_to_string ( ) ) <EOL> if self . timeout is not None : <EOL> args += ( self . timeout , ) <EOL> self . client . set ( * args ) <EOL> class CompiledLoader ( jinja2 . BaseLoader ) : <EOL> """<STR_LIT>""" <EOL> def load ( self , env , name , glob = { } ) : <EOL> source , filename , uptodate = self . get_source ( environment , name ) <EOL> code = compile ( source , filename , '<STR_LIT>' ) <EOL> return env . template_class . from_code ( env , code , glob , uptodate ) <EOL> class SubdirLoader ( jinja2 . 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 jinja2 . TemplateNotFound ( template ) <EOL> try : <EOL> return loader . get_source ( environment , name ) <EOL> except jinja2 . TemplateNotFound : <EOL> raise jinja2 . 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> def setup_cache ( ) : <EOL> pass <EOL> def setup ( template_paths = { } , autoescape = False , cache_size = <NUM_LIT:100> , auto_reload = True , bytecode_cache = True ) : <EOL> """<STR_LIT>""" <EOL> global _jinja_env , _jinja_loaders <EOL> if not _jinja_env : <EOL> _jinja_env = JinjaEnviroment ( <EOL> autoescape = autoescape , <EOL> cache_size = cache_size , <EOL> auto_reload = auto_reload , <EOL> bytecode_cache = None ) <EOL> if type ( template_paths ) == type ( '<STR_LIT>' ) : <EOL> template_paths = { '<STR_LIT>' : template_paths } <EOL> if len ( template_paths ) < <NUM_LIT:1> : <EOL> logging . exception ( '<STR_LIT>' ) <EOL> return False <EOL> if len ( template_paths ) == <NUM_LIT:1> : <EOL> template_set_name = template_paths . keys ( ) [ <NUM_LIT:0> ] <EOL> tp = template_paths [ template_set_name ] <EOL> if tp in _jinja_loaders : <EOL> _jinja_env . loader = _jinja_loaders [ tp ] <EOL> else : <EOL> _jinja_env . loader = _jinja_loaders [ tp ] = jinja2 . FileSystemLoader ( tp ) <EOL> return True <EOL> if len ( template_paths ) > <NUM_LIT:1> : <EOL> loaders = { } <EOL> for dirn , path in template_paths . items ( ) : <EOL> loaders [ dirn ] = jinja2 . FileSystemLoader ( path ) <EOL> _jinja_env . loader = SubdirLoader ( loaders ) <EOL> return True <EOL> logging . error ( '<STR_LIT>' ) <EOL> logging . error ( _jinja_loaders ) <EOL> def render ( template_name , template_vars = { } , template_set = '<STR_LIT>' , template_theme = None , template_extension = '<STR_LIT:html>' , template_content = None ) : <EOL> """<STR_LIT>""" <EOL> global _jinja_env <EOL> if not _jinja_env : <EOL> raise '<STR_LIT>' <EOL> try : <EOL> _jinja_env . filters [ '<STR_LIT>' ] = timesince <EOL> _jinja_env . filters [ '<STR_LIT>' ] = timeuntil <EOL> _jinja_env . filters [ '<STR_LIT:date>' ] = date_format <EOL> _jinja_env . filters [ '<STR_LIT:time>' ] = time_format <EOL> _jinja_env . filters [ '<STR_LIT>' ] = short_date <EOL> _jinja_env . filters [ '<STR_LIT>' ] = iso_date <EOL> _jinja_env . filters [ '<STR_LIT>' ] = rfc2822_date <EOL> _jinja_env . filters [ '<STR_LIT>' ] = datetimeformat <EOL> _jinja_env . filters [ '<STR_LIT>' ] = timestamp <EOL> except NameError as errstr : <EOL> logging . info ( '<STR_LIT>' % errstr ) <EOL> _template_name = "<STR_LIT>" % ( template_name , template_extension ) <EOL> template = _jinja_env . get_template ( _template_name , parent = template_theme ) <EOL> return template . render ( template_vars ) <EOL> def compile_file ( env , src_path , dst_path , encoding = '<STR_LIT:utf-8>' , base_dir = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> src_file = file ( src_path , '<STR_LIT:r>' ) <EOL> source = src_file . read ( ) . decode ( encoding ) <EOL> name = src_path . replace ( base_dir , '<STR_LIT>' ) <EOL> raw = env . compile ( source , name = name , filename = name , raw = True ) <EOL> src_file . close ( ) <EOL> dst_file = open ( dst_path , '<STR_LIT:w>' ) <EOL> dst_file . write ( raw ) <EOL> dst_file . close ( ) <EOL> def compile_dir ( env , src_path , dst_path , pattern = r'<STR_LIT>' , encoding = '<STR_LIT:utf-8>' , base_dir = None ) : <EOL> """<STR_LIT>""" <EOL> from os import path , listdir , mkdir <EOL> file_re = re . compile ( pattern ) <EOL> if base_dir is None : <EOL> base_dir = src_path <EOL> for filename in listdir ( src_path ) : <EOL> src_name = path . join ( src_path , filename ) <EOL> dst_name = path . join ( dst_path , filename ) <EOL> if path . isdir ( src_name ) : <EOL> mkdir ( dst_name ) <EOL> compile_dir ( env , src_name , dst_name , encoding = encoding , base_dir = base_dir ) <EOL> elif path . isfile ( src_name ) and file_re . match ( filename ) : <EOL> compile_file ( env , src_name , dst_name , encoding = encoding , base_dir = base_dir ) </s>
