Completed
Pull Request — master (#9)
by George
02:20
created

publish_sns()   A

Complexity

Conditions 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 13
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
1 ignored issue
show
Configuration introduced by
The import click could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
8
9
from . import __version__
10
from .conf import settings
11
from .manager import LoaferManager
12
from .aws.publisher import sqs_publish, sns_publish
13
14
logger = logging.getLogger(__name__)
1 ignored issue
show
Coding Style Naming introduced by
The name logger does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
15
16
17
def _bootstrap():
18
    logging.basicConfig(level=settings.LOAFER_LOGLEVEL,
19
                        format=settings.LOAFER_LOG_FORMAT)
20
21
    click.echo('>. Version: {}'.format(__version__))
22
    click.echo('>. Maximum concurrent jobs: {}'.format(settings.LOAFER_MAX_JOBS))
23
    click.echo('>. Routes:')
24
    for route in settings.LOAFER_ROUTES:
25
        click.echo('>.\t{}:'.format(route['name']))
26
        click.echo('>.\t\tSource: {}'.format(route['source']))
27
        click.echo('>.\t\tHandler: {}'.format(route['handler']))
28
        click.echo('>.\t\tMessage Translator: {}'.format(route['message_translator']))
29
        click.echo('>.\t\tConsumer: {}'.format(settings.LOAFER_DEFAULT_CONSUMER_CLASS))
30
        click.echo('>.\t\tConsumer Options: {}'.format(settings.LOAFER_DEFAULT_CONSUMER_OPTIONS))
31
32
33
def main(**kwargs):
0 ignored issues
show
Unused Code introduced by
The argument kwargs seems to be unused.
Loading history...
34
    click.secho('>. Starting Loafer ...', bold=True, fg='green')
35
36
    _bootstrap()
37
38
    click.secho('>. Hit CTRL-C to stop', bold=True, fg='yellow')
39
40
    loafer = LoaferManager()
41
    loafer.start()
42
43
44
#
45
# CLI
46
#
47
48
CLICK_CONTEXT_SETTINGS = {'help_option_names': ['-h', '--help']}
49
50
51
def show_version(ctx, param, value):
0 ignored issues
show
Unused Code introduced by
The argument param seems to be unused.
Loading history...
52
    """Show Loafer version"""
53
    if not value or ctx.resilient_parsing:
54
        return
55
56
    click.echo(__version__)
57
    ctx.exit()
58
59
60
@click.group(invoke_without_command=True,
61
             context_settings=CLICK_CONTEXT_SETTINGS)
62
@click.option('-v', default=False, is_flag=True,
63
              help='Verbose mode (set LOAFER_LOGLEVEL=INFO)')
64
@click.option('-vv', default=False, is_flag=True,
65
              help='Very verbose mode (set LOAFER_LOGLEVEL=DEBUG)')
66
@click.option('--version', is_flag=True, is_eager=True, expose_value=False,
67
              callback=show_version, help="Show Loafer's version and exit")
68
@click.option('--max-jobs', default=None, type=int,
69
              help='Maximum concurrent jobs, overrides LOAFER_MAX_JOBS')
70
@click.option('--max-threads', default=None, type=int,
71
              help='Maximum threads, overrides LOAFER_MAX_THREAD_POOL')
72
@click.option('--source', default=None,
73
              help='The route source, updates the default route source')
74
@click.option('--handler', default=None,
75
              help='The route handler, updates the default route handler')
76
@click.option('--translator', default=None,
77
              help='The message translator class, updates the default route message translator')
78
@click.option('--consumer', default=None,
79
              help='The consumer class, overrides LOAFER_DEFAULT_CONSUMER_CLASS')
80
@click.option('--consumer-opts', default=None,
81
              help='The consumer options (assumes json), overrides LOAFER_DEFAULT_CONSUMER_OPTIONS')
82
@click.pass_context
83
def cli(context, v, vv, max_jobs, max_threads, source, handler, translator,
2 ignored issues
show
Coding Style Naming introduced by
The name v does not conform to the argument naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name vv does not conform to the argument naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
84
        consumer, consumer_opts):
85
    if v:
86
        settings.LOAFER_LOGLEVEL = 'INFO'
1 ignored issue
show
Coding Style Naming introduced by
The name LOAFER_LOGLEVEL does not conform to the attribute naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
87
    if vv:
88
        settings.LOAFER_LOGLEVEL = 'DEBUG'
89
    if max_jobs and max_jobs >= 1:
90
        settings.LOAFER_MAX_JOBS = max_jobs
1 ignored issue
show
Coding Style Naming introduced by
The name LOAFER_MAX_JOBS does not conform to the attribute naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
91
    if max_threads and max_threads >= 1:
92
        settings.LOAFER_MAX_THREAD_POOL = max_threads
1 ignored issue
show
Coding Style Naming introduced by
The name LOAFER_MAX_THREAD_POOL does not conform to the attribute naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
93
    if source:
94
        settings.LOAFER_ROUTES[0]['source'] = source
95
    if handler:
96
        settings.LOAFER_ROUTES[0]['handler'] = handler
97
    if translator:
98
        settings.LOAFER_ROUTES[0]['message_translator'] = translator
99
    if consumer:
100
        settings.LOAFER_DEFAULT_CONSUMER_CLASS = consumer
1 ignored issue
show
Coding Style Naming introduced by
The name LOAFER_DEFAULT_CONSUMER_CLASS does not conform to the attribute naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
101
    if consumer_opts:
102
        opts = json.loads(consumer_opts)
103
        settings.LOAFER_DEFAULT_CONSUMER_OPTIONS = opts
1 ignored issue
show
Coding Style Naming introduced by
The name LOAFER_DEFAULT_CONSUMER_OPTIONS does not conform to the attribute naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
104
105
    if context.invoked_subcommand is None:
106
        main()
107
108
109
@cli.command()
110
@click.option('--queue', default=None, help='SQS queue name ou url')
111
@click.option('--msg', help='Message to publish (assumes json format)')
112
def publish_sqs(queue, msg):
113
    """Publish messages to AWS SQS"""
114
    response = sqs_publish(queue, json.dumps(msg))
115
    click.echo('Response: {}'.format(response))
116
117
118
@cli.command()
119
@click.option('--topic', default=None, help='SNS topic name ou arn')
120
@click.option('--msg', help='Message to publish (assumes json format)')
121
def publish_sns(topic, msg):
122
    """Publish messages to AWS SNS"""
123
    # We need this validation because our sns_publish always use json format
124
    try:
125
        json.loads(msg)
126
    except json.decoder.JSONDecodeError:
1 ignored issue
show
Bug introduced by
The Module json.decoder does not seem to have a member named JSONDecodeError.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
127
        click.secho('"{}" should be a valid json'.format(msg), fg='red')
128
    else:
129
        response = sns_publish(topic, msg)
130
        click.echo('Response: {}'.format(response))
131