def register_plugins(directory_name): """ take a directory name, scan it for python modules and register them (module,registration criteria, priority) """ pluginList = list() # tuple of module,registration dict,priority if os.path.exists(directory_name): modules = pynsive.list_modules(directory_name) for mname in modules: module = pynsive.import_module(mname) if not module: raise ImportError("Unable to load module {}".format(mname)) else: if "message" in dir(module): mclass = module.message() mreg = mclass.registration if "priority" in dir(mclass): mpriority = mclass.priority else: mpriority = 100 if isinstance(mreg, list): logger.info( "[*] plugin {0} registered to receive messages with {1}".format( mname, mreg ) ) pluginList.append((mclass, mreg, mpriority)) return pluginList
def registerPlugins(): pluginList = list() # tuple of module,registration dict,priority if os.path.exists('plugins'): modules = pynsive.list_modules('plugins') for mname in modules: module = pynsive.import_module(mname) importlib.reload(module) if not module: raise ImportError('Unable to load module {}'.format(mname)) else: if 'message' in dir(module): mclass = module.message() mreg = mclass.registration if type(mreg) != list: raise ImportError( 'Plugin {0} registration needs to be a list'. format(mname)) if 'priority' in dir(mclass): mpriority = mclass.priority else: mpriority = 100 if isinstance(mreg, list): logger.info( '[*] plugin {0} registered to receive messages with {1}' .format(mname, mreg)) pluginList.append((mclass, mreg, mpriority)) return pluginList
def identify_plugins(self, enabled_plugins): if not os.path.exists(self.plugin_location): return [] module_name = os.path.basename(self.plugin_location) root_plugin_directory = self.plugin_location plugin_manager = pynsive.PluginManager() plugin_manager.plug_into(root_plugin_directory) plugins = [] found_modules = pynsive.list_modules(module_name) for found_module in found_modules: module_filename, module_name = found_module.split('.') if enabled_plugins is not None and module_name not in enabled_plugins: # Skip this plugin since it's not listed as enabled plugins # as long as we have specified some enabled plugins though # this allows us to specify no specific plugins and get all of them continue module_obj = pynsive.import_module(found_module) reload(module_obj) plugin_class_obj = module_obj.Command() logger.info('Plugin {0} registered to receive command with {1}'.format(module_name, plugin_class_obj.command_name)) plugins.append( { 'plugin_class': plugin_class_obj, 'command_name': plugin_class_obj.command_name, 'help_text': plugin_class_obj.help_text } ) return plugins
def load_check_files(checks_location): checks_location_module_str = checks_location.replace('/', '.') found_check_modules = pynsive.list_modules(checks_location_module_str) found_checks = [] for found_module in found_check_modules: module_obj = pynsive.import_module(found_module) for name, arg in inspect.getmembers(module_obj): if name == 'BasicCheck' or name == 'HTTPPostCheck': continue elif not name.endswith('Check'): continue found_checks.append(arg) return found_checks
def load_checks(self): logger.debug("Loading checks source from " + str(self.checks_location)) checks_location_module_str = self.checks_location.replace('/', '.') found_check_modules = pynsive.list_modules(checks_location_module_str) for found_module in found_check_modules: module_obj = pynsive.import_module(found_module) for name, arg in inspect.getmembers(module_obj): if name == 'BasicCheck': continue elif not name.endswith('Check'): continue logger.debug(" Found " + arg.__name__) self.add_check(arg)
def registerPlugins(): '''walk the plugins directory and register modules in pluginList as a tuple: (mfile, mname, mdescription, mreg, mpriority, mclass) ''' plugin_location = os.path.join(os.path.dirname(__file__), "plugins") module_name = os.path.basename(plugin_location) root_plugin_directory = os.path.join(plugin_location, '..') plugin_manager = pynsive.PluginManager() plugin_manager.plug_into(root_plugin_directory) if os.path.exists(plugin_location): modules = pynsive.list_modules(module_name) for mfile in modules: module = pynsive.import_module(mfile) importlib.reload(module) if not module: raise ImportError('Unable to load module {}'.format(mfile)) else: if 'message' in dir(module): mclass = module.message() mreg = mclass.registration mclass.restoptions = options.__dict__ if 'priority' in dir(mclass): mpriority = mclass.priority else: mpriority = 100 if 'name' in dir(mclass): mname = mclass.name else: mname = mfile if 'description' in dir(mclass): mdescription = mclass.description else: mdescription = mfile if isinstance(mreg, list): logger.info( '[*] plugin {0} registered to receive messages from /{1}' .format(mfile, mreg)) pluginList.append((mfile, mname, mdescription, mreg, mpriority, mclass))
def identify_plugins(self, enabled_plugins): if not os.path.exists(self.plugin_location): return [] module_name = os.path.basename(self.plugin_location) root_plugin_directory = os.path.join(self.plugin_location, '..') plugin_manager = pynsive.PluginManager() plugin_manager.plug_into(root_plugin_directory) plugins = [] found_modules = pynsive.list_modules(module_name) for found_module in found_modules: module_filename, module_name = found_module.split('.') if enabled_plugins is not None and module_name not in enabled_plugins: # Skip this plugin since it's not listed as enabled plugins # as long as we have specified some enabled plugins though # this allows us to specify no specific plugins and get all of them continue try: module_obj = pynsive.import_module(found_module) reload(module_obj) plugin_class_obj = module_obj.message() if 'priority' in dir(plugin_class_obj): priority = plugin_class_obj.priority else: priority = 100 logger.info( '[*] plugin {0} registered to receive messages with {1}'. format(module_name, plugin_class_obj.registration)) plugins.append({ 'plugin_class': plugin_class_obj, 'registration': plugin_class_obj.registration, 'priority': priority }) except Exception as e: logger.exception( 'Received exception when loading {0} plugins\n{1}'.format( module_name, e.message)) plugin_manager.destroy() return plugins
def registerPlugins(): '''walk the plugins directory and register modules in pluginList as a tuple: (mfile, mname, mdescription, mreg, mpriority, mclass) ''' plugin_location = os.path.join(os.path.dirname(__file__), "plugins") module_name = os.path.basename(plugin_location) root_plugin_directory = os.path.join(plugin_location, '..') plugin_manager = pynsive.PluginManager() plugin_manager.plug_into(root_plugin_directory) if os.path.exists(plugin_location): modules = pynsive.list_modules(module_name) for mfile in modules: module = pynsive.import_module(mfile) reload(module) if not module: raise ImportError('Unable to load module {}'.format(mfile)) else: if 'message' in dir(module): mclass = module.message() mreg = mclass.registration mclass.restoptions = options.__dict__ if 'priority' in dir(mclass): mpriority = mclass.priority else: mpriority = 100 if 'name' in dir(mclass): mname = mclass.name else: mname = mfile if 'description' in dir(mclass): mdescription = mclass.description else: mdescription = mfile if isinstance(mreg, list): logger.info('[*] plugin {0} registered to receive messages from /{1}'.format(mfile, mreg)) pluginList.append((mfile, mname, mdescription, mreg, mpriority, mclass))
def identify_plugins(self, enabled_plugins): if not os.path.exists(self.plugin_location): return [] module_name = os.path.basename(self.plugin_location) root_plugin_directory = os.path.join(self.plugin_location, '..') plugin_manager = pynsive.PluginManager() plugin_manager.plug_into(root_plugin_directory) plugins = [] found_modules = pynsive.list_modules(module_name) for found_module in found_modules: module_filename, module_name = found_module.split('.') if enabled_plugins is not None and module_name not in enabled_plugins: # Skip this plugin since it's not listed as enabled plugins # as long as we have specified some enabled plugins though # this allows us to specify no specific plugins and get all of them continue try: module_obj = pynsive.import_module(found_module) reload(module_obj) plugin_class_obj = module_obj.message() if 'priority' in dir(plugin_class_obj): priority = plugin_class_obj.priority else: priority = 100 logger.info('[*] plugin {0} registered to receive messages with {1}'.format(module_name, plugin_class_obj.registration)) plugins.append( { 'plugin_class': plugin_class_obj, 'registration': plugin_class_obj.registration, 'priority': priority } ) except Exception as e: logger.exception('Received exception when loading {0} plugins\n{1}'.format(module_name, e.message)) plugin_manager.destroy() return plugins
def registerPlugins(): pluginList = list() # tuple of module,registration dict,priority if os.path.exists('plugins'): modules = pynsive.list_modules('plugins') for mname in modules: module = pynsive.import_module(mname) reload(module) if not module: raise ImportError('Unable to load module {}'.format(mname)) else: if 'message' in dir(module): mclass = module.message() mreg = mclass.registration if 'priority' in dir(mclass): mpriority = mclass.priority else: mpriority = 100 if isinstance(mreg, list): logger.info('[*] plugin {0} registered to receive messages with {1}'.format(mname, mreg)) pluginList.append((mclass, mreg, mpriority)) return pluginList
def registerPlugins(): '''walk the ./plugins directory and register modules in pluginList as a tuple: (mfile, mname, mdescription, mreg, mpriority, mclass) ''' plugin_manager = pynsive.PluginManager() if os.path.exists('plugins'): modules = pynsive.list_modules('plugins') for mfile in modules: module = pynsive.import_module(mfile) reload(module) if not module: raise ImportError('Unable to load module {}'.format(mfile)) else: if 'message' in dir(module): mclass = module.message() mreg = mclass.registration mclass.restoptions = options if 'priority' in dir(mclass): mpriority = mclass.priority else: mpriority = 100 if 'name' in dir(mclass): mname = mclass.name else: mname = mfile if 'description' in dir(mclass): mdescription = mclass.description else: mdescription = mfile if isinstance(mreg, list): logger.info( '[*] plugin {0} registered to receive messages from /{1}' .format(mfile, mreg)) pluginList.append((mfile, mname, mdescription, mreg, mpriority, mclass))
def registerPlugins(): pluginList = list() # tuple of module,registration dict,priority plugin_manager = pynsive.PluginManager() if os.path.exists("plugins"): modules = pynsive.list_modules("plugins") for mname in modules: module = pynsive.import_module(mname) reload(module) if not module: raise ImportError("Unable to load module {}".format(mname)) else: if "message" in dir(module): mclass = module.message() mreg = mclass.registration if "priority" in dir(mclass): mpriority = mclass.priority else: mpriority = 100 if isinstance(mreg, list): print("[*] plugin {0} registered to receive messages with {1}".format(mname, mreg)) pluginList.append((mclass, mreg, mpriority)) return pluginList
def registerPlugins(): '''walk the ./plugins directory and register modules in pluginList as a tuple: (mfile, mname, mdescription, mreg, mpriority, mclass) ''' plugin_manager = pynsive.PluginManager() if os.path.exists('plugins'): modules = pynsive.list_modules('plugins') for mfile in modules: module = pynsive.import_module(mfile) reload(module) if not module: raise ImportError('Unable to load module {}'.format(mfile)) else: if 'message' in dir(module): mclass = module.message() mreg = mclass.registration mclass.restoptions = options if 'priority' in dir(mclass): mpriority = mclass.priority else: mpriority = 100 if 'name' in dir(mclass): mname = mclass.name else: mname = mfile if 'description' in dir(mclass): mdescription = mclass.description else: mdescription = mfile if isinstance(mreg, list): print('[*] plugin {0} registered to receive messages from /{1}'.format(mfile, mreg)) pluginList.append((mfile, mname, mdescription, mreg, mpriority, mclass))
def test_crawling_modules(self): found_modules = pynsive.list_modules('pynsive_test') self.assertEqual(1, len(found_modules)) self.assertTrue('pynsive_test.test_classes' in found_modules)
def test_crawling_plugged_in_modules(self): found_modules = pynsive.list_modules('pynsive_test') self.assertTrue('pynsive_test.test_classes' in found_modules)
def test_importing_missing_module(self): with self.assertRaises(ImportError): pynsive.list_modules('this.is.totally.fake.and.a.bad.name')