From 69315d8fe26545c8dc609f817b50b300a560dbce Mon Sep 17 00:00:00 2001 From: thurask Date: Sat, 14 Oct 2017 19:51:38 -0400 Subject: [PATCH] replace numeric constants with enums --- tclcheck.py | 10 +++++----- tclcheck_allfull.py | 8 ++++---- tclcheck_allota.py | 4 ++-- tclcheck_findprd.py | 8 ++++---- tclcheck_findver.py | 2 +- tcllib.py | 32 +++++++++++++++++--------------- 6 files changed, 33 insertions(+), 31 deletions(-) diff --git a/tclcheck.py b/tclcheck.py index 20368fb..b729ebd 100755 --- a/tclcheck.py +++ b/tclcheck.py @@ -8,7 +8,7 @@ import sys import tcllib fc = tcllib.FotaCheck() -fc.cltp = 10 +fc.cltp = fc.CLTP.MOBILE fc.serid = "3531510" #fc.osvs = "7.1.1" @@ -20,16 +20,16 @@ args = dp.parse_args(sys.argv[1:]) if len(sys.argv) == 3: # python tclcheck.py $PRD $FV = OTA delta for $PRD from $FV fc.curef = args.prd fc.fv = args.fvver - fc.mode = fc.MODE_OTA + fc.mode = fc.MODE.OTA elif len(sys.argv) == 2: # python tclcheck.py $PRD = FULL for $PRD fc.curef = args.prd fc.fv = "AAA000" - fc.mode = fc.MODE_FULL - fc.cltp = 2010 + fc.mode = fc.MODE.FULL + fc.cltp = fc.CLTP.DESKTOP else: # python tclcheck.py = OTA for default PRD, FV fc.curef = "PRD-63117-011" fc.fv = "AAM481" - fc.mode = fc.MODE_OTA + fc.mode = fc.MODE.OTA check_xml = fc.do_check() print(fc.pretty_xml(check_xml)) diff --git a/tclcheck_allfull.py b/tclcheck_allfull.py index 61b4289..65de717 100755 --- a/tclcheck_allfull.py +++ b/tclcheck_allfull.py @@ -11,11 +11,11 @@ tcllib.make_escapes_work() fc = tcllib.FotaCheck() fc.serid = "3531510" fc.fv = "AAA000" -fc.mode = fc.MODE_FULL +fc.mode = fc.MODE.FULL -# CLTP = 10 (only show actual updates or HTTP 206) / 2010 (always show latest version for MODE_FULL) -#fc.cltp = 10 -fc.cltp = 2010 +# CLTP = 10 (only show actual updates or HTTP 206) / 2010 (always show latest version for MODE.FULL) +#fc.cltp = fc.CLTP.MOBILE +fc.cltp = fc.CLTP.DESKTOP print("List of latest FULL firmware by PRD:") diff --git a/tclcheck_allota.py b/tclcheck_allota.py index 6c712ae..1fbf1cb 100755 --- a/tclcheck_allota.py +++ b/tclcheck_allota.py @@ -12,8 +12,8 @@ tcllib.make_escapes_work() fc = tcllib.FotaCheck() fc.serid = "3531510" #fc.osvs = "7.1.1" -fc.mode = fc.MODE_OTA -fc.cltp = 10 +fc.mode = fc.MODE.OTA +fc.cltp = fc.CLTP.MOBILE dp = tcllib.DefaultParser(__file__) dp.add_argument("forcever", nargs="?", default=None) diff --git a/tclcheck_findprd.py b/tclcheck_findprd.py index 3904e66..3f76fed 100644 --- a/tclcheck_findprd.py +++ b/tclcheck_findprd.py @@ -13,11 +13,11 @@ tcllib.make_escapes_work() fc = tcllib.FotaCheck() fc.serid = "3531510" fc.fv = "AAA000" -fc.mode = fc.MODE_FULL +fc.mode = fc.MODE.FULL -# CLTP = 10 (only show actual updates or HTTP 206) / 2010 (always show latest version for MODE_FULL) -#fc.cltp = 10 -fc.cltp = 2010 +# CLTP = 10 (only show actual updates or HTTP 206) / 2010 (always show latest version for MODE.FULL) +#fc.cltp = fc.CLTP.MOBILE +fc.cltp = fc.CLTP.DESKTOP dp = tcllib.DefaultParser(__file__) dp.add_argument("tocheck", nargs="?", default=None) diff --git a/tclcheck_findver.py b/tclcheck_findver.py index 0c579d2..f2f2f5b 100644 --- a/tclcheck_findver.py +++ b/tclcheck_findver.py @@ -11,7 +11,7 @@ tcllib.make_escapes_work() fc = tcllib.FotaCheck() fc.serid = "3531510" -fc.mode = fc.MODE_OTA +fc.mode = fc.MODE.OTA dp = tcllib.DefaultParser(__file__) dp.add_argument("prd") diff --git a/tcllib.py b/tcllib.py index 66c547d..5558b5d 100644 --- a/tcllib.py +++ b/tcllib.py @@ -5,6 +5,7 @@ import argparse import base64 import binascii +import enum import hashlib import platform import random @@ -36,6 +37,10 @@ def make_escapes_work(): colorama.init() +def default_enum(enumname, vardict): + return enum.IntEnum(enumname, vardict, module=__name__, qualname="tcllib.FotaCheck.{}".format(enumname)) + + class DefaultParser(argparse.ArgumentParser): def __init__(self, appname): super().__init__(prog=appname.replace(".py", ""), epilog="https://github.com/mbirth/tcl_ota_check") @@ -43,26 +48,23 @@ class DefaultParser(argparse.ArgumentParser): class FotaCheck: VDKEY = b"eJwdjwEOwDAIAr8kKFr//7HhmqXp8AIIDrYAgg8byiUXrwRJRXja+d6iNxu0AhUooDCN9rd6rDLxmGIakUVWo3IGCTRWqCAt6X4jGEIUAxgN0eYWnp+LkpHQAg/PsO90ELsy0Npm/n2HbtPndFgGEV31R9OmT4O4nrddjc3Qt6nWscx7e+WRHq5UnOudtjw5skuV09pFhvmqnOEIs4ljPeel1wfLYUF4\n" - CKTP_CHECKAUTO = 1 - CKTP_CHECKMANUAL = 2 - MODE_OTA = 2 - MODE_FULL = 4 - RTD_ROOTED = 2 - RTD_UNROOTED = 1 - CHNL_WIFI = 2 - CHNL_3G = 1 + CKTP = default_enum("CKTP", ["AUTO", "MANUAL"]) + MODE = default_enum("MODE", {"OTA": 2, "FULL": 4}) + RTD = default_enum("RTD", ["UNROOTED", "ROOTED"]) + CHNL = default_enum("CHNL", ["3G", "WIFI"]) + CLTP = default_enum("CLTP", {"MOBILE": 10, "DESKTOP": 2010}) def __init__(self): self.serid = "543212345000000" self.curef = "PRD-63117-011" self.fv = "AAM481" self.osvs = "7.1.1" - self.mode = self.MODE_FULL + self.mode = self.MODE.FULL self.ftype = "Firmware" - self.cltp = 10 - self.cktp = self.CKTP_CHECKMANUAL - self.rtd = self.RTD_UNROOTED - self.chnl = self.CHNL_WIFI + self.cltp = self.CLTP.MOBILE + self.cktp = self.CKTP.MANUAL + self.rtd = self.RTD.UNROOTED + self.chnl = self.CHNL.WIFI self.g2master = None self.master_servers = [ "g2master-us-east.tclclouds.com", @@ -80,7 +82,7 @@ class FotaCheck: def reset_session(self): self.g2master = self.get_master_server() self.sess = requests.Session() - if self.mode == self.MODE_FULL: + if self.mode == self.MODE.FULL: self.sess.headers.update({"User-Agent": "com.tcl.fota/5.1.0.2.0029.0, Android"}) else: self.sess.headers.update({"User-Agent": "tcl"}) @@ -249,7 +251,7 @@ class FotaCheck: params["cltp"] = self.cltp params["cktp"] = self.cktp params["rtd"] = self.rtd - if self.mode == self.MODE_FULL: + if self.mode == self.MODE.FULL: params["foot"] = 1 params["chnl"] = self.chnl