/oldstuff/sleepy/dumper.py
https://bitbucket.org/tenuki/sleepy · Python · 80 lines · 60 code · 19 blank · 1 comment · 11 complexity · e376be83e2a5753352a5c1b3ef0a9279 MD5 · raw file
- from __future__ import with_statement
- import pickle
- from StringIO import StringIO
- from exceptions import *
- class SF(object):
- def __init__(self, frame):
- self.frame = frame
- def __str__(self):
- return '<%s:%d(%d)>' % (self.filename, self.lineno, self.lasti)
- @property
- def lasti(self):
- return self.frame.f_lasti
- @property
- def filename(self):
- return self.frame.f_code.co_filename
- @property
- def lineno(self):
- return self.frame.f_lineno
- class FrameChainInfo(object):
- def __init__(self, filename=None, frame=None, chain=None, truncate=True):
- self.filename = filename
- if chain is None:
- self.chain = self.convert(frame, truncate)
- else:
- self.chain = chain
- def convert(self, frame, truncate):
- """ convert a frame and its parents into a tuple list """
- def gen():
- f = frame
- while f:
- if truncate and (f.f_code.co_filename == '<string>'):
- return
- yield (f.f_code.co_filename, f.f_lineno, f.f_lasti)
- f = f.f_back
- return [f for f in gen()]
- def __str__(self):
- st = StringIO()
- for notFirst, f in enumerate(self.chain):
- if notFirst:
- st.write('\n')
- st.write(self.frameRepr(f))
- return st.getvalue()
- def frameRepr(self, frame):
- _filename, _lineno, _lasti = frame
- return '<%s:%d(%d)>' % (_filename, _lineno, _lasti)
- def save(self, fname):
- with open(fname, 'wb') as f:
- pickle.dump(self.chain, f)
- @classmethod
- def load(cls, fname):
- with open(fname, 'rb') as f:
- return cls(chain=pickle.load(f))
- class StateDumper(object):
- def __init__(self, filename=None):
- self.filename = filename
- def dumpState(self, frame):
- fci = FrameChainInfo(self.filename, frame)
- print str(fci)
- fci.save('dump.pck')
- if __name__ == "__main__":
- pass