main_cli.py 2.95 KB
Newer Older
1
2
3
4
"""This is the main entry to beat's cmdlines scripts.
"""

import logging
Samuel GAIST's avatar
Samuel GAIST committed
5

6
import click
Samuel GAIST's avatar
Samuel GAIST committed
7
8
import pkg_resources

9
from click_plugins import with_plugins
Samuel GAIST's avatar
Samuel GAIST committed
10

11
from ..click_helper import AliasedGroup
12
from ..config import Configuration
Samuel GAIST's avatar
Samuel GAIST committed
13
from ..decorators import verbosity_option
14

15
16
17
18
# defines our own logging level for extra information to be printed
logging.EXTRA = 15
logging.addLevelName(logging.EXTRA, "EXTRA")

19

20
21
22
23
def _extra(self, message, *args, **kws):
    if self.isEnabledFor(logging.EXTRA):
        self._log(logging.EXTRA, message, args, **kws)

24

25
26
logging.Logger.extra = _extra

27
28

@with_plugins(pkg_resources.iter_entry_points("beat.cli"))
29
30
31
@click.group(
    cls=AliasedGroup, context_settings=dict(help_option_names=["-?", "-h", "--help"])
)
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
71
72
73
74
75
76
77
78
79
@click.option(
    "-T",
    "--test-mode",
    help="Assume test mode and doesn't setup " "the logging module",
    default=False,
    is_flag=True,
)
@click.option(
    "-p",
    "--prefix",
    help="Overrides the prefix of your local data. "
    "If not set use the value from your RC file",
    type=click.STRING,
)
@click.option(
    "-c",
    "--cache",
    help="Overrides the cache prefix. If not set, use the value " "from your RC file",
    type=click.STRING,
)
@click.option(
    "-t",
    "--token",
    help="Overrides the user token for server "
    "operations. If not set, use the value from your RC file.",
    type=click.STRING,
)
@click.option(
    "-u",
    "--user",
    help="Overrides the user name on the remote "
    "platform. If not set, use the value from your RC file.",
    type=click.STRING,
)
@click.option(
    "-m",
    "--platform",
    help="The URL of the BEAT platform to " "access.",
    type=click.STRING,
)
@click.option(
    "-e",
    "--editor",
    help="Overrides the user editor to edit local files. If not "
    "set, use the value from your environment. There are no "
    "defaults for this option.",
    type=click.STRING,
)
80
@click.version_option()
81
82
@verbosity_option()
@click.pass_context
83
def main(ctx, test_mode, prefix, cache, user, token, platform, editor):
84
85
86
    """The main command line interface for beat cmdline. Look below for available
    commands."""

87
88
89
90
91
92
    ctx.meta["--prefix"] = prefix
    ctx.meta["--cache"] = cache
    ctx.meta["--user"] = user
    ctx.meta["--token"] = token
    ctx.meta["--platform"] = platform
    ctx.meta["--editor"] = editor
93

94
95
96
97
98
    # Check that we are in a BEAT working folder
    config = Configuration(ctx.meta)

    # Sets up the central logger
    if not test_mode:
99
        verbosity = ctx.meta["verbosity"]
100
101
102
103

        # Console logging
        console_handler = logging.StreamHandler()
        format_str = "%(message)s"
104
105

        if verbosity > 2:
106
107
108
109
            format_str = "[%(asctime)s - %(name)s] %(levelname)s: %(message)s"

        formatter = logging.Formatter(format_str, datefmt="%d/%b/%Y %H:%M:%S")
        console_handler.setFormatter(formatter)
110
111

        logger = logging.getLogger("beat")
112
113
114
        logger.addHandler(console_handler)

    # Execute the command
115
    ctx.meta["config"] = config