def reloadControllerLib(self, module_name, path=None, reload=True): """Reloads the given library(=module) names :raises: :exc:`sardana.pool.poolexception.UnknownController` in case the controller is unknown or :exc:`ImportError` if the reload process is not successful :param str module_name: controller library name (=python module name) :param seq<str> path: a list of absolute path to search for libraries [default: None, meaning the current ControllerPath will be used] :return: the ControllerLib object for the reloaded controller lib :rtype: sardana.pool.poolmetacontroller.ControllerLibrary """ path = path or self.getControllerPath() # reverse the path order: # more priority elements last. This way if there are repeated elements # they first ones (lower priority) will be overwritten by the last ones if path: path = copy.copy(path) path.reverse() # if there was previous Controller Lib info remove it if self._modules.has_key(module_name): self._modules.pop(module_name) m, exc_info = None, None try: m = ModuleManager().reloadModule(module_name, path, reload=reload) except: exc_info = sys.exc_info() controller_lib = None params = dict(module=m, name=module_name, pool=self.get_pool()) if m is None or exc_info is not None: params['exc_info'] = exc_info controller_lib = ControllerLibrary(**params) self._modules[module_name] = controller_lib else: controller_lib = ControllerLibrary(**params) lib_contains_controllers = False abs_file = controller_lib.file_path for _, klass in inspect.getmembers(m, inspect.isclass): if issubclass(klass, controller.Controller): # if it is a class defined in some other class forget it to # avoid replicating the same controller in different # controller files # use normcase to treat case insensitivity of paths on # certain platforms e.g. Windows if os.path.normcase(inspect.getabsfile(klass)) !=\ os.path.normcase(abs_file): continue lib_contains_controllers = True self.addController(controller_lib, klass) if lib_contains_controllers: self._modules[module_name] = controller_lib return controller_lib
def reloadTypeModule(self, module_name, path=None): """Loads/reloads the given module name""" #path = path or [ os.path.dirname(__file__) ] m = None try: m = ModuleManager().reloadModule(module_name, path) except: pass if m is None: if module_name in self._modules: self._modules.pop(module_name) return self._modules[module_name] = {} abs_file = inspect.getabsfile(m) ms = self.macro_server for name in dir(m): if name.startswith("_"): continue klass = getattr(m, name) try: if not issubclass(klass, ParamType): continue except: continue if klass in AbstractParamTypes: continue if inspect.getabsfile(klass) != abs_file: continue t = klass(ms, name) self.addType(t)
def set_python_path(self, path): mod_man = ModuleManager() if self._path_id is not None: mod_man.remove_python_path(self._path_id) self._path_id = mod_man.add_python_path(path)
def reloadRecorderLib(self, module_name, path=None): """Reloads the given library(=module) names. :param module_name: recorder library name (=python module name) :param path: a list of absolute path to search for libraries [default: None, means the current RecorderPath will be used]""" path = path or self.getRecorderPath() # reverse the path order: # more priority elements last. This way if there are repeated elements # they first ones (lower priority) will be overwritten by the last ones if path: path = copy.copy(path) path.reverse() # if there was previous Recorder Library info remove it old_recorder_lib = self._modules.pop(module_name, None) if old_recorder_lib is not None: for recorder in old_recorder_lib.get_recorders(): self._recorder_dict.pop(recorder.name) # remove recorders from the map for _, recorders in self._scan_recorder_map.iteritems(): try: recorders.remove(recorder) except: pass mod_manager = ModuleManager() m, exc_info = None, None try: m = mod_manager.reloadModule( module_name, path, reload=reload) except: exc_info = sys.exc_info() params = dict(module=m, name=module_name, macro_server=self.macro_server, exc_info=exc_info) if m is None: file_name = self._findRecorderLibName(module_name) if file_name is None: if exc_info: msg = format_exception_only_str(*exc_info[:2]) else: msg = "Error (re)loading recorder library '%s'" \ % module_name raise LibraryError(msg, exc_info=exc_info) params['file_path'] = file_name recorder_lib = RecorderLibrary(**params) self._modules[module_name] = recorder_lib else: recorder_lib = RecorderLibrary(**params) lib_contains_recorders = False abs_file = recorder_lib.file_path for _, klass in inspect.getmembers(m, inspect.isclass): if issubclass(klass, DataRecorder): # if it is a class defined in some other class forget it to # avoid replicating the same recorder in different # recorder files # use normcase to treat case insensitivity of paths on # certain platforms e.g. Windows if os.path.normcase(inspect.getabsfile(klass)) !=\ os.path.normcase(abs_file): continue lib_contains_recorders = True self.addRecorder(recorder_lib, klass) if lib_contains_recorders: self._modules[module_name] = recorder_lib return recorder_lib
def reloadRecorderLib(self, module_name, path=None): """Reloads the given library(=module) names. :param module_name: recorder library name (=python module name) :param path: a list of absolute path to search for libraries [default: None, means the current RecorderPath will be used]""" path = path or self.getRecorderPath() # reverse the path order: # more priority elements last. This way if there are repeated elements # they first ones (lower priority) will be overwritten by the last ones if path: path = copy.copy(path) path.reverse() # if there was previous Recorder Library info remove it old_recorder_lib = self._modules.pop(module_name, None) if old_recorder_lib is not None: for recorder in old_recorder_lib.get_recorders(): self._recorder_dict.pop(recorder.name) # remove recorders from the map for _, recorders in self._scan_recorder_map.iteritems(): try: recorders.remove(recorder) except: pass mod_manager = ModuleManager() m, exc_info = None, None try: m = mod_manager.reloadModule(module_name, path, reload=reload) except: exc_info = sys.exc_info() params = dict(module=m, name=module_name, macro_server=self.macro_server, exc_info=exc_info) if m is None: file_name = self._findRecorderLibName(module_name) if file_name is None: if exc_info: msg = format_exception_only_str(*exc_info[:2]) else: msg = "Error (re)loading recorder library '%s'" \ % module_name raise LibraryError(msg, exc_info=exc_info) params['file_path'] = file_name recorder_lib = RecorderLibrary(**params) self._modules[module_name] = recorder_lib else: recorder_lib = RecorderLibrary(**params) lib_contains_recorders = False abs_file = recorder_lib.file_path for _, klass in inspect.getmembers(m, inspect.isclass): if issubclass(klass, DataRecorder): # if it is a class defined in some other class forget it to # avoid replicating the same recorder in different # recorder files # use normcase to treat case insensitivity of paths on # certain platforms e.g. Windows if os.path.normcase(inspect.getabsfile(klass)) !=\ os.path.normcase(abs_file): continue lib_contains_recorders = True self.addRecorder(recorder_lib, klass) if lib_contains_recorders: self._modules[module_name] = recorder_lib return recorder_lib