Total Complexity | 23 |
Total Lines | 66 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | # -*- coding: utf-8 -*- |
||
|
|||
2 | |||
3 | import json |
||
4 | import os |
||
5 | import requests |
||
6 | import time |
||
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: |
||
14 | @staticmethod |
||
15 | def get_devicelist(force=False, output_diff=True): |
||
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: |
||
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: |
||
32 | df.write(prds_json) |
||
33 | |||
34 | with open(DEVICELIST_FILE, "rt") as df: |
||
35 | prds = json.load(df) |
||
36 | |||
37 | if old_prds and output_diff: |
||
38 | DevList.print_prd_diff(old_prds, prds) |
||
39 | |||
40 | return prds |
||
41 | |||
42 | @staticmethod |
||
43 | def print_prd_diff(old_prds, new_prds): |
||
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
|
|||
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
|
|||
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
|
|||
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
|
|||
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
|
|||
66 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.