From 73328468051b115e0cae9e023d6570cd87d992b1 Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Mon, 5 Feb 2018 16:32:26 +0100 Subject: [PATCH] Turned DevListMixin into namespaced methods. --- tclcheck_allfull.py | 3 +- tclcheck_allota.py | 3 +- tclcheck_findprd.py | 3 +- tclcheck_findprd2.py | 3 +- tcllib/__init__.py | 1 - tcllib/devlist.py | 98 +++++++++++++++++++++----------------------- 6 files changed, 55 insertions(+), 56 deletions(-) diff --git a/tclcheck_allfull.py b/tclcheck_allfull.py index e8f5301..eb01dc7 100644 --- a/tclcheck_allfull.py +++ b/tclcheck_allfull.py @@ -12,6 +12,7 @@ from requests.exceptions import RequestException import tcllib import tcllib.argparser from tcllib import ansi +from tcllib import devlist fc = tcllib.FotaCheck() @@ -34,7 +35,7 @@ fc.cltp = fc.CLTP.DESKTOP prdcheck = "" if args.tocheck is None else args.tocheck print("Loading list of devices.") -prds = tcllib.FotaCheck.get_devicelist() +prds = devlist.get_devicelist() print("List of latest FULL firmware by PRD:") diff --git a/tclcheck_allota.py b/tclcheck_allota.py index ee3cac6..0d8c2b3 100644 --- a/tclcheck_allota.py +++ b/tclcheck_allota.py @@ -12,6 +12,7 @@ from requests.exceptions import RequestException import tcllib import tcllib.argparser from tcllib import ansi +from tcllib import devlist fc = tcllib.FotaCheck() @@ -37,7 +38,7 @@ else: prdcheck = "" if args.tocheck is None else args.tocheck print("Loading list of devices.") -prds = tcllib.FotaCheck.get_devicelist() +prds = devlist.get_devicelist() print("List of latest OTA firmware{} by PRD:".format(force_ver_text)) diff --git a/tclcheck_findprd.py b/tclcheck_findprd.py index 0085e25..02126e3 100755 --- a/tclcheck_findprd.py +++ b/tclcheck_findprd.py @@ -13,6 +13,7 @@ from requests.exceptions import RequestException, Timeout import tcllib import tcllib.argparser from tcllib import ansi +from tcllib import devlist fc = tcllib.FotaCheck() @@ -41,7 +42,7 @@ if ceiling < floor: raise SystemExit print("Loading list of devices...", end="", flush=True) -prd_db = tcllib.FotaCheck.get_devicelist() +prd_db = devlist.get_devicelist() print(" OK") print("Valid PRDs not already in database:") diff --git a/tclcheck_findprd2.py b/tclcheck_findprd2.py index aa153a2..9844c59 100644 --- a/tclcheck_findprd2.py +++ b/tclcheck_findprd2.py @@ -12,6 +12,7 @@ from requests.exceptions import RequestException, Timeout import tcllib import tcllib.argparser from tcllib import ansi +from tcllib import devlist # Variants to scan for @@ -42,7 +43,7 @@ if ceiling < floor: raise SystemExit print("Loading list of known devices...", end="", flush=True) -prd_db = tcllib.FotaCheck.get_devicelist() +prd_db = devlist.get_devicelist() print(" OK") print("Valid PRDs not already in database:") diff --git a/tcllib/__init__.py b/tcllib/__init__.py index 709facf..a5f83ec 100644 --- a/tcllib/__init__.py +++ b/tcllib/__init__.py @@ -25,7 +25,6 @@ class FotaCheck( tclencheader.TclEncHeaderMixin, servervote.ServerVoteMixin, credentials.CredentialsMixin, - devlist.DevListMixin, dumpmgr.DumpMgrMixin ): """Main API handler class.""" diff --git a/tcllib/devlist.py b/tcllib/devlist.py index f3fdcac..4e04457 100644 --- a/tcllib/devlist.py +++ b/tcllib/devlist.py @@ -19,59 +19,55 @@ DEVICELIST_FILE = "prds.json" DEVICELIST_CACHE_SECONDS = 86400 -class DevListMixin: - """A mixin component for device list management.""" - @staticmethod - def get_devicelist(force=False, output_diff=True): - """Return device list from saved database.""" - need_download = True - - old_prds = None - try: - filestat = os.stat(DEVICELIST_FILE) - filemtime = filestat.st_mtime - if filemtime > time.time() - DEVICELIST_CACHE_SECONDS: - need_download = False - with open(DEVICELIST_FILE, "rt") as dlfile: - old_prds = json.load(dlfile) - except FileNotFoundError: - pass - - if need_download or force: - prds_json = requests.get(DEVICELIST_URL).text - with open(DEVICELIST_FILE, "wt") as dlfile: - dlfile.write(prds_json) +def get_devicelist(force=False, output_diff=True): + """Return device list from saved database.""" + need_download = True + old_prds = None + try: + filestat = os.stat(DEVICELIST_FILE) + filemtime = filestat.st_mtime + if filemtime > time.time() - DEVICELIST_CACHE_SECONDS: + need_download = False with open(DEVICELIST_FILE, "rt") as dlfile: - prds = json.load(dlfile) + old_prds = json.load(dlfile) + except FileNotFoundError: + pass - if old_prds and output_diff: - DevListMixin.print_prd_diff(old_prds, prds) + if need_download or force: + prds_json = requests.get(DEVICELIST_URL).text + with open(DEVICELIST_FILE, "wt") as dlfile: + dlfile.write(prds_json) - return prds + with open(DEVICELIST_FILE, "rt") as dlfile: + prds = json.load(dlfile) - @staticmethod - def print_prd_diff(old_prds, new_prds): - """Print changes between old and new databases.""" - added_prds = [prd for prd in new_prds if prd not in old_prds] - removed_prds = [prd for prd in old_prds if prd not in new_prds] - for prd in removed_prds: - print("> Removed device {} (was at {} / OTA: {}).".format(ansi.RED + prd + ansi.RESET, old_prds[prd]["last_full"], old_prds[prd]["last_ota"])) - for prd in added_prds: - print("> New device {} ({} / OTA: {}).".format(ansi.GREEN + prd + ansi.RESET, new_prds[prd]["last_full"], new_prds[prd]["last_ota"])) - for prd, pdata in new_prds.items(): - if prd in added_prds: - continue - odata = old_prds[prd] - if pdata["last_full"] != odata["last_full"] and pdata["last_ota"] != odata["last_ota"]: - print("> {}: {} ⇨ {} (OTA: {} ⇨ {})".format( - prd, - ansi.CYAN_DARK + str(odata["last_full"]) + ansi.RESET, - ansi.CYAN + str(pdata["last_full"]) + ansi.RESET, - ansi.YELLOW_DARK + str(odata["last_ota"]) + ansi.RESET, - ansi.YELLOW + str(pdata["last_ota"]) + ansi.RESET - )) - elif pdata["last_full"] != odata["last_full"]: - print("> {}: {} ⇨ {} (FULL)".format(prd, ansi.CYAN_DARK + str(odata["last_full"]) + ansi.RESET, ansi.CYAN + str(pdata["last_full"]) + ansi.RESET)) - elif pdata["last_ota"] != odata["last_ota"]: - print("> {}: {} ⇨ {} (OTA)".format(prd, ansi.YELLOW_DARK + str(odata["last_ota"]) + ansi.RESET, ansi.YELLOW + str(pdata["last_ota"]) + ansi.RESET)) + if old_prds and output_diff: + print_prd_diff(old_prds, prds) + + return prds + +def print_prd_diff(old_prds, new_prds): + """Print changes between old and new databases.""" + added_prds = [prd for prd in new_prds if prd not in old_prds] + removed_prds = [prd for prd in old_prds if prd not in new_prds] + for prd in removed_prds: + print("> Removed device {} (was at {} / OTA: {}).".format(ansi.RED + prd + ansi.RESET, old_prds[prd]["last_full"], old_prds[prd]["last_ota"])) + for prd in added_prds: + print("> New device {} ({} / OTA: {}).".format(ansi.GREEN + prd + ansi.RESET, new_prds[prd]["last_full"], new_prds[prd]["last_ota"])) + for prd, pdata in new_prds.items(): + if prd in added_prds: + continue + odata = old_prds[prd] + if pdata["last_full"] != odata["last_full"] and pdata["last_ota"] != odata["last_ota"]: + print("> {}: {} ⇨ {} (OTA: {} ⇨ {})".format( + prd, + ansi.CYAN_DARK + str(odata["last_full"]) + ansi.RESET, + ansi.CYAN + str(pdata["last_full"]) + ansi.RESET, + ansi.YELLOW_DARK + str(odata["last_ota"]) + ansi.RESET, + ansi.YELLOW + str(pdata["last_ota"]) + ansi.RESET + )) + elif pdata["last_full"] != odata["last_full"]: + print("> {}: {} ⇨ {} (FULL)".format(prd, ansi.CYAN_DARK + str(odata["last_full"]) + ansi.RESET, ansi.CYAN + str(pdata["last_full"]) + ansi.RESET)) + elif pdata["last_ota"] != odata["last_ota"]: + print("> {}: {} ⇨ {} (OTA)".format(prd, ansi.YELLOW_DARK + str(odata["last_ota"]) + ansi.RESET, ansi.YELLOW + str(pdata["last_ota"]) + ansi.RESET))