Refactor Cli 13/35713/2
authorTaseer <taseer94@gmail.com>
Fri, 2 Jun 2017 09:23:55 +0000 (14:23 +0500)
committerTaseer <taseer94@gmail.com>
Fri, 2 Jun 2017 14:41:05 +0000 (19:41 +0500)
- Remove reduntant contexts
- Remove verbose option
- Use builtin help and version options

Change-Id: Ifb66009b5f9ab95428ce87c90f739b5221ccdd6f
Signed-off-by: Taseer Ahmed <taseer94@gmail.com>
qtip/cli/commands/cmd_metric.py
qtip/cli/commands/cmd_qpi.py
qtip/cli/commands/cmd_report.py
qtip/cli/entry.py
tests/unit/cli/options_test.py

index 1741fb4..0a38589 100644 (file)
@@ -14,22 +14,17 @@ import os
 from qtip.base.error import InvalidContentError
 from qtip.base.error import NotFoundError
 from qtip.cli import utils
-from qtip.cli.entry import Context
 from qtip.loader.metric import MetricSpec
 
-pass_context = click.make_pass_decorator(Context, ensure=False)
-
 
 @click.group()
-@pass_context
-def cli(ctx):
+def cli():
     ''' Performance Metrics Group '''
     pass
 
 
 @cli.command('list', help='List all the Metric Groups')
-@pass_context
-def cmd_list(ctx):
+def cmd_list():
     metrics = MetricSpec.list_all()
     table = utils.table('Metrics', metrics)
     click.echo(table)
@@ -37,8 +32,7 @@ def cmd_list(ctx):
 
 @cli.command('show', help='View details of a Metric')
 @click.argument('name')
-@pass_context
-def show(ctx, name):
+def show(name):
     try:
         metric = MetricSpec('{}.yaml'.format(name))
     except NotFoundError as nf:
@@ -54,8 +48,7 @@ def show(ctx, name):
 @cli.command('run', help='Run performance test')
 @click.argument('name')
 @click.option('-p', '--path', help='Path to store results')
-@pass_context
-def run(ctx, name, path):
+def run(name, path):
     runner_path = os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir,
                                'runner/runner.py')
     os.system('python {0} -b {1} -d {2}'.format(runner_path, name, path))
index a47442b..d08842a 100644 (file)
@@ -15,22 +15,17 @@ import os
 from qtip.base.error import InvalidContentError
 from qtip.base.error import NotFoundError
 from qtip.cli import utils
-from qtip.cli.entry import Context
 from qtip.loader.qpi import QPISpec
 
-pass_context = click.make_pass_decorator(Context, ensure=False)
-
 
 @click.group()
-@pass_context
-def cli(ctx):
+def cli():
     ''' Collection of performance tests '''
     pass
 
 
 @cli.command('list', help='List all the QPI specs')
-@pass_context
-def cmd_list(ctx):
+def cmd_list():
     qpis = QPISpec.list_all()
     table = utils.table('QPIs', qpis)
     click.echo(table)
@@ -38,8 +33,7 @@ def cmd_list(ctx):
 
 @cli.command('show', help='View details of a QPI')
 @click.argument('name')
-@pass_context
-def show(ctx, name):
+def show(name):
     try:
         qpi = QPISpec('{}.yaml'.format(name))
     except NotFoundError as nf:
@@ -55,8 +49,7 @@ def show(ctx, name):
 @cli.command('run', help='Run performance tests for the specified QPI')
 @click.argument('name')
 @click.option('-p', '--path', help='Path to store results')
-@pass_context
-def run(ctx, name, path):
+def run(name, path):
     runner_path = path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir,
                             'runner/runner.py')
     os.system('python {0} -b all -d {1}'.format(runner_path, path))
index ebc0ef7..4176fd9 100644 (file)
@@ -9,15 +9,11 @@
 
 import click
 
-from qtip.cli.entry import Context
 from qtip.reporter.console import ConsoleReporter
 
-pass_context = click.make_pass_decorator(Context, ensure=False)
-
 
 @click.group()
-@pass_context
-def cli(ctx):
+def cli():
     """ View QTIP results"""
     pass
 
@@ -25,8 +21,7 @@ def cli(ctx):
 @cli.command('show')
 @click.argument('metric')
 @click.option('-p', '--path', help='Path to result directory')
-@pass_context
-def show(ctx, metric, path):
+def show(metric, path):
     reporter = ConsoleReporter({})
     report = reporter.render(metric, path)
     click.echo(report)
index b557047..0825d5e 100644 (file)
@@ -9,22 +9,12 @@
 
 import click
 import os
-import pkg_resources as pkg
 import sys
 
 from qtip.cli.commands.cmd_project import cli as project_commands
 
-CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
 
-# TODO (taseer) define user friendly error messages
 sys.tracebacklimit = 0
-
-
-class Context(object):
-    """ Load configuration and pass to subcommands """
-
-
-pass_context = click.make_pass_decorator(Context, ensure=True)
 cmd_folder = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                           'commands'))
 
@@ -51,9 +41,9 @@ class SubCommand(click.MultiCommand):
         return mod.cli
 
 
-@click.command(cls=SubCommand, context_settings=CONTEXT_SETTINGS,
+@click.command(cls=SubCommand,
                invoke_without_command=True)
-def sub_commands(ctx, verbose, debug):
+def sub_commands(debug):
     pass
 
 
@@ -61,10 +51,8 @@ def sub_commands(ctx, verbose, debug):
                help="Platform performance benchmarking",
                sources=[sub_commands, project_commands],
                invoke_without_command=True)
-@click.option('-v', '--verbose', is_flag=True, help='Enable verbose mode.')
 @click.option('-d', '--debug', is_flag=True, help='Enable debug mode.')
-@click.version_option(pkg.require("qtip")[0])
-@pass_context
-def cli(ctx, verbose, debug):
+@click.version_option()
+def cli(debug):
     if debug:
         sys.tracebacklimit = 8
index d7c0f70..6aef139 100644 (file)
@@ -21,10 +21,6 @@ class TestClass(object):
     def runner(self):
         return CliRunner()
 
-    def test_verbose(self, runner):
-        result = runner.invoke(cli, ['-v'])
-        assert result.output == ''
-
     def test_version(self, runner):
         result = runner.invoke(cli, ['--version'])
         assert re.search(r'\d+\.\d+\.\d+', result.output)