Commits

jayven committed 44b7919

Seperate some common code

  • Participants
  • Parent commits 5713f22
  • Branches add_traceback_html

Comments (0)

Files changed (2)

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# vim: set ai et si ts=4 sts=4 sw=4:
+
+from itertools import izip, islice
+import linecache
+
+
+def get_file_part(filename, lineno, lines_around, g=None):
+
+    linecache.checkcache(filename)
+    lines = linecache.getlines(filename, g)
+    line = linecache.getline(filename, lineno, g)
+    if not line or not lines:
+        return '', []
+
+    b, e = lineno - lines_around - 1, lineno + lines_around
+    if b < 0:
+        b = 0
+    if e > len(lines):
+        e = len(lines)
+
+    return line, [(ln, l) for ln, line in izip(
+        xrange(b + 1, e + 1), islice(lines, b, e))]
+
+
+def iter_tracback(tb, lines_around=7):
+    depth = 1
+
+    while tb is not None:
+        f = tb.tb_frame
+        co = f.f_code
+        filename = co.co_filename
+        lineno = tb.tb_lineno
+        where = co.co_name
+        line, lines = get_file_part(filename, lineno, lines_around,
+                f.f_globals)
+        yield depth, filename, where, lineno, line, lines, f.f_locals
+
+        tb = tb.tb_next
+        depth += 1
+
 import linecache
 import base64
 from datetime import datetime
-from itertools import izip, islice
 from pprint import pformat
 from osv import osv, fields
 
+import codebase
 import hook
 from bgconsole import BGConsole
 try:
     return []
 
 
-def get_file_part(filename, lineno, lines_around, g):
-
-    linecache.checkcache(filename)
-    lines = linecache.getlines(filename, g)
-    line = linecache.getline(filename, lineno, g)
-    if not line or not lines:
-        return '', ''
-
-    b, e = lineno - lines_around - 1, lineno + lines_around
-    if b < 0:
-        b = 0
-    if e > len(lines):
-        e = len(lines)
-    w = len(str(e+1))
-
-    return line, ''.join(["%*d %s" % (w, ln, line) for ln, line in 
-        izip(xrange(b+1, e+1), islice(lines, b, e))])
-
-    
 class DebugConsole(osv.osv_memory):
 
     @property
         # now add tracebacks
         lines_around = context.get('lines_around', 5)
         tb = last_traceback.tb_next # skip the first one
-        depth = 1
-        while tb is not None:
-            f = tb.tb_frame
-            co = f.f_code
-            filename = co.co_filename
-            lineno = tb.tb_lineno
-            where = co.co_name
-            line, lines = get_file_part(filename, lineno,
-                    lines_around,
-                    f.f_globals)
+
+        for depth, filename, where, lineno, line, lines, local in \
+                codebase.iter_tracback(tb, lines_around):
+
+            w = len(str(lines[-1][0]))
+            lines = ''.join(("%*d%s %s" % (w, ln, 
+                (ln == lineno) and '>' or '.', l) for ln, l in lines))
+
             tb_id = self.tb_obj.create(cr, uid, {'console': id,
                 'depth': depth,
                 'filename': filename,
                 'locals': [(0, 0, {
                     'name': n,
                     'value': pformat(v, indent=4),
-                }) for n, v in f.f_locals.iteritems()],
+                }) for n, v in local.iteritems()],
             }, context=context)
             
-            tb = tb.tb_next
-            depth += 1
-
     def create(self, cr, uid, vals, context=None):
         # create the associated bgconsole and create the record
         bgconsole_id = self.bgconsole_obj.create(cr, uid, {}, context=context)