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

import logging
import pkg_resources
import click
from click_plugins import with_plugins
8
from .click_helper import verbosity_option, AliasedGroup
9
10
from ..config import Configuration

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

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

logging.Logger.extra = _extra

21
@with_plugins(pkg_resources.iter_entry_points('beat.cli'))
22
@click.group(cls=AliasedGroup)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@click.option('-T', '--test-mode', help='Assume test mode and doesn\'t setup '
              'the logging module', default=False, is_flag=True)
@click.option('-V', '--version', help='Show version', 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)
41
42
43
44
@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)
45
46
@verbosity_option()
@click.pass_context
47
def main(ctx, test_mode, version, prefix, cache, user, token, platform, editor):
48
49
50
    """The main command line interface for beat cmdline. Look below for available
    commands."""

51
52
53
54
55
56
    ctx.meta['--version'] = version
    ctx.meta['--prefix'] = prefix
    ctx.meta['--cache'] = cache
    ctx.meta['--user'] = user
    ctx.meta['--token'] = token
    ctx.meta['--platform'] = platform
57
    ctx.meta['--editor'] = editor
58

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    # Check that we are in a BEAT working folder
    config = Configuration(ctx.meta)

    # Sets up the central logger
    if not test_mode:
        logger = logging.getLogger()
        logger.setLevel(logging.DEBUG) #lets everything pass by default

        # Console logging
        console_handler = logging.StreamHandler()
        console_handler.setLevel(logging.INFO) # default level

        format_str = "%(message)s"
        if 'verbosity' in ctx.meta and ctx.meta['verbosity'] >= 2:
            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)
        logger.addHandler(console_handler)

    # Execute the command
    ctx.meta['config'] = config