Module EDModule
[hide private]
[frames] | no frames]

Source Code for Module EDModule

  1  # coding: utf8 
  2  # 
  3  #    Project: The EDNA Kernel 
  4  #             http://www.edna-site.org 
  5  # 
  6  #    File: "$Id: EDModule.py 2539 2010-11-29 09:37:11Z kieffer $" 
  7  # 
  8  #    Copyright (C) 2008-2011 European Synchrotron Radiation Facility 
  9  #                            Grenoble, France 
 10  # 
 11  #    Principal author:    Jérôme Kieffer (jerome.kieffer@esrf.eu) 
 12  # 
 13  #    This program is free software: you can redistribute it and/or modify 
 14  #    it under the terms of the GNU Lesser General Public License as published 
 15  #    by the Free Software Foundation, either version 3 of the License, or 
 16  #    (at your option) any later version. 
 17  # 
 18  #    This program is distributed in the hope that it will be useful, 
 19  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 20  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 21  #    GNU Lesser General Public License for more details. 
 22  # 
 23  #    You should have received a copy of the GNU General Public License 
 24  #    and the GNU Lesser General Public License  along with this program.   
 25  #    If not, see <http://www.gnu.org/licenses/>. 
 26  # 
 27   
 28  __authors__ = ["Jérôme Kieffer" ] 
 29  __contact__ = "Jerome.Kieffer@esrf.eu" 
 30  __license__ = "LGPLv3+" 
 31  __copyright__ = "European Synchrotron Radiation Facility, Grenoble, France" 
 32   
 33  import sys, os 
 34  from EDLogging import  EDLogging 
 35   
 36   
37 -class EDModule(EDLogging):
38 """ 39 This class is a container for a module, it contains its name, it's path and the dict of modules 40 """ 41
42 - def __init__(self, _strModuleName, _strMethodVersion=None):
43 EDLogging.__init__(self) 44 self.DEBUG("EDModule.__init__ on %s" % _strModuleName) 45 self.__name = _strModuleName 46 self.__module = None 47 self.__path = None 48 self.__dictSubModules = {} 49 self.__version = None 50 self.__strMethodVersion = _strMethodVersion
51 52
53 - def preImport(self, _strPath=None, _strMethodVersion=None):
54 """ 55 Load the module in memory 56 @param _strPath: Path to the module to import 57 @param _strMethodVersion: property or method to get the version number (should return a string) 58 @return: reference to the module loaded 59 """ 60 self.DEBUG("EDModule.preImport on %s from %s" % (self.__name, _strPath)) 61 self.__strMethodVersion = _strMethodVersion 62 if (self.__module is None): 63 if _strPath and os.path.isdir(_strPath): 64 self.__path = _strPath 65 if _strPath in sys.path: 66 sys.path.remove(_strPath) 67 sys.path.insert(1, _strPath) 68 self.synchronizeOn() 69 dictModulesPreImport = sys.modules.copy() 70 try: 71 self.__module = __import__(self.__name) 72 self.DEBUG("preImport of %s from %s" % (self.__name, _strPath)) 73 except Exception: 74 self.__module = None 75 self.ERROR("Import error while preImporting %s from %s" % (self.__name, _strPath)) 76 self.writeErrorTrace() 77 self.ERROR("Revert fragments of imported stuff from %s" % (self.__name)) 78 #Solution from http://lucumr.pocoo.org/2011/9/21/python-import-blackbox/ 79 exc_type, exc_value, tb_root = sys.exc_info() 80 tb = tb_root 81 while tb is not None: 82 if tb.tb_frame.f_globals.get('__name__') == self.__name: 83 raise exc_type, exc_value, tb_root 84 tb = tb.tb_next 85 self.__module = None 86 87 dictModulesPostImport = sys.modules.copy() 88 for module in dictModulesPreImport: 89 if module in dictModulesPostImport: 90 dictModulesPostImport.pop(module) 91 self.__dictSubModules = dictModulesPostImport 92 self.synchronizeOff() 93 94 self.retrieveVersion(self.__strMethodVersion) 95 96 try: 97 self.__path = self.module.__file__ 98 except Exception: 99 pass 100 return self.__module
101
102 - def retrieveVersion(self, _strMethodVersion=None):
103 """ 104 Try to retrieve the version of a module using an attribute or a method ... 105 @param _strMethodVersion: property or method to get the version number (should return a string) 106 @return: Version number 107 """ 108 self.__strMethodVersion = _strMethodVersion 109 if self.__strMethodVersion is None: 110 return self.__version 111 112 objVersion = self.__module 113 for submod in self.__strMethodVersion.split("."): 114 if submod in dir(objVersion): 115 objVersion = getattr(objVersion, submod) 116 else: 117 self.ERROR("EDModule.retrieveVersion: Module %s has no attr/method %s in %s." % (self.__name, self.__strMethodVersion, submod)) 118 objVersion = None 119 break 120 if "__call__" in dir(objVersion): 121 try: 122 self.__version = objVersion() 123 except Exception: 124 pass 125 else: 126 try: 127 self.__version = objVersion 128 except Exception: 129 pass 130 # if not isinstance(self.__version, (str, unicode)): 131 # self.__version = str(self.__version) 132 return self.__version
133 134
135 - def unImport(self):
136 """ 137 Method that unload a module from the system memory. 138 """ 139 self.DEBUG("EDModule.unImport on %s" % self.__name) 140 self.synchronizeOn() 141 if self.__module is not None: 142 for module in sys.modules.copy(): 143 if module in self.__dictSubModules: 144 sys.modules.pop(module) 145 self.__dictSubModules = {} 146 self.__module = None 147 self.synchronizeOff()
148 149
150 - def getName(self):
151 return self.__name
152 name = property(getName) 153
154 - def getVersion(self):
155 return self.__version or ""
156 - def setVersion(self, _version):
157 if self.__version is None: 158 self.__version = _version 159 else: 160 self.WARNING("Redefinition of version number for module %s (%s) is not allowed (%s)." % (self.name, self.__version, _version))
161 version = property(getVersion, setVersion) 162
163 - def getModule(self):
164 return self.__module
165 module = property(getModule) 166
167 - def getPath(self):
168 return self.__path
169 path = property(getPath) 170
171 - def getDictSubModules(self):
172 return self.__dictSubModules
173 dictSubModules = property(getDictSubModules)
174