HTMLForException.py :  » Web-Frameworks » Webware » Webware-1.0.2 » WebUtils » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Webware 
Webware » Webware 1.0.2 » WebUtils » HTMLForException.py
import os, re, sys, traceback, urllib

from Funcs import htmlEncode


HTMLForExceptionOptions = {
  'table': 'background-color:#F0F0F0',
  'default': 'color:#000000',
  'row.location': 'color:#000099',
  'row.code': 'color:#990000',
  '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.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'))
      parts[0] = '<span style="%s">%s</span>' \
        % (opt['row.location'], parts[0])
      if opt['editlink']:
        parts[0] = ('%s <a href="%s?filename=%s&amp;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 HTMLForStackTrace(frame=None, options=None):
  """Get HTML for displaying a stack trace.

  Returns an HTML string that presents useful information to the developer
  about the stack. The first argument is a stack frame such as returned by
  sys._getframe() which is in fact invoked if a stack frame isn't provided.

  """

  # Get the stack frame if needed:
  if frame is None:
    frame = sys._getframe()

  # Return formatted stack traceback
  return HTMLForLines(traceback.format_stack(frame), options)


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.

  """

  # Get the excInfo if needed:
  if excInfo is None:
    excInfo = sys.exc_info()

  # Return formatted exception traceback
  return HTMLForLines(traceback.format_exception(*excInfo), options)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.