From bb48a7e22f34bef976f68b9b34a8f92ff2db3384 Mon Sep 17 00:00:00 2001 From: meitingli Date: Tue, 15 Mar 2022 21:32:31 +0800 Subject: [PATCH 1/2] Update code logic --- hwcompatible/device.py | 48 ++++++++++----------- hwcompatible/document.py | 93 +++++++++++++++++++--------------------- 2 files changed, 65 insertions(+), 76 deletions(-) diff --git a/hwcompatible/device.py b/hwcompatible/device.py index f8ea301..0f5a682 100755 --- a/hwcompatible/device.py +++ b/hwcompatible/device.py @@ -38,6 +38,7 @@ class CertDevice: """ Certified device """ + def __init__(self): self.devices = None @@ -53,29 +54,26 @@ class CertDevice: properties = dict() while True: line = pipe.readline() - if line: - if line == "\n": - if len(properties) > 0: - device = Device(properties) - if device.path != "": - self.devices.append(device) - properties = dict() - else: - prop = line.split(":", 1) - if len(prop) == 2: - tp = prop[0].strip('\ \'\n') - attribute = prop[1].strip('\ \'\n') - if tp == "E": - keyvalue = attribute.split("=", 1) - if len(keyvalue) == 2: - properties[keyvalue[0]] = keyvalue[1] - elif tp == "P": - properties["INFO"] = attribute - else: + if not line: break + if line == "\n" and len(properties) > 0: + device = Device(properties) + if device.path != "": + self.devices.append(device) + properties = dict() + else: + prop = line.split(":", 1) + if len(prop) == 2: + tp = prop[0].strip('\ \'\n') + attribute = prop[1].strip('\ \'\n') + if tp == "E": + keyvalue = attribute.split("=", 1) + if len(keyvalue) == 2: + properties[keyvalue[0]] = keyvalue[1] + elif tp == "P": + properties["INFO"] = attribute except Exception as e: - print("Warning: get devices fail") - print(e) + print("Warning: get devices fail", e) self.devices.sort(key=lambda k: k.path) return self.devices @@ -84,6 +82,7 @@ class Device: """ get device properties """ + def __init__(self, properties=None): self.path = "" if properties: @@ -98,10 +97,7 @@ class Device: :param prop: :return: """ - try: - return self.properties[prop] - except KeyError: - return "" + return self.properties.get(prop, "") def get_name(self): """ @@ -114,4 +110,4 @@ class Device: return self.properties["DEVNAME"].split("/")[-1] if self.path: return self.path.split("/")[-1] - return "" + return "" \ No newline at end of file diff --git a/hwcompatible/document.py b/hwcompatible/document.py index fd24315..526b67a 100755 --- a/hwcompatible/document.py +++ b/hwcompatible/document.py @@ -22,6 +22,7 @@ from .sysinfo import SysInfo from .env import CertEnv from .constants import * + class Document(): """ Read and write documents @@ -41,8 +42,7 @@ class Document(): json.dump(self.document, save_f, indent=4) save_f.close() except Exception as concrete_error: - print("Error: doc save fail.") - print(concrete_error) + print("Error: doc save fail.", concrete_error) return False return True @@ -52,9 +52,9 @@ class Document(): with open(self.filename, "r") as load_f: self.document = json.load(load_f) load_f.close() - return True except Exception: return False + return True class CertDocument(Document): @@ -80,19 +80,17 @@ class CertDocument(Document): self.document = dict() while True: line = pipe.readline() - if line: - property_right = line.split(":", 1) - if len(property_right) != 2: - continue - key = property_right[0].strip() - value = property_right[1].strip() - if key in ["Manufacturer", "Product Name", "Version"]: - self.document[key] = value - else: + if not line: break + property_right = line.split(":", 1) + if len(property_right) != 2: + continue + key = property_right[0].strip() + value = property_right[1].strip() + if key in ["Manufacturer", "Product Name", "Version"]: + self.document[key] = value except Exception as concrete_error: - print("Error: get hardware info fail.") - print(concrete_error) + print("Error: get hardware info fail.", concrete_error) sysinfo = SysInfo(CertEnv.releasefile) self.document[OS] = sysinfo.product + " " + sysinfo.get_version() @@ -102,7 +100,7 @@ class CertDocument(Document): self.document[PRODUCTURL] = CommandUI().prompt( "Please provide your Product URL:") self.document[SERVER] = CommandUI().prompt("Please provide the Compatibility Test " - "Server (Hostname or Ipaddr):") + "Server (Hostname or Ipaddr):") def get_hardware(self): """ @@ -167,14 +165,14 @@ class FactoryDocument(Document): self.filename = filename if not factory: self.load() - else: - for member in factory: - element = dict() - element[NAME] = member[NAME] - element[DEVICE] = member[DEVICE].properties - element[RUN] = member[RUN] - element[STATUS] = member[STATUS] - self.document.append(element) + return + for member in factory: + element = dict() + element[NAME] = member[NAME] + element[DEVICE] = member[DEVICE].properties + element[RUN] = member[RUN] + element[STATUS] = member[STATUS] + self.document.append(element) def get_factory(self): """ @@ -222,12 +220,7 @@ class ConfigFile: """ Get parameter """ - if self.parameters: - try: - return self.parameters[name] - except KeyError: - pass - return None + return self.parameters.get(name, None) def dump(self): """ @@ -243,12 +236,12 @@ class ConfigFile: """ add parameter """ - if not self.getParameter(name): - self.parameters[name] = value - self.config.append("%s %s\n" % (name, value)) - self.save() - return True - return False + if self.get_parameter(name): + return False + self.parameters[name] = value + self.config.append("%s %s\n" % (name, value)) + self.save() + return True def remove_parameter(self, name): """ @@ -256,20 +249,21 @@ class ConfigFile: :param name: :return: """ - if self.getParameter(name): - del self.parameters[name] - newconfig = list() - for line in self.config: - if line.strip() and line.strip()[0] == "#": - newconfig.append(line) - continue - words = line.strip().split(" ") - if words and words[0] == name: - continue - else: - newconfig.append(line) - self.config = newconfig - self.save() + if not self.get_parameter(name): + return + del self.parameters[name] + newconfig = list() + for line in self.config: + if line.strip() and line.strip()[0] == "#": + newconfig.append(line) + continue + words = line.strip().split(" ") + if words and words[0] == name: + continue + else: + newconfig.append(line) + self.config = newconfig + self.save() def save(self): """ @@ -280,4 +274,3 @@ class ConfigFile: for line in self.config: fp_info.write(line) fp_info.close() - -- Gitee From 8bb0833ffa18b1e93ecc7e320930c339e20ae20e Mon Sep 17 00:00:00 2001 From: meitingli Date: Wed, 16 Mar 2022 17:21:32 +0800 Subject: [PATCH 2/2] update --- hwcompatible/constants.py | 3 +++ hwcompatible/device.py | 2 +- hwcompatible/document.py | 4 ++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/hwcompatible/constants.py b/hwcompatible/constants.py index 3fd4842..652314a 100644 --- a/hwcompatible/constants.py +++ b/hwcompatible/constants.py @@ -53,4 +53,7 @@ WIFI = "wifi" WLAN = "wlan" CDROM = "cdrom" IPMI = "ipmi" +FC="fc" +RAID="raid" +GPU="gpu" diff --git a/hwcompatible/device.py b/hwcompatible/device.py index 0f5a682..a39a8b9 100755 --- a/hwcompatible/device.py +++ b/hwcompatible/device.py @@ -73,7 +73,7 @@ class CertDevice: elif tp == "P": properties["INFO"] = attribute except Exception as e: - print("Warning: get devices fail", e) + print("Warning: get devices fail.\n", e) self.devices.sort(key=lambda k: k.path) return self.devices diff --git a/hwcompatible/document.py b/hwcompatible/document.py index 526b67a..79947da 100755 --- a/hwcompatible/document.py +++ b/hwcompatible/document.py @@ -42,7 +42,7 @@ class Document(): json.dump(self.document, save_f, indent=4) save_f.close() except Exception as concrete_error: - print("Error: doc save fail.", concrete_error) + print("Error: doc save fail.\n", concrete_error) return False return True @@ -90,7 +90,7 @@ class CertDocument(Document): if key in ["Manufacturer", "Product Name", "Version"]: self.document[key] = value except Exception as concrete_error: - print("Error: get hardware info fail.", concrete_error) + print("Error: get hardware info fail.\n", concrete_error) sysinfo = SysInfo(CertEnv.releasefile) self.document[OS] = sysinfo.product + " " + sysinfo.get_version() -- Gitee