/[ascend]/trunk/pygtk/canvas/obrowser.py
ViewVC logotype

Contents of /trunk/pygtk/canvas/obrowser.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1984 - (show annotations) (download) (as text)
Wed Jan 21 22:56:28 2009 UTC (13 years, 5 months ago) by jpye
File MIME type: text/x-python
File size: 3576 byte(s)
Added Library::loadString method for loading string modules.
First steps on 'run' command for canvas-based models.
Fixed bug with use of obrowser.
Minor correction to blocktypes.a4c.
1 #!/usr/bin/env python
2
3 import pygtk
4 pygtk.require('2.0')
5 import gtk
6
7 class Browser:
8 def make_row( self, piter, name, value ):
9 info = repr(value)
10 if not hasattr(value, "__dict__"):
11 if len(info) > 80:
12 # it's a big list, or dict etc.
13 info = info[:80] + "..."
14 _piter = self.treestore.append( piter, [ name, type(value).__name__, info ] )
15 return _piter
16
17 def make_instance( self, value, piter ):
18 if hasattr( value, "__dict__" ):
19 for _name, _value in value.__dict__.items():
20 _piter = self.make_row( piter, "."+_name, _value )
21 _path = self.treestore.get_path( _piter )
22 self.otank[ _path ] = (_name, _value)
23
24 def make_mapping( self, value, piter ):
25 keys = []
26 if hasattr( value, "iteritems"):
27 for k,v in value.iteritems():
28 _name = "[%s]"%str(k)
29 _piter = self.make_row( piter, _name, v )
30 _path = self.treestore.get_path( _piter )
31 self.otank[ _path ] = (_name, v)
32 return
33 elif hasattr(value,'__getitem__') and hasattr(value,'__len__'):
34 keys = range(len(value))
35 elif hasattr(value,"__iter__"):
36 # handle 'set' objects...
37 for v in value:
38 _name = "..."
39 _piter = self.make_row(piter,_name, v)
40 _path = self.treestore.get_path(_piter)
41 self.otank[_path] = (_name,v)
42 return
43
44 for key in keys:
45 _name = "[%s]"%str(key)
46 _piter = self.make_row( piter, _name, value[key] )
47 _path = self.treestore.get_path( _piter )
48 self.otank[ _path ] = (_name, value[key])
49
50 def make(self, name=None, value=None, path=None, depth=1):
51 if path is None:
52 # make root node
53 piter = self.make_row( None, name, value )
54 path = self.treestore.get_path( piter )
55 self.otank[ path ] = (name, value)
56 else:
57 name, value = self.otank[ path ]
58
59 piter = self.treestore.get_iter( path )
60 if not self.treestore.iter_has_child( piter ):
61 self.make_mapping( value, piter )
62 self.make_instance( value, piter )
63
64 if depth:
65 for i in range( self.treestore.iter_n_children( piter ) ):
66 self.make( path = path+(i,), depth = depth - 1 )
67
68 def row_expanded( self, treeview, piter, path ):
69 self.make( path = path )
70
71 def delete_event(self, widget, event, data=None):
72 if self.quit:
73 gtk.main_quit()
74 return False
75
76 def __init__(self, name, value, quit=False):
77 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
78 self.window.set_title("Browser")
79 self.window.set_size_request(512, 320)
80 self.window.connect("delete_event", self.delete_event)
81 self.quit = quit
82
83 # we will store the name, the type name, and the repr
84 columns = [str,str,str]
85 self.treestore = gtk.TreeStore(*columns)
86
87 # the otank tells us what object we put at each node in the tree
88 self.otank = {} # map path -> (name,value)
89 self.make( name, value )
90
91 self.treeview = gtk.TreeView(self.treestore)
92 self.treeview.connect("row-expanded", self.row_expanded )
93
94 self.tvcolumns = [ gtk.TreeViewColumn() for _type in columns ]
95 i = 0
96 for tvcolumn in self.tvcolumns:
97 self.treeview.append_column(tvcolumn)
98 cell = gtk.CellRendererText()
99 tvcolumn.pack_start(cell, True)
100 tvcolumn.add_attribute(cell, 'text', i)
101 i = i + 1
102
103 view = gtk.ScrolledWindow()
104 view.add_with_viewport(self.treeview)
105 self.window.add(view)
106 #self.window.add(self.treeview)
107 self.window.show_all()
108
109
110 def dump( name, value ):
111 browser = Browser(name, value, quit=True)
112 gtk.main()
113
114 def test():
115 class Nil:
116 pass
117 a = Nil()
118 b = Nil()
119 c = Nil()
120 d = Nil()
121 a.b=b
122 a.x=set([3,4,5])
123 b.c=c
124 c.d=d
125 d.a=a # circular chain
126 dump( "a", a )
127
128 if __name__ == "__main__":
129 test()
130

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22