def testConfigurationFileExistence(self): """ Test if the configuration file has been properly written. """ # activate the only loaded plugin self.plugin_activate() # get rid of the plugin manager and create a new one del self.pluginManager del self.config_parser self.config_parser = ConfigParser() self.config_parser.read(self.config_file) self.assertTrue(self.config_parser.has_section("Plugin Management")) self.assertTrue( self.config_parser.has_option("Plugin Management", "default_plugins_to_load")) self.pluginManager = ConfigurablePluginManager( directories_list=[ os.path.join(os.path.dirname(os.path.abspath(__file__)), "plugins") ], plugin_info_ext="yapsy-config-plugin", configparser_instance=self.config_parser, config_change_trigger=self.update_config) self.pluginManager.collectPlugins() self.plugin_loading_check() self.assertTrue(self.plugin_info.plugin_object.is_activated) self.pluginManager.deactivatePluginByName(self.plugin_info.name, self.plugin_info.category) # check that activating the plugin once again, won't cause an error self.pluginManager.activatePluginByName(self.plugin_info.name, self.plugin_info.category) # Will be used later self.plugin_info = None
class ConfigurablePMWithDefaultChangeTriggerTestCase(unittest.TestCase, ConfigTestMixin): """Test the correctness of default values of args specific to the ConfigurablePM in its construtor. """ def setUp(self): """ init """ # create a config file self.config_parser = ConfigParser() self.plugin_info = None # create the plugin manager self.pluginManager = ConfigurablePluginManager( directories_list=[os.path.join( os.path.dirname(os.path.abspath(__file__)),"plugins")], plugin_info_ext="yapsy-config-plugin", configparser_instance=self.config_parser) # load the plugins that may be found self.pluginManager.collectPlugins() def testPluginOptions(self): """ Test is the plugin can register and access options from the ConfigParser. """ self.plugin_activate() plugin = self.plugin_info.plugin_object plugin.choseTestOption("voila") self.assertTrue(plugin.checkTestOption()) self.assertEqual(plugin.getTestOption(),"voila")
def init_plugin_manager(self): # Configuring plugin locator plugin_locator = PluginFileLocator() plugin_locator.setPluginPlaces(PLUGIN_PLACES) # Initializing plugin manager... # categories_filter={"Default": IPlugin, "Custom": ICustomPlugin}, self._plmanager = PluginManager( categories_filter={"Default": IAlgorithmPlugin}, plugin_locator=plugin_locator) # decorate plugin manager with configurable feature self._cpmanager = ConfigurablePluginManager( decorated_manager=self._plmanager) # create parser for config file config_parser = ConfigParser() # set config file location config_parser.read(get_config_file_path()) # set parser to configurable decorator self._cpmanager.setConfigParser( configparser_instance=config_parser, config_change_trigger=config_change_trigger) # plugin_manager.collectPlugins() # configurable_plugin_manager.loadPlugins() self._cpmanager.collectPlugins() self.plugins_info() for plugin_info in self._plmanager.getAllPlugins(): if plugin_info.is_activated: plugin_info.plugin_object.set_image_manager(self._immanager)
class ConfigurablePMWithDefaultChangeTriggerTestCase(unittest.TestCase, ConfigTestMixin): """Test the correctness of default values of args specific to the ConfigurablePM in its construtor. """ def setUp(self): """ init """ # create a config file self.config_parser = ConfigParser() self.plugin_info = None # create the plugin manager self.pluginManager = ConfigurablePluginManager( directories_list=[ os.path.join(os.path.dirname(os.path.abspath(__file__)), "plugins") ], plugin_info_ext="yapsy-config-plugin", configparser_instance=self.config_parser) # load the plugins that may be found self.pluginManager.collectPlugins() def testPluginOptions(self): """ Test is the plugin can register and access options from the ConfigParser. """ self.plugin_activate() plugin = self.plugin_info.plugin_object plugin.choseTestOption("voila") self.assertTrue(plugin.checkTestOption()) self.assertEqual(plugin.getTestOption(), "voila")
def __init__(self, bus: Bus, config: Config, graph: Graph): self._bus = bus self._graph = graph logger.debug("action=init paths=%s", config.plugin_paths()) self._plugin_manager = ConfigurablePluginManager( decorated_manager=VersionedPluginManager()) self._plugin_manager.setPluginPlaces(config.plugin_paths()) self._plugin_manager.setPluginInfoExtension("plugin") self._plugin_manager.setConfigParser(config.parser, config.save)
def testConfigurationFileExistence(self): """ Test if the configuration file has been properly written. """ # activate the only loaded plugin self.plugin_activate() # get rid of the plugin manager and create a new one del self.pluginManager del self.config_parser self.config_parser = ConfigParser() self.config_parser.read(self.config_file) self.assertTrue(self.config_parser.has_section("Plugin Management")) self.assertTrue(self.config_parser.has_option("Plugin Management", "default_plugins_to_load")) self.pluginManager = ConfigurablePluginManager( directories_list=[os.path.join( os.path.dirname(os.path.abspath(__file__)),"plugins")], plugin_info_ext="yapsy-config-plugin", configparser_instance=self.config_parser, config_change_trigger=self.update_config) self.pluginManager.collectPlugins() self.plugin_loading_check() self.assertTrue(self.plugin_info.plugin_object.is_activated) self.pluginManager.deactivatePluginByName(self.plugin_info.name, self.plugin_info.category) # check that activating the plugin once again, won't cause an error self.pluginManager.activatePluginByName(self.plugin_info.name, self.plugin_info.category) # Will be used later self.plugin_info = None
def activatePlugin(self, pluginInfo, save_state=True, checkDependencies=True): from lunchinator.utilities import handleMissingDependencies from lunchinator import get_server if checkDependencies: missing = self.checkActivation([pluginInfo]) result = handleMissingDependencies(missing) if result == INSTALL_FAIL: # maybe there were dependencies installed, better re-check missing = self.checkActivation([pluginInfo]) if missing: self._logCannotLoad(pluginInfo, missing) get_notification_center().emitPluginDeactivated(pluginInfo.name, pluginInfo.category) return elif result == INSTALL_CANCEL: # user chose not to install -> deactivate get_notification_center().emitPluginDeactivated(pluginInfo.name, pluginInfo.category) self._deactivateMissing(missing) return elif result in (INSTALL_SUCCESS, INSTALL_IGNORE): missing = {} elif result == INSTALL_RESTART: # store that the plugin is activated now self.__addPluginToConfig(pluginInfo.category, pluginInfo.name) return getCoreLogger().info("Activating plugin '%s' of type '%s'", pluginInfo.name, pluginInfo.category) try: pluginInfo.plugin_object.setPluginName(pluginInfo.name) result = ConfigurablePluginManager.activatePluginByName(self, pluginInfo.name, category_name=pluginInfo.category, save_state=save_state) if self._emitSignals and result != None: get_notification_center().emitPluginActivated(pluginInfo.name, pluginInfo.category) except: getCoreLogger().exception("Error activating plugin '%s' of type '%s'", pluginInfo.name, pluginInfo.category)
def setUp(self): """ init """ # create a config file self.config_file = self.CONFIG_FILE self.config_parser = ConfigParser.SafeConfigParser() self.plugin_info = None # create the plugin manager self.pluginManager = ConfigurablePluginManager( directories_list=[os.path.join( os.path.dirname(os.path.abspath(__file__)),"plugins")], plugin_info_ext="yapsy-config-plugin", configparser_instance=self.config_parser, config_change_trigger=self.update_config) # load the plugins that may be found self.pluginManager.collectPlugins()
def testConfigurationFileExistence(self): """ Test if the configuration file has been properly written. """ # activate the only loaded plugin self.plugin_activate() # get rid of the plugin manager and create a new one del self.pluginManager del self.config_parser self.config_parser = ConfigParser.SafeConfigParser() self.config_parser.read(self.config_file) self.assert_(self.config_parser.has_section("Plugin Management")) self.assert_(self.config_parser.has_option("Plugin Management", "default_plugins_to_load")) self.pluginManager = ConfigurablePluginManager( directories_list=[os.path.join( os.path.dirname(os.path.abspath(__file__)),"plugins")], plugin_info_ext="yapsy-config-plugin", configparser_instance=self.config_parser, config_change_trigger=self.update_config) # Will be used later self.plugin_info = None
def setUp(self): """ init """ # create a config file self.config_parser = ConfigParser() self.plugin_info = None # create the plugin manager self.pluginManager = ConfigurablePluginManager( directories_list=[os.path.join( os.path.dirname(os.path.abspath(__file__)),"plugins")], plugin_info_ext="yapsy-config-plugin", configparser_instance=self.config_parser) # load the plugins that may be found self.pluginManager.collectPlugins()
def deactivatePlugins(self, pluginInfos, save_state=True): # first, unload regular plugins (no database, no force activation) # second, also unload force activated non-db plugins # last, unload db plugins unloadFirst = [] unloadSecond = [] unloadThird = [] for pluginInfo in pluginInfos: if pluginInfo is None or not pluginInfo.plugin_object.is_activated: continue if pluginInfo.category == "db": unloadThird.append(pluginInfo) elif pluginInfo.plugin_object.force_activation: unloadSecond.append(pluginInfo) else: unloadFirst.append(pluginInfo) for piList in (unloadFirst, unloadSecond, unloadThird): # first, inform about deactivation for pluginInfo in piList: getCoreLogger().info("Preparing to deactivate plugin '%s' of type '%s'", pluginInfo.name, pluginInfo.category) try: # this is a direct connection, exception will be propagated here get_notification_center().emitPluginWillBeDeactivated(pluginInfo.name, pluginInfo.category) except: getCoreLogger().exception("An error occured while deactivating %s", pluginInfo.name) # then deactivate plugins for pluginInfo in piList: getCoreLogger().info("Deactivating plugin '%s' of type '%s'", pluginInfo.name, pluginInfo.category) try: result = ConfigurablePluginManager.deactivatePluginByName(self, pluginInfo.name, category_name=pluginInfo.category, save_state=save_state) if self._emitSignals and result != None: get_notification_center().emitPluginDeactivated(pluginInfo.name, pluginInfo.category) except: getCoreLogger().exception("An error occured while deactivating plugin '%s' of type '%s'", pluginInfo.name, pluginInfo.category)
class ConfigTestCase(unittest.TestCase): """ Test the correct loading of a plugin that uses a configuration file through a ConfigurablePluginManager as well as basic commands. """ CONFIG_FILE = test_settings.TEMP_CONFIG_FILE_NAME def setUp(self): """ init """ # create a config file self.config_file = self.CONFIG_FILE self.config_parser = ConfigParser() self.plugin_info = None # create the plugin manager self.pluginManager = ConfigurablePluginManager( directories_list=[os.path.join( os.path.dirname(os.path.abspath(__file__)),"plugins")], plugin_info_ext="yapsy-config-plugin", configparser_instance=self.config_parser, config_change_trigger=self.update_config) # load the plugins that may be found self.pluginManager.collectPlugins() def tearDown(self): """ When the test has been performed erase the temp file. """ if os.path.isfile(self.config_file): os.remove(self.config_file) def testConfigurationFileExistence(self): """ Test if the configuration file has been properly written. """ # activate the only loaded plugin self.plugin_activate() # get rid of the plugin manager and create a new one del self.pluginManager del self.config_parser self.config_parser = ConfigParser() self.config_parser.read(self.config_file) self.assertTrue(self.config_parser.has_section("Plugin Management")) self.assertTrue(self.config_parser.has_option("Plugin Management", "default_plugins_to_load")) self.pluginManager = ConfigurablePluginManager( directories_list=[os.path.join( os.path.dirname(os.path.abspath(__file__)),"plugins")], plugin_info_ext="yapsy-config-plugin", configparser_instance=self.config_parser, config_change_trigger=self.update_config) self.pluginManager.collectPlugins() self.plugin_loading_check() self.assertTrue(self.plugin_info.plugin_object.is_activated) self.pluginManager.deactivatePluginByName(self.plugin_info.name, self.plugin_info.category) # check that activating the plugin once again, won't cause an error self.pluginManager.activatePluginByName(self.plugin_info.name, self.plugin_info.category) # Will be used later self.plugin_info = None def testLoaded(self): """ Test if the correct plugin has been loaded. """ self.plugin_loading_check() def testActivationAndDeactivation(self): """ Test if the activation/deactivaion procedures work. """ self.plugin_activate() self.pluginManager.deactivatePluginByName(self.plugin_info.name, self.plugin_info.category) self.assertTrue(not self.plugin_info.plugin_object.is_activated) def testPluginOptions(self): """ Test is the plugin can register and access options from the ConfigParser. """ self.plugin_activate() plugin = self.plugin_info.plugin_object plugin.choseTestOption("voila") self.assertTrue(plugin.checkTestOption()) self.assertEqual(plugin.getTestOption(),"voila") #--- UTILITIES def plugin_loading_check(self): """ Test if the correct plugin has been loaded. """ if self.plugin_info is None: # check nb of categories self.assertEqual(len(self.pluginManager.getCategories()),1) sole_category = self.pluginManager.getCategories()[0] # check the number of plugins self.assertEqual(len(self.pluginManager.getPluginsOfCategory(sole_category)),1) self.plugin_info = self.pluginManager.getPluginsOfCategory(sole_category)[0] # test that the name of the plugin has been correctly defined self.assertEqual(self.plugin_info.name,"Config Plugin") self.assertEqual(sole_category,self.plugin_info.category) else: self.assertTrue(True) def plugin_activate(self): """ Activate the plugin with basic checking """ self.plugin_loading_check() self.assertTrue(not self.plugin_info.plugin_object.is_activated) self.pluginManager.activatePluginByName(self.plugin_info.name, self.plugin_info.category) self.assertTrue(self.plugin_info.plugin_object.is_activated) def update_config(self): """ Write the content of the ConfigParser in a file. """ cf = open(self.config_file,"a") self.config_parser.write(cf) cf.close()
class ConfigTestCase(unittest.TestCase): """ Test the correct loading of a plugin that uses a configuration file through a ConfigurablePluginManager as well as basic commands. """ CONFIG_FILE = test_settings.TEMP_CONFIG_FILE_NAME def setUp(self): """ init """ # create a config file self.config_file = self.CONFIG_FILE self.config_parser = ConfigParser.SafeConfigParser() self.plugin_info = None # create the plugin manager self.pluginManager = ConfigurablePluginManager( directories_list=[os.path.join( os.path.dirname(os.path.abspath(__file__)),"plugins")], plugin_info_ext="yapsy-config-plugin", configparser_instance=self.config_parser, config_change_trigger=self.update_config) # load the plugins that may be found self.pluginManager.collectPlugins() def tearDown(self): """ When the test has been performed erase the temp file. """ if os.path.isfile(self.config_file): os.remove(self.config_file) def testConfigurationFileExistence(self): """ Test if the configuration file has been properly written. """ # activate the only loaded plugin self.plugin_activate() # get rid of the plugin manager and create a new one del self.pluginManager del self.config_parser self.config_parser = ConfigParser.SafeConfigParser() self.config_parser.read(self.config_file) self.assert_(self.config_parser.has_section("Plugin Management")) self.assert_(self.config_parser.has_option("Plugin Management", "default_plugins_to_load")) self.pluginManager = ConfigurablePluginManager( directories_list=[os.path.join( os.path.dirname(os.path.abspath(__file__)),"plugins")], plugin_info_ext="yapsy-config-plugin", configparser_instance=self.config_parser, config_change_trigger=self.update_config) # Will be used later self.plugin_info = None def testLoaded(self): """ Test if the correct plugin has been loaded. """ self.plugin_loading_check() def testActivationAndDeactivation(self): """ Test if the activation/deactivaion procedures work. """ self.plugin_activate() self.pluginManager.deactivatePluginByName(self.plugin_info.name, self.plugin_info.category) self.assert_(not self.plugin_info.plugin_object.is_activated) def testPluginOptions(self): """ Test is the plugin can register and access options from the ConfigParser. """ self.plugin_activate() plugin = self.plugin_info.plugin_object plugin.choseTestOption("voila") self.assert_(plugin.checkTestOption()) self.assertEqual(plugin.getTestOption(),"voila") #--- UTILITIES def plugin_loading_check(self): """ Test if the correct plugin has been loaded. """ if self.plugin_info is None: # check nb of categories self.assertEqual(len(self.pluginManager.getCategories()),1) sole_category = self.pluginManager.getCategories()[0] # check the number of plugins self.assertEqual(len(self.pluginManager.getPluginsOfCategory(sole_category)),1) self.plugin_info = self.pluginManager.getPluginsOfCategory(sole_category)[0] # test that the name of the plugin has been correctly defined self.assertEqual(self.plugin_info.name,"Config Plugin") self.assertEqual(sole_category,self.plugin_info.category) else: self.assert_(True) def plugin_activate(self): """ Activate the plugin with basic checking """ self.plugin_loading_check() self.assert_(not self.plugin_info.plugin_object.is_activated) self.pluginManager.activatePluginByName(self.plugin_info.name, self.plugin_info.category) self.assert_(self.plugin_info.plugin_object.is_activated) def update_config(self): """ Write the content of the ConfigParser in a file. """ cf = open(self.config_file,"a") self.config_parser.write(cf) cf.close()
class PluginEngine: @inject(bus="tomate.bus", config="tomate.config", graph=Graph) def __init__(self, bus: Bus, config: Config, graph: Graph): self._bus = bus self._graph = graph logger.debug("action=init paths=%s", config.plugin_paths()) self._plugin_manager = ConfigurablePluginManager( decorated_manager=VersionedPluginManager()) self._plugin_manager.setPluginPlaces(config.plugin_paths()) self._plugin_manager.setPluginInfoExtension("plugin") self._plugin_manager.setConfigParser(config.parser, config.save) def collect(self) -> None: logger.debug("action=collect") self._plugin_manager.locatePlugins() self._plugin_manager.loadPlugins(callback_after=self._configure_plugin) def _configure_plugin(self, plugin: PluginInfo) -> None: if plugin.error is None: plugin.plugin_object.configure(self._bus, self._graph) def deactivate(self, name: str) -> None: self._plugin_manager.deactivatePluginByName(name) def activate(self, name: str) -> None: self._plugin_manager.activatePluginByName(name) def all(self) -> List[PluginInfo]: logger.debug("action=all") return sorted(self._plugin_manager.getAllPlugins(), key=lambda info: info.name) def lookup(self, name: str, category="Default") -> Optional[PluginInfo]: logger.debug("action=lookup name=%s category=%s", name, category) return self._plugin_manager.getPluginByName(name, category) def has_plugins(self) -> bool: has = len(self.all()) > 0 logger.debug("action=has_plugin has=%s", has) return has def remove(self, plugin: object, category="Default") -> None: self._plugin_manager.removePluginFromCategory(plugin, category)
class AlgorithmsManager(QObject): def __init__(self, imanager): QObject.__init__(self) self._plmanager = None self._cpmanager = None self._immanager = imanager self._algorithms = [] self._algolist = dict() self._menu = QMenu(_("Algorithms")) self.init_plugin_manager() self.get_actions() self.get_widgets() self._databases = dict() pass def databases_list(self): return self._databases.keys() def database_settings(self, name, settings_type): return self._databases.get(name) def algorithms_menu(self): return self._menu def algorithms_settings(self): return self._algorithms def algorithms_list(self): return self._algolist.keys() def get_actions(self): for plugin_info in self._plmanager.getAllPlugins(): if plugin_info.is_activated: actions = plugin_info.plugin_object.get_algorithms_actions( self._menu) for action in actions: if action and isinstance(action, QAction): self._menu.addAction(action) if action and isinstance(action, QMenu): self._menu.addMenu(action) algo_list = plugin_info.plugin_object.get_algorithms_list() for algo in algo_list: self._algolist[algo] = plugin_info.plugin_object def get_widgets(self): for plugin_info in self._plmanager.getAllPlugins(): if plugin_info.is_activated: widgets = plugin_info.plugin_object.get_interfaces() if widgets: for widget in widgets: if widget: self._algorithms.append(widget) def algosettings(self, name): plugin = self._algolist.get(name) if plugin is not None: return plugin.settings(name) return None def apply_algorithm(self, name, settings=dict()): plugin = self._algolist.get(name) if plugin is not None: return plugin.apply(name, settings) return None def init_plugin_manager(self): # Configuring plugin locator plugin_locator = PluginFileLocator() plugin_locator.setPluginPlaces(PLUGIN_PLACES) # Initializing plugin manager... # categories_filter={"Default": IPlugin, "Custom": ICustomPlugin}, self._plmanager = PluginManager( categories_filter={"Default": IAlgorithmPlugin}, plugin_locator=plugin_locator) # decorate plugin manager with configurable feature self._cpmanager = ConfigurablePluginManager( decorated_manager=self._plmanager) # create parser for config file config_parser = ConfigParser() # set config file location config_parser.read(get_config_file_path()) # set parser to configurable decorator self._cpmanager.setConfigParser( configparser_instance=config_parser, config_change_trigger=config_change_trigger) # plugin_manager.collectPlugins() # configurable_plugin_manager.loadPlugins() self._cpmanager.collectPlugins() self.plugins_info() for plugin_info in self._plmanager.getAllPlugins(): if plugin_info.is_activated: plugin_info.plugin_object.set_image_manager(self._immanager) def plugins_info(self): # Output of various information and activation of each plugin for plugin_info in self._plmanager.getAllPlugins(): print "Loading plugin '%s' ..." % plugin_info.name # configurable_plugin_manager.activatePluginByName(plugin_info.name) print " name : %s" % plugin_info.name print " path : %s" % plugin_info.path print " version : %s" % plugin_info.version print " author : %s" % plugin_info.author print " copyright : %s" % plugin_info.copyright print " website : %s" % plugin_info.website print " description : %s" % plugin_info.description print " details : %s" % plugin_info.details print " is Activated : %s" % plugin_info.is_activated print " categories : %s" % plugin_info.categories print " plugin object : %s" % plugin_info.plugin_object print " error : %s" % plugin_info.error