def test_module_load_from_file_fail(self): try: plugin_path = os.path.join(SRC_ROOT, 'plugin/sampleplugin.py') plugin_loader.register_plugin(LoaderTest.DummyPlugin, plugin_path) self.assertTrue(False, 'Import error is expected.') except ImportError: self.assertTrue(True)
def test_syspath_unchanged_load_multiple_plugins(self): plugin_1_path = os.path.join(SRC_ROOT, 'plugin/sampleplugin.py') try: plugin_loader.register_plugin(LoaderTest.DummyPlugin, plugin_1_path) except ImportError: pass old_sys_path = copy.copy(sys.path) plugin_2_path = os.path.join(SRC_ROOT, 'plugin/sampleplugin2.py') try: plugin_loader.register_plugin(LoaderTest.DummyPlugin, plugin_2_path) except ImportError: pass self.assertEqual(old_sys_path, sys.path, 'Should be equal.')
def _get_action_instance(self): actions_cls = action_loader.register_plugin(Action, self._file_path) action_cls = actions_cls[0] if actions_cls and len(actions_cls) > 0 else None if not action_cls: raise Exception('File "%s" has no action or the file doesn\'t exist.' % (self._file_path)) config_parser = ContentPackConfigParser(pack_name=self._pack) config = config_parser.get_action_config(action_file_path=self._file_path) if config: LOG.info('Using config "%s" for action "%s"' % (config.file_path, self._file_path)) action_instance = action_cls(config=config.config) else: LOG.info('No config found for action "%s"' % (self._file_path)) action_instance = action_cls(config={}) # Setup action_instance proeprties action_instance.logger = self._set_up_logger(action_cls.__name__) action_instance.datastore = DatastoreService(logger=action_instance.logger, pack_name=self._pack, class_name=action_cls.__name__, api_username="******") return action_instance
def _get_action_instance(self): actions_cls = action_loader.register_plugin(Action, self._file_path) action_cls = actions_cls[0] if actions_cls and len( actions_cls) > 0 else None if not action_cls: raise Exception( 'File "%s" has no action or the file doesn\'t exist.' % (self._file_path)) self._class_name = action_cls.__class__.__name__ config_loader = ContentPackConfigLoader(pack_name=self._pack, user=self._user) config = config_loader.get_config() if config: LOG.info('Found config for action "%s"' % (self._file_path)) else: LOG.info('No config found for action "%s"' % (self._file_path)) config = None action_service = ActionService(action_wrapper=self) action_instance = get_action_class_instance( action_cls=action_cls, config=config, action_service=action_service) return action_instance
def _get_action_instance(self): actions_cls = action_loader.register_plugin(Action, self._file_path) action_cls = actions_cls[0] if actions_cls and len( actions_cls) > 0 else None if not action_cls: raise Exception( 'File "%s" has no action or the file doesn\'t exist.' % (self._file_path)) config_parser = ContentPackConfigParser(pack_name=self._pack) config = config_parser.get_action_config( action_file_path=self._file_path) if config: LOG.info('Using config "%s" for action "%s"' % (config.file_path, self._file_path)) action_instance = action_cls(config=config.config) else: LOG.info('No config found for action "%s"' % (self._file_path)) action_instance = action_cls(config={}) # Setup action_instance proeprties action_instance.logger = self._set_up_logger(action_cls.__name__) action_instance.datastore = DatastoreService( logger=action_instance.logger, pack_name=self._pack, class_name=action_cls.__name__, api_username="******") return action_instance
def _get_action_instance(self): try: actions_cls = action_loader.register_plugin(Action, self._file_path) except Exception as e: tb_msg = traceback.format_exc() msg = ('Failed to load action class from file "%s" (action file most likely doesn\'t ' 'exist or contains invalid syntax): %s' % (self._file_path, str(e))) msg += '\n\n' + tb_msg exc_cls = type(e) raise exc_cls(msg) action_cls = actions_cls[0] if actions_cls and len(actions_cls) > 0 else None if not action_cls: raise Exception('File "%s" has no action class or the file doesn\'t exist.' % (self._file_path)) self._class_name = action_cls.__class__.__name__ config_loader = ContentPackConfigLoader(pack_name=self._pack, user=self._user) config = config_loader.get_config() if config: LOG.info('Found config for action "%s"' % (self._file_path)) else: LOG.info('No config found for action "%s"' % (self._file_path)) config = None action_service = ActionService(action_wrapper=self) action_instance = get_action_class_instance(action_cls=action_cls, config=config, action_service=action_service) return action_instance
def test_syspath_unchanged_load_multiple_plugins(self): plugin_1_path = os.path.join(SRC_ROOT, 'plugin/sampleplugin.py') try: plugin_loader.register_plugin( LoaderTest.DummyPlugin, plugin_1_path) except ImportError: pass old_sys_path = copy.copy(sys.path) plugin_2_path = os.path.join(SRC_ROOT, 'plugin/sampleplugin2.py') try: plugin_loader.register_plugin( LoaderTest.DummyPlugin, plugin_2_path) except ImportError: pass self.assertEqual(old_sys_path, sys.path, 'Should be equal.')
def _get_action_instance(self): try: actions_cls = action_loader.register_plugin(Action, self._file_path) except Exception as e: tb_msg = traceback.format_exc() msg = ('Failed to load action class from file "%s" (action file most likely doesn\'t ' 'exist or contains invalid syntax): %s' % (self._file_path, str(e))) msg += '\n\n' + tb_msg exc_cls = type(e) raise exc_cls(msg) action_cls = actions_cls[0] if actions_cls and len(actions_cls) > 0 else None if not action_cls: raise Exception('File "%s" has no action class or the file doesn\'t exist.' % (self._file_path)) # Retrieve name of the action class # Note - we need to either use cls.__name_ or inspect.getmro(cls)[0].__name__ to # retrieve a correct name self._class_name = action_cls.__name__ action_service = ActionService(action_wrapper=self) action_instance = get_action_class_instance(action_cls=action_cls, config=self._config, action_service=action_service) return action_instance
def test_module_load_from_file(self): plugin_path = os.path.join(SRC_ROOT, 'plugin/standaloneplugin.py') plugin_classes = plugin_loader.register_plugin( LoaderTest.DummyPlugin, plugin_path) # Even though there are two classes in that file, only one # matches the specs of DummyPlugin class. self.assertEqual(1, len(plugin_classes)) # Validate sys.path now contains the plugin directory. self.assertIn(os.path.abspath(os.path.join(SRC_ROOT, 'plugin')), sys.path) # Validate the individual plugins for plugin_class in plugin_classes: try: plugin_instance = plugin_class() ret_val = plugin_instance.do_work() self.assertIsNotNone(ret_val, 'Should be non-null.') except: pass
def test_module_load_from_file(self): plugin_path = os.path.join(SRC_ROOT, 'plugin/standaloneplugin.py') plugin_classes = plugin_loader.register_plugin( LoaderTest.DummyPlugin, plugin_path) # Even though there are two classes in that file, only one # matches the specs of DummyPlugin class. self.assertEqual(1, len(plugin_classes)) # Validate sys.path now contains the plugin directory. self.assertTrue(os.path.join(SRC_ROOT, 'plugin') in sys.path) # Validate the individual plugins for plugin_class in plugin_classes: try: plugin_instance = plugin_class() ret_val = plugin_instance.do_work() self.assertIsNotNone(ret_val, 'Should be non-null.') except: pass
def _load_action(self): actions_kls = action_loader.register_plugin(Action, self.entry_point) action_kls = actions_kls[0] if actions_kls and len(actions_kls) > 0 else None if not action_kls: raise Exception('%s has no action.' % self.entry_point) config_parser = ContentPackConfigParser(pack_name=self.pack) config = config_parser.get_action_config(action_file_path=self.entry_point) if config: LOG.info('Using config "%s" for action "%s"' % (config.file_path, self.entry_point)) return action_kls(config=config.config) else: LOG.info('No config found for action "%s"' % (self.entry_point)) return action_kls(config={})
def _get_action_instance(self): actions_cls = action_loader.register_plugin(Action, self._file_path) action_cls = actions_cls[0] if actions_cls and len(actions_cls) > 0 else None if not action_cls: raise Exception('File "%s" has no action or the file doesn\'t exist.' % (self._file_path)) config_parser = ContentPackConfigParser(pack_name=self._pack) config = config_parser.get_action_config(action_file_path=self._file_path) if config: LOG.info('Using config "%s" for action "%s"' % (config.file_path, self._file_path)) return action_cls(config=config.config) else: LOG.info('No config found for action "%s"' % (self._file_path)) return action_cls(config={})
def _get_action_instance(self): actions_cls = action_loader.register_plugin(Action, self._file_path) action_cls = actions_cls[0] if actions_cls and len(actions_cls) > 0 else None if not action_cls: raise Exception('File "%s" has no action or the file doesn\'t exist.' % (self._file_path)) config_loader = ContentPackConfigLoader(pack_name=self._pack, user=self._user) config = config_loader.get_config() if config: LOG.info('Found config for action "%s"' % (self._file_path)) else: LOG.info('No config found for action "%s"' % (self._file_path)) config = None action_service = ActionService(action_wrapper=self) action_instance = get_action_class_instance(action_cls=action_cls, config=config, action_service=action_service) return action_instance
def _get_action_instance(self): try: actions_cls = action_loader.register_plugin( Action, self._file_path) except Exception as e: tb_msg = traceback.format_exc() msg = ( 'Failed to load action class from file "%s" (action file most likely doesn\'t ' 'exist or contains invalid syntax): %s' % (self._file_path, str(e))) msg += '\n\n' + tb_msg exc_cls = type(e) raise exc_cls(msg) action_cls = actions_cls[0] if actions_cls and len( actions_cls) > 0 else None if not action_cls: raise Exception( 'File "%s" has no action class or the file doesn\'t exist.' % (self._file_path)) self._class_name = action_cls.__class__.__name__ config_loader = ContentPackConfigLoader(pack_name=self._pack, user=self._user) config = config_loader.get_config() if config: LOG.info('Found config for action "%s"' % (self._file_path)) else: LOG.info('No config found for action "%s"' % (self._file_path)) config = None action_service = ActionService(action_wrapper=self) action_instance = get_action_class_instance( action_cls=action_cls, config=config, action_service=action_service) return action_instance
def _get_action_instance(self): actions_cls = action_loader.register_plugin(Action, self._file_path) action_cls = actions_cls[0] if actions_cls and len( actions_cls) > 0 else None if not action_cls: raise Exception( 'File "%s" has no action or the file doesn\'t exist.' % (self._file_path)) config_parser = ContentPackConfigParser(pack_name=self._pack) config = config_parser.get_action_config( action_file_path=self._file_path) if config: LOG.info('Using config "%s" for action "%s"' % (config.file_path, self._file_path)) return action_cls(config=config.config) else: LOG.info('No config found for action "%s"' % (self._file_path)) return action_cls(config={})
def _load_sensor(self, sensor_file_path): return sensors_loader.register_plugin(Sensor, sensor_file_path)