"""HTMLForException.py Create HTML for exceptions. """ import os import re import sys import traceback import urllib.request import urllib.parse import urllib.error from .Funcs import htmlEncode HTMLForExceptionOptions = { 'table': 'background-color:#f0f0f0', 'default': 'color:#000', 'row.location': 'color:#009', 'row.code': 'color:#900', 'editlink': None, } fileRE = re.compile(r'File "([^"]*)", line ([0-9]+), in ([^ ]*)') def htmlForLines(lines, options=None): """Create HTML for exceptions and tracebacks from a list of strings.""" # Set up the options: if options: opt = HTMLForExceptionOptions | options else: opt = HTMLForExceptionOptions # Create the HTML: res = ['
\n'.format(opt['default'])]
for line in lines:
match = fileRE.search(line)
if match:
parts = list(map(htmlEncode, line.split('\n', 2)))
parts[0] = '{}'.format(
opt['row.location'], parts[0])
if opt['editlink']:
parts[0] = (
'{} '
'[edit]'.format(
parts[0], opt['editlink'], urllib.parse.quote(
os.path.abspath(match.group(1))), match.group(2)))
parts[1] = '{}'.format(
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([' |