Completed
Pull Request — master (#5)
by George
01:21
created

show_version()   A

Complexity

Conditions 3

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 7
rs 9.4285
1
# -*- coding: utf-8 -*-
2
# vi:si:et:sw=4:sts=4:ts=4
3
4
import json
5
import logging
6
7
import click
8
9
from . import __version__
10
from .conf import settings
11
from .manager import LoaferManager
12
from .aws.publisher import Publisher
13
14
15
logger = logging.getLogger(__name__)
16
17
18
def _bootstrap():
19
    logging.basicConfig(level=settings.LOAFER_LOGLEVEL,
20
                        format=settings.LOAFER_LOG_FORMAT)
21
22
    click.echo('>. Version: {}'.format(__version__))
23
    click.echo('>. Maximum concurrent jobs: {}'.format(settings.LOAFER_MAX_JOBS))
24
    click.echo('>. Routes:')
25
    for route in settings.LOAFER_ROUTES:
26
        click.echo('>.\t{}:'.format(route['name']))
27
        click.echo('>.\t\tSource: {}'.format(route['source']))
28
        click.echo('>.\t\tHandler: {}'.format(route['handler']))
29
        click.echo('>.\t\tMessage Translator: {}'.format(route['message_translator']))
30
        click.echo('>.\t\tConsumer: {}'.format(settings.LOAFER_DEFAULT_CONSUMER_CLASS))
31
        click.echo('>.\t\tConsumer Options: {}'.format(settings.LOAFER_DEFAULT_CONSUMER_OPTIONS))
32
33
34
def main(**kwargs):
35
    click.secho('>. Starting Loafer ...', bold=True, fg='green')
36
37
    _bootstrap()
38
39
    click.secho('>. Hit CTRL-C to stop', bold=True, fg='yellow')
40
41
    loafer = LoaferManager()
42
    loafer.start()
43
44
45
#
46
# CLI
47
#
48
49
CLICK_CONTEXT_SETTINGS = {'help_option_names': ['-h', '--help']}
50
51
52
def show_version(ctx, param, value):
53
    """Show Loafer version"""
54
    if not value or ctx.resilient_parsing:
55
        return
56
57
    click.echo(__version__)
58
    ctx.exit()
59
60
61
@click.group(invoke_without_command=True,
62
             context_settings=CLICK_CONTEXT_SETTINGS)
63
@click.option('-v', default=False, is_flag=True,
64
              help='Verbose mode (set LOAFER_LOGLEVEL=INFO)')
65
@click.option('-vv', default=False, is_flag=True,
66
              help='Very verbose mode (set LOAFER_LOGLEVEL=DEBUG)')
67
@click.option('--version', is_flag=True, is_eager=True, expose_value=False,
68
              callback=show_version, help="Show Loafer's version and exit")
69
@click.option('--max-jobs', default=None, type=int,
70
              help='Maximum concurrent jobs, overrides LOAFER_MAX_JOBS')
71
@click.option('--max-threads', default=None, type=int,
72
              help='Maximum threads, overrides LOAFER_MAX_THREAD_POOL')
73
@click.option('--source', default=None,
74
              help='The route source, updates the default route source')
75
@click.option('--handler', default=None,
76
              help='The route handler, updates the default route handler')
77
@click.option('--translator', default=None,
78
              help='The message translator class, updates the default route message translator')
79
@click.option('--consumer', default=None,
80
              help='The consumer class, overrides LOAFER_DEFAULT_CONSUMER_CLASS')
81
@click.option('--consumer-opts', default=None,
82
              help='The consumer options (assumes json), overrides LOAFER_DEFAULT_CONSUMER_OPTIONS')
83
@click.pass_context
84
def cli(context, v, vv, max_jobs, max_threads, source, handler, translator,
85
        consumer, consumer_opts):
86
    if v:
87
        settings.LOAFER_LOGLEVEL = 'INFO'
88
    if vv:
89
        settings.LOAFER_LOGLEVEL = 'DEBUG'
90
    if max_jobs and max_jobs >= 1:
91
        settings.LOAFER_MAX_JOBS = max_jobs
92
    if max_threads and max_threads >= 1:
93
        settings.LOAFER_MAX_THREAD_POOL = max_threads
94
    if source:
95
        settings.LOAFER_ROUTES[0]['source'] = source
96
    if handler:
97
        settings.LOAFER_ROUTES[0]['handler'] = handler
98
    if translator:
99
        settings.LOAFER_ROUTES[0]['message_translator'] = translator
100
    if consumer:
101
        settings.LOAFER_DEFAULT_CONSUMER_CLASS = consumer
102
    if consumer_opts:
103
        opts = json.loads(consumer_opts)
104
        settings.LOAFER_DEFAULT_CONSUMER_OPTIONS = opts
105
106
    if context.invoked_subcommand is None:
107
        main()
108
109
110
@cli.command()
111
@click.option('--queue', default=None, help='SQS queue name ou url')
112
@click.option('--topic', default=None, help='SNS topic name ou arn')
113
@click.option('--msg', help='Message to publish (assumes json format)')
114
def publish(queue, topic, msg):
115
    """Publish messages"""
116
    # TODO: check the "click" way to validate parameters
117
    if not (queue or topic):
118
        raise click.UsageError('--queue or --topic parameter are missing')
119
    if queue and topic:
120
        raise click.UsageError('Use --queue OR --topic, not both')
121
122
    publisher = Publisher()
123
    if queue:
124
        service = 'sqs'
125
        destination = queue
126
    if topic:
127
        service = 'sns'
128
        destination = topic
129
130
    response = publisher.publish(service, destination, msg)
131
    click.echo('Response: {}'.format(response))
132