def __plugins_load(self): from middlewared.service import Service, CRUDService, ConfigService plugins_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'plugins', ) self.logger.debug('Loading plugins from {0}'.format(plugins_dir)) if not os.path.exists(plugins_dir): raise ValueError('plugins dir not found') for f in os.listdir(plugins_dir): if not f.endswith('.py'): continue f = f[:-3] fp, pathname, description = imp.find_module(f, [plugins_dir]) try: mod = imp.load_module(f, fp, pathname, description) finally: if fp: fp.close() for attr in dir(mod): attr = getattr(mod, attr) if not inspect.isclass(attr): continue if attr in (Service, CRUDService, ConfigService): continue if issubclass(attr, Service): self.add_service(attr(self)) if hasattr(mod, 'setup'): mod.setup(self) # Now that all plugins have been loaded we can resolve all method params # to make sure every schema is patched and references match from middlewared.schema import resolver # Lazy import so namespace match to_resolve = [] for service in self.__services.values(): for attr in dir(service): to_resolve.append(getattr(service, attr)) resolved = 0 while len(to_resolve) > 0: for method in list(to_resolve): try: resolver(self, method) except ValueError: pass else: to_resolve.remove(method) resolved += 1 if resolved == 0: raise ValueError("Not all could be resolved") self.logger.debug('All plugins loaded')
async def __plugins_load(self): from middlewared.service import Service, CRUDService, ConfigService, SystemServiceService main_plugins_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'plugins', ) plugins_dirs = [os.path.join(overlay_dir, 'plugins') for overlay_dir in self.overlay_dirs] plugins_dirs.insert(0, main_plugins_dir) self.logger.debug('Loading plugins from {0}'.format(','.join(plugins_dirs))) setup_funcs = [] for plugins_dir in plugins_dirs: if not os.path.exists(plugins_dir): raise ValueError(f'plugins dir not found: {plugins_dir}') for mod in load_modules(plugins_dir): for cls in load_classes(mod, Service, (ConfigService, CRUDService, SystemServiceService)): self.add_service(cls(self)) if hasattr(mod, 'setup'): setup_funcs.append(mod.setup) # Now that all plugins have been loaded we can resolve all method params # to make sure every schema is patched and references match from middlewared.schema import resolver # Lazy import so namespace match to_resolve = [] for service in list(self.__services.values()): for attr in dir(service): to_resolve.append(getattr(service, attr)) while len(to_resolve) > 0: resolved = 0 for method in list(to_resolve): try: resolver(self, method) except ResolverError: pass else: to_resolve.remove(method) resolved += 1 if resolved == 0: raise ValueError(f'Not all schemas could be resolved: {to_resolve}') # Only call setup after all schemas have been resolved because # they can call methods with schemas defined. for f in setup_funcs: call = f(self) # Allow setup to be a coroutine if asyncio.iscoroutinefunction(f): await call self.logger.debug('All plugins loaded')
async def __plugins_load(self): from middlewared.service import Service, CRUDService, ConfigService main_plugins_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'plugins', ) plugins_dirs = list(self.plugins_dirs) plugins_dirs.insert(0, main_plugins_dir) self.logger.debug('Loading plugins from {0}'.format( ','.join(plugins_dirs))) setup_funcs = [] for plugins_dir in plugins_dirs: if not os.path.exists(plugins_dir): raise ValueError(f'plugins dir not found: {plugins_dir}') for f in os.listdir(plugins_dir): if not f.endswith('.py'): continue f = f[:-3] fp, pathname, description = imp.find_module(f, [plugins_dir]) try: mod = imp.load_module(f, fp, pathname, description) finally: if fp: fp.close() for attr in dir(mod): attr = getattr(mod, attr) if not inspect.isclass(attr): continue if attr in (Service, CRUDService, ConfigService): continue if issubclass(attr, Service): self.add_service(attr(self)) if hasattr(mod, 'setup'): setup_funcs.append(mod.setup) # Now that all plugins have been loaded we can resolve all method params # to make sure every schema is patched and references match from middlewared.schema import resolver # Lazy import so namespace match to_resolve = [] for service in list(self.__services.values()): for attr in dir(service): to_resolve.append(getattr(service, attr)) while len(to_resolve) > 0: resolved = 0 for method in list(to_resolve): try: resolver(self, method) except ResolverError: pass else: to_resolve.remove(method) resolved += 1 if resolved == 0: raise ValueError( f'Not all schemas could be resolved: {to_resolve}') # Only call setup after all schemas have been resolved because # they can call methods with schemas defined. for f in setup_funcs: call = f(self) # Allow setup to be a coroutine if asyncio.iscoroutinefunction(f): await call self.logger.debug('All plugins loaded')