<s> """<STR_LIT>""" <EOL> import traceback , sys , re <EOL> from mako import util <EOL> class MakoException ( Exception ) : <EOL> pass <EOL> class RuntimeException ( MakoException ) : <EOL> pass <EOL> def _format_filepos ( lineno , pos , filename ) : <EOL> if filename is None : <EOL> return "<STR_LIT>" % ( lineno , pos ) <EOL> else : <EOL> return "<STR_LIT>" % ( filename , lineno , pos ) <EOL> class CompileException ( MakoException ) : <EOL> def __init__ ( self , message , source , lineno , pos , filename ) : <EOL> MakoException . __init__ ( self , message + _format_filepos ( lineno , pos , filename ) ) <EOL> self . lineno = lineno <EOL> self . pos = pos <EOL> self . filename = filename <EOL> self . source = source <EOL> class SyntaxException ( MakoException ) : <EOL> def __init__ ( self , message , source , lineno , pos , filename ) : <EOL> MakoException . __init__ ( self , message + _format_filepos ( lineno , pos , filename ) ) <EOL> self . lineno = lineno <EOL> self . pos = pos <EOL> self . filename = filename <EOL> self . source = source <EOL> class UnsupportedError ( MakoException ) : <EOL> """<STR_LIT>""" <EOL> class TemplateLookupException ( MakoException ) : <EOL> pass <EOL> class TopLevelLookupException ( TemplateLookupException ) : <EOL> pass <EOL> class RichTraceback ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , error = None , traceback = None ) : <EOL> self . source , self . lineno = "<STR_LIT>" , <NUM_LIT:0> <EOL> if error is None or traceback is None : <EOL> t , value , tback = sys . exc_info ( ) <EOL> if error is None : <EOL> error = value or t <EOL> if traceback is None : <EOL> traceback = tback <EOL> self . error = error <EOL> self . records = self . _init ( traceback ) <EOL> if isinstance ( self . error , ( CompileException , SyntaxException ) ) : <EOL> import mako . template <EOL> self . source = self . error . source <EOL> self . lineno = self . error . lineno <EOL> self . _has_source = True <EOL> self . _init_message ( ) <EOL> @ property <EOL> def errorname ( self ) : <EOL> return util . exception_name ( self . error ) <EOL> def _init_message ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . message = str ( self . error ) <EOL> except UnicodeError : <EOL> try : <EOL> self . message = str ( self . error ) <EOL> except UnicodeEncodeError : <EOL> self . message = self . error . args [ <NUM_LIT:0> ] <EOL> if not isinstance ( self . message , str ) : <EOL> self . message = str ( self . message , '<STR_LIT:ascii>' , '<STR_LIT:replace>' ) <EOL> def _get_reformatted_records ( self , records ) : <EOL> for rec in records : <EOL> if rec [ <NUM_LIT:6> ] is not None : <EOL> yield ( rec [ <NUM_LIT:4> ] , rec [ <NUM_LIT:5> ] , rec [ <NUM_LIT:2> ] , rec [ <NUM_LIT:6> ] ) <EOL> else : <EOL> yield tuple ( rec [ <NUM_LIT:0> : <NUM_LIT:4> ] ) <EOL> @ property <EOL> def traceback ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . _get_reformatted_records ( self . records ) ) <EOL> @ property <EOL> def reverse_records ( self ) : <EOL> return reversed ( self . records ) <EOL> @ property <EOL> def reverse_traceback ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . _get_reformatted_records ( self . reverse_records ) ) <EOL> def _init ( self , trcback ) : <EOL> """<STR_LIT>""" <EOL> import mako . template <EOL> mods = { } <EOL> rawrecords = traceback . extract_tb ( trcback ) <EOL> new_trcback = [ ] <EOL> for filename , lineno , function , line in rawrecords : <EOL> if not line : <EOL> line = '<STR_LIT>' <EOL> try : <EOL> ( line_map , template_lines ) = mods [ filename ] <EOL> except KeyError : <EOL> try : <EOL> info = mako . template . _get_module_info ( filename ) <EOL> module_source = info . code <EOL> template_source = info . source <EOL> template_filename = info . template_filename or filename <EOL> except KeyError : <EOL> if not util . py3k : <EOL> try : <EOL> fp = open ( filename , '<STR_LIT:rb>' ) <EOL> encoding = util . parse_encoding ( fp ) <EOL> fp . close ( ) <EOL> except IOError : <EOL> encoding = None <EOL> if encoding : <EOL> line = line . decode ( encoding ) <EOL> else : <EOL> line = line . decode ( '<STR_LIT:ascii>' , '<STR_LIT:replace>' ) <EOL> new_trcback . append ( ( filename , lineno , function , line , <EOL> None , None , None , None ) ) <EOL> continue <EOL> template_ln = module_ln = <NUM_LIT:1> <EOL> line_map = { } <EOL> for line in module_source . split ( "<STR_LIT:\n>" ) : <EOL> match = re . match ( r'<STR_LIT>' , line ) <EOL> if match : <EOL> template_ln = int ( match . group ( <NUM_LIT:1> ) ) <EOL> module_ln += <NUM_LIT:1> <EOL> line_map [ module_ln ] = template_ln <EOL> template_lines = [ line for line in <EOL> template_source . split ( "<STR_LIT:\n>" ) ] <EOL> mods [ filename ] = ( line_map , template_lines ) <EOL> template_ln = line_map [ lineno ] <EOL> if template_ln <= len ( template_lines ) : <EOL> template_line = template_lines [ template_ln - <NUM_LIT:1> ] <EOL> else : <EOL> template_line = None <EOL> new_trcback . append ( ( filename , lineno , function , <EOL> line , template_filename , template_ln , <EOL> template_line , template_source ) ) <EOL> if not self . source : <EOL> for l in range ( len ( new_trcback ) - <NUM_LIT:1> , <NUM_LIT:0> , - <NUM_LIT:1> ) : <EOL> if new_trcback [ l ] [ <NUM_LIT:5> ] : <EOL> self . source = new_trcback [ l ] [ <NUM_LIT:7> ] <EOL> self . lineno = new_trcback [ l ] [ <NUM_LIT:5> ] <EOL> break <EOL> else : <EOL> if new_trcback : <EOL> try : <EOL> fp = open ( new_trcback [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] , '<STR_LIT:rb>' ) <EOL> encoding = util . parse_encoding ( fp ) <EOL> fp . seek ( <NUM_LIT:0> ) <EOL> self . source = fp . read ( ) <EOL> fp . close ( ) <EOL> if encoding : <EOL> self . source = self . source . decode ( encoding ) <EOL> except IOError : <EOL> self . source = '<STR_LIT>' <EOL> self . lineno = new_trcback [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> return new_trcback <EOL> def text_error_template ( lookup = None ) : <EOL> """<STR_LIT>""" <EOL> import mako . template <EOL> return mako . template . Template ( r"""<STR_LIT>""" ) <EOL> def html_error_template ( ) : <EOL> """<STR_LIT>""" <EOL> import mako . template <EOL> return mako . template . Template ( r"""<STR_LIT>""" , output_encoding = sys . getdefaultencoding ( ) , encoding_errors = '<STR_LIT>' ) </s>
