Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../idlelib
File: ClassBrowser.py
"""Class browser.
[0] Fix | Delete
[1] Fix | Delete
XXX TO DO:
[2] Fix | Delete
[3] Fix | Delete
- reparse when source changed (maybe just a button would be OK?)
[4] Fix | Delete
(or recheck on window popup)
[5] Fix | Delete
- add popup menu with more options (e.g. doc strings, base classes, imports)
[6] Fix | Delete
- show function argument list? (have to do pattern matching on source)
[7] Fix | Delete
- should the classes and methods lists also be in the module's menu bar?
[8] Fix | Delete
- add base classes to class browser tree
[9] Fix | Delete
"""
[10] Fix | Delete
[11] Fix | Delete
import os
[12] Fix | Delete
import sys
[13] Fix | Delete
import pyclbr
[14] Fix | Delete
[15] Fix | Delete
from idlelib import PyShell
[16] Fix | Delete
from idlelib.WindowList import ListedToplevel
[17] Fix | Delete
from idlelib.TreeWidget import TreeNode, TreeItem, ScrolledCanvas
[18] Fix | Delete
from idlelib.configHandler import idleConf
[19] Fix | Delete
[20] Fix | Delete
file_open = None # Method...Item and Class...Item use this.
[21] Fix | Delete
# Normally PyShell.flist.open, but there is no PyShell.flist for htest.
[22] Fix | Delete
[23] Fix | Delete
class ClassBrowser:
[24] Fix | Delete
[25] Fix | Delete
def __init__(self, flist, name, path, _htest=False):
[26] Fix | Delete
# XXX This API should change, if the file doesn't end in ".py"
[27] Fix | Delete
# XXX the code here is bogus!
[28] Fix | Delete
"""
[29] Fix | Delete
_htest - bool, change box when location running htest.
[30] Fix | Delete
"""
[31] Fix | Delete
global file_open
[32] Fix | Delete
if not _htest:
[33] Fix | Delete
file_open = PyShell.flist.open
[34] Fix | Delete
self.name = name
[35] Fix | Delete
self.file = os.path.join(path[0], self.name + ".py")
[36] Fix | Delete
self._htest = _htest
[37] Fix | Delete
self.init(flist)
[38] Fix | Delete
[39] Fix | Delete
def close(self, event=None):
[40] Fix | Delete
self.top.destroy()
[41] Fix | Delete
self.node.destroy()
[42] Fix | Delete
[43] Fix | Delete
def init(self, flist):
[44] Fix | Delete
self.flist = flist
[45] Fix | Delete
# reset pyclbr
[46] Fix | Delete
pyclbr._modules.clear()
[47] Fix | Delete
# create top
[48] Fix | Delete
self.top = top = ListedToplevel(flist.root)
[49] Fix | Delete
top.protocol("WM_DELETE_WINDOW", self.close)
[50] Fix | Delete
top.bind("<Escape>", self.close)
[51] Fix | Delete
if self._htest: # place dialog below parent if running htest
[52] Fix | Delete
top.geometry("+%d+%d" %
[53] Fix | Delete
(flist.root.winfo_rootx(), flist.root.winfo_rooty() + 200))
[54] Fix | Delete
self.settitle()
[55] Fix | Delete
top.focus_set()
[56] Fix | Delete
# create scrolled canvas
[57] Fix | Delete
theme = idleConf.CurrentTheme()
[58] Fix | Delete
background = idleConf.GetHighlight(theme, 'normal')['background']
[59] Fix | Delete
sc = ScrolledCanvas(top, bg=background, highlightthickness=0, takefocus=1)
[60] Fix | Delete
sc.frame.pack(expand=1, fill="both")
[61] Fix | Delete
item = self.rootnode()
[62] Fix | Delete
self.node = node = TreeNode(sc.canvas, None, item)
[63] Fix | Delete
node.update()
[64] Fix | Delete
node.expand()
[65] Fix | Delete
[66] Fix | Delete
def settitle(self):
[67] Fix | Delete
self.top.wm_title("Class Browser - " + self.name)
[68] Fix | Delete
self.top.wm_iconname("Class Browser")
[69] Fix | Delete
[70] Fix | Delete
def rootnode(self):
[71] Fix | Delete
return ModuleBrowserTreeItem(self.file)
[72] Fix | Delete
[73] Fix | Delete
class ModuleBrowserTreeItem(TreeItem):
[74] Fix | Delete
[75] Fix | Delete
def __init__(self, file):
[76] Fix | Delete
self.file = file
[77] Fix | Delete
[78] Fix | Delete
def GetText(self):
[79] Fix | Delete
return os.path.basename(self.file)
[80] Fix | Delete
[81] Fix | Delete
def GetIconName(self):
[82] Fix | Delete
return "python"
[83] Fix | Delete
[84] Fix | Delete
def GetSubList(self):
[85] Fix | Delete
sublist = []
[86] Fix | Delete
for name in self.listclasses():
[87] Fix | Delete
item = ClassBrowserTreeItem(name, self.classes, self.file)
[88] Fix | Delete
sublist.append(item)
[89] Fix | Delete
return sublist
[90] Fix | Delete
[91] Fix | Delete
def OnDoubleClick(self):
[92] Fix | Delete
if os.path.normcase(self.file[-3:]) != ".py":
[93] Fix | Delete
return
[94] Fix | Delete
if not os.path.exists(self.file):
[95] Fix | Delete
return
[96] Fix | Delete
PyShell.flist.open(self.file)
[97] Fix | Delete
[98] Fix | Delete
def IsExpandable(self):
[99] Fix | Delete
return os.path.normcase(self.file[-3:]) == ".py"
[100] Fix | Delete
[101] Fix | Delete
def listclasses(self):
[102] Fix | Delete
dir, file = os.path.split(self.file)
[103] Fix | Delete
name, ext = os.path.splitext(file)
[104] Fix | Delete
if os.path.normcase(ext) != ".py":
[105] Fix | Delete
return []
[106] Fix | Delete
try:
[107] Fix | Delete
dict = pyclbr.readmodule_ex(name, [dir] + sys.path)
[108] Fix | Delete
except ImportError:
[109] Fix | Delete
return []
[110] Fix | Delete
items = []
[111] Fix | Delete
self.classes = {}
[112] Fix | Delete
for key, cl in dict.items():
[113] Fix | Delete
if cl.module == name:
[114] Fix | Delete
s = key
[115] Fix | Delete
if hasattr(cl, 'super') and cl.super:
[116] Fix | Delete
supers = []
[117] Fix | Delete
for sup in cl.super:
[118] Fix | Delete
if type(sup) is type(''):
[119] Fix | Delete
sname = sup
[120] Fix | Delete
else:
[121] Fix | Delete
sname = sup.name
[122] Fix | Delete
if sup.module != cl.module:
[123] Fix | Delete
sname = "%s.%s" % (sup.module, sname)
[124] Fix | Delete
supers.append(sname)
[125] Fix | Delete
s = s + "(%s)" % ", ".join(supers)
[126] Fix | Delete
items.append((cl.lineno, s))
[127] Fix | Delete
self.classes[s] = cl
[128] Fix | Delete
items.sort()
[129] Fix | Delete
list = []
[130] Fix | Delete
for item, s in items:
[131] Fix | Delete
list.append(s)
[132] Fix | Delete
return list
[133] Fix | Delete
[134] Fix | Delete
class ClassBrowserTreeItem(TreeItem):
[135] Fix | Delete
[136] Fix | Delete
def __init__(self, name, classes, file):
[137] Fix | Delete
self.name = name
[138] Fix | Delete
self.classes = classes
[139] Fix | Delete
self.file = file
[140] Fix | Delete
try:
[141] Fix | Delete
self.cl = self.classes[self.name]
[142] Fix | Delete
except (IndexError, KeyError):
[143] Fix | Delete
self.cl = None
[144] Fix | Delete
self.isfunction = isinstance(self.cl, pyclbr.Function)
[145] Fix | Delete
[146] Fix | Delete
def GetText(self):
[147] Fix | Delete
if self.isfunction:
[148] Fix | Delete
return "def " + self.name + "(...)"
[149] Fix | Delete
else:
[150] Fix | Delete
return "class " + self.name
[151] Fix | Delete
[152] Fix | Delete
def GetIconName(self):
[153] Fix | Delete
if self.isfunction:
[154] Fix | Delete
return "python"
[155] Fix | Delete
else:
[156] Fix | Delete
return "folder"
[157] Fix | Delete
[158] Fix | Delete
def IsExpandable(self):
[159] Fix | Delete
if self.cl:
[160] Fix | Delete
try:
[161] Fix | Delete
return not not self.cl.methods
[162] Fix | Delete
except AttributeError:
[163] Fix | Delete
return False
[164] Fix | Delete
[165] Fix | Delete
def GetSubList(self):
[166] Fix | Delete
if not self.cl:
[167] Fix | Delete
return []
[168] Fix | Delete
sublist = []
[169] Fix | Delete
for name in self.listmethods():
[170] Fix | Delete
item = MethodBrowserTreeItem(name, self.cl, self.file)
[171] Fix | Delete
sublist.append(item)
[172] Fix | Delete
return sublist
[173] Fix | Delete
[174] Fix | Delete
def OnDoubleClick(self):
[175] Fix | Delete
if not os.path.exists(self.file):
[176] Fix | Delete
return
[177] Fix | Delete
edit = file_open(self.file)
[178] Fix | Delete
if hasattr(self.cl, 'lineno'):
[179] Fix | Delete
lineno = self.cl.lineno
[180] Fix | Delete
edit.gotoline(lineno)
[181] Fix | Delete
[182] Fix | Delete
def listmethods(self):
[183] Fix | Delete
if not self.cl:
[184] Fix | Delete
return []
[185] Fix | Delete
items = []
[186] Fix | Delete
for name, lineno in self.cl.methods.items():
[187] Fix | Delete
items.append((lineno, name))
[188] Fix | Delete
items.sort()
[189] Fix | Delete
list = []
[190] Fix | Delete
for item, name in items:
[191] Fix | Delete
list.append(name)
[192] Fix | Delete
return list
[193] Fix | Delete
[194] Fix | Delete
class MethodBrowserTreeItem(TreeItem):
[195] Fix | Delete
[196] Fix | Delete
def __init__(self, name, cl, file):
[197] Fix | Delete
self.name = name
[198] Fix | Delete
self.cl = cl
[199] Fix | Delete
self.file = file
[200] Fix | Delete
[201] Fix | Delete
def GetText(self):
[202] Fix | Delete
return "def " + self.name + "(...)"
[203] Fix | Delete
[204] Fix | Delete
def GetIconName(self):
[205] Fix | Delete
return "python" # XXX
[206] Fix | Delete
[207] Fix | Delete
def IsExpandable(self):
[208] Fix | Delete
return 0
[209] Fix | Delete
[210] Fix | Delete
def OnDoubleClick(self):
[211] Fix | Delete
if not os.path.exists(self.file):
[212] Fix | Delete
return
[213] Fix | Delete
edit = file_open(self.file)
[214] Fix | Delete
edit.gotoline(self.cl.methods[self.name])
[215] Fix | Delete
[216] Fix | Delete
def _class_browser(parent): #Wrapper for htest
[217] Fix | Delete
try:
[218] Fix | Delete
file = __file__
[219] Fix | Delete
except NameError:
[220] Fix | Delete
file = sys.argv[0]
[221] Fix | Delete
if sys.argv[1:]:
[222] Fix | Delete
file = sys.argv[1]
[223] Fix | Delete
else:
[224] Fix | Delete
file = sys.argv[0]
[225] Fix | Delete
dir, file = os.path.split(file)
[226] Fix | Delete
name = os.path.splitext(file)[0]
[227] Fix | Delete
flist = PyShell.PyShellFileList(parent)
[228] Fix | Delete
global file_open
[229] Fix | Delete
file_open = flist.open
[230] Fix | Delete
ClassBrowser(flist, name, [dir], _htest=True)
[231] Fix | Delete
[232] Fix | Delete
if __name__ == "__main__":
[233] Fix | Delete
from idlelib.idle_test.htest import run
[234] Fix | Delete
run(_class_browser)
[235] Fix | Delete
[236] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function