1 | n/a | # XXX TO DO: |
---|
2 | n/a | # - popup menu |
---|
3 | n/a | # - support partial or total redisplay |
---|
4 | n/a | # - more doc strings |
---|
5 | n/a | # - tooltips |
---|
6 | n/a | |
---|
7 | n/a | # object browser |
---|
8 | n/a | |
---|
9 | n/a | # XXX TO DO: |
---|
10 | n/a | # - for classes/modules, add "open source" to object browser |
---|
11 | n/a | from reprlib import Repr |
---|
12 | n/a | |
---|
13 | n/a | from idlelib.tree import TreeItem, TreeNode, ScrolledCanvas |
---|
14 | n/a | |
---|
15 | n/a | myrepr = Repr() |
---|
16 | n/a | myrepr.maxstring = 100 |
---|
17 | n/a | myrepr.maxother = 100 |
---|
18 | n/a | |
---|
19 | n/a | class ObjectTreeItem(TreeItem): |
---|
20 | n/a | def __init__(self, labeltext, object, setfunction=None): |
---|
21 | n/a | self.labeltext = labeltext |
---|
22 | n/a | self.object = object |
---|
23 | n/a | self.setfunction = setfunction |
---|
24 | n/a | def GetLabelText(self): |
---|
25 | n/a | return self.labeltext |
---|
26 | n/a | def GetText(self): |
---|
27 | n/a | return myrepr.repr(self.object) |
---|
28 | n/a | def GetIconName(self): |
---|
29 | n/a | if not self.IsExpandable(): |
---|
30 | n/a | return "python" |
---|
31 | n/a | def IsEditable(self): |
---|
32 | n/a | return self.setfunction is not None |
---|
33 | n/a | def SetText(self, text): |
---|
34 | n/a | try: |
---|
35 | n/a | value = eval(text) |
---|
36 | n/a | self.setfunction(value) |
---|
37 | n/a | except: |
---|
38 | n/a | pass |
---|
39 | n/a | else: |
---|
40 | n/a | self.object = value |
---|
41 | n/a | def IsExpandable(self): |
---|
42 | n/a | return not not dir(self.object) |
---|
43 | n/a | def GetSubList(self): |
---|
44 | n/a | keys = dir(self.object) |
---|
45 | n/a | sublist = [] |
---|
46 | n/a | for key in keys: |
---|
47 | n/a | try: |
---|
48 | n/a | value = getattr(self.object, key) |
---|
49 | n/a | except AttributeError: |
---|
50 | n/a | continue |
---|
51 | n/a | item = make_objecttreeitem( |
---|
52 | n/a | str(key) + " =", |
---|
53 | n/a | value, |
---|
54 | n/a | lambda value, key=key, object=self.object: |
---|
55 | n/a | setattr(object, key, value)) |
---|
56 | n/a | sublist.append(item) |
---|
57 | n/a | return sublist |
---|
58 | n/a | |
---|
59 | n/a | class ClassTreeItem(ObjectTreeItem): |
---|
60 | n/a | def IsExpandable(self): |
---|
61 | n/a | return True |
---|
62 | n/a | def GetSubList(self): |
---|
63 | n/a | sublist = ObjectTreeItem.GetSubList(self) |
---|
64 | n/a | if len(self.object.__bases__) == 1: |
---|
65 | n/a | item = make_objecttreeitem("__bases__[0] =", |
---|
66 | n/a | self.object.__bases__[0]) |
---|
67 | n/a | else: |
---|
68 | n/a | item = make_objecttreeitem("__bases__ =", self.object.__bases__) |
---|
69 | n/a | sublist.insert(0, item) |
---|
70 | n/a | return sublist |
---|
71 | n/a | |
---|
72 | n/a | class AtomicObjectTreeItem(ObjectTreeItem): |
---|
73 | n/a | def IsExpandable(self): |
---|
74 | n/a | return 0 |
---|
75 | n/a | |
---|
76 | n/a | class SequenceTreeItem(ObjectTreeItem): |
---|
77 | n/a | def IsExpandable(self): |
---|
78 | n/a | return len(self.object) > 0 |
---|
79 | n/a | def keys(self): |
---|
80 | n/a | return range(len(self.object)) |
---|
81 | n/a | def GetSubList(self): |
---|
82 | n/a | sublist = [] |
---|
83 | n/a | for key in self.keys(): |
---|
84 | n/a | try: |
---|
85 | n/a | value = self.object[key] |
---|
86 | n/a | except KeyError: |
---|
87 | n/a | continue |
---|
88 | n/a | def setfunction(value, key=key, object=self.object): |
---|
89 | n/a | object[key] = value |
---|
90 | n/a | item = make_objecttreeitem("%r:" % (key,), value, setfunction) |
---|
91 | n/a | sublist.append(item) |
---|
92 | n/a | return sublist |
---|
93 | n/a | |
---|
94 | n/a | class DictTreeItem(SequenceTreeItem): |
---|
95 | n/a | def keys(self): |
---|
96 | n/a | keys = list(self.object.keys()) |
---|
97 | n/a | try: |
---|
98 | n/a | keys.sort() |
---|
99 | n/a | except: |
---|
100 | n/a | pass |
---|
101 | n/a | return keys |
---|
102 | n/a | |
---|
103 | n/a | dispatch = { |
---|
104 | n/a | int: AtomicObjectTreeItem, |
---|
105 | n/a | float: AtomicObjectTreeItem, |
---|
106 | n/a | str: AtomicObjectTreeItem, |
---|
107 | n/a | tuple: SequenceTreeItem, |
---|
108 | n/a | list: SequenceTreeItem, |
---|
109 | n/a | dict: DictTreeItem, |
---|
110 | n/a | type: ClassTreeItem, |
---|
111 | n/a | } |
---|
112 | n/a | |
---|
113 | n/a | def make_objecttreeitem(labeltext, object, setfunction=None): |
---|
114 | n/a | t = type(object) |
---|
115 | n/a | if t in dispatch: |
---|
116 | n/a | c = dispatch[t] |
---|
117 | n/a | else: |
---|
118 | n/a | c = ObjectTreeItem |
---|
119 | n/a | return c(labeltext, object, setfunction) |
---|
120 | n/a | |
---|
121 | n/a | |
---|
122 | n/a | def _object_browser(parent): # htest # |
---|
123 | n/a | import sys |
---|
124 | n/a | from tkinter import Toplevel |
---|
125 | n/a | top = Toplevel(parent) |
---|
126 | n/a | top.title("Test debug object browser") |
---|
127 | n/a | x, y = map(int, parent.geometry().split('+')[1:]) |
---|
128 | n/a | top.geometry("+%d+%d" % (x + 100, y + 175)) |
---|
129 | n/a | top.configure(bd=0, bg="yellow") |
---|
130 | n/a | top.focus_set() |
---|
131 | n/a | sc = ScrolledCanvas(top, bg="white", highlightthickness=0, takefocus=1) |
---|
132 | n/a | sc.frame.pack(expand=1, fill="both") |
---|
133 | n/a | item = make_objecttreeitem("sys", sys) |
---|
134 | n/a | node = TreeNode(sc.canvas, None, item) |
---|
135 | n/a | node.update() |
---|
136 | n/a | |
---|
137 | n/a | if __name__ == '__main__': |
---|
138 | n/a | from idlelib.idle_test.htest import run |
---|
139 | n/a | run(_object_browser) |
---|