Passed
Push — master ( 2f86f7...74d48b )
by Markus
01:48
created

tcllib.devlist.DevList.print_prd_diff()   F

Complexity

Conditions 13

Size

Total Lines 24
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 23
nop 2
dl 0
loc 24
rs 2.8487
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like tcllib.devlist.DevList.print_prd_diff() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# -*- coding: utf-8 -*-
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
3
import json
4
import os
5
import requests
6
import time
0 ignored issues
show
introduced by
standard import "import time" should be placed before "import requests"
Loading history...
7
from . import ansi
8
9
DEVICELIST_URL = "https://tclota.birth-online.de/json_lastupdates.php"
10
DEVICELIST_FILE = "prds.json"
11
DEVICELIST_CACHE_SECONDS = 86400
12
13
class DevListMixin:
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
14
    @staticmethod
15
    def get_devicelist(force=False, output_diff=True):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
16
        need_download = True
17
18
        old_prds = None
19
        try:
20
            filestat = os.stat(DEVICELIST_FILE)
21
            filemtime = filestat.st_mtime
22
            if filemtime > time.time() - DEVICELIST_CACHE_SECONDS:
23
                need_download = False
24
            with open(DEVICELIST_FILE, "rt") as df:
0 ignored issues
show
Coding Style Naming introduced by
The name df does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[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...
25
                old_prds = json.load(df)
26
        except FileNotFoundError:
27
            pass
28
29
        if need_download or force:
30
            prds_json = requests.get(DEVICELIST_URL).text
31
            with open(DEVICELIST_FILE, "wt") as df:
0 ignored issues
show
Coding Style Naming introduced by
The name df does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[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...
32
                df.write(prds_json)
33
34
        with open(DEVICELIST_FILE, "rt") as df:
0 ignored issues
show
Coding Style Naming introduced by
The name df does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[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...
35
            prds = json.load(df)
36
37
        if old_prds and output_diff:
38
            DevList.print_prd_diff(old_prds, prds)
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'DevList'
Loading history...
Comprehensibility Best Practice introduced by
The variable DevList does not seem to be defined.
Loading history...
39
40
        return prds
41
42
    @staticmethod
43
    def print_prd_diff(old_prds, new_prds):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
44
        added_prds = [prd for prd in new_prds if prd not in old_prds]
45
        removed_prds = [prd for prd in old_prds if prd not in new_prds]
46
        for prd in removed_prds:
47
            print("> Removed device {} (was at {} / OTA: {}).".format(ansi.RED + prd + ansi.RESET, old_prds[prd]["last_full"], old_prds[prd]["last_ota"]))
1 ignored issue
show
Coding Style introduced by
This line is too long as per the coding-style (154/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
48
        for prd in added_prds:
49
            print("> New device {} ({} / OTA: {}).".format(ansi.GREEN + prd + ansi.RESET, new_prds[prd]["last_full"], new_prds[prd]["last_ota"]))
1 ignored issue
show
Coding Style introduced by
This line is too long as per the coding-style (145/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
50
        for prd, pdata in new_prds.items():
51
            if prd in added_prds:
52
                continue
53
            odata = old_prds[prd]
54
            if pdata["last_full"] != odata["last_full"] and pdata["last_ota"] != odata["last_ota"]:
1 ignored issue
show
Coding Style introduced by
This line is too long as per the coding-style (99/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
55
                print("> {}: {} ⇨ {} (OTA: {} ⇨ {})".format(
56
                    prd,
57
                    ansi.CYAN_DARK + str(odata["last_full"]) + ansi.RESET,
58
                    ansi.CYAN + str(pdata["last_full"]) + ansi.RESET,
59
                    ansi.YELLOW_DARK + str(odata["last_ota"]) + ansi.RESET,
60
                    ansi.YELLOW + str(pdata["last_ota"]) + ansi.RESET
61
                ))
62
            elif pdata["last_full"] != odata["last_full"]:
63
                print("> {}: {} ⇨ {} (FULL)".format(prd, ansi.CYAN_DARK + str(odata["last_full"]) + ansi.RESET, ansi.CYAN + str(pdata["last_full"]) + ansi.RESET))
1 ignored issue
show
Coding Style introduced by
This line is too long as per the coding-style (162/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
64
            elif pdata["last_ota"] != odata["last_ota"]:
65
                print("> {}: {} ⇨ {} (OTA)".format(prd, ansi.YELLOW_DARK + str(odata["last_ota"]) + ansi.RESET, ansi.YELLOW + str(pdata["last_ota"]) + ansi.RESET))
1 ignored issue
show
Coding Style introduced by
This line is too long as per the coding-style (163/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
66