def HTMLForLines(lines, options=None): """Create HTML for exceptions and tracebacks from a list of strings.""" # Set up the options: if options: opt = HTMLForExceptionOptions.copy() opt.update(options) else: opt = HTMLForExceptionOptions # Create the HTML: res = ['<table style="width:100%%;%s">\n' % opt['table'], '<tr><td><pre style="%s">\n' % opt['default']] for line in lines: match = fileRE.search(line) if match: parts = map(htmlEncode, line.split('\n', 2)) parts[0] = '<span style="%s">%s</span>' % ( opt['row.location'], parts[0]) if opt['editlink']: parts[0] = ('%s <a href="%s?filename=%s&line=%s">[edit]</a>' % (parts[0], opt['editlink'], urllib.quote( os.path.abspath(match.group(1))), match.group(2))) parts[1] = '<span style="%s">%s</span>' % ( opt['row.code'], parts[1]) line = '\n'.join(parts) res.append(line) else: res.append(htmlEncode(line)) if lines: if res[-1][-1] == '\n': res[-1] = res[-1].rstrip() res.extend(['</pre></td></tr>\n', '</table>\n']) return ''.join(res)
def HTMLForException(excInfo=None, options=None): """ Returns an HTML string that presents useful information to the developer about the exception. The first argument is a tuple such as returned by sys.exc_info() which is in fact, invoked if the tuple isn't provided. """ # @@ 2000-04-17 ce: Maybe excInfo should default to None and get set to sys.excInfo() if not specified. If so, then clean up other code. # Get the excInfo if needed if excInfo is None: excInfo = sys.exc_info() # Set up the options if options: opt = HTMLForExceptionOptions.copy() opt.update(options) else: opt = HTMLForExceptionOptions # Create the HTML res = [ '<table bgcolor=%s width=100%% cellpadding=4><tr><td>\n' % opt['table.bgcolor'], '<pre><font color=%s>' % opt['default.fgcolor'] ] out = apply(traceback.format_exception, excInfo) for line in out: if string.find(line, 'File ') != -1: parts = string.split(line, '\n') parts = map(lambda s: htmlEncode(s), parts) parts[0] = '<font color=%s>%s</font>' % ( opt['row.location.fgcolor'], parts[0]) parts[1] = '<font color=%s>%s</font>' % (opt['row.code.fgcolor'], parts[1]) line = string.join(parts, '\n') res.append(line) else: res.append(htmlEncode(line)) if out: if res[-1][-1] == '\n': res[-1] = string.rstrip(res[-1]) res.extend(['</font></pre>', '</td></tr></table>\n']) return string.join(res, '')
def HTMLForException(excInfo=None, options=None): """Get HTML for displaying an exception. Returns an HTML string that presents useful information to the developer about the exception. The first argument is a tuple such as returned by sys.exc_info() which is in fact, invoked if the tuple isn't provided. """ # @@ 2000-04-17 ce: Maybe excInfo should default to None and get set # to sys.excInfo() if not specified. If so, then clean up other code. # Get the excInfo if needed: if excInfo is None: excInfo = sys.exc_info() # Set up the options: if options: opt = HTMLForExceptionOptions.copy() opt.update(options) else: opt = HTMLForExceptionOptions # Create the HTML: res = ['<table style="%s" width=100%%' ' cellpadding="2" cellspacing="2">\n' % opt['table'], '<tr><td><pre style="%s">\n' % opt['default']] out = traceback.format_exception(*excInfo) for line in out: match = fileRE.search(line) if match: parts = map(htmlEncode, line.split('\n')) parts[0] = '<span style="%s">%s</span>' \ % (opt['row.location'], parts[0]) if opt['editlink']: parts[0] = '%s <a href="%s?filename=%s&line=%s">[edit]</a>' \ % (parts[0], opt['editlink'], urllib.quote( os.path.join(os.getcwd(), match.group(1))), match.group(2)) parts[1] = '<span style="%s">%s</span>' \ % (opt['row.code'], parts[1]) line = '\n'.join(parts) res.append(line) else: res.append(htmlEncode(line)) if out: if res[-1][-1] == '\n': res[-1] = res[-1].rstrip() res.extend(['</pre></td></tr>\n', '</table>\n']) return ''.join(res)
def HTMLForLines(lines, options=None): """Create HTML for exceptions and tracebacks from a list of strings.""" # Set up the options: if options: opt = HTMLForExceptionOptions.copy() opt.update(options) else: opt = HTMLForExceptionOptions # Create the HTML: res = [ '<table style="%s" width="100%%"' ' cellpadding="2" cellspacing="2">\n' % opt["table"], '<tr><td><pre style="%s">\n' % opt["default"], ] for line in lines: match = fileRE.search(line) if match: parts = map(htmlEncode, line.split("\n", 2)) parts[0] = '<span style="%s">%s</span>' % (opt["row.location"], parts[0]) if opt["editlink"]: parts[0] = '%s <a href="%s?filename=%s&line=%s">[edit]</a>' % ( parts[0], opt["editlink"], urllib.quote(os.path.abspath(match.group(1))), match.group(2), ) parts[1] = '<span style="%s">%s</span>' % (opt["row.code"], parts[1]) line = "\n".join(parts) res.append(line) else: res.append(htmlEncode(line)) if lines: if res[-1][-1] == "\n": res[-1] = res[-1].rstrip() res.extend(["</pre></td></tr>\n", "</table>\n"]) return "".join(res)