Completed
Push — develop ( b34257...4c6de9 )
by Jace
12s
created

call()   B

Complexity

Conditions 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.0466

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
c 3
b 0
f 0
dl 0
loc 22
ccs 12
cts 14
cp 0.8571
crap 4.0466
rs 8.9197
1
"""Update project metrics on The Coverage Space.
2
3
Usage:
4
  coverage.space <owner/repo> <metric> [<value>] [--exit-code]
5
  coverage.space (-h | --help)
6
  coverage.space (-V | --version)
7
8
Options:
9
  -h --help         Show this help screen.
10
  -V --version      Show the program version.
11
  --exit-code       Return non-zero exit code on failures.
12
13
"""
14
15 1
from __future__ import unicode_literals
16
17 1
import sys
18 1
import json
19
20 1
import six
21 1
from docopt import docopt
0 ignored issues
show
Configuration introduced by Jace Browning
The import docopt 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...
22 1
import requests
23 1
import colorama
24 1
from backports.shutil_get_terminal_size import get_terminal_size
0 ignored issues
show
Configuration introduced by Jace Browning
The import backports.shutil_get_terminal_size 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...
25
26 1
from . import API, VERSION
27
28 1
from .plugins import get_coverage
29 1
from .cache import Cache
30
31
32 1
cache = Cache()
33
34
35 1
def main():
36
    """Run the program."""
37 1
    colorama.init(autoreset=True)
38 1
    arguments = docopt(__doc__, version=VERSION)
39
40 1
    slug = arguments['<owner/repo>']
41 1
    metric = arguments['<metric>']
42 1
    value = arguments.get('<value>') or get_coverage()
43
44 1
    success = call(slug, metric, value)
45
46 1
    if not success and arguments['--exit-code']:
47 1
        sys.exit(1)
48
49
50 1
def call(slug, metric, value):
51
    """Call the API and display errors."""
52 1
    url = "{}/{}".format(API, slug)
53 1
    data = {metric: value}
54
55 1
    response = cache.get(url, data)
56 1
    if response is None:
57 1
        response = requests.put(url, data=data)
58 1
        cache.set(url, data, response)
59
60 1
    if response.status_code == 200:
61 1
        return True
62
63 1
    elif response.status_code == 422:
64 1
        display("coverage decreased", response.json(),
65
                colorama.Fore.YELLOW + colorama.Style.BRIGHT)
0 ignored issues
show
Bug introduced by Jace Browning
The Instance of AnsiCodes does not seem to have a member named YELLOW.

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...
Bug introduced by Jace Browning
The Instance of AnsiCodes does not seem to have a member named BRIGHT.

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...
66 1
        return False
67
68
    else:
69
        display("coverage unknown", response.json(),
70
                colorama.Fore.RED + colorama.Style.BRIGHT)
0 ignored issues
show
Bug introduced by Jace Browning
The Instance of AnsiCodes does not seem to have a member named RED.

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...
Bug introduced by Jace Browning
The Instance of AnsiCodes does not seem to have a member named BRIGHT.

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...
71
        return False
72
73
74 1
def display(title, data, color=""):
75
    """Write colored text to the console."""
76 1
    width, _ = get_terminal_size()
77 1
    six.print_(color + "{0:=^{1}}".format(' ' + title + ' ', width))
78 1
    six.print_(color + json.dumps(data, indent=4))
79
    six.print_(color + '=' * width)
80