<s> """<STR_LIT>""" <EOL> from mako . lexer import Lexer <EOL> from mako import runtime , util , exceptions , codegen , cache , compat <EOL> import os <EOL> import re <EOL> import shutil <EOL> import stat <EOL> import sys <EOL> import tempfile <EOL> import types <EOL> import weakref <EOL> class Template ( object ) : <EOL> """<STR_LIT>""" <EOL> lexer_cls = Lexer <EOL> def __init__ ( self , <EOL> text = None , <EOL> filename = None , <EOL> uri = None , <EOL> format_exceptions = False , <EOL> error_handler = None , <EOL> lookup = None , <EOL> output_encoding = None , <EOL> encoding_errors = '<STR_LIT:strict>' , <EOL> module_directory = None , <EOL> cache_args = None , <EOL> cache_impl = '<STR_LIT>' , <EOL> cache_enabled = True , <EOL> cache_type = None , <EOL> cache_dir = None , <EOL> cache_url = None , <EOL> module_filename = None , <EOL> input_encoding = None , <EOL> disable_unicode = False , <EOL> module_writer = None , <EOL> bytestring_passthrough = False , <EOL> default_filters = None , <EOL> buffer_filters = ( ) , <EOL> strict_undefined = False , <EOL> imports = None , <EOL> future_imports = None , <EOL> enable_loop = True , <EOL> preprocessor = None , <EOL> lexer_cls = None ) : <EOL> if uri : <EOL> self . module_id = re . sub ( r'<STR_LIT>' , "<STR_LIT:_>" , uri ) <EOL> self . uri = uri <EOL> elif filename : <EOL> self . module_id = re . sub ( r'<STR_LIT>' , "<STR_LIT:_>" , filename ) <EOL> drive , path = os . path . splitdrive ( filename ) <EOL> path = os . path . normpath ( path ) . replace ( os . path . sep , "<STR_LIT:/>" ) <EOL> self . uri = path <EOL> else : <EOL> self . module_id = "<STR_LIT>" + hex ( id ( self ) ) <EOL> self . uri = self . module_id <EOL> u_norm = self . uri <EOL> if u_norm . startswith ( "<STR_LIT:/>" ) : <EOL> u_norm = u_norm [ <NUM_LIT:1> : ] <EOL> u_norm = os . path . normpath ( u_norm ) <EOL> if u_norm . startswith ( "<STR_LIT:..>" ) : <EOL> raise exceptions . TemplateLookupException ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % self . uri ) <EOL> self . input_encoding = input_encoding <EOL> self . output_encoding = output_encoding <EOL> self . encoding_errors = encoding_errors <EOL> self . disable_unicode = disable_unicode <EOL> self . bytestring_passthrough = bytestring_passthrough or disable_unicode <EOL> self . enable_loop = enable_loop <EOL> self . strict_undefined = strict_undefined <EOL> self . module_writer = module_writer <EOL> if compat . py3k and disable_unicode : <EOL> raise exceptions . UnsupportedError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> elif output_encoding and disable_unicode : <EOL> raise exceptions . UnsupportedError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if default_filters is None : <EOL> if compat . py3k or self . disable_unicode : <EOL> self . default_filters = [ '<STR_LIT:str>' ] <EOL> else : <EOL> self . default_filters = [ '<STR_LIT>' ] <EOL> else : <EOL> self . default_filters = default_filters <EOL> self . buffer_filters = buffer_filters <EOL> self . imports = imports <EOL> self . future_imports = future_imports <EOL> self . preprocessor = preprocessor <EOL> if lexer_cls is not None : <EOL> self . lexer_cls = lexer_cls <EOL> if text is not None : <EOL> ( code , module ) = _compile_text ( self , text , filename ) <EOL> self . _code = code <EOL> self . _source = text <EOL> ModuleInfo ( module , None , self , filename , code , text ) <EOL> elif filename is not None : <EOL> if module_filename is not None : <EOL> path = module_filename <EOL> elif module_directory is not None : <EOL> path = os . path . abspath ( <EOL> os . path . join ( <EOL> os . path . normpath ( module_directory ) , <EOL> u_norm + "<STR_LIT>" <EOL> ) <EOL> ) <EOL> else : <EOL> path = None <EOL> module = self . _compile_from_file ( path , filename ) <EOL> else : <EOL> raise exceptions . RuntimeException ( <EOL> "<STR_LIT>" ) <EOL> self . module = module <EOL> self . filename = filename <EOL> self . callable_ = self . module . render_body <EOL> self . format_exceptions = format_exceptions <EOL> self . error_handler = error_handler <EOL> self . lookup = lookup <EOL> self . module_directory = module_directory <EOL> self . _setup_cache_args ( <EOL> cache_impl , cache_enabled , cache_args , <EOL> cache_type , cache_dir , cache_url <EOL> ) <EOL> @ util . memoized_property <EOL> def reserved_names ( self ) : <EOL> if self . enable_loop : <EOL> return codegen . RESERVED_NAMES <EOL> else : <EOL> return codegen . RESERVED_NAMES . difference ( [ '<STR_LIT>' ] ) <EOL> def _setup_cache_args ( self , <EOL> cache_impl , cache_enabled , cache_args , <EOL> cache_type , cache_dir , cache_url ) : <EOL> self . cache_impl = cache_impl <EOL> self . cache_enabled = cache_enabled <EOL> if cache_args : <EOL> self . cache_args = cache_args <EOL> else : <EOL> self . cache_args = { } <EOL> if cache_type : <EOL> self . cache_args [ '<STR_LIT:type>' ] = cache_type <EOL> if cache_dir : <EOL> self . cache_args [ '<STR_LIT>' ] = cache_dir <EOL> if cache_url : <EOL> self . cache_args [ '<STR_LIT:url>' ] = cache_url <EOL> def _compile_from_file ( self , path , filename ) : <EOL> if path is not None : <EOL> util . verify_directory ( os . path . dirname ( path ) ) <EOL> filemtime = os . stat ( filename ) [ stat . ST_MTIME ] <EOL> if not os . path . exists ( path ) or os . stat ( path ) [ stat . ST_MTIME ] < filemtime : <EOL> data = util . read_file ( filename ) <EOL> _compile_module_file ( <EOL> self , <EOL> data , <EOL> filename , <EOL> path , <EOL> self . module_writer ) <EOL> module = compat . load_module ( self . module_id , path ) <EOL> del sys . modules [ self . module_id ] <EOL> if module . _magic_number != codegen . MAGIC_NUMBER : <EOL> data = util . read_file ( filename ) <EOL> _compile_module_file ( <EOL> self , <EOL> data , <EOL> filename , <EOL> path , <EOL> self . module_writer ) <EOL> module = compat . load_module ( self . module_id , path ) <EOL> del sys . modules [ self . module_id ] <EOL> ModuleInfo ( module , path , self , filename , None , None ) <EOL> else : <EOL> data = util . read_file ( filename ) <EOL> code , module = _compile_text ( <EOL> self , <EOL> data , <EOL> filename ) <EOL> self . _source = None <EOL> self . _code = code <EOL> ModuleInfo ( module , None , self , filename , code , None ) <EOL> return module <EOL> @ property <EOL> def source ( self ) : <EOL> """<STR_LIT>""" <EOL> return _get_module_info_from_callable ( self . callable_ ) . source <EOL> @ property <EOL> def code ( self ) : <EOL> """<STR_LIT>""" <EOL> return _get_module_info_from_callable ( self . callable_ ) . code <EOL> @ util . memoized_property <EOL> def cache ( self ) : <EOL> return cache . Cache ( self ) <EOL> @ property <EOL> def cache_dir ( self ) : <EOL> return self . cache_args [ '<STR_LIT>' ] <EOL> @ property <EOL> def cache_url ( self ) : <EOL> return self . cache_args [ '<STR_LIT:url>' ] <EOL> @ property <EOL> def cache_type ( self ) : <EOL> return self . cache_args [ '<STR_LIT:type>' ] <EOL> def render ( self , * args , ** data ) : <EOL> """<STR_LIT>""" <EOL> return runtime . _render ( self , self . callable_ , args , data ) <EOL> def render_unicode ( self , * args , ** data ) : <EOL> """<STR_LIT>""" <EOL> return runtime . _render ( self , <EOL> self . callable_ , <EOL> args , <EOL> data , <EOL> as_unicode = True ) <EOL> def render_context ( self , context , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if getattr ( context , '<STR_LIT>' , None ) is None : <EOL> context . _set_with_template ( self ) <EOL> runtime . _render_context ( self , <EOL> self . callable_ , <EOL> context , <EOL> * args , <EOL> ** kwargs ) <EOL> def has_def ( self , name ) : <EOL> return hasattr ( self . module , "<STR_LIT>" % name ) <EOL> def get_def ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return DefTemplate ( self , getattr ( self . module , "<STR_LIT>" % name ) ) <EOL> def _get_def_callable ( self , name ) : <EOL> return getattr ( self . module , "<STR_LIT>" % name ) <EOL> @ property <EOL> def last_modified ( self ) : <EOL> return self . module . _modified_time <EOL> class ModuleTemplate ( Template ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , module , <EOL> module_filename = None , <EOL> template = None , <EOL> template_filename = None , <EOL> module_source = None , <EOL> template_source = None , <EOL> output_encoding = None , <EOL> encoding_errors = '<STR_LIT:strict>' , <EOL> disable_unicode = False , <EOL> bytestring_passthrough = False , <EOL> format_exceptions = False , <EOL> error_handler = None , <EOL> lookup = None , <EOL> cache_args = None , <EOL> cache_impl = '<STR_LIT>' , <EOL> cache_enabled = True , <EOL> cache_type = None , <EOL> cache_dir = None , <EOL> cache_url = None , <EOL> ) : <EOL> self . module_id = re . sub ( r'<STR_LIT>' , "<STR_LIT:_>" , module . _template_uri ) <EOL> self . uri = module . _template_uri <EOL> self . input_encoding = module . _source_encoding <EOL> self . output_encoding = output_encoding <EOL> self . encoding_errors = encoding_errors <EOL> self . disable_unicode = disable_unicode <EOL> self . bytestring_passthrough = bytestring_passthrough or disable_unicode <EOL> self . enable_loop = module . _enable_loop <EOL> if compat . py3k and disable_unicode : <EOL> raise exceptions . UnsupportedError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> elif output_encoding and disable_unicode : <EOL> raise exceptions . UnsupportedError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . module = module <EOL> self . filename = template_filename <EOL> ModuleInfo ( module , <EOL> module_filename , <EOL> self , <EOL> template_filename , <EOL> module_source , <EOL> template_source ) <EOL> self . callable_ = self . module . render_body <EOL> self . format_exceptions = format_exceptions <EOL> self . error_handler = error_handler <EOL> self . lookup = lookup <EOL> self . _setup_cache_args ( <EOL> cache_impl , cache_enabled , cache_args , <EOL> cache_type , cache_dir , cache_url <EOL> ) <EOL> class DefTemplate ( Template ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent , callable_ ) : <EOL> self . parent = parent <EOL> self . callable_ = callable_ <EOL> self . output_encoding = parent . output_encoding <EOL> self . module = parent . module <EOL> self . encoding_errors = parent . encoding_errors <EOL> self . format_exceptions = parent . format_exceptions <EOL> self . error_handler = parent . error_handler <EOL> self . enable_loop = parent . enable_loop <EOL> self . lookup = parent . lookup <EOL> self . bytestring_passthrough = parent . bytestring_passthrough <EOL> def get_def ( self , name ) : <EOL> return self . parent . get_def ( name ) <EOL> class ModuleInfo ( object ) : <EOL> """<STR_LIT>""" <EOL> _modules = weakref . WeakValueDictionary ( ) <EOL> def __init__ ( self , <EOL> module , <EOL> module_filename , <EOL> template , <EOL> template_filename , <EOL> module_source , <EOL> template_source ) : <EOL> self . module = module <EOL> self . module_filename = module_filename <EOL> self . template_filename = template_filename <EOL> self . module_source = module_source <EOL> self . template_source = template_source <EOL> self . _modules [ module . __name__ ] = template . _mmarker = self <EOL> if module_filename : <EOL> self . _modules [ module_filename ] = self <EOL> @ property <EOL> def code ( self ) : <EOL> if self . module_source is not None : <EOL> return self . module_source <EOL> else : <EOL> return util . read_python_file ( self . module_filename ) <EOL> @ property <EOL> def source ( self ) : <EOL> if self . template_source is not None : <EOL> if self . module . _source_encoding and not isinstance ( self . template_source , compat . text_type ) : <EOL> return self . template_source . decode ( <EOL> self . module . _source_encoding ) <EOL> else : <EOL> return self . template_source <EOL> else : <EOL> data = util . read_file ( self . template_filename ) <EOL> if self . module . _source_encoding : <EOL> return data . decode ( self . module . _source_encoding ) <EOL> else : <EOL> return data <EOL> def _compile ( template , text , filename , generate_magic_comment ) : <EOL> lexer = template . lexer_cls ( text , <EOL> filename , <EOL> disable_unicode = template . disable_unicode , <EOL> input_encoding = template . input_encoding , <EOL> preprocessor = template . preprocessor ) <EOL> node = lexer . parse ( ) <EOL> source = codegen . compile ( node , <EOL> template . uri , <EOL> filename , <EOL> default_filters = template . default_filters , <EOL> buffer_filters = template . buffer_filters , <EOL> imports = template . imports , <EOL> future_imports = template . future_imports , <EOL> source_encoding = lexer . encoding , <EOL> generate_magic_comment = generate_magic_comment , <EOL> disable_unicode = template . disable_unicode , <EOL> strict_undefined = template . strict_undefined , <EOL> enable_loop = template . enable_loop , <EOL> reserved_names = template . reserved_names ) <EOL> return source , lexer <EOL> def _compile_text ( template , text , filename ) : <EOL> identifier = template . module_id <EOL> source , lexer = _compile ( template , text , filename , <EOL> generate_magic_comment = template . disable_unicode ) <EOL> cid = identifier <EOL> if not compat . py3k and isinstance ( cid , compat . text_type ) : <EOL> cid = cid . encode ( ) <EOL> module = types . ModuleType ( cid ) <EOL> code = compile ( source , cid , '<STR_LIT>' ) <EOL> exec ( code , module . __dict__ , module . __dict__ ) <EOL> return ( source , module ) <EOL> def _compile_module_file ( template , text , filename , outputpath , module_writer ) : <EOL> source , lexer = _compile ( template , text , filename , <EOL> generate_magic_comment = True ) <EOL> if isinstance ( source , compat . text_type ) : <EOL> source = source . encode ( lexer . encoding or '<STR_LIT:ascii>' ) <EOL> if module_writer : <EOL> module_writer ( source , outputpath ) <EOL> else : <EOL> ( dest , name ) = tempfile . mkstemp ( dir = os . path . dirname ( outputpath ) ) <EOL> os . write ( dest , source ) <EOL> os . close ( dest ) <EOL> shutil . move ( name , outputpath ) <EOL> def _get_module_info_from_callable ( callable_ ) : <EOL> if compat . py3k : <EOL> return _get_module_info ( callable_ . __globals__ [ '<STR_LIT>' ] ) <EOL> else : <EOL> return _get_module_info ( callable_ . func_globals [ '<STR_LIT>' ] ) <EOL> def _get_module_info ( filename ) : <EOL> return ModuleInfo . _modules [ filename ] </s>
