def test__get_command_hook(self): """ registration.get_command_hook: returns the registered command plugin for the provided key. """ registration._command_hooks.register(MockPlugin.command, MockPlugin) command = MockPlugin.command self.assertIsInstance(registration.get_command_hook(command), MockPlugin)
def testGoodPlugins__load(self): """ Environment.load: successfully loads valid plugins. """ # build test command plugin and load plugin_dir = os.path.join(self.env._data_dir, 'plugins') os.makedirs(plugin_dir) filename = os.path.join(plugin_dir, 'testplugin.py') with open(filename, 'w', 0) as f: f.write('from focus.plugin import Plugin\n') f.write('class MyTestPlugin(Plugin):\n') f.write(' name = "MyTestPlugin"\n') f.write(' target_version = ">=0.1.0"\n') f.write(' version = "1.0"\n') f.write(' command = "oh_hai"\n') f.write(' def execute(self, env): env.io.write("You rang.")\n') f.write(' def help(self, env): return u"focus oh_hai"\n') self.env.load() # confirm plugin registration in a couple ways.. # 1) scan registered list # 2) get command hook version # 3) execute methods and compare plugin = None for p in registration.get_registered(command_hooks=True): if p.name == 'MyTestPlugin': plugin = p break success = False if plugin: test_plugin = registration.get_command_hook('oh_hai') if test_plugin == plugin: # test execute() plugin.execute(self.env) self.assertEqual(self.env.io.test__write_data, 'You rang.\n') # test help() data = plugin.help(self.env) self.assertEqual(data, u'focus oh_hai') success = True self.assertTrue(success) # clean up self.clean_paths(filename, filename + 'c')
def _handle_command(self, command, env, args): """ Handles calling appropriate command plugin based on the arguments provided. `command` Command string. `env` Runtime ``Environment`` instance. `args` List of argument strings passed. Returns ``False`` if nothing handled. * Raises ``HelpBanner`` exception if mismatched command arguments. """ # get command plugin registered for command # note, we're guaranteed to have a command string by this point plugin_obj = registration.get_command_hook(command, env.task.active) # check if plugin is task-specific or has option hooks implying # task-specific behavior if plugin_obj and not env.task.active: if plugin_obj.task_only or plugin_obj.options: plugin_obj = None if plugin_obj: # plugin needs root, setup root access via sudo if plugin_obj.needs_root: registration.setup_sudo_access(plugin_obj) # parse arguments parser = self._get_plugin_parser(plugin_obj) parsed_args = parser.parse_args(args) # run plugin plugin_obj.execute(env, parsed_args) return True return False
def _handle_help(self, env, args): """ Handles showing help information for arguments provided. `env` Runtime ``Environment`` instance. `args` List of argument strings passed. Returns ``False`` if nothing handled. * Raises ``HelpBanner`` exception if valid subcommand provided. """ if args: # command help (focus help [command]) # get command plugin registered for command active = env.task.active plugin_obj = registration.get_command_hook(args[0], active) if plugin_obj: parser = self._get_plugin_parser(plugin_obj) raise HelpBanner(parser.format_help(), code=0) return False