Created
October 12, 2012 23:00
-
-
Save robo-corg/3882123 to your computer and use it in GitHub Desktop.
Traceback parser that opens the last file mentioned in the traceback.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import re | |
import subprocess | |
from functools import partial | |
class TracebackParser(object): | |
traceback_line_re = re.compile(r'\s+File\s+\"([^"]*)\"') | |
def __init__(self, on_traceback=None): | |
self.in_traceback = False | |
self.traceback = [] | |
self.on_traceback = on_traceback or (lambda tb: None) | |
def parse_line(self, line): | |
if not line: | |
return | |
if self.in_traceback: | |
if line[:2] != ' ' * 2: | |
self.in_traceback = False | |
self.on_traceback(self.traceback) | |
return | |
match = self.traceback_line_re.match(line) | |
if match: | |
self.traceback.append(match.group(1)) | |
else: | |
if 'Traceback' in line: | |
self.in_traceback = True | |
def parse_file(self, tb_file): | |
while True: | |
line = tb_file.readline() | |
self.parse_line(line) | |
def open_trace_back(app, tb): | |
subprocess.check_call(['open', '-a', app, tb[-1]]) | |
if __name__ == '__main__': | |
def print_last(tb): | |
print 'last:' + tb[-1] | |
TracebackParser(partial(open_trace_back, sys.argv[1])).parse_file(sys.stdin) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment