main_cli.py 2.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
"""This is the main entry to beat's cmdlines scripts.
"""

import logging
import pkg_resources
import click
from click_plugins import with_plugins
from .click_helper import verbosity_option
from ..config import Configuration

@with_plugins(pkg_resources.iter_entry_points('beat.cli'))
@click.group()
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@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)
31
32
@verbosity_option()
@click.pass_context
33
def main(ctx, test_mode, version, prefix, cache, user, token, platform):
34
35
36
    """The main command line interface for beat cmdline. Look below for available
    commands."""

37
38
39
40
41
42
43
    ctx.meta['--version'] = version
    ctx.meta['--prefix'] = prefix
    ctx.meta['--cache'] = cache
    ctx.meta['--user'] = user
    ctx.meta['--token'] = token
    ctx.meta['--platform'] = platform

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    # 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