[wwwsearch-commits] r32793 - wwwsearch/release_scripts

jjlee at codespeak.net jjlee at codespeak.net
Sun Oct 1 19:51:01 CEST 2006


Author: jjlee
Date: Sun Oct  1 19:50:59 2006
New Revision: 32793

Modified:
   wwwsearch/release_scripts/colorize.py
Log:
Add coverage support from an ASPN recipe

Modified: wwwsearch/release_scripts/colorize.py
==============================================================================
--- wwwsearch/release_scripts/colorize.py	(original)
+++ wwwsearch/release_scripts/colorize.py	Sun Oct  1 19:50:59 2006
@@ -2,13 +2,23 @@
 
 Taken from Python Cookbook (originally from MoinMoin Python Source Parser).
 
+HTML code coverage support:
+
+Original recipe:
+ http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52298
+
+Original Authors:
+ - J\ufffdrgen Hermann
+ - Mike Brown <http://skew.org/~mike/>
+ - Christopher Arndt <http://chrisarndt.de>
+
 Hacked a bit by John J Lee <jjl at pobox.com>.  Reduced the amount of markup
 generated, and I forget what else...
 
 """
 
 # Imports
-import cgi, string, sys, cStringIO
+import os, cgi, string, sys, cStringIO
 import keyword, token, tokenize
 
 
@@ -69,11 +79,13 @@
 
     """
 
-    def __init__(self, raw, out = sys.stdout):
+    def __init__(self, raw, out = sys.stdout, not_covered=[]):
         """ Store the source text.
         """
         self.raw = string.expandtabs(raw).rstrip()
         self.out = out
+        self.not_covered = not_covered  # not covered list of lines
+        self.cover_flag = False  # is there a <span> tag opened?
 
     def format(self, formatter, form):
         """ Parse and send the colored source.
@@ -112,10 +124,17 @@
         newpos = self.lines[srow] + scol
         self.pos = newpos + len(toktext)
 
+        if not self.cover_flag and srow in self.not_covered:
+            self.out.write('<span class="notcovered">')
+            self.cover_flag = True
+
         # handle newlines
         if toktype in [token.NEWLINE, tokenize.NL]:
-            self.out.write('\n')
-            return
+            if self.cover_flag:
+                self.out.write('</span>')
+                self.cover_flag = False
+##             self.out.write('\n')
+##             return
 
         # send the original whitespace, if needed
         if newpos > oldpos:
@@ -144,6 +163,73 @@
         if color != "py":
             self.out.write('</span>')
 
+# code coverage
+# --------------------------------------------------------------------
+
+_HTML_HEADER = """\
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
+  "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head>
+<title>code coverage of %(title)s</title>
+<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
+
+<style type="text/css">
+pre.code {font-style: Lucida,"Courier New";}
+.pystr {color:#a08070;}
+.pycmt {color:#a34727;}
+.pykw {color:#9668d7;}
+.notcovered {background-color: #FFB2B2;}
+</style>
+
+</head>
+<body>
+"""
+
+_HTML_FOOTER = """\
+</body>
+</html>
+"""
+
+class MissingList(list):
+    def __init__(self, i):
+        list.__init__(self, i)
+
+    def __contains__(self, elem):
+        for i in list.__iter__(self):
+            v_ = m_ = s_ = None
+            try:
+                v_ = int(i)
+            except ValueError:
+                m_, s_ = i.split('-')
+            if v_ is not None and v_ == elem:
+                return True
+            elif (m_ is not None) and (s_ is not None) and \
+                     (int(m_) <= elem) and (elem <= int(s_)):
+                return True
+        return False
+
+def colorize_file(filename, outstream=sys.stdout, not_covered=[]):
+    """
+    Convert a python source file into colorized HTML.
+
+    Reads file and writes to outstream (default sys.stdout).
+    """
+    fo = file(filename, 'rb')
+    try:
+        source = fo.read()
+    finally:
+        fo.close()
+    outstream.write(_HTML_HEADER % {'title': os.path.basename(filename)})
+    Parser(source, out=outstream,
+           not_covered=MissingList((not_covered and \
+                                    not_covered.split(', ')) or \
+                                   [])).format(None, None)
+    outstream.write(_HTML_FOOTER)
+
+
+# --------------------------------------------------------------------
+
 
 def test_main():
     import doctest


More information about the wwwsearch-commits mailing list