From 5c8b73c81fea42a2462b6133b7696495f27fcbd7 Mon Sep 17 00:00:00 2001 From: Sourcery AI Date: Wed, 10 Jun 2020 13:11:22 +0000 Subject: [PATCH] Refactored by Sourcery --- adapter.py | 20 ++++++------ dnsmasqRange.py | 11 +++---- hostapd.py | 25 ++++++-------- interfacesReader.py | 79 +++++++++++++++++++++------------------------ 4 files changed, 59 insertions(+), 76 deletions(-) diff --git a/adapter.py b/adapter.py index ee3d924..0a4598f 100644 --- a/adapter.py +++ b/adapter.py @@ -48,9 +48,8 @@ def validateOne(self, opt, validations, val): else: if not isinstance(val, validations['type']): raise ValueError("{} should be {}".format(opt, str(validations['type']))) - if 'in' in validations: - if not val in validations['in']: - raise ValueError("{} should be in {}".format(opt, ", ".join(validations['in']))) + if 'in' in validations and val not in validations['in']: + raise ValueError("{} should be in {}".format(opt, ", ".join(validations['in']))) def validateIP(self, ip): ''' @@ -183,7 +182,7 @@ def appendPostDown(self, cmd): def setUnknown(self, key, val): ''' it's impossible to know about all available options, so storing uncommon ones as if ''' - if not 'unknown' in self._ifAttributes: + if 'unknown' not in self._ifAttributes: self._ifAttributes['unknown'] = {} self._ifAttributes['unknown'][key] = val @@ -225,12 +224,13 @@ def __init__(self, options=None): def reset(self): ''' Initialize attribute storage structre. ''' - self._ifAttributes = {} - self._ifAttributes['bridge-opts'] = {} - self._ifAttributes['up'] = [] - self._ifAttributes['down'] = [] - self._ifAttributes['pre-up'] = [] - self._ifAttributes['post-down'] = [] + self._ifAttributes = { + 'bridge-opts': {}, + 'up': [], + 'down': [], + 'pre-up': [], + 'post-down': [], + } def set_options(self, options): ''' raise ValueError or socket.error on issue ''' diff --git a/dnsmasqRange.py b/dnsmasqRange.py index 7de1f12..3a288bd 100644 --- a/dnsmasqRange.py +++ b/dnsmasqRange.py @@ -12,10 +12,7 @@ class DnsmasqRange(object): def __init__(self, path, backup_path=None): self._config = {} self._path = path - if not backup_path: - self.backup_path = path + ".bak" - else: - self.backup_path = backup_path + self.backup_path = path + ".bak" if not backup_path else backup_path @property def config(self): @@ -23,7 +20,7 @@ def config(self): def set(self, key, value): if key == "dhcp-range": - if not "dhcp-range" in self._config: + if "dhcp-range" not in self._config: self._config["dhcp-range"] = [] if isinstance(value, str): value = self._extract_range_info(value) @@ -36,7 +33,7 @@ def validate(self): for r in self._config["dhcp-range"]: required = ["interface", "start", "end", "lease_time"] for k in required: - if not k in r: + if k not in r: raise ValueError("Missing option : {}".format(k)) socket.inet_aton(r["start"]) socket.inet_aton(r["end"]) @@ -47,7 +44,7 @@ def validate(self): def get_itf_range(self, if_name): ''' If no if, return None ''' - if not "dhcp-range" in self._config: + if "dhcp-range" not in self._config: return None for v in self._config['dhcp-range']: if v["interface"] == if_name: diff --git a/hostapd.py b/hostapd.py index b9c5fa6..744b723 100644 --- a/hostapd.py +++ b/hostapd.py @@ -7,10 +7,7 @@ class Hostapd(object): def __init__(self, path, backup_path=None): self._config = {} self._path = path - if not backup_path: - self.backup_path = path + ".bak" - else: - self.backup_path = backup_path + self.backup_path = path + ".bak" if not backup_path else backup_path @property def config(self): @@ -36,8 +33,6 @@ def validate(self): basic = ['interface', 'driver'] bridge = ['bridge'] wireless = ['ssid', 'channel', 'hw_mode'] - auth = ['wpa', 'wpa_passphrase', 'wpa_key_mgmt'] - for k in basic: if self._config[k] is None: raise ValueError("Missing required {} option".format(k)) @@ -55,17 +50,17 @@ def validate(self): if 'wpa' in self._config: self._config['wpa'] = int(self._config['wpa']) - if not self._config['wpa'] in [1, 2, 3]: + if self._config['wpa'] not in [1, 2, 3]: raise ValueError("Wpa option is not valid") + auth = ['wpa', 'wpa_passphrase', 'wpa_key_mgmt'] + for k in auth: if self._config[k] is None: raise ValueError("Missing required {} option for wireless security".format(k)) - if self._config['wpa'] in [1, 3]: - if not self._config['wpa_pairwise']: - raise ValueError("Missing required option for wireless security : wpa_pairwise") - if self._config['wpa'] in [2, 3]: - if not self._config['rsn_pairwise']: - raise ValueError("Missing required option for wireless security rsn_pairwise") + if self._config['wpa'] in [1, 3] and not self._config['wpa_pairwise']: + raise ValueError("Missing required option for wireless security : wpa_pairwise") + if self._config['wpa'] in [2, 3] and not self._config['rsn_pairwise']: + raise ValueError("Missing required option for wireless security rsn_pairwise") def set_defaults(self): ''' Defaults for my needs, you should probably override this one ''' @@ -108,9 +103,7 @@ def read(self, path=None): with open(path, "r") as hostapd: for line in hostapd: - if line.startswith('#') is True: - pass - else: + if line.startswith('#') is not True: param, value = line.split("=") if param and value: self.set(param, value) diff --git a/interfacesReader.py b/interfacesReader.py index f016376..de3a3f0 100644 --- a/interfacesReader.py +++ b/interfacesReader.py @@ -56,14 +56,10 @@ def _read_lines_from_file(self, fileObj): for line in fileObj: # Identify the clauses by analyzing the first word of each line. # Go to the next line if the current line is a comment. - if line.strip().startswith("#") is True: - pass - else: + if line.strip().startswith("#") is not True: self._parse_iface(line) # Ignore blank lines. - if line.isspace() is True: - pass - else: + if line.isspace() is not True: self._parse_details(line) self._read_auto(line) self._read_hotplug(line) @@ -78,46 +74,45 @@ def _parse_iface(self, line): self._adapters[self._context].setAddrFam(sline[2]) def _parse_details(self, line): - if line[0].isspace() is True: - sline = line.split() - if sline[0] == 'address': - self._adapters[self._context].setAddress(sline[1]) - elif sline[0] == 'netmask': - self._adapters[self._context].setNetmask(sline[1]) - elif sline[0] == 'gateway': - self._adapters[self._context].setGateway(sline[1]) - elif sline[0] == 'broadcast': - self._adapters[self._context].setBroadcast(sline[1]) - elif sline[0] == 'network': - self._adapters[self._context].setNetwork(sline[1]) - elif sline[0].startswith('bridge') is True: - opt = sline[0].split('_') - sline.pop(0) - ifs = " ".join(sline) - self._adapters[self._context].replaceBropt(opt[1], ifs) - elif sline[0] == 'up' or sline[0] == 'down' or sline[0] == 'pre-up' or sline[0] == 'post-down': - ud = sline.pop(0) - cmd = ' '.join(sline) - if ud == 'up': - self._adapters[self._context].appendUp(cmd) - elif ud == 'down': - self._adapters[self._context].appendDown(cmd) - elif ud == 'pre-up': - self._adapters[self._context].appendPreUp(cmd) - elif ud == 'post-down': - self._adapters[self._context].appendPostDown(cmd) - else: - # store as if so as not to loose it - self._adapters[self._context].setUnknown(sline[0], sline[1]) + if line[0].isspace() is not True: + return + sline = line.split() + if sline[0] == 'address': + self._adapters[self._context].setAddress(sline[1]) + elif sline[0] == 'netmask': + self._adapters[self._context].setNetmask(sline[1]) + elif sline[0] == 'gateway': + self._adapters[self._context].setGateway(sline[1]) + elif sline[0] == 'broadcast': + self._adapters[self._context].setBroadcast(sline[1]) + elif sline[0] == 'network': + self._adapters[self._context].setNetwork(sline[1]) + elif sline[0].startswith('bridge') is True: + opt = sline[0].split('_') + sline.pop(0) + ifs = " ".join(sline) + self._adapters[self._context].replaceBropt(opt[1], ifs) + elif sline[0] in ['up', 'down', 'pre-up', 'post-down']: + ud = sline.pop(0) + cmd = ' '.join(sline) + if ud == 'up': + self._adapters[self._context].appendUp(cmd) + elif ud == 'down': + self._adapters[self._context].appendDown(cmd) + elif ud == 'pre-up': + self._adapters[self._context].appendPreUp(cmd) + elif ud == 'post-down': + self._adapters[self._context].appendPostDown(cmd) + else: + # store as if so as not to loose it + self._adapters[self._context].setUnknown(sline[0], sline[1]) def _read_auto(self, line): ''' Identify which adapters are flagged auto. ''' if line.startswith('auto'): sline = line.split() for word in sline: - if word == 'auto': - pass - else: + if word != 'auto': self._auto_list.append(word) def _read_hotplug(self, line): @@ -125,9 +120,7 @@ def _read_hotplug(self, line): if line.startswith('allow-hotplug'): sline = line.split() for word in sline: - if word == 'allow-hotplug': - pass - else: + if word != 'allow-hotplug': self._hotplug_list.append(word) def _reset(self):