Package Vision :: Module API
[hide private]
[frames] | no frames]

Source Code for Module Vision.API

  1  ######################################################################### 
  2  # 
  3  # Date: May 2004  Authors: Daniel Stoffler, Michel Sanner 
  4  # 
  5  #       stoffler@scripps.edu 
  6  #       sanner@scripps.edu 
  7  # 
  8  #       The Scripps Research Institute (TSRI) 
  9  #       Molecular Graphics Lab 
 10  #       La Jolla, CA 92037, USA 
 11  # 
 12  # Copyright: Daniel Stoffler, Michel Sanner, and TSRI 
 13  # 
 14  ######################################################################### 
 15   
 16  import warnings 
 17   
18 -class VisionInterface:
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
38 - def __init__(self, ed=None, lib=None):
39 self.ed = ed # handle to Vision 40 self.lib = lib # handle to the Vision library 41 self.objects = [] # list of objects to be added as nodes to Vision 42 # this contains tuples of (object, name, kw) 43 44 self.lookup = {} # lookup table that defines what node class,
45 # which category. Will be used to assign a 46 # Vision node to the object 47 # key: the object 48 # value: a tuple with the node class and 49 # category name 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 # find out which Vision node corresponds to this object, based on our 62 # lookup table 63 if obj.__class__ in self.lookup.keys(): 64 # add to our list of objects 65 self.objects.append( (obj, name, kw) ) 66 67 # add node to Visual Programming Environment library if this exists 68 if self.ed is not None and self.lib is not None: 69 self.addNodeToLibrary(obj, name, kw) 70 return 71 else: # the class was not found, try to see if a base class is there 72 for k in self.lookup.keys(): 73 if isinstance(obj, k): 74 # add to our list of objects 75 self.objects.append( (obj, name, kw) ) 76 77 # add node to Visual Programming Environment library if this exists 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
87 - def remove(self, obj):
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 # is the object in our list of objects? 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 # delete from our list of objects 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 # if Vision was not started yet, return here 107 if self.ed is None: 108 return 109 110 ## STEP 1) Delete node proxy in category 111 112 # note: here, the 'node' is not a node instance, but a class, 113 # therefore we can just pass the object 114 self.lib.deleteNodeFromCategoryFrame(cat, node, found.name) 115 116 ## STEP 2) Delete all instances of this node in all networks 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
123 - def setEditor(self, ed):
124 """helper method to get a handle to Vision""" 125 from Vision.VPE import VisualProgramingEnvironment 126 assert isinstance(ed, VisualProgramingEnvironment) 127 self.ed = ed
128 129
130 - def setLibrary(self, lib):
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
137 - def addToLookup(self, obj, node, cat):
138 """Add a class instance of a Vision node to this list""" 139 self.lookup[obj] = {'node':node, 'category':cat}
140 141 142
143 - def addNodeToLibrary(self, obj, name, kw=None):
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 # if a node is added to an empty category, we resize all 162 if len(self.lib.libraryDescr[cat]['nodes']) == 1: 163 self.lib.resizeCategories()
164