def start_plugins(self): for plugin in self.config.get("plugins") or []: module_name = plugin["module"] item_name = plugin["item"] config = plugin.get("config", {}) self.logger.debug("Starting plugin '%s.%s' with config %s ", module_name, item_name, config) PluginClass = get_item_from_module(module_name, item_name) plugin = PluginClass(self.unwanted_resources, self.problematic_resources, self.dry_run, **config) plugin.run() self.logger.debug("Plugin '%s.%s' finished successfully", module_name, item_name)
def create_logging_handler(handler_config): args = handler_config.get('args') or () kwargs = handler_config.get('kwargs') or {} print("Logs are written to {module}.{klass} with args {args!r} and " "kwargs {kwargs!r})".format( module=handler_config['module'], klass=handler_config['class'], args=args, kwargs=kwargs )) klass = get_item_from_module(handler_config['module'], handler_config['class']) try: handler = klass(*args, **kwargs) except Exception as exc: message = ("Could not instantiate logging handler class '{klass}' " "with args '{args}', kwargs '{kwargs}': {exc}") raise Exception(message.format(klass=klass, args=args, kwargs=kwargs, exc=exc)) return handler
def test_get_item_from_sub_module(self): self.assertEqual(os.path.join, get_item_from_module("os.path", "join"))
def test_get_item_from_module(self): self.assertEqual(os.system, get_item_from_module("os", "system"))