Skip to content

Instantly share code, notes, and snippets.

@erikkaplun
Created December 9, 2012 23:29
Show Gist options
  • Save erikkaplun/4247491 to your computer and use it in GitHub Desktop.
Save erikkaplun/4247491 to your computer and use it in GitHub Desktop.
Traceback parser and merger
class Traceback(object):
@classmethod
def parse(cls, tb_str):
tb_lines = tb_str.strip().split('\n')
firstline = tb_lines.pop(0)
step_lines = []
while True:
if tb_lines[0].startswith(' '):
step_lines.append(tb_lines.pop(0))
continue
assert len(step_lines) % 2 == 0
steps = ['%s\n%s' % x for x in zip(step_lines[::2], step_lines[1::2])]
exc_descr = '\n'.join(tb_lines)
break
return cls(firstline=firstline, steps=steps, exc_descr=exc_descr)
firstline = None
steps = None
exc_descr = None
def __init__(self, firstline, steps, exc_descr):
self.firstline = firstline
self.steps = steps
self.exc_descr = exc_descr
def without_steps(self, starting=None, count=None, until=None):
if count is not None and until is not None:
raise TypeError("both 'count' and 'until' cannot be given")
if count is not None:
if starting < 0:
assert starting + count <= 0
until = starting + count
if starting < 0 and until == 0:
until = None
steps = self.steps[:]
steps[starting:until] = []
return Traceback(firstline=self.firstline, steps=steps, exc_descr=self.exc_descr)
def without_step(self, index):
return self.without_steps(starting=index, count=1)
def attach(self, other, splitter_title=None):
line = "/\\" * 10
splitter = (
" %s %s %s" % (line + line[-2], splitter_title, line + line[-2])
if splitter_title is not None else
line * 2 + line[-2]
)
return Traceback(
firstline=self.firstline,
steps=self.steps + [splitter] + other.steps,
exc_descr=other.exc_descr
)
def __repr__(self):
return "%s\n%s\n%s" % (self.firstline, '\n'.join(self.steps), self.exc_descr)
__str__ = __repr__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment