Python cliff.app.App() Examples

The following are 20 code examples of cliff.app.App(). 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.app , or try the search function .
Example #1
Source File: sphinxext.py    From cliff with Apache License 2.0 6 votes vote down vote up
def _load_app(self):
        mod_str, _sep, class_str = self.arguments[0].rpartition('.')
        if not mod_str:
            return
        try:
            importlib.import_module(mod_str)
        except ImportError:
            return
        try:
            cliff_app_class = getattr(sys.modules[mod_str], class_str)
        except AttributeError:
            return
        if not inspect.isclass(cliff_app_class):
            return
        if not issubclass(cliff_app_class, app.App):
            return
        app_arguments = self.options.get('arguments', '').split()
        return cliff_app_class(*app_arguments) 
Example #2
Source File: test_help.py    From cliff with Apache License 2.0 6 votes vote down vote up
def test_show_help_for_command(self):
        # FIXME(dhellmann): Are commands tied too closely to the app? Or
        # do commands know too much about apps by using them to get to the
        # command manager?
        stdout = StringIO()
        app = application.App('testing', '1',
                              utils.TestCommandManager(utils.TEST_NAMESPACE),
                              stdout=stdout)
        app.NAME = 'test'
        help_cmd = help.HelpCommand(app, mock.Mock())
        parser = help_cmd.get_parser('test')
        parsed_args = parser.parse_args(['one'])
        try:
            help_cmd.run(parsed_args)
        except SystemExit:
            pass
        self.assertEqual('TestParser', stdout.getvalue()) 
Example #3
Source File: test_help.py    From cliff with Apache License 2.0 6 votes vote down vote up
def test_list_matching_commands(self):
        # FIXME(dhellmann): Are commands tied too closely to the app? Or
        # do commands know too much about apps by using them to get to the
        # command manager?
        stdout = StringIO()
        app = application.App('testing', '1',
                              utils.TestCommandManager(utils.TEST_NAMESPACE),
                              stdout=stdout)
        app.NAME = 'test'
        help_cmd = help.HelpCommand(app, mock.Mock())
        parser = help_cmd.get_parser('test')
        parsed_args = parser.parse_args(['t'])
        try:
            help_cmd.run(parsed_args)
        except SystemExit:
            pass
        help_output = stdout.getvalue()
        self.assertIn('Command "t" matches:', help_output)
        self.assertIn('three word command\n  two words\n', help_output) 
Example #4
Source File: test_help.py    From cliff with Apache License 2.0 6 votes vote down vote up
def test_list_matching_commands_no_match(self):
        # FIXME(dhellmann): Are commands tied too closely to the app? Or
        # do commands know too much about apps by using them to get to the
        # command manager?
        stdout = StringIO()
        app = application.App('testing', '1',
                              utils.TestCommandManager(utils.TEST_NAMESPACE),
                              stdout=stdout)
        app.NAME = 'test'
        help_cmd = help.HelpCommand(app, mock.Mock())
        parser = help_cmd.get_parser('test')
        parsed_args = parser.parse_args(['z'])
        self.assertRaises(
            ValueError,
            help_cmd.run,
            parsed_args,
        ) 
Example #5
Source File: test_help.py    From cliff with Apache License 2.0 6 votes vote down vote up
def test_list_deprecated_commands(self):
        # FIXME(dhellmann): Are commands tied too closely to the app? Or
        # do commands know too much about apps by using them to get to the
        # command manager?
        stdout = StringIO()
        app = application.App('testing', '1',
                              utils.TestCommandManager(utils.TEST_NAMESPACE),
                              stdout=stdout)
        app.NAME = 'test'
        try:
            app.run(['--help'])
        except SystemExit:
            pass
        help_output = stdout.getvalue()
        self.assertIn('two words', help_output)
        self.assertIn('three word command', help_output)
        self.assertNotIn('old cmd', help_output) 
Example #6
Source File: test_help.py    From cliff with Apache License 2.0 6 votes vote down vote up
def test_show_help_with_ep_load_fail(self, mock_load):
        stdout = StringIO()
        app = application.App('testing', '1',
                              utils.TestCommandManager(utils.TEST_NAMESPACE),
                              stdout=stdout)
        app.NAME = 'test'
        app.options = mock.Mock()
        app.options.debug = False
        help_cmd = help.HelpCommand(app, mock.Mock())
        parser = help_cmd.get_parser('test')
        parsed_args = parser.parse_args([])
        try:
            help_cmd.run(parsed_args)
        except SystemExit:
            pass
        help_output = stdout.getvalue()
        self.assertIn('Commands:', help_output)
        self.assertIn('Could not load', help_output)
        self.assertNotIn('Exception: Could not load EntryPoint', help_output) 
Example #7
Source File: test_help.py    From cliff with Apache License 2.0 6 votes vote down vote up
def test_show_help_print_exc_with_ep_load_fail(self, mock_load):
        stdout = StringIO()
        app = application.App('testing', '1',
                              utils.TestCommandManager(utils.TEST_NAMESPACE),
                              stdout=stdout)
        app.NAME = 'test'
        app.options = mock.Mock()
        app.options.debug = True
        help_cmd = help.HelpCommand(app, mock.Mock())
        parser = help_cmd.get_parser('test')
        parsed_args = parser.parse_args([])
        try:
            help_cmd.run(parsed_args)
        except SystemExit:
            pass
        help_output = stdout.getvalue()
        self.assertIn('Commands:', help_output)
        self.assertIn('Could not load', help_output)
        self.assertIn('Exception: Could not load EntryPoint', help_output) 
Example #8
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 #9
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 #10
Source File: test_app.py    From cliff with Apache License 2.0 6 votes vote down vote up
def test_list_matching_commands(self):
        stdout = StringIO()
        app = application.App('testing', '1',
                              test_utils.TestCommandManager(
                                  test_utils.TEST_NAMESPACE),
                              stdout=stdout)
        app.NAME = 'test'
        try:
            self.assertEqual(2, app.run(['t']))
        except SystemExit:
            pass
        output = stdout.getvalue()
        self.assertIn("test: 't' is not a test command. See 'test --help'.",
                      output)
        self.assertIn('Did you mean one of these?', output)
        self.assertIn('three word command\n  two words\n', output) 
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: 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 #13
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 #14
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 #15
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 #16
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 #17
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 #18
Source File: test_app.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
    command = mock.MagicMock(spec=c_cmd.Command)
    command_inst = mock.MagicMock(spec=c_cmd.Command)
    command_inst.run.return_value = 0
    command.return_value = command_inst
    cmd_mgr.add_command('mock', command)

    # Register a command that fails
    err_command = mock.Mock(name='err_command', spec=c_cmd.Command)
    err_command_inst = mock.Mock(spec=c_cmd.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 interactive mode',
                          '1',
                          cmd_mgr,
                          stderr=mock.Mock(),  # suppress warning messages
                          **kwargs
                          )
    return app, command 
Example #19
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 #20
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