1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import warnings
17
19 """This class provides a mechanism to interface between Vision and
20 another application (for example, it allows Pmv to add nodes to Vision)
21 Objects created in the application (e.g. in Pmv) are stored in the
22 self.objects list. Here we store the object, a name to be used for the
23 Vision node, and optional keywords if the Vision node needs them. For
24 example the kw 'constrkw' is used in Vision for saving/restoring properly.
25 In addition, there is a lookup table to describe the connection between
26 an object in the application (such as a molecule) and the corresponding
27 Vision node.
28
29 Adding an object to this interface using the add() method will
30 automatically add the node to Vision -- if Vision is running at this
31 moment. If Vision is not running, we still add the object to our list and
32 in the command that starts Vision inside the application (e.g. in Pmv this
33 would be 'visionCommands') a mechanism has to be implemented to loop over
34 the objects in our list and add them to Vision -- that's 2 lines of code:
35 see visionCommands!"""
36
37
39 self.ed = ed
40 self.lib = lib
41 self.objects = []
42
43
44 self.lookup = {}
45
46
47
48
49
50
51 - def add(self, obj, name, kw=None):
52 """Add an object to this API. This will automatically add a node
53 to a Vision library if Vision is running.
54 - obj: the appliaction object (such as a molecule)
55 - name: name to be used for the Vision node
56 - kw: optional Vision node constructor keywords.
57 """
58 if kw is None:
59 kw = {}
60
61
62
63 if obj.__class__ in self.lookup.keys():
64
65 self.objects.append( (obj, name, kw) )
66
67
68 if self.ed is not None and self.lib is not None:
69 self.addNodeToLibrary(obj, name, kw)
70 return
71 else:
72 for k in self.lookup.keys():
73 if isinstance(obj, k):
74
75 self.objects.append( (obj, name, kw) )
76
77
78 if self.ed is not None and self.lib is not None:
79 self.addNodeToLibrary(obj, name, kw)
80 return
81 import traceback
82 traceback.print_exc()
83 warnings.warn(
84 "ERROR: Object not found in lookup. Cannot create node")
85
86
88 """Delete an object from the list of objects. This also deletes the
89 node proxy in the category and all node instances in all networks."""
90
91
92 found = None
93 for i in range(len(self.objects)):
94 if self.objects[i][0] == obj:
95 found = self.objects[i][0]
96
97 del self.objects[i]
98 break
99
100 if found is None:
101 return
102
103 node = self.lookup[found.__class__]['node']
104 cat = self.lookup[found.__class__]['category']
105
106
107 if self.ed is None:
108 return
109
110
111
112
113
114 self.lib.deleteNodeFromCategoryFrame(cat, node, found.name)
115
116
117 for net in self.ed.networks.values():
118 for n in net.nodes:
119 if n.__class__ == node:
120 net.deleteNodes([n])
121
122
128
129
131 """helper method to get a handle to a Vision library"""
132 from Vision.VPE import NodeLibrary
133 assert isinstance(lib, NodeLibrary)
134 self.lib = lib
135
136
138 """Add a class instance of a Vision node to this list"""
139 self.lookup[obj] = {'node':node, 'category':cat}
140
141
142
144 """add node to Visual Programming Environment library"""
145
146 if kw is None:
147 kw = {}
148
149 if obj.__class__ in self.lookup.keys():
150 o = self.lookup[obj.__class__]
151 else:
152 for k in self.lookup.keys():
153 if isinstance(obj, k):
154 o = self.lookup[k]
155 break
156 node = o['node']
157 cat = o['category']
158
159 self.lib.addNode(node, name, cat, (), kw)
160
161
162 if len(self.lib.libraryDescr[cat]['nodes']) == 1:
163 self.lib.resizeCategories()
164