def __init__(self, configuration, bot_name="vex"): # get the settings path and then load the settings from file settings_path = configuration.get('settings_path') settings = configuration.load_settings(settings_path) self.messaging = Messaging(settings) # create the plugin manager self.plugin_manager = pluginmanager.PluginInterface() # add the entry points of interest self.plugin_manager.add_entry_points( ('vexbot.plugins', 'vexbot.adapters')) # create the subprocess manager and add in the plugins self.subprocess_manager = SubprocessManager() self._update_plugins(settings, self.subprocess_manager, self.plugin_manager) subprocesses_to_start = settings.get('startup_adapters', []) subprocesses_to_start.extend(settings.get('startup_plugins', [])) self.subprocess_manager.start(subprocesses_to_start) self.name = bot_name self._logger = logging.getLogger(__name__) self.command_manager = BotCommandManager(robot=self) try: import setproctitle setproctitle.setproctitle(bot_name) except ImportError: pass
def __init__(self, configuration, bot_name="vex"): # get the settings path and then load the settings from file settings_path = configuration.get('settings_path') settings = configuration.load_settings(settings_path) self.messaging = Messaging(settings) # create the plugin manager self.plugin_manager = pluginmanager.PluginInterface() # add the entry points of interest self.plugin_manager.add_entry_points(('vexbot.plugins', 'vexbot.adapters')) # create the subprocess manager and add in the plugins self.subprocess_manager = SubprocessManager() self._update_plugins(settings, self.subprocess_manager, self.plugin_manager) subprocesses_to_start = settings.get('startup_adapters', []) subprocesses_to_start.extend(settings.get('startup_plugins', [])) self.subprocess_manager.start(subprocesses_to_start) self.name = bot_name self._logger = logging.getLogger(__name__) self.command_manager = BotCommandManager(robot=self) try: import setproctitle setproctitle.setproctitle(bot_name) except ImportError: pass
def setUp(self): robot = MockRobot() self.command_manager = BotCommandManager(robot) self.command_manager._messaging = Message() self.subprocess_manager = robot.subprocess_manager self.messaging = self.command_manager._messaging
class TestBotCommandManager(unittest.TestCase): def setUp(self): robot = MockRobot() self.command_manager = BotCommandManager(robot) self.command_manager._messaging = Message() self.subprocess_manager = robot.subprocess_manager self.messaging = self.command_manager._messaging def test_settings(self): message = vexmessage.Message('target', 'source', 'CMD', command='subprocess', args='settings test', parsed_args=['test', ]) self.subprocess_manager.update_settings('test', {'value': 'test_value'}) self.command_manager.parse_commands(message) response = self.messaging.response[1]['response'] self.assertIn('value', response) def test_kill(self): mock = MockProcess() self.subprocess_manager._subprocess['test'] = mock message = vexmessage.Message('target', 'source', 'CMD', command='kill test') self.command_manager.parse_commands(message) self.assertTrue(mock.killed) def test_killall(self): def _mock_kill(*args, **kwargs): # due to scope issues, it's easiest to just tack on to the original # message to show that it was called correctly args[0].contents['killed'] = True self.subprocess_manager.killall = _mock_kill self.command_manager._commands['killall'] = _mock_kill message = vexmessage.Message('target', 'source', 'CMD', command='killall') self.command_manager.parse_commands(message) self.assertTrue(message.contents.get('killed')) def test_restart_bot(self): pass """ message = vexmessage.Message('target', 'source', 'CMD', command='restart_bot') with self.assertRaises(SystemExit): self.command_manager.parse_commands(message) """ def test_alive(self): self.subprocess_manager.register('test2', object()) message = vexmessage.Message('tgt', 'source', 'CMD', command='alive') self.command_manager.parse_commands(message) commands = self.messaging.commands self.assertEqual(commands[1].get('command'), 'alive') def test_start(self): # TODO: finish message = vexmessage.Message('tgt', 'source', 'CMD', command='start') self.command_manager.parse_commands(message) def test_subprocesses(self): self.subprocess_manager.register('test', object()) message = vexmessage.Message('tgt', 'source', 'CMD', command='subprocesses') self.command_manager.parse_commands(message) response = self.command_manager._messaging.response[1]['response'] self.assertIn('test', response) def test_restart(self): # TODO: finish message = vexmessage.Message('tgt', 'source', 'CMD', command='restart') self.command_manager.parse_commands(message) def test_terminate(self): # TODO: finish message = vexmessage.Message('target', 'source', 'CMD', command='terminate') self.command_manager.parse_commands(message) def test_running(self): self.subprocess_manager._subprocess['test'] = MockProcess() message = vexmessage.Message('tgt', 'source', 'CMD', command='running') self.command_manager.parse_commands(message) response = self.command_manager._messaging.response[1]['response'] self.assertIn('test', response)
class Robot: def __init__(self, configuration, bot_name="vex"): # get the settings path and then load the settings from file settings_path = configuration.get('settings_path') settings = configuration.load_settings(settings_path) self.messaging = Messaging(settings) # create the plugin manager self.plugin_manager = pluginmanager.PluginInterface() # add the entry points of interest self.plugin_manager.add_entry_points(('vexbot.plugins', 'vexbot.adapters')) # create the subprocess manager and add in the plugins self.subprocess_manager = SubprocessManager() self._update_plugins(settings, self.subprocess_manager, self.plugin_manager) subprocesses_to_start = settings.get('startup_adapters', []) subprocesses_to_start.extend(settings.get('startup_plugins', [])) self.subprocess_manager.start(subprocesses_to_start) self.name = bot_name self._logger = logging.getLogger(__name__) self.command_manager = BotCommandManager(robot=self) try: import setproctitle setproctitle.setproctitle(bot_name) except ImportError: pass def run(self): while True: frame = self.messaging.subscription_socket.recv_multipart() msg = None try: msg = decode_vex_message(frame) except Exception: pass if msg: # Right now this is hardcoded into being only # the shell adapter # change this to some kind of auth code if ((msg.source == 'shell' or msg.source == 'command_line') and msg.type == 'CMD'): self.command_manager.parse_commands(msg) def _update_plugins(self, settings, subprocess_manager=None, plugin_manager=None): """ Helper process which loads the plugins from the entry points """ if subprocess_manager is None: subprocess_manager = self.subprocess_manager if plugin_manager is None: plugin_manager = self.plugin_manager collect_ep = plugin_manager.collect_entry_point_plugins plugins, plugin_names = collect_ep() plugins = [plugin.__file__ for plugin in plugins] for plugin, name in zip(plugins, plugin_names): subprocess_manager.register(name, sys.executable, {'filepath': plugin}) for name in plugin_names: try: plugin_settings = settings[name] except KeyError: plugin_settings = {} self.subprocess_manager.update_settings(name, plugin_settings)
class TestBotCommandManager(unittest.TestCase): def setUp(self): robot = MockRobot() self.command_manager = BotCommandManager(robot) self.command_manager._messaging = Message() self.subprocess_manager = robot.subprocess_manager self.messaging = self.command_manager._messaging def test_settings(self): message = vexmessage.Message('target', 'source', 'CMD', command='subprocess', args='settings test', parsed_args=[ 'test', ]) self.subprocess_manager.update_settings('test', {'value': 'test_value'}) self.command_manager.parse_commands(message) response = self.messaging.response[1]['response'] self.assertIn('value', response) def test_kill(self): mock = MockProcess() self.subprocess_manager._subprocess['test'] = mock message = vexmessage.Message('target', 'source', 'CMD', command='kill test') self.command_manager.parse_commands(message) self.assertTrue(mock.killed) def test_killall(self): def _mock_kill(*args, **kwargs): # due to scope issues, it's easiest to just tack on to the original # message to show that it was called correctly args[0].contents['killed'] = True self.subprocess_manager.killall = _mock_kill self.command_manager._commands['killall'] = _mock_kill message = vexmessage.Message('target', 'source', 'CMD', command='killall') self.command_manager.parse_commands(message) self.assertTrue(message.contents.get('killed')) def test_restart_bot(self): pass """ message = vexmessage.Message('target', 'source', 'CMD', command='restart_bot') with self.assertRaises(SystemExit): self.command_manager.parse_commands(message) """ def test_alive(self): self.subprocess_manager.register('test2', object()) message = vexmessage.Message('tgt', 'source', 'CMD', command='alive') self.command_manager.parse_commands(message) commands = self.messaging.commands self.assertEqual(commands[1].get('command'), 'alive') def test_start(self): # TODO: finish message = vexmessage.Message('tgt', 'source', 'CMD', command='start') self.command_manager.parse_commands(message) def test_subprocesses(self): self.subprocess_manager.register('test', object()) message = vexmessage.Message('tgt', 'source', 'CMD', command='subprocesses') self.command_manager.parse_commands(message) response = self.command_manager._messaging.response[1]['response'] self.assertIn('test', response) def test_restart(self): # TODO: finish message = vexmessage.Message('tgt', 'source', 'CMD', command='restart') self.command_manager.parse_commands(message) def test_terminate(self): # TODO: finish message = vexmessage.Message('target', 'source', 'CMD', command='terminate') self.command_manager.parse_commands(message) def test_running(self): self.subprocess_manager._subprocess['test'] = MockProcess() message = vexmessage.Message('tgt', 'source', 'CMD', command='running') self.command_manager.parse_commands(message) response = self.command_manager._messaging.response[1]['response'] self.assertIn('test', response)
class Robot: def __init__(self, configuration, bot_name="vex"): # get the settings path and then load the settings from file settings_path = configuration.get('settings_path') settings = configuration.load_settings(settings_path) self.messaging = Messaging(settings) # create the plugin manager self.plugin_manager = pluginmanager.PluginInterface() # add the entry points of interest self.plugin_manager.add_entry_points( ('vexbot.plugins', 'vexbot.adapters')) # create the subprocess manager and add in the plugins self.subprocess_manager = SubprocessManager() self._update_plugins(settings, self.subprocess_manager, self.plugin_manager) subprocesses_to_start = settings.get('startup_adapters', []) subprocesses_to_start.extend(settings.get('startup_plugins', [])) self.subprocess_manager.start(subprocesses_to_start) self.name = bot_name self._logger = logging.getLogger(__name__) self.command_manager = BotCommandManager(robot=self) try: import setproctitle setproctitle.setproctitle(bot_name) except ImportError: pass def run(self): while True: frame = self.messaging.subscription_socket.recv_multipart() msg = None try: msg = decode_vex_message(frame) except Exception: pass if msg: # Right now this is hardcoded into being only # the shell adapter # change this to some kind of auth code if ((msg.source == 'shell' or msg.source == 'command_line') and msg.type == 'CMD'): self.command_manager.parse_commands(msg) def _update_plugins(self, settings, subprocess_manager=None, plugin_manager=None): """ Helper process which loads the plugins from the entry points """ if subprocess_manager is None: subprocess_manager = self.subprocess_manager if plugin_manager is None: plugin_manager = self.plugin_manager collect_ep = plugin_manager.collect_entry_point_plugins plugins, plugin_names = collect_ep() plugins = [plugin.__file__ for plugin in plugins] for plugin, name in zip(plugins, plugin_names): subprocess_manager.register(name, sys.executable, {'filepath': plugin}) for name in plugin_names: try: plugin_settings = settings[name] except KeyError: plugin_settings = {} self.subprocess_manager.update_settings(name, plugin_settings)