Python cliff.commandmanager.CommandManager() Examples

The following are 30 code examples of cliff.commandmanager.CommandManager(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cliff.commandmanager , or try the search function .
Example #1
Source File: shell.py    From python-tackerclient with Apache License 2.0 6 votes vote down vote up
def __init__(self, apiversion):
        super(TackerShell, self).__init__(
            description=__doc__.strip(),
            version=VERSION,
            command_manager=commandmanager.CommandManager('tacker.cli'), )
        self.commands = COMMANDS
        for k, v in self.commands[apiversion].items():
            self.command_manager.add_command(k, v)

        self._register_extensions(VERSION)

        # Pop the 'complete' to correct the outputs of 'tacker help'.
        self.command_manager.commands.pop('complete')

        # This is instantiated in initialize_app() only when using
        # password flow auth
        self.auth_client = None
        self.api_version = apiversion 
Example #2
Source File: sphinxext.py    From cliff with Apache License 2.0 6 votes vote down vote up
def _load_commands(self):
        # TODO(sfinucan): We should probably add this wildcarding functionality
        # to the CommandManager itself to allow things like "show me the
        # commands like 'foo *'"
        command_pattern = self.options.get('command')
        manager = commandmanager.CommandManager(self.arguments[0])
        if command_pattern:
            commands = [x for x in manager.commands
                        if fnmatch.fnmatch(x, command_pattern)]
        else:
            commands = manager.commands.keys()

        if not commands:
            msg = 'No commands found in the "{}" namespace'
            if command_pattern:
                msg += ' using the "{}" command name/pattern'
            msg += ('. Are you sure this is correct and the application being '
                    'documented is installed?')
            raise self.warning(msg.format(self.arguments[0], command_pattern))

        return dict((name, self._load_command(manager, name))
                    for name in commands) 
Example #3
Source File: test_commandmanager.py    From cliff with Apache License 2.0 6 votes vote down vote up
def test_get_command_names(self):
        mock_cmd_one = mock.Mock()
        mock_cmd_one.name = 'one'
        mock_cmd_two = mock.Mock()
        mock_cmd_two.name = 'cmd two'
        mock_pkg_resources = mock.Mock(
            return_value=[mock_cmd_one, mock_cmd_two],
        )
        with mock.patch(
            'pkg_resources.iter_entry_points',
            mock_pkg_resources,
        ) as iter_entry_points:
            mgr = commandmanager.CommandManager('test')
            iter_entry_points.assert_called_once_with('test')
            cmds = mgr.get_command_names('test')
            self.assertEqual(['one', 'cmd two'], cmds) 
Example #4
Source File: test_app.py    From cliff with Apache License 2.0 6 votes vote down vote up
def test_conflicting_option_should_throw(self):
        class MyApp(application.App):
            def __init__(self):
                super(MyApp, self).__init__(
                    description='testing',
                    version='0.1',
                    command_manager=commandmanager.CommandManager('tests'),
                )

            def build_option_parser(self, description, version):
                parser = super(MyApp, self).build_option_parser(description,
                                                                version)
                parser.add_argument(
                    '-h', '--help',
                    default=self,  # tricky
                    help="Show help message and exit.",
                )

        self.assertRaises(
            argparse.ArgumentError,
            MyApp,
        ) 
Example #5
Source File: test_app.py    From cliff with Apache License 2.0 6 votes vote down vote up
def test_conflicting_option_custom_arguments_should_not_throw(self):
        class MyApp(application.App):
            def __init__(self):
                super(MyApp, self).__init__(
                    description='testing',
                    version='0.1',
                    command_manager=commandmanager.CommandManager('tests'),
                )

            def build_option_parser(self, description, version):
                argparse_kwargs = {'conflict_handler': 'resolve'}
                parser = super(MyApp, self).build_option_parser(
                    description,
                    version,
                    argparse_kwargs=argparse_kwargs)
                parser.add_argument(
                    '-h', '--help',
                    default=self,  # tricky
                    help="Show help message and exit.",
                )

        MyApp() 
Example #6
Source File: barbican.py    From python-barbicanclient with Apache License 2.0 6 votes vote down vote up
def __init__(self, **kwargs):
        self.client = None

        # Patch command.Command to add a default auth_required = True
        command.Command.auth_required = True

        # Some commands do not need authentication
        help.HelpCommand.auth_required = False
        complete.CompleteCommand.auth_required = False

        super(Barbican, self).__init__(
            description=__doc__.strip(),
            version=barbicanclient.__version__,
            command_manager=commandmanager.CommandManager(
                'openstack.key_manager.v1'),
            deferred_help=True,
            **kwargs
        ) 
Example #7
Source File: api.py    From lightbulb-framework with MIT License 6 votes vote down vote up
def __init__(self):
        """
        Initialization function of the class. It creates instances
        of the lightbulb CLI show and use functionalities.
        """
        super(LightBulb, self).__init__(
            description='LightBulb App',
            version='0.1',
            command_manager=CommandManager('cliff.lightbulb'),
            deferred_help=False,
        )
        self.library = LibraryModules(self, self, None)
        self.librarycat = LibraryCat(self, self, None)
        self.startsaved = StartSaved(self, self, None)
        self.use = Use(self, self, None)
        self.back = Back(self, self, None)
        self.define = Define(self, self, None) 
Example #8
Source File: test_cliff_api_example_1.py    From lightbulb-framework with MIT License 5 votes vote down vote up
def __init__(self):
        super(LightBulb, self).__init__(
            description='LightBulb App',
            version='0.1',
            command_manager=CommandManager('cliff.lightbulb'),
            deferred_help=False,
            )
        self.library = LibraryModules(self,self,None)
        self.libraryCat = LibraryCat(self,self,None) 
Example #9
Source File: cli.py    From stestr with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        super(StestrCLI, self).__init__(
            description="A parallel Python test runner built around subunit",
            version=__version__,
            command_manager=commandmanager.CommandManager('stestr.cm'),
            deferred_help=True,
            ) 
Example #10
Source File: test_cliff_api_example_2.py    From lightbulb-framework with MIT License 5 votes vote down vote up
def __init__(self):
        super(LightBulb, self).__init__(
            description='LightBulb App',
            version='0.1',
            command_manager=CommandManager('cliff.lightbulb'),
            deferred_help=False,
        )
        self.startsaved = StartSaved(self, self, None)
        self.use = Use(self, self, None)
        self.back = Back(self, self, None)
        self.define = Define(self, self, None) 
Example #11
Source File: lightbulb.py    From lightbulb-framework with MIT License 5 votes vote down vote up
def __init__(self):
        super(LightBulb, self).__init__(
            description='LightBulb App',
            version='0.1',
            command_manager=CommandManager('cliff.lightbulb'),
            deferred_help=False,
            ) 
Example #12
Source File: vbmc.py    From virtualbmc with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        super(VirtualBMCApp, self).__init__(
            description='Virtual Baseboard Management Controller (BMC) backed '
                        'by virtual machines',
            version=virtualbmc.__version__,
            command_manager=CommandManager('virtualbmc'),
            deferred_help=True,
        ) 
Example #13
Source File: main.py    From auDeep with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        super(AuDeepApp, self).__init__(
            description="Deep Representation Learning Toolkit for Acoustic Data",
            version=pkg_resources.require("audeep")[0].version,
            command_manager=CommandManager("audeep.commands"),
            deferred_help=True
        ) 
Example #14
Source File: main.py    From kaggle-cli with MIT License 5 votes vote down vote up
def __init__(self):
        super(KaggleCLI, self).__init__(
            description='An unofficial Kaggle command line tool.',
            version=VERSION,
            command_manager=CommandManager('kaggle_cli'),
        ) 
Example #15
Source File: cli.py    From optuna with MIT License 5 votes vote down vote up
def __init__(self):
        # type: () -> None

        super(_OptunaApp, self).__init__(
            description="",
            version=optuna.__version__,
            command_manager=CommandManager("optuna.command"),
        ) 
Example #16
Source File: shell.py    From python-cloudkittyclient with Apache License 2.0 5 votes vote down vote up
def __init__(self, args):
        self._args = args
        self.cloud_config = os_client_config.OpenStackConfig()
        super(CloudKittyShell, self).__init__(
            description='CloudKitty CLI client',
            version=utils.get_version(),
            command_manager=CommandManager('cloudkittyclient_v{}'.format(
                self._get_api_version(args[:]),
            )),
            deferred_help=True,
        )
        self._client = None

    # NOTE(peschk_l): Used to warn users about command syntax change in Rocky.
    # To be deleted in S. 
Example #17
Source File: shell.py    From ara-archive with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        super(AraCli, self).__init__(
            description='A CLI client to query ARA databases',
            version=__version__,
            command_manager=CommandManager('ara.cli'),
            deferred_help=True) 
Example #18
Source File: shell.py    From python-mistralclient with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        super(MistralShell, self).__init__(
            description=__doc__.strip(),
            version=mistralclient.__version__,
            command_manager=commandmanager.CommandManager('mistral.cli'),
        )

        # Set v2 commands by default
        self._set_shell_commands(self._get_commands_v2()) 
Example #19
Source File: kayobe.py    From kayobe with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        release_version = version.version_info.release_string()
        super(KayobeApp, self).__init__(
            description='Kayobe Command Line Interface (CLI)',
            version=release_version,
            command_manager=CommandManager('kayobe.cli'),
            deferred_help=True,
            ) 
Example #20
Source File: test_commandmanager.py    From cliff with Apache License 2.0 5 votes vote down vote up
def test_load_commands_replace_underscores(self):
        testcmd = mock.Mock()
        testcmd.name = 'test_cmd'
        mock_pkg_resources = mock.Mock(return_value=[testcmd])
        with mock.patch('pkg_resources.iter_entry_points',
                        mock_pkg_resources) as iter_entry_points:
            mgr = commandmanager.CommandManager(
                'test',
                convert_underscores=True,
            )
            iter_entry_points.assert_called_once_with('test')
            names = [n for n, v in mgr]
            self.assertEqual(['test cmd'], names) 
Example #21
Source File: test_commandmanager.py    From cliff with Apache License 2.0 5 votes vote down vote up
def test_load_commands_keep_underscores(self):
        testcmd = mock.Mock()
        testcmd.name = 'test_cmd'
        mock_pkg_resources = mock.Mock(return_value=[testcmd])
        with mock.patch('pkg_resources.iter_entry_points',
                        mock_pkg_resources) as iter_entry_points:
            mgr = commandmanager.CommandManager(
                'test',
                convert_underscores=False,
            )
            iter_entry_points.assert_called_once_with('test')
            names = [n for n, v in mgr]
            self.assertEqual(['test_cmd'], names) 
Example #22
Source File: test_commandmanager.py    From cliff with Apache License 2.0 5 votes vote down vote up
def test_load_commands(self):
        testcmd = mock.Mock(name='testcmd')
        testcmd.name.replace.return_value = 'test'
        mock_pkg_resources = mock.Mock(return_value=[testcmd])
        with mock.patch('pkg_resources.iter_entry_points',
                        mock_pkg_resources) as iter_entry_points:
            mgr = commandmanager.CommandManager('test')
            iter_entry_points.assert_called_once_with('test')
            names = [n for n, v in mgr]
            self.assertEqual(['test'], names) 
Example #23
Source File: test_app.py    From cliff with Apache License 2.0 5 votes vote down vote up
def test_fuzzy_no_prefix(self):
        # search by distance, no common prefix with any command
        cmd_mgr = commandmanager.CommandManager('cliff.fuzzy')
        app = application.App('test', '1.0', cmd_mgr)
        cmd_mgr.add_command('user', test_utils.TestCommand)
        matches = app.get_fuzzy_matches('uesr')
        self.assertEqual(['user'], matches) 
Example #24
Source File: test_app.py    From cliff with Apache License 2.0 5 votes vote down vote up
def test_fuzzy_common_prefix(self):
        # searched string is a prefix of all commands
        cmd_mgr = commandmanager.CommandManager('cliff.fuzzy')
        app = application.App('test', '1.0', cmd_mgr)
        cmd_mgr.commands = {}
        cmd_mgr.add_command('user list', test_utils.TestCommand)
        cmd_mgr.add_command('user show', test_utils.TestCommand)
        matches = app.get_fuzzy_matches('user')
        self.assertEqual(['user list', 'user show'], matches) 
Example #25
Source File: test_app.py    From cliff with Apache License 2.0 5 votes vote down vote up
def test_fuzzy_no_commands(self):
        cmd_mgr = commandmanager.CommandManager('cliff.fuzzy')
        app = application.App('test', '1.0', cmd_mgr)
        cmd_mgr.commands = {}
        matches = app.get_fuzzy_matches('foo')
        self.assertEqual([], matches) 
Example #26
Source File: test_app.py    From cliff with Apache License 2.0 5 votes vote down vote up
def test_option_parser_abbrev_issue(self):
        class MyCommand(c_cmd.Command):
            def get_parser(self, prog_name):
                parser = super(MyCommand, self).get_parser(prog_name)
                parser.add_argument("--end")
                return parser

            def take_action(self, parsed_args):
                assert(parsed_args.end == '123')

        class MyCommandManager(commandmanager.CommandManager):
            def load_commands(self, namespace):
                self.add_command("mycommand", MyCommand)

        class MyApp(application.App):
            def __init__(self):
                super(MyApp, self).__init__(
                    description='testing',
                    version='0.1',
                    command_manager=MyCommandManager(None),
                )

            def build_option_parser(self, description, version):
                parser = super(MyApp, self).build_option_parser(
                    description,
                    version,
                    argparse_kwargs={'allow_abbrev': False})
                parser.add_argument('--endpoint')
                return parser

        app = MyApp()
        # NOTE(jd) --debug is necessary so assert in take_action()
        # raises correctly here
        app.run(['--debug', 'mycommand', '--end', '123']) 
Example #27
Source File: test_complete.py    From cliff with Apache License 2.0 5 votes vote down vote up
def given_complete_command(self):
        cmd_mgr = commandmanager.CommandManager('cliff.tests')
        app = application.App('testing', '1', cmd_mgr, stdout=FakeStdout())
        sot = complete.CompleteCommand(app, mock.Mock())
        cmd_mgr.add_command('complete', complete.CompleteCommand)
        return sot, app, cmd_mgr 
Example #28
Source File: test_command_hooks.py    From cliff with Apache License 2.0 5 votes vote down vote up
def make_app(**kwargs):
    cmd_mgr = commandmanager.CommandManager('cliff.tests')

    # Register a command that succeeds
    cmd = mock.MagicMock(spec=command.Command)
    command_inst = mock.MagicMock(spec=command.Command)
    command_inst.run.return_value = 0
    cmd.return_value = command_inst
    cmd_mgr.add_command('mock', cmd)

    # Register a command that fails
    err_command = mock.Mock(name='err_command', spec=command.Command)
    err_command_inst = mock.Mock(spec=command.Command)
    err_command_inst.run = mock.Mock(
        side_effect=RuntimeError('test exception')
    )
    err_command.return_value = err_command_inst
    cmd_mgr.add_command('error', err_command)

    app = application.App('testing command hooks',
                          '1',
                          cmd_mgr,
                          stderr=mock.Mock(),  # suppress warning messages
                          **kwargs
                          )
    return app 
Example #29
Source File: sphinxext.py    From cliff with Apache License 2.0 5 votes vote down vote up
def _load_command(self, manager, command_name):
        """Load a command using an instance of a `CommandManager`."""
        try:
            # find_command expects the value of argv so split to emulate that
            return manager.find_command(command_name.split())[0]
        except ValueError:
            raise self.error('"{}" is not a valid command in the "{}" '
                             'namespace'.format(
                                 command_name, manager.namespace)) 
Example #30
Source File: main.py    From cliff with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        super(DemoApp, self).__init__(
            description='cliff demo app',
            version='0.1',
            command_manager=CommandManager('cliff.demo'),
            deferred_help=True,
            )