0001 # XXX TO DO: 0002 # - popup menu 0003 # - support partial or total redisplay 0004 # - more doc strings 0005 # - tooltips 0006 0007 # object browser 0008 0009 # XXX TO DO: 0010 # - for classes/modules, add "open source" to object browser 0011 0012 from TreeWidget import TreeItem, TreeNode, ScrolledCanvas 0013 0014 from repr import Repr 0015 0016 myrepr = Repr() 0017 myrepr.maxstring = 100 0018 myrepr.maxother = 100 0019 0020 class ObjectTreeItem(TreeItem): 0021 def __init__(self, labeltext, object, setfunction=None): 0022 self.labeltext = labeltext 0023 self.object = object 0024 self.setfunction = setfunction 0025 def GetLabelText(self): 0026 return self.labeltext 0027 def GetText(self): 0028 return myrepr.repr(self.object) 0029 def GetIconName(self): 0030 if not self.IsExpandable(): 0031 return "python" 0032 def IsEditable(self): 0033 return self.setfunction is not None 0034 def SetText(self, text): 0035 try: 0036 value = eval(text) 0037 self.setfunction(value) 0038 except: 0039 pass 0040 else: 0041 self.object = value 0042 def IsExpandable(self): 0043 return not not dir(self.object) 0044 def GetSubList(self): 0045 keys = dir(self.object) 0046 sublist = [] 0047 for key in keys: 0048 try: 0049 value = getattr(self.object, key) 0050 except AttributeError: 0051 continue 0052 item = make_objecttreeitem( 0053 str(key) + " =", 0054 value, 0055 lambda value, key=key, object=self.object: 0056 setattr(object, key, value)) 0057 sublist.append(item) 0058 return sublist 0059 0060 class InstanceTreeItem(ObjectTreeItem): 0061 def IsExpandable(self): 0062 return True 0063 def GetSubList(self): 0064 sublist = ObjectTreeItem.GetSubList(self) 0065 sublist.insert(0, 0066 make_objecttreeitem("__class__ =", self.object.__class__)) 0067 return sublist 0068 0069 class ClassTreeItem(ObjectTreeItem): 0070 def IsExpandable(self): 0071 return True 0072 def GetSubList(self): 0073 sublist = ObjectTreeItem.GetSubList(self) 0074 if len(self.object.__bases__) == 1: 0075 item = make_objecttreeitem("__bases__[0] =", 0076 self.object.__bases__[0]) 0077 else: 0078 item = make_objecttreeitem("__bases__ =", self.object.__bases__) 0079 sublist.insert(0, item) 0080 return sublist 0081 0082 class AtomicObjectTreeItem(ObjectTreeItem): 0083 def IsExpandable(self): 0084 return 0 0085 0086 class SequenceTreeItem(ObjectTreeItem): 0087 def IsExpandable(self): 0088 return len(self.object) > 0 0089 def keys(self): 0090 return range(len(self.object)) 0091 def GetSubList(self): 0092 sublist = [] 0093 for key in self.keys(): 0094 try: 0095 value = self.object[key] 0096 except KeyError: 0097 continue 0098 def setfunction(value, key=key, object=self.object): 0099 object[key] = value 0100 item = make_objecttreeitem("%r:" % (key,), value, setfunction) 0101 sublist.append(item) 0102 return sublist 0103 0104 class DictTreeItem(SequenceTreeItem): 0105 def keys(self): 0106 keys = self.object.keys() 0107 try: 0108 keys.sort() 0109 except: 0110 pass 0111 return keys 0112 0113 from types import * 0114 0115 dispatch = { 0116 IntType: AtomicObjectTreeItem, 0117 LongType: AtomicObjectTreeItem, 0118 FloatType: AtomicObjectTreeItem, 0119 StringType: AtomicObjectTreeItem, 0120 TupleType: SequenceTreeItem, 0121 ListType: SequenceTreeItem, 0122 DictType: DictTreeItem, 0123 InstanceType: InstanceTreeItem, 0124 ClassType: ClassTreeItem, 0125 } 0126 0127 def make_objecttreeitem(labeltext, object, setfunction=None): 0128 t = type(object) 0129 if dispatch.has_key(t): 0130 c = dispatch[t] 0131 else: 0132 c = ObjectTreeItem 0133 return c(labeltext, object, setfunction) 0134 0135 # Test script 0136 0137 def _test(): 0138 import sys 0139 from Tkinter import Tk 0140 root = Tk() 0141 root.configure(bd=0, bg="yellow") 0142 root.focus_set() 0143 sc = ScrolledCanvas(root, bg="white", highlightthickness=0, takefocus=1) 0144 sc.frame.pack(expand=1, fill="both") 0145 item = make_objecttreeitem("sys", sys) 0146 node = TreeNode(sc.canvas, None, item) 0147 node.update() 0148 root.mainloop() 0149 0150 if __name__ == '__main__': 0151 _test() 0152
Generated by PyXR 0.9.4