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 getPluginNameAndModuleFromStream(self, infoFileObject, candidate_infofile=None): """ Extract the name and module of a plugin from the content of the info file that describes it and which is stored in ``infoFileObject``. .. note:: Prefer using ``_extractCorePluginInfo`` instead, whenever possible... .. warning:: ``infoFileObject`` must be a file-like object: either an opened file for instance or a string buffer wrapped in a StringIO instance as another example. .. note:: ``candidate_infofile`` must be provided whenever possible to get better error messages. Return a 3-uple with the name of the plugin, its module and the config_parser used to gather the core data *in a tuple*, if the required info could be localised, else return ``(None,None,None)``. .. note:: This is supposed to be used internally by subclasses and decorators. """ # parse the information buffer to get info about the plugin config_parser = ConfigParser() try: if is_py2: config_parser.readfp(infoFileObject) else: config_parser.read_file(infoFileObject) except Exception as e: log.debug("Could not parse the plugin file '%s' (exception raised was '%s')" % (candidate_infofile,e)) return (None, None, None) # check if the basic info is available if not config_parser.has_section("Core"): log.debug("Plugin info file has no 'Core' section (in '%s')" % candidate_infofile) return (None, None, None) if not config_parser.has_option("Core","Name") or not config_parser.has_option("Core","Module"): log.debug("Plugin info file has no 'Name' or 'Module' section (in '%s')" % candidate_infofile) return (None, None, None) # check that the given name is valid name = config_parser.get("Core", "Name") name = name.strip() if PLUGIN_NAME_FORBIDEN_STRING in name: log.debug("Plugin name contains forbiden character: %s (in '%s')" % (PLUGIN_NAME_FORBIDEN_STRING, candidate_infofile)) return (None, None, None) return (name, config_parser.get("Core", "Module"), config_parser)
def __init__(self, plugin_name, plugin_path): """ Set the basic information (at least name and path) about the plugin as well as the default values for other usefull variables. *plugin_name* is a simple string describing the name of the plugin. *plugin_path* describe the location where the plugin can be found. .. warning:: The ``path`` attribute is the full path to the plugin if it is organised as a directory or the full path to a file without the ``.py`` extension if the plugin is defined by a simple file. In the later case, the actual plugin is reached via ``plugin_info.path+'.py'``. """ self.__details = ConfigParser() self.name = plugin_name self.path = plugin_path self._ensureDetailsDefaultsAreBackwardCompatible() # Storage for stuff created during the plugin lifetime self.plugin_object = None self.categories = [] self.error = None
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 __init__(self, plugin_name, plugin_path): self.__details = ConfigParser() self.name = plugin_name self.path = plugin_path self._ensureDetailsDefaultsAreBackwardCompatible() # Storage for stuff created during the plugin lifetime self.plugin_object = None self.categories = [] self.error = None
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 PluginManagerSingleton.setBehaviour( [ConfigurablePluginManager, VersionedPluginManager]) pluginManager = PluginManagerSingleton.get() pluginManager.setPluginPlaces( directories_list=[os.path.dirname(os.path.abspath(__file__))]) pluginManager.setPluginInfoExtension("yapsy-config-plugin") pluginManager.setConfigParser(self.config_parser, self.update_config) # load the plugins that may be found pluginManager.collectPlugins()
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 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 PluginManagerSingleton.setBehaviour([ConfigurablePluginManager,VersionedPluginManager]) pluginManager = PluginManagerSingleton.get() pluginManager.setPluginPlaces(directories_list=[os.path.dirname(os.path.abspath(__file__))]) pluginManager.setPluginInfoExtension("yapsy-config-plugin") pluginManager.setConfigParser(self.config_parser,self.update_config) # load the plugins that may be found pluginManager.collectPlugins()
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 getInfosDictFromPlugin(self, dirpath, filename): """ Returns the extracted plugin informations as a dictionary. This function ensures that "name" and "path" are provided. """ # use the filename alone to extract minimal informations. infos = {} module_name = os.path.splitext(filename)[0] plugin_filename = os.path.join(dirpath, filename) if module_name == "__init__": module_name = os.path.basename(dirpath) plugin_filename = dirpath infos["name"] = "%s" % module_name infos["path"] = plugin_filename cf_parser = ConfigParser() cf_parser.add_section("Core") cf_parser.set("Core", "Name", infos["name"]) cf_parser.set("Core", "Module", infos["path"]) return infos, cf_parser
def getInfosDictFromPlugin(self, dirpath, filename): """ Returns the extracted plugin informations as a dictionary. This function ensures that "name" and "path" are provided. """ # use the filename alone to extract minimal informations. infos = {} module_name = os.path.splitext(filename)[0] plugin_filename = os.path.join(dirpath,filename) if module_name == "__init__": module_name = os.path.basename(dirpath) plugin_filename = dirpath infos["name"] = "%s" % module_name infos["path"] = plugin_filename cf_parser = ConfigParser() cf_parser.add_section("Core") cf_parser.set("Core","Name",infos["name"]) cf_parser.set("Core","Module",infos["path"]) return infos,cf_parser
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 ConfigSingletonTestsCase(unittest.TestCase): """ Test the correct loading of a simple plugin as well as basic commands, use the Singleton version of the ConfigurablePluginManager. """ 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 PluginManagerSingleton.setBehaviour( [ConfigurablePluginManager, VersionedPluginManager]) pluginManager = PluginManagerSingleton.get() pluginManager.setPluginPlaces( directories_list=[os.path.dirname(os.path.abspath(__file__))]) pluginManager.setPluginInfoExtension("yapsy-config-plugin") pluginManager.setConfigParser(self.config_parser, self.update_config) # load the plugins that may be found 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 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")) 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() PluginManagerSingleton.get().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: pluginManager = PluginManagerSingleton.get() # check nb of categories self.assertEqual(len(pluginManager.getCategories()), 1) sole_category = pluginManager.getCategories()[0] # check the number of plugins self.assertEqual( len(pluginManager.getPluginsOfCategory(sole_category)), 1) self.plugin_info = 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() if not self.plugin_info.plugin_object.is_activated: PluginManagerSingleton.get().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()
def testDetailsAccessors(self): pi = PluginInfo("mouf","/bla/mouf") details = ConfigParser() details.add_section("Core") details.set("Core","Name","hop") details.set("Core","Module","/greuh") details.add_section("Documentation") details.set("Documentation","Author","me") pi.details = details # Beware this is not so obvious: the plugin info still points # (and possibly modifies) the same instance of ConfigParser self.assertEqual(details,pi.details) # also the name and path are kept to their original value when # the details is set in one go. self.assertEqual("mouf",pi.name) self.assertEqual("/bla/mouf",pi.path) # check that some other info do change... self.assertEqual("me",pi.author)
class ConfigSingletonTestsCase(unittest.TestCase): """ Test the correct loading of a simple plugin as well as basic commands, use the Singleton version of the ConfigurablePluginManager. """ 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 PluginManagerSingleton.setBehaviour([ConfigurablePluginManager,VersionedPluginManager]) pluginManager = PluginManagerSingleton.get() pluginManager.setPluginPlaces(directories_list=[os.path.dirname(os.path.abspath(__file__))]) pluginManager.setPluginInfoExtension("yapsy-config-plugin") pluginManager.setConfigParser(self.config_parser,self.update_config) # load the plugins that may be found 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 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")) 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() PluginManagerSingleton.get().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: pluginManager = PluginManagerSingleton.get() # check nb of categories self.assertEqual(len(pluginManager.getCategories()),1) sole_category = pluginManager.getCategories()[0] # check the number of plugins self.assertEqual(len(pluginManager.getPluginsOfCategory(sole_category)),1) self.plugin_info = 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() if not self.plugin_info.plugin_object.is_activated: PluginManagerSingleton.get().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() 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()
def getPluginNameAndModuleFromStream(self, infoFileObject, candidate_infofile=None): """ Extract the name and module of a plugin from the content of the info file that describes it and which is stored in ``infoFileObject``. .. note:: Prefer using ``_extractCorePluginInfo`` instead, whenever possible... .. warning:: ``infoFileObject`` must be a file-like object: either an opened file for instance or a string buffer wrapped in a StringIO instance as another example. .. note:: ``candidate_infofile`` must be provided whenever possible to get better error messages. Return a 3-uple with the name of the plugin, its module and the config_parser used to gather the core data *in a tuple*, if the required info could be localised, else return ``(None,None,None)``. .. note:: This is supposed to be used internally by subclasses and decorators. """ # parse the information buffer to get info about the plugin config_parser = ConfigParser() try: if is_py2: config_parser.readfp(infoFileObject) else: config_parser.read_file(infoFileObject) except Exception as e: log.debug( "Could not parse the plugin file '%s' (exception raised was '%s')" % (candidate_infofile, e)) return (None, None, None) # check if the basic info is available if not config_parser.has_section("Core"): log.debug("Plugin info file has no 'Core' section (in '%s')" % candidate_infofile) return (None, None, None) if not config_parser.has_option( "Core", "Name") or not config_parser.has_option( "Core", "Module"): log.debug( "Plugin info file has no 'Name' or 'Module' section (in '%s')" % candidate_infofile) return (None, None, None) # check that the given name is valid name = config_parser.get("Core", "Name") name = name.strip() if PLUGIN_NAME_FORBIDEN_STRING in name: log.debug("Plugin name contains forbiden character: %s (in '%s')" % (PLUGIN_NAME_FORBIDEN_STRING, candidate_infofile)) return (None, None, None) return (name, config_parser.get("Core", "Module"), config_parser)
def getInfosDictFromPlugin(self, dirpath, filename): return {"name": filename, "path": dirpath}, ConfigParser()