Passed
Push — master ( c9b83a...258af3 )
by George
02:04
created

show_version()   A

Complexity

Conditions 3

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
import json
2
import logging
3
4
import click
5
6
from .conf import settings
7
from .manager import LoaferManager
8
from .aws.publisher import sqs_publish, sns_publish
9
10
11
def _bootstrap():
12
    logging.basicConfig(level=settings.LOAFER_LOGLEVEL,
13
                        format=settings.LOAFER_LOG_FORMAT)
14
15
    click.echo('>. Maximum concurrent jobs: {}'.format(settings.LOAFER_MAX_JOBS))
16
    click.echo('>. Routes:')
17
    for route in settings.LOAFER_ROUTES:
18
        click.echo('>.\t{}:'.format(route['name']))
19
        click.echo('>.\t\tSource: {}'.format(route['source']))
20
        click.echo('>.\t\tHandler: {}'.format(route['handler']))
21
        click.echo('>.\t\tMessage Translator: {}'.format(route['message_translator']))
22
        click.echo('>.\t\tConsumer: {}'.format(settings.LOAFER_DEFAULT_CONSUMER_CLASS))
23
        click.echo('>.\t\tConsumer Options: {}'.format(settings.LOAFER_DEFAULT_CONSUMER_OPTIONS))
24
25
26
def main(**kwargs):
0 ignored issues
show
Unused Code introduced by
The argument kwargs seems to be unused.
Loading history...
27
    click.secho('>. Starting Loafer ...', bold=True, fg='green')
28
29
    _bootstrap()
30
31
    click.secho('>. Hit CTRL-C to stop', bold=True, fg='yellow')
32
33
    loafer = LoaferManager()
34
    loafer.start()
35
36
37
#
38
# CLI
39
#
40
41
CLICK_CONTEXT_SETTINGS = {'help_option_names': ['-h', '--help']}
42
43
44
@click.group(invoke_without_command=True,
45
             context_settings=CLICK_CONTEXT_SETTINGS)
46
@click.option('-v', default=False, is_flag=True,
47
              help='Verbose mode (set LOAFER_LOGLEVEL=INFO)')
48
@click.option('-vv', default=False, is_flag=True,
49
              help='Very verbose mode (set LOAFER_LOGLEVEL=DEBUG)')
50
@click.option('--max-jobs', default=None, type=int,
51
              help='Maximum concurrent jobs, overrides LOAFER_MAX_JOBS')
52
@click.option('--max-threads', default=None, type=int,
53
              help='Maximum threads, overrides LOAFER_MAX_THREAD_POOL')
54
@click.option('--source', default=None,
55
              help='The route source, updates the default route source')
56
@click.option('--handler', default=None,
57
              help='The route handler, updates the default route handler')
58
@click.option('--translator', default=None,
59
              help='The message translator class, updates the default route message translator')
60
@click.option('--consumer', default=None,
61
              help='The consumer class, overrides LOAFER_DEFAULT_CONSUMER_CLASS')
62
@click.option('--consumer-opts', default=None,
63
              help='The consumer options (assumes json), overrides LOAFER_DEFAULT_CONSUMER_OPTIONS')
64
@click.pass_context
65
def cli(context, v, vv, max_jobs, max_threads, source, handler, translator,
66
        consumer, consumer_opts):
67
    if v:
68
        settings.LOAFER_LOGLEVEL = 'INFO'
69
    if vv:
70
        settings.LOAFER_LOGLEVEL = 'DEBUG'
71
    if max_jobs and max_jobs >= 1:
72
        settings.LOAFER_MAX_JOBS = max_jobs
73
    if max_threads and max_threads >= 1:
74
        settings.LOAFER_MAX_THREAD_POOL = max_threads
75
    if source:
76
        settings.LOAFER_ROUTES[0]['source'] = source
77
    if handler:
78
        settings.LOAFER_ROUTES[0]['handler'] = handler
79
    if translator:
80
        settings.LOAFER_ROUTES[0]['message_translator'] = translator
81
    if consumer:
82
        settings.LOAFER_DEFAULT_CONSUMER_CLASS = consumer
83
    if consumer_opts:
84
        opts = json.loads(consumer_opts)
85
        settings.LOAFER_DEFAULT_CONSUMER_OPTIONS = opts
86
87
    if context.invoked_subcommand is None:
88
        main()
89
90
91
@cli.command()
92
@click.option('--queue', default=None, help='SQS queue name ou url')
93
@click.option('--msg', help='Message to publish (assumes json format)')
94
def publish_sqs(queue, msg):
95
    """Publish messages to AWS SQS"""
96
    response = sqs_publish(queue, json.dumps(msg))
97
    click.echo('Response: {}'.format(response))
98
99
100
@cli.command()
101
@click.option('--topic', default=None, help='SNS topic name ou arn')
102
@click.option('--msg', help='Message to publish (assumes json format)')
103
def publish_sns(topic, msg):
104
    """Publish messages to AWS SNS"""
105
    # We need this validation because our sns_publish always use json format
106
    try:
107
        json.loads(msg)
108
    except json.decoder.JSONDecodeError:
109
        click.secho('"{}" should be a valid json'.format(msg), fg='red')
110
    else:
111
        response = sns_publish(topic, msg)
112
        click.echo('Response: {}'.format(response))
113