Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../idlelib
File: ObjectBrowser.py
# XXX TO DO:
[0] Fix | Delete
# - popup menu
[1] Fix | Delete
# - support partial or total redisplay
[2] Fix | Delete
# - more doc strings
[3] Fix | Delete
# - tooltips
[4] Fix | Delete
[5] Fix | Delete
# object browser
[6] Fix | Delete
[7] Fix | Delete
# XXX TO DO:
[8] Fix | Delete
# - for classes/modules, add "open source" to object browser
[9] Fix | Delete
[10] Fix | Delete
import re
[11] Fix | Delete
[12] Fix | Delete
from idlelib.TreeWidget import TreeItem, TreeNode, ScrolledCanvas
[13] Fix | Delete
[14] Fix | Delete
from repr import Repr
[15] Fix | Delete
[16] Fix | Delete
myrepr = Repr()
[17] Fix | Delete
myrepr.maxstring = 100
[18] Fix | Delete
myrepr.maxother = 100
[19] Fix | Delete
[20] Fix | Delete
class ObjectTreeItem(TreeItem):
[21] Fix | Delete
def __init__(self, labeltext, object, setfunction=None):
[22] Fix | Delete
self.labeltext = labeltext
[23] Fix | Delete
self.object = object
[24] Fix | Delete
self.setfunction = setfunction
[25] Fix | Delete
def GetLabelText(self):
[26] Fix | Delete
return self.labeltext
[27] Fix | Delete
def GetText(self):
[28] Fix | Delete
return myrepr.repr(self.object)
[29] Fix | Delete
def GetIconName(self):
[30] Fix | Delete
if not self.IsExpandable():
[31] Fix | Delete
return "python"
[32] Fix | Delete
def IsEditable(self):
[33] Fix | Delete
return self.setfunction is not None
[34] Fix | Delete
def SetText(self, text):
[35] Fix | Delete
try:
[36] Fix | Delete
value = eval(text)
[37] Fix | Delete
self.setfunction(value)
[38] Fix | Delete
except:
[39] Fix | Delete
pass
[40] Fix | Delete
else:
[41] Fix | Delete
self.object = value
[42] Fix | Delete
def IsExpandable(self):
[43] Fix | Delete
return not not dir(self.object)
[44] Fix | Delete
def GetSubList(self):
[45] Fix | Delete
keys = dir(self.object)
[46] Fix | Delete
sublist = []
[47] Fix | Delete
for key in keys:
[48] Fix | Delete
try:
[49] Fix | Delete
value = getattr(self.object, key)
[50] Fix | Delete
except AttributeError:
[51] Fix | Delete
continue
[52] Fix | Delete
item = make_objecttreeitem(
[53] Fix | Delete
str(key) + " =",
[54] Fix | Delete
value,
[55] Fix | Delete
lambda value, key=key, object=self.object:
[56] Fix | Delete
setattr(object, key, value))
[57] Fix | Delete
sublist.append(item)
[58] Fix | Delete
return sublist
[59] Fix | Delete
[60] Fix | Delete
class InstanceTreeItem(ObjectTreeItem):
[61] Fix | Delete
def IsExpandable(self):
[62] Fix | Delete
return True
[63] Fix | Delete
def GetSubList(self):
[64] Fix | Delete
sublist = ObjectTreeItem.GetSubList(self)
[65] Fix | Delete
sublist.insert(0,
[66] Fix | Delete
make_objecttreeitem("__class__ =", self.object.__class__))
[67] Fix | Delete
return sublist
[68] Fix | Delete
[69] Fix | Delete
class ClassTreeItem(ObjectTreeItem):
[70] Fix | Delete
def IsExpandable(self):
[71] Fix | Delete
return True
[72] Fix | Delete
def GetSubList(self):
[73] Fix | Delete
sublist = ObjectTreeItem.GetSubList(self)
[74] Fix | Delete
if len(self.object.__bases__) == 1:
[75] Fix | Delete
item = make_objecttreeitem("__bases__[0] =",
[76] Fix | Delete
self.object.__bases__[0])
[77] Fix | Delete
else:
[78] Fix | Delete
item = make_objecttreeitem("__bases__ =", self.object.__bases__)
[79] Fix | Delete
sublist.insert(0, item)
[80] Fix | Delete
return sublist
[81] Fix | Delete
[82] Fix | Delete
class AtomicObjectTreeItem(ObjectTreeItem):
[83] Fix | Delete
def IsExpandable(self):
[84] Fix | Delete
return 0
[85] Fix | Delete
[86] Fix | Delete
class SequenceTreeItem(ObjectTreeItem):
[87] Fix | Delete
def IsExpandable(self):
[88] Fix | Delete
return len(self.object) > 0
[89] Fix | Delete
def keys(self):
[90] Fix | Delete
return range(len(self.object))
[91] Fix | Delete
def GetSubList(self):
[92] Fix | Delete
sublist = []
[93] Fix | Delete
for key in self.keys():
[94] Fix | Delete
try:
[95] Fix | Delete
value = self.object[key]
[96] Fix | Delete
except KeyError:
[97] Fix | Delete
continue
[98] Fix | Delete
def setfunction(value, key=key, object=self.object):
[99] Fix | Delete
object[key] = value
[100] Fix | Delete
item = make_objecttreeitem("%r:" % (key,), value, setfunction)
[101] Fix | Delete
sublist.append(item)
[102] Fix | Delete
return sublist
[103] Fix | Delete
[104] Fix | Delete
class DictTreeItem(SequenceTreeItem):
[105] Fix | Delete
def keys(self):
[106] Fix | Delete
keys = self.object.keys()
[107] Fix | Delete
try:
[108] Fix | Delete
keys.sort()
[109] Fix | Delete
except:
[110] Fix | Delete
pass
[111] Fix | Delete
return keys
[112] Fix | Delete
[113] Fix | Delete
from types import *
[114] Fix | Delete
[115] Fix | Delete
dispatch = {
[116] Fix | Delete
IntType: AtomicObjectTreeItem,
[117] Fix | Delete
LongType: AtomicObjectTreeItem,
[118] Fix | Delete
FloatType: AtomicObjectTreeItem,
[119] Fix | Delete
StringType: AtomicObjectTreeItem,
[120] Fix | Delete
TupleType: SequenceTreeItem,
[121] Fix | Delete
ListType: SequenceTreeItem,
[122] Fix | Delete
DictType: DictTreeItem,
[123] Fix | Delete
InstanceType: InstanceTreeItem,
[124] Fix | Delete
ClassType: ClassTreeItem,
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
def make_objecttreeitem(labeltext, object, setfunction=None):
[128] Fix | Delete
t = type(object)
[129] Fix | Delete
if t in dispatch:
[130] Fix | Delete
c = dispatch[t]
[131] Fix | Delete
else:
[132] Fix | Delete
c = ObjectTreeItem
[133] Fix | Delete
return c(labeltext, object, setfunction)
[134] Fix | Delete
[135] Fix | Delete
[136] Fix | Delete
def _object_browser(parent):
[137] Fix | Delete
import sys
[138] Fix | Delete
from Tkinter import Tk
[139] Fix | Delete
root = Tk()
[140] Fix | Delete
root.title("Test ObjectBrowser")
[141] Fix | Delete
width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
[142] Fix | Delete
root.geometry("+%d+%d"%(x, y + 150))
[143] Fix | Delete
root.configure(bd=0, bg="yellow")
[144] Fix | Delete
root.focus_set()
[145] Fix | Delete
sc = ScrolledCanvas(root, bg="white", highlightthickness=0, takefocus=1)
[146] Fix | Delete
sc.frame.pack(expand=1, fill="both")
[147] Fix | Delete
item = make_objecttreeitem("sys", sys)
[148] Fix | Delete
node = TreeNode(sc.canvas, None, item)
[149] Fix | Delete
node.update()
[150] Fix | Delete
root.mainloop()
[151] Fix | Delete
[152] Fix | Delete
if __name__ == '__main__':
[153] Fix | Delete
from idlelib.idle_test.htest import run
[154] Fix | Delete
run(_object_browser)
[155] Fix | Delete
[156] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function