def init_plugins(self): self.plugins = load_plugins(self.settings) for plugin in self.plugins: logger.debug('Registering plugin `%s`', plugin.__name__) try: plugin.register() except Exception as e: logger.error('Cannot register plugin `%s`\n%s', plugin.__name__, e)
def init_plugins(self): self.plugins = [] for plugin in load_plugins(self.settings): name = get_plugin_name(plugin) logger.debug('Registering plugin `%s`', name) try: plugin.register() self.plugins.append(plugin) except Exception as e: logger.error('Cannot register plugin `%s`\n%s', name, e) self.settings['PLUGINS'] = [get_plugin_name(p) for p in self.plugins]
def test_load_plugins(self): def get_plugin_names(plugins): return set( plugin.NAME if hasattr(plugin, 'NAME') else plugin.__name__ for plugin in plugins) # existing namespace plugins existing_ns_plugins = load_plugins({}) with tmp_namespace_path(self._NS_PLUGIN_FOLDER): # with no `PLUGINS` setting, load namespace plugins plugins = load_plugins({}) self.assertEqual(len(plugins), len(existing_ns_plugins) + 1, plugins) self.assertEqual({'namespace plugin'} | get_plugin_names(existing_ns_plugins), get_plugin_names(plugins)) # disable namespace plugins with `PLUGINS = []` SETTINGS = {'PLUGINS': []} plugins = load_plugins(SETTINGS) self.assertEqual(len(plugins), 0, plugins) # with `PLUGINS`, load only specified plugins # normal plugin SETTINGS = { 'PLUGINS': ['normal_plugin'], 'PLUGIN_PATHS': [self._NORMAL_PLUGIN_FOLDER] } plugins = load_plugins(SETTINGS) self.assertEqual(len(plugins), 1, plugins) self.assertEqual({'normal plugin'}, get_plugin_names(plugins)) # namespace plugin short SETTINGS = {'PLUGINS': ['ns_plugin']} plugins = load_plugins(SETTINGS) self.assertEqual(len(plugins), 1, plugins) self.assertEqual({'namespace plugin'}, get_plugin_names(plugins)) # namespace plugin long SETTINGS = {'PLUGINS': ['pelican.plugins.ns_plugin']} plugins = load_plugins(SETTINGS) self.assertEqual(len(plugins), 1, plugins) self.assertEqual({'namespace plugin'}, get_plugin_names(plugins)) # normal and namespace plugin SETTINGS = { 'PLUGINS': ['normal_plugin', 'ns_plugin'], 'PLUGIN_PATHS': [self._NORMAL_PLUGIN_FOLDER] } plugins = load_plugins(SETTINGS) self.assertEqual(len(plugins), 2, plugins) self.assertEqual({'normal plugin', 'namespace plugin'}, get_plugin_names(plugins))
def test_load_plugins(self): # no plugins plugins = load_plugins({}) self.assertEqual(len(plugins), 0) with tmp_namespace_path(self._NS_PLUGIN_FOLDER): # with no `PLUGINS` setting, load namespace plugins plugins = load_plugins({}) self.assertEqual(len(plugins), 1, plugins) self.assertEqual({'namespace plugin'}, set(plugin.NAME for plugin in plugins)) # disable namespace plugins with `PLUGINS = []` SETTINGS = {'PLUGINS': []} plugins = load_plugins(SETTINGS) self.assertEqual(len(plugins), 0, plugins) # using `PLUGINS` # normal plugin SETTINGS = { 'PLUGINS': ['normal_plugin'], 'PLUGIN_PATHS': [self._NORMAL_PLUGIN_FOLDER] } plugins = load_plugins(SETTINGS) self.assertEqual(len(plugins), 1, plugins) self.assertEqual({'normal plugin'}, set(plugin.NAME for plugin in plugins)) # namespace plugin short SETTINGS = {'PLUGINS': ['ns_plugin']} plugins = load_plugins(SETTINGS) self.assertEqual(len(plugins), 1, plugins) self.assertEqual({'namespace plugin'}, set(plugin.NAME for plugin in plugins)) # namespace plugin long SETTINGS = {'PLUGINS': ['pelican.plugins.ns_plugin']} plugins = load_plugins(SETTINGS) self.assertEqual(len(plugins), 1, plugins) self.assertEqual({'namespace plugin'}, set(plugin.NAME for plugin in plugins)) # normal and namespace plugin SETTINGS = { 'PLUGINS': ['normal_plugin', 'ns_plugin'], 'PLUGIN_PATHS': [self._NORMAL_PLUGIN_FOLDER] } plugins = load_plugins(SETTINGS) self.assertEqual(len(plugins), 2, plugins) self.assertEqual({'normal plugin', 'namespace plugin'}, set(plugin.NAME for plugin in plugins))
def test_load_plugins(self): def get_plugin_names(plugins): return { plugin.NAME if hasattr(plugin, 'NAME') else plugin.__name__ for plugin in plugins} # existing namespace plugins existing_ns_plugins = load_plugins({}) with tmp_namespace_path(self._NS_PLUGIN_FOLDER): # with no `PLUGINS` setting, load namespace plugins plugins = load_plugins({}) self.assertEqual(len(plugins), len(existing_ns_plugins)+1, plugins) self.assertEqual( {'namespace plugin'} | get_plugin_names(existing_ns_plugins), get_plugin_names(plugins)) # disable namespace plugins with `PLUGINS = []` SETTINGS = { 'PLUGINS': [] } plugins = load_plugins(SETTINGS) self.assertEqual(len(plugins), 0, plugins) # with `PLUGINS`, load only specified plugins # normal plugin SETTINGS = { 'PLUGINS': ['normal_plugin'], 'PLUGIN_PATHS': [self._NORMAL_PLUGIN_FOLDER] } plugins = load_plugins(SETTINGS) self.assertEqual(len(plugins), 1, plugins) self.assertEqual( {'normal plugin'}, get_plugin_names(plugins)) # normal submodule/subpackage plugins SETTINGS = { 'PLUGINS': [ 'normal_submodule_plugin.subplugin', 'normal_submodule_plugin.subpackage.subpackage', ], 'PLUGIN_PATHS': [self._NORMAL_PLUGIN_FOLDER] } plugins = load_plugins(SETTINGS) self.assertEqual(len(plugins), 2, plugins) self.assertEqual( {'normal submodule plugin', 'normal subpackage plugin'}, get_plugin_names(plugins)) # ensure normal plugins are loaded only once SETTINGS = { 'PLUGINS': ['normal_plugin'], 'PLUGIN_PATHS': [self._NORMAL_PLUGIN_FOLDER], } plugins = load_plugins(SETTINGS) for plugin in load_plugins(SETTINGS): # The second load_plugins() should return the same plugin # objects as the first one self.assertIn(plugin, plugins) # namespace plugin short SETTINGS = { 'PLUGINS': ['ns_plugin'] } plugins = load_plugins(SETTINGS) self.assertEqual(len(plugins), 1, plugins) self.assertEqual( {'namespace plugin'}, get_plugin_names(plugins)) # namespace plugin long SETTINGS = { 'PLUGINS': ['pelican.plugins.ns_plugin'] } plugins = load_plugins(SETTINGS) self.assertEqual(len(plugins), 1, plugins) self.assertEqual( {'namespace plugin'}, get_plugin_names(plugins)) # normal and namespace plugin SETTINGS = { 'PLUGINS': ['normal_plugin', 'ns_plugin'], 'PLUGIN_PATHS': [self._NORMAL_PLUGIN_FOLDER] } plugins = load_plugins(SETTINGS) self.assertEqual(len(plugins), 2, plugins) self.assertEqual( {'normal plugin', 'namespace plugin'}, get_plugin_names(plugins))