aboutsummaryrefslogtreecommitdiffstats
path: root/qtip/cli/entry.py
blob: b557047d69d4503d124974e62c3b2b01c1f6532c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
##############################################################################
# Copyright (c) 2017 taseer94@gmail.com and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

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'))


class SubCommand(click.MultiCommand):

    def list_commands(self, ctx):
        rv = []
        for filename in os.listdir(cmd_folder):
            if filename.endswith('.py') and \
               filename.startswith('cmd_'):
                rv.append(filename[4:-3])
        rv.sort()
        return rv

    def get_command(self, ctx, name):
        try:
            if sys.version_info[0] == 2:
                name = name.encode('ascii', 'replace')
            mod = __import__('qtip.cli.commands.cmd_' + name,
                             None, None, ['cli'])
        except ImportError:
            return
        return mod.cli


@click.command(cls=SubCommand, context_settings=CONTEXT_SETTINGS,
               invoke_without_command=True)
def sub_commands(ctx, verbose, debug):
    pass


@click.command(cls=click.CommandCollection,
               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):
    if debug:
        sys.tracebacklimit = 8