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

Source Code for Module Vision.UserLibBuild

  1  ######################################################################## 
  2  # 
  3  # Date: January 2006 Authors: Guillaume Vareille, Michel Sanner 
  4  # 
  5  #    vareille@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: Guillaume Vareille, Michel Sanner and TSRI 
 13  # 
 14  ######################################################################### 
 15  # 
 16  # $Header$ 
 17  # 
 18  # $Id$ 
 19  # 
 20   
 21  import os 
 22  import sys 
 23  import warnings 
 24  import stat 
 25  from inspect import isclass 
 26   
 27   
28 -def addDirToSysPath(dirToAdd):
29 fullpath = os.path.abspath(dirToAdd) 30 found = False 31 for p in sys.path: 32 if fullpath == p: 33 # do nothing if this one is already in sys.path 34 return 35 # else, append parent to sys.path 36 sys.path.append(fullpath)
37 38
39 -def userLibBuild(libInstance, callingFile):
40 thisPath = os.path.split(callingFile)[0] 41 libdir = os.path.split(thisPath)[-1] 42 for dirItem in os.listdir(thisPath): 43 subdir = os.path.join(thisPath, dirItem) 44 if ( not os.path.isdir(subdir) ) or \ 45 ( len( dirItem.split(' ') ) > 1 ) or \ 46 ( len( dirItem.split('_') ) > 1 ): 47 continue 48 for subdirItem in os.listdir(subdir): 49 if subdirItem.endswith('.py') and (subdirItem != '__init__.py'): 50 filenameWithoutExt = os.path.splitext(subdirItem)[0] 51 moduleName = libdir + '.' + dirItem + '.' + filenameWithoutExt 52 if '__init__.py' not in os.listdir(subdir): 53 lCategory1Init = thisPath + os.sep + dirItem + os.sep + '__init__.py' 54 f = open(lCategory1Init, "w") 55 f.close() 56 try: 57 mod = __import__(moduleName, globals(), locals(),[filenameWithoutExt]) 58 reload(mod) 59 for modItemName in dir(mod): 60 modItem = getattr(mod, modItemName) 61 from NetworkEditor.items import NetworkNode 62 if isclass(modItem) and issubclass(modItem, NetworkNode): 63 if modItem.__module__ == moduleName: 64 if hasattr(modItem, 'mRequiredTypes'): 65 for lTypeName, lModuleName in modItem.mRequiredTypes.items(): 66 mad = __import__(lModuleName, globals(), locals(), [lTypeName]) 67 madItem = getattr(mad, lTypeName) 68 libInstance.typesTable.append(madItem()) 69 if hasattr(modItem, 'mRequiredSynonyms'): 70 for lSynonym in modItem.mRequiredSynonyms: 71 libInstance.synonymsTable.append(lSynonym) 72 libInstance.addNode(modItem, modItemName, dirItem) 73 except Exception, e: 74 print "userLibBuild: unable to load library %s\n" % moduleName, e
75 76
77 -def ensureDefaultUserLibFile():
78 ################################################################## 79 # verify or generate the default user lib file 80 ################################################################## 81 from mglutil.util.packageFilePath import getResourceFolderWithVersion 82 userResourceFolder = getResourceFolderWithVersion() 83 if userResourceFolder is None: 84 return 85 userVisionDir = userResourceFolder + os.sep + 'Vision' + os.sep 86 userLibsDir = userVisionDir + 'UserLibs' + os.sep 87 defaultLibDir = userLibsDir + 'MyDefaultLib' + os.sep 88 defaultLibInit = defaultLibDir + '__init__.py' 89 if os.path.isfile(defaultLibInit) is False: 90 try: 91 if os.path.isdir(userResourceFolder) is False: 92 os.mkdir(userResourceFolder) 93 if os.path.isdir(userVisionDir) is False: 94 os.mkdir(userVisionDir) 95 if os.path.isdir(userLibsDir) is False: 96 os.mkdir(userLibsDir) 97 userLibsInit = userLibsDir + '__init__.py' 98 if os.path.isfile(userLibsInit) is False: 99 f = open(userLibsInit, "w") 100 f.close() 101 if os.path.isdir(defaultLibDir) is False: 102 os.mkdir(defaultLibDir) 103 category1Dir = defaultLibDir + 'input' + os.sep 104 if os.path.isdir(category1Dir) is False: 105 os.mkdir(category1Dir) 106 category1Init = category1Dir + '__init__.py' 107 if os.path.isfile(category1Init) is False: 108 f = open(category1Init, "w") 109 f.close() 110 category2Dir = defaultLibDir + 'output' + os.sep 111 if os.path.isdir(category2Dir) is False: 112 os.mkdir(category2Dir) 113 category2Init = category2Dir + '__init__.py' 114 if os.path.isfile(category2Init) is False: 115 f = open(category2Init, "w") 116 f.close() 117 category3Dir = defaultLibDir + 'macro' + os.sep 118 if os.path.isdir(category3Dir) is False: 119 os.mkdir(category3Dir) 120 category3Init = category3Dir + '__init__.py' 121 if os.path.isfile(category3Init) is False: 122 f = open(category3Init, "w") 123 f.close() 124 category4Dir = defaultLibDir + 'other' + os.sep 125 if os.path.isdir(category4Dir) is False: 126 os.mkdir(category4Dir) 127 category4Init = category4Dir + '__init__.py' 128 if os.path.isfile(category4Init) is False: 129 f = open(category4Init, "w") 130 f.close() 131 f = open(defaultLibInit, "w") 132 txt = """######################################################################## 133 # 134 # Date: Jan 2006 Authors: Guillaume Vareille, Michel Sanner 135 # 136 # vareille@scripps.edu 137 # sanner@scripps.edu 138 # 139 # The Scripps Research Institute (TSRI) 140 # Molecular Graphics Lab 141 # La Jolla, CA 92037, USA 142 # 143 # Copyright: Guillaume Vareille, Michel Sanner and TSRI 144 # 145 # Vision Library Loader 146 # 147 ######################################################################### 148 # 149 # %s 150 # Vision will generate this file automatically if it can't find it 151 # 152 153 from os import sep, path 154 from Vision.VPE import NodeLibrary 155 from Vision.UserLibBuild import userLibBuild 156 157 fileSplit = __file__.split(sep) 158 if fileSplit[-1] == '__init__.pyc' or fileSplit[-1] == '__init__.py': 159 libInstanceName = fileSplit[-2].lower() 160 else: 161 libInstanceName = path.splitext(fileSplit[-1])[0].lower() 162 locals()[libInstanceName] = NodeLibrary(libInstanceName, '#FF7700', mode='readWrite') 163 userLibBuild(eval(libInstanceName), __file__) 164 165 """ % defaultLibInit 166 map( lambda x, f=f: f.write(x), txt ) 167 f.close() 168 os.chmod(defaultLibInit, stat.S_IREAD) #make it read only 169 except: 170 txt = "Cannot write the init file %s" %defaultLibInit 171 warnings.warn(txt)
172 173
174 -def ensureVisionResourceFile():
175 ################################################################## 176 # verify or generate _visionrc file 177 ################################################################## 178 from Vision.nodeLibraries import libraries 179 from mglutil.util.packageFilePath import getResourceFolderWithVersion 180 181 visionrcDir = getResourceFolderWithVersion()+os.sep+'Vision' 182 if os.path.isdir(visionrcDir) is False: 183 try: 184 os.mkdir(visionrcDir) 185 except: 186 txt = "can not create folder for _visionrc" 187 warnings.warn(txt) 188 return 189 190 visionrcFile = visionrcDir + os.sep + '_visionrc' 191 if os.path.isfile(visionrcFile) is False: 192 try: 193 f = open(visionrcFile, "w") 194 txt = """######################################################################## 195 # 196 # Date: Jan 2006 Authors: Guillaume Vareille, Michel Sanner 197 # 198 # vareille@scripps.edu 199 # sanner@scripps.edu 200 # 201 # The Scripps Research Institute (TSRI) 202 # Molecular Graphics Lab 203 # La Jolla, CA 92037, USA 204 # 205 # Copyright: Guillaume Vareille, Michel Sanner and TSRI 206 # 207 # Vision Resource File 208 # 209 ######################################################################### 210 # To customize Vision, you can modify the _visionrc file: 211 # ~/.mgltools/[version number]/Vision/_visionrc 212 # Vision will generate this file automatically if it can't find it 213 # Do not modify the original source file 214 ################################################################## 215 216 from os import sep 217 from Vision.UserLibBuild import addDirToSysPath 218 from mglutil.util.packageFilePath import getResourceFolderWithVersion 219 from Vision.nodeLibraries import libraries 220 221 ################################################################## 222 # Modify the font for everything 223 ################################################################## 224 #self.setFont('All', ('helvetica', 9, 'bold')) 225 226 ################################################################## 227 # To toggle ports' icons in the library GUI 228 ################################################################## 229 self.drawPortInLibraryGui = True 230 231 ################################################################## 232 # To set the network default directory 233 ################################################################## 234 import user 235 networkDefaultDirectory = user.home 236 237 ################################################################## 238 # Add paths to additionnal user libraries 239 ################################################################## 240 #this line adds the path to mydefaultlib ~/.mgltools/Vision/UserLibs 241 addDirToSysPath(getResourceFolderWithVersion()+sep+'Vision'+sep+'UserLibs') 242 243 ################################################################## 244 # add these lines to ease runtime loading of frequently used Libraries 245 ################################################################## 246 libraries['vizlib'] = ('DejaVu.VisionInterface.DejaVuNodes', ['DejaVu']) 247 libraries['molkitlib'] = ('MolKit.VisionInterface.MolKitNodes', ['MolKit']) 248 libraries['symlib'] = ('symserv.VisionInterface.SymservNodes', ['symserv']) 249 libraries['vollib'] = ('Volume.VisionInterface.VolumeNodes', ['Volume']) 250 libraries['imagelib'] = ('Vision.PILNodes', ['Image', 'ImageTk']) 251 libraries['matplotlib'] = ('Vision.matplotlibNodes', ['matplotlib']) 252 libraries['mydefaultlib'] = ('MyDefaultLib') 253 254 ################################################################## 255 # Load Libraries of Nodes 256 # argument1: Name of the library module 257 # argument2: list of names of dependent module (optionnal, just for verifications) 258 # Comment/Uncomment these lines to disable/enable loading of the libraries at startup 259 ################################################################## 260 self.loadLibModule('Vision.StandardNodes') 261 #self.loadLibModule('DejaVu.VisionInterface.DejaVuNodes', ['DejaVu']) 262 #self.loadLibModule('Vision.PILNodes', ['Image', 'ImageTk']) 263 #self.loadLibModule('Vision.matplotlibNodes', ['matplotlib']) 264 #self.loadLibModule('MolKit.VisionInterface.MolKitNodes', ['MolKit']) 265 #self.loadLibModule('FlexTree.VisionInterface.FlexTreeNodes', ['FlexTree']) 266 #self.loadLibModule('symserv.VisionInterface.SymservNodes', ['symserv']) 267 #self.loadLibModule('Volume.VisionInterface.VolumeNodes', ['Volume']) 268 #self.loadLibModule('MyDefaultLib') 269 270 """ 271 map( lambda x, f=f: f.write(x), txt ) 272 f.close() 273 except: 274 txt = "can not create _visionrc" 275 warnings.warn(txt)
276 277 278
279 -def addTypes(libOrTypeManagerInstance, typesModuleName):
280 mod = __import__(typesModuleName, globals(), locals(),[typesModuleName]) 281 for modItemName in dir(mod): 282 modItem = getattr(mod, modItemName) 283 from inspect import isclass 284 if isclass(modItem): 285 from NetworkEditor.datatypes import AnyArrayType, AnyType 286 if issubclass(modItem, AnyType) or issubclass(modItem, AnyArrayType): 287 libOrTypeManagerInstance.addType(modItem())
288