[Git][NTPsec/ntpsec][ntp.util-codacy] Codacy Python codestyle: solve some issues with the worst 3

James Browning gitlab at mg.gitlab.com
Tue Oct 13 17:13:53 UTC 2020



James Browning pushed to branch ntp.util-codacy at NTPsec / ntpsec


Commits:
37009134 by James Browning at 2020-10-13T10:13:03-07:00
Codacy Python codestyle: solve some issues with the worst 3

- - - - -


3 changed files:

- ntpclients/ntpsweep.py
- ntpclients/ntptrace.py
- ntpclients/ntpwait.py


Changes:

=====================================
ntpclients/ntpsweep.py
=====================================
@@ -40,12 +40,12 @@ except ImportError as e:
     sys.exit(1)
 
 
-def ntp_peers(host):
+def ntp_peers(server):
     """Return: a list of peer IP addrs for a specified host,
             empty list if query failed.
     """
     try:
-        with os.popen("ntpq -npw " + host) as rp:
+        with os.popen("ntpq -npw " + server) as rp:
             hostlines = rp.readlines()[2:]      # Drop display header
             # Strip tally mark from first field
             return [ln.split()[0][1:] for
@@ -118,7 +118,7 @@ def scan_host(host, level):
             printhost = printhost.replace(strip, "")
         # append number of peers in brackets if requested and valid
         if (recurse and (known_host_info[host] != " ?") and
-           (host in known_host_peers)):
+                (host in known_host_peers)):
             printhost += " (%d)" % len(known_host_peers[host])
         # Finally print complete host line
         print("%-32s %s" % (printhost[:32], known_host_info[host]))
@@ -139,7 +139,7 @@ def scan_host(host, level):
                     # Might cause problems in the future.  First
                     # part of the guard is an attempt to skip
                     # NTPsec-style clock IDs.
-                    if peer[0].isdigit() and not peer.startswith("127"):
+                    if peer[0].isdigit() and not peer.startswith("127.127."):
                         scan_host(peer, level + 1)
     else:   # We did not get answers from this host
         printhost = (' ' * level) + (ntp.util.canonicalize_dns(host) or host)
@@ -166,21 +166,21 @@ if __name__ == '__main__':
     recurse = False
     strip = ""
     for (switch, val) in options:
-        if switch == "-h" or switch == "--host":
+        if switch in ("-h", "--host"):
             hostlist = [val]
-        elif switch == "-l" or switch == "--host-list":
+        elif switch in ("-l", "--host-list"):
             hostlist = val.split(",")
-        elif switch == "-m" or switch == "--maxlevel":
+        elif switch in ("-m", "--maxlevel"):
             errmsg = "Error: -m parameter '%s' not a number\n"
             maxlevel = ntp.util.safeargcast(val, int, errmsg, __doc__)
-        elif switch == "-p" or switch == "--peers":
+        elif switch in ("-p", "--peers"):
             recurse = True
-        elif switch == "-s" or switch == "--strip":
+        elif switch in ("-s", "--strip"):
             strip = val
-        elif switch == "-?" or switch == "--help":
+        elif switch in ("-?", "--help"):
             print(__doc__, file=sys.stderr)
             raise SystemExit(0)
-        elif switch == "-V" or switch == "--version":
+        elif switch in ("-V", "--version"):
             print("ntpsweep %s" % ntp.util.stdversion())
             raise SystemExit(0)
 


=====================================
ntpclients/ntptrace.py
=====================================
@@ -29,32 +29,29 @@ except ImportError as e:
 
 
 def get_info(host):
-    info = ntp_read_vars(0, [], host)
-    if info is None or 'stratum' not in info:
-        return
+    info3 = ntp_read_vars(0, [], host)
+    if info3 is None or 'stratum' not in info3:
+        return Nonw
 
-    info['offset'] = round(float(info['offset']) / 1000, 6)
-    info['syncdistance'] = \
-        (float(info['rootdisp']) + (float(info['rootdelay']) / 2)) / 1000
+    info3['offset'] = round(float(info3['offset']) / 1000, 6)
+    info3['syncdistance'] = \
+        (float(info3['rootdisp']) + (float(info3['rootdelay']) / 2)) / 1000
 
-    return info
+    return info3
 
 
 def get_next_host(peer, host):
-    info = ntp_read_vars(peer, ["srcadr"], host)
-    if info is None:
-        return
-    return info['srcadr']
+    info2 = ntp_read_vars(peer, ["srcadr"], host)
+    if info2 is None:
+        return None
+    return info2['srcadr']
 
 
 def ntp_read_vars(peer, vars, host):
     obsolete = {'phase': 'offset',
                 'rootdispersion': 'rootdisp'}
 
-    if not vars:
-        do_all = True
-    else:
-        do_all = False
+    do_all = bool(not vars)
     outvars = {}.fromkeys(vars)
 
     if do_all:
@@ -95,8 +92,7 @@ def ntp_read_vars(peer, vars, host):
             key = match.group(1)
             val = match.group(2)
             val = re.sub(r'^"([^"]+)"$', r'\1', val)
-            if key in obsolete:
-                key = obsolete[key]
+            key = dict.get(obsolete, key, key)
             if do_all or key in outvars:
                 outvars[key] = val
 
@@ -134,18 +130,18 @@ numeric = False
 maxhosts = 99
 host = '127.0.0.1'
 
-for (switch, val) in options:
-    if switch == "-m" or switch == "--max-hosts":
+for (switch, value) in options:
+    if switch in ("-m", "--max-hosts"):
         errmsg = "Error: -m parameter '%s' not a number\n"
-        maxhosts = ntp.util.safeargcast(val, int, errmsg, usage)
-    elif switch == "-n" or switch == "--numeric":
+        maxhosts = ntp.util.safeargcast(value, int, errmsg, usage)
+    elif switch in ("-n", "--numeric"):
         numeric = True
-    elif switch == "-r" or switch == "--host":
-        host = val
-    elif switch == "-?" or switch == "--help" or switch == "--more-help":
+    elif switch in ("-r", "--host"):
+        host = value
+    elif switch in ("-?", "--help", "--more-help"):
         print(usage, file=sys.stderr)
         raise SystemExit(0)
-    elif switch == "-V" or switch == "--version":
+    elif switch in ("-V", "--version"):
         print("ntptrace %s" % ntp.util.stdversion())
         raise SystemExit(0)
 


=====================================
ntpclients/ntpwait.py
=====================================
@@ -24,8 +24,8 @@ signal.signal(signal.SIGINT, lambda signal, frame: sys.exit(2))
 
 import getopt
 import re
-import time
 import socket
+import time
 
 try:
     import ntp.magic



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/-/commit/370091348d88b5b51c75772dfd73c1efb0a3d932

-- 
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/-/commit/370091348d88b5b51c75772dfd73c1efb0a3d932
You're receiving this email because of your account on gitlab.com.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20201013/5c482fc1/attachment-0001.htm>


More information about the vc mailing list