def load(self): if self.modules_path not in sys.path: sys.path.append(self.modules_path) modules_files = [fname for fname in listdir(self.modules_path) if isdir(join(self.modules_path, fname))] del self.imported_modules[:] for mod_name in modules_files: mod_file = abspath(join(self.modules_path, mod_name, "module.py")) try: mod = importlib.import_module(".module", mod_name) except Exception as err: logger.warning("Cannot load %s as a package (%s), trying as module..", mod_name, err) continue try: is_our_type = self.modules_type in mod.properties["daemons"] except Exception as err: logger.warning( "Bad module file for %s : cannot check its properties['daemons'] attribute : %s", mod_file, err ) else: # We want to keep only the modules of our type if is_our_type: self.imported_modules.append(mod) # Now we want to find in theses modules the ones we are looking for del self.modules_assoc[:] for mod_conf in self.modules: module_type = uniform_module_type(mod_conf.module_type) for module in self.imported_modules: if uniform_module_type(module.properties["type"]) == module_type: self.modules_assoc.append((mod_conf, module)) break else: # No module is suitable, we emit a Warning logger.warning("The module type %s for %s was not found in modules!", module_type, mod_conf.get_name())
def get_module(self, mod_name): if self.modules_dir and self.modules_dir not in sys.path: sys.path.append(self.modules_dir) try: return importlib.import_module('.module', mod_name) except Exception as err: logger.warning('Cannot import %s as a package (%s) ; trying as bare module..', mod_name, err) raise
def try_best_load(cls, name, package=None): try: mod = importlib.import_module(name, package) except Exception as err: logger.warning("Cannot import %s : %s", '%s.%s' % (package, name) if package else name, err) return # if the module have a 'properties' and a 'get_instance' # then we are happy and we'll use that: try: mod.properties mod.get_instance except AttributeError: return return mod
def load(self): if self.modules_path not in sys.path: sys.path.append(self.modules_path) modules_files = [ fname for fname in listdir(self.modules_path) if isdir(join(self.modules_path, fname)) ] del self.imported_modules[:] for mod_name in modules_files: mod_file = abspath(join(self.modules_path, mod_name, 'module.py')) try: mod = importlib.import_module('.module', mod_name) except Exception as err: logger.warning( 'Cannot load %s as a package (%s), trying as module..', mod_name, err) continue try: is_our_type = self.modules_type in mod.properties['daemons'] except Exception as err: logger.warning( "Bad module file for %s : cannot check its properties['daemons']" "attribute : %s", mod_file, err) else: # We want to keep only the modules of our type if is_our_type: self.imported_modules.append(mod) # Now we want to find in theses modules the ones we are looking for del self.modules_assoc[:] for mod_conf in self.modules: module_type = uniform_module_type(mod_conf.module_type) for module in self.imported_modules: if uniform_module_type( module.properties['type']) == module_type: self.modules_assoc.append((mod_conf, module)) break else: # No module is suitable, we emit a Warning logger.warning( "The module type %s for %s was not found in modules!", module_type, mod_conf.get_name())
def try_very_bad_load(cls, mod_dir): prev_module = sys.modules.get('module') # cache locally any previously imported 'module' .. logger.warning( "Trying to load %r as an (very-)old-style shinken \"module\" : " "by adding its path to sys.path. This can be (very) bad in case " "of name conflicts within the files part of %s and others " "top-level python modules; I'll try to limit that.", # by removing the mod_dir from sys.path after while. mod_dir, mod_dir ) sys.path.insert(0, mod_dir) try: return importlib.import_module('module') except Exception as err: logger.exception("Could not import bare 'module.py' from %s : %s", mod_dir, err) return finally: sys.path.remove(mod_dir) if prev_module is not None: # and restore it after we have loaded our one (or not) sys.modules['module'] = prev_module
def try_very_bad_load(cls, mod_dir): prev_module = sys.modules.get( 'module') # cache locally any previously imported 'module' .. logger.warning( "Trying to load %r as an (very-)old-style shinken \"module\" : " "by adding its path to sys.path. This can be (very) bad in case " "of name conflicts within the files part of %s and others " "top-level python modules; I'll try to limit that.", # by removing the mod_dir from sys.path after while. mod_dir, mod_dir) sys.path.insert(0, mod_dir) try: return importlib.import_module('module') except Exception as err: logger.exception("Could not import bare 'module.py' from %s : %s", mod_dir, err) return finally: sys.path.remove(mod_dir) if prev_module is not None: # and restore it after we have loaded our one (or not) sys.modules['module'] = prev_module