Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../hotshot
File: log.py
import _hotshot
[0] Fix | Delete
import os.path
[1] Fix | Delete
import parser
[2] Fix | Delete
import symbol
[3] Fix | Delete
[4] Fix | Delete
from _hotshot import \
[5] Fix | Delete
WHAT_ENTER, \
[6] Fix | Delete
WHAT_EXIT, \
[7] Fix | Delete
WHAT_LINENO, \
[8] Fix | Delete
WHAT_DEFINE_FILE, \
[9] Fix | Delete
WHAT_DEFINE_FUNC, \
[10] Fix | Delete
WHAT_ADD_INFO
[11] Fix | Delete
[12] Fix | Delete
[13] Fix | Delete
__all__ = ["LogReader", "ENTER", "EXIT", "LINE"]
[14] Fix | Delete
[15] Fix | Delete
[16] Fix | Delete
ENTER = WHAT_ENTER
[17] Fix | Delete
EXIT = WHAT_EXIT
[18] Fix | Delete
LINE = WHAT_LINENO
[19] Fix | Delete
[20] Fix | Delete
[21] Fix | Delete
class LogReader:
[22] Fix | Delete
def __init__(self, logfn):
[23] Fix | Delete
# fileno -> filename
[24] Fix | Delete
self._filemap = {}
[25] Fix | Delete
# (fileno, lineno) -> filename, funcname
[26] Fix | Delete
self._funcmap = {}
[27] Fix | Delete
[28] Fix | Delete
self._reader = _hotshot.logreader(logfn)
[29] Fix | Delete
self._nextitem = self._reader.next
[30] Fix | Delete
self._info = self._reader.info
[31] Fix | Delete
if 'current-directory' in self._info:
[32] Fix | Delete
self.cwd = self._info['current-directory']
[33] Fix | Delete
else:
[34] Fix | Delete
self.cwd = None
[35] Fix | Delete
[36] Fix | Delete
# This mirrors the call stack of the profiled code as the log
[37] Fix | Delete
# is read back in. It contains tuples of the form:
[38] Fix | Delete
#
[39] Fix | Delete
# (file name, line number of function def, function name)
[40] Fix | Delete
#
[41] Fix | Delete
self._stack = []
[42] Fix | Delete
self._append = self._stack.append
[43] Fix | Delete
self._pop = self._stack.pop
[44] Fix | Delete
[45] Fix | Delete
def close(self):
[46] Fix | Delete
self._reader.close()
[47] Fix | Delete
[48] Fix | Delete
def fileno(self):
[49] Fix | Delete
"""Return the file descriptor of the log reader's log file."""
[50] Fix | Delete
return self._reader.fileno()
[51] Fix | Delete
[52] Fix | Delete
def addinfo(self, key, value):
[53] Fix | Delete
"""This method is called for each additional ADD_INFO record.
[54] Fix | Delete
[55] Fix | Delete
This can be overridden by applications that want to receive
[56] Fix | Delete
these events. The default implementation does not need to be
[57] Fix | Delete
called by alternate implementations.
[58] Fix | Delete
[59] Fix | Delete
The initial set of ADD_INFO records do not pass through this
[60] Fix | Delete
mechanism; this is only needed to receive notification when
[61] Fix | Delete
new values are added. Subclasses can inspect self._info after
[62] Fix | Delete
calling LogReader.__init__().
[63] Fix | Delete
"""
[64] Fix | Delete
pass
[65] Fix | Delete
[66] Fix | Delete
def get_filename(self, fileno):
[67] Fix | Delete
try:
[68] Fix | Delete
return self._filemap[fileno]
[69] Fix | Delete
except KeyError:
[70] Fix | Delete
raise ValueError, "unknown fileno"
[71] Fix | Delete
[72] Fix | Delete
def get_filenames(self):
[73] Fix | Delete
return self._filemap.values()
[74] Fix | Delete
[75] Fix | Delete
def get_fileno(self, filename):
[76] Fix | Delete
filename = os.path.normcase(os.path.normpath(filename))
[77] Fix | Delete
for fileno, name in self._filemap.items():
[78] Fix | Delete
if name == filename:
[79] Fix | Delete
return fileno
[80] Fix | Delete
raise ValueError, "unknown filename"
[81] Fix | Delete
[82] Fix | Delete
def get_funcname(self, fileno, lineno):
[83] Fix | Delete
try:
[84] Fix | Delete
return self._funcmap[(fileno, lineno)]
[85] Fix | Delete
except KeyError:
[86] Fix | Delete
raise ValueError, "unknown function location"
[87] Fix | Delete
[88] Fix | Delete
# Iteration support:
[89] Fix | Delete
# This adds an optional (& ignored) parameter to next() so that the
[90] Fix | Delete
# same bound method can be used as the __getitem__() method -- this
[91] Fix | Delete
# avoids using an additional method call which kills the performance.
[92] Fix | Delete
[93] Fix | Delete
def next(self, index=0):
[94] Fix | Delete
while 1:
[95] Fix | Delete
# This call may raise StopIteration:
[96] Fix | Delete
what, tdelta, fileno, lineno = self._nextitem()
[97] Fix | Delete
[98] Fix | Delete
# handle the most common cases first
[99] Fix | Delete
[100] Fix | Delete
if what == WHAT_ENTER:
[101] Fix | Delete
filename, funcname = self._decode_location(fileno, lineno)
[102] Fix | Delete
t = (filename, lineno, funcname)
[103] Fix | Delete
self._append(t)
[104] Fix | Delete
return what, t, tdelta
[105] Fix | Delete
[106] Fix | Delete
if what == WHAT_EXIT:
[107] Fix | Delete
try:
[108] Fix | Delete
return what, self._pop(), tdelta
[109] Fix | Delete
except IndexError:
[110] Fix | Delete
raise StopIteration
[111] Fix | Delete
[112] Fix | Delete
if what == WHAT_LINENO:
[113] Fix | Delete
filename, firstlineno, funcname = self._stack[-1]
[114] Fix | Delete
return what, (filename, lineno, funcname), tdelta
[115] Fix | Delete
[116] Fix | Delete
if what == WHAT_DEFINE_FILE:
[117] Fix | Delete
filename = os.path.normcase(os.path.normpath(tdelta))
[118] Fix | Delete
self._filemap[fileno] = filename
[119] Fix | Delete
elif what == WHAT_DEFINE_FUNC:
[120] Fix | Delete
filename = self._filemap[fileno]
[121] Fix | Delete
self._funcmap[(fileno, lineno)] = (filename, tdelta)
[122] Fix | Delete
elif what == WHAT_ADD_INFO:
[123] Fix | Delete
# value already loaded into self.info; call the
[124] Fix | Delete
# overridable addinfo() handler so higher-level code
[125] Fix | Delete
# can pick up the new value
[126] Fix | Delete
if tdelta == 'current-directory':
[127] Fix | Delete
self.cwd = lineno
[128] Fix | Delete
self.addinfo(tdelta, lineno)
[129] Fix | Delete
else:
[130] Fix | Delete
raise ValueError, "unknown event type"
[131] Fix | Delete
[132] Fix | Delete
def __iter__(self):
[133] Fix | Delete
return self
[134] Fix | Delete
[135] Fix | Delete
#
[136] Fix | Delete
# helpers
[137] Fix | Delete
#
[138] Fix | Delete
[139] Fix | Delete
def _decode_location(self, fileno, lineno):
[140] Fix | Delete
try:
[141] Fix | Delete
return self._funcmap[(fileno, lineno)]
[142] Fix | Delete
except KeyError:
[143] Fix | Delete
#
[144] Fix | Delete
# This should only be needed when the log file does not
[145] Fix | Delete
# contain all the DEFINE_FUNC records needed to allow the
[146] Fix | Delete
# function name to be retrieved from the log file.
[147] Fix | Delete
#
[148] Fix | Delete
if self._loadfile(fileno):
[149] Fix | Delete
filename = funcname = None
[150] Fix | Delete
try:
[151] Fix | Delete
filename, funcname = self._funcmap[(fileno, lineno)]
[152] Fix | Delete
except KeyError:
[153] Fix | Delete
filename = self._filemap.get(fileno)
[154] Fix | Delete
funcname = None
[155] Fix | Delete
self._funcmap[(fileno, lineno)] = (filename, funcname)
[156] Fix | Delete
return filename, funcname
[157] Fix | Delete
[158] Fix | Delete
def _loadfile(self, fileno):
[159] Fix | Delete
try:
[160] Fix | Delete
filename = self._filemap[fileno]
[161] Fix | Delete
except KeyError:
[162] Fix | Delete
print "Could not identify fileId", fileno
[163] Fix | Delete
return 1
[164] Fix | Delete
if filename is None:
[165] Fix | Delete
return 1
[166] Fix | Delete
absname = os.path.normcase(os.path.join(self.cwd, filename))
[167] Fix | Delete
[168] Fix | Delete
try:
[169] Fix | Delete
fp = open(absname)
[170] Fix | Delete
except IOError:
[171] Fix | Delete
return
[172] Fix | Delete
st = parser.suite(fp.read())
[173] Fix | Delete
fp.close()
[174] Fix | Delete
[175] Fix | Delete
# Scan the tree looking for def and lambda nodes, filling in
[176] Fix | Delete
# self._funcmap with all the available information.
[177] Fix | Delete
funcdef = symbol.funcdef
[178] Fix | Delete
lambdef = symbol.lambdef
[179] Fix | Delete
[180] Fix | Delete
stack = [st.totuple(1)]
[181] Fix | Delete
[182] Fix | Delete
while stack:
[183] Fix | Delete
tree = stack.pop()
[184] Fix | Delete
try:
[185] Fix | Delete
sym = tree[0]
[186] Fix | Delete
except (IndexError, TypeError):
[187] Fix | Delete
continue
[188] Fix | Delete
if sym == funcdef:
[189] Fix | Delete
self._funcmap[(fileno, tree[2][2])] = filename, tree[2][1]
[190] Fix | Delete
elif sym == lambdef:
[191] Fix | Delete
self._funcmap[(fileno, tree[1][2])] = filename, "<lambda>"
[192] Fix | Delete
stack.extend(list(tree[1:]))
[193] Fix | Delete
[194] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function