[Git][NTPsec/ntpsec][master] 2 commits: Cleaned up IfstatsSummary.summary() a bit

Ian Bruene gitlab at mg.gitlab.com
Fri Aug 25 00:10:20 UTC 2017


Ian Bruene pushed to branch master at NTPsec / ntpsec


Commits:
d9fcb0c6 by Ian Bruene at 2017-08-24T19:09:55-05:00
Cleaned up IfstatsSummary.summary() a bit

- - - - -
a327911c by Ian Bruene at 2017-08-24T19:09:55-05:00
Updated comments in __parse_varlist()

- - - - -


2 changed files:

- pylib/packet.py
- pylib/util.py


Changes:

=====================================
pylib/packet.py
=====================================
--- a/pylib/packet.py
+++ b/pylib/packet.py
@@ -1259,9 +1259,6 @@ class ControlSession:
         # Strip out NULs and binary garbage from text;
         # ntpd seems prone to generate these, especially
         # in reslist responses.
-        # ==================
-        # TODO: remove raw switch, always return (key, value, castedvalue)
-        # ==================
         kvpairs = []
         instring = False
         response = ""
@@ -1277,7 +1274,7 @@ class ControlSession:
             elif 0 < cord < 127:
                 # if it isn't a special case or garbage, add it
                 response += c
-        if len(response) > 0:  # The last item won't be caught in the loop
+        if len(response) > 0:  # The last item won't be caught by the loop
             kvpairs.append(response.strip())
         items = []
         for pair in kvpairs:
@@ -1288,7 +1285,6 @@ class ControlSession:
                 key, value = pair, ""
             key, value = key.strip(), value.strip()
             # Start trying to cast to non-string types
-            # I don't like this block, look for a less ugly method
             if value:
                 try:
                     castedvalue = int(value, 0)


=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -1203,17 +1203,22 @@ class IfstatsSummary:
 
     def summary(self, i, variables):
         formatted = {}
-        for name in self.fields.keys():
-            value = variables.get(name, "?")
-            if value == "?":
-                fmt = value
-            else:
-                fmt = self.fields[name] % value
-            formatted[name] = fmt
         try:
+            # Format the fields
+            for name in self.fields.keys():
+                value = variables.get(name, "?")
+                if value == "?":
+                    fmt = value
+                else:
+                    fmt = self.fields[name] % value
+                formatted[name] = fmt
             enFlag = '.' if variables.get('en', False) else 'D'
-            s = ("%3u %-24.24s %c %4s %6s %6s %6s %5s %8s\n    %s\n"
-                 % (i, formatted['name'],
+            address = variables.get("addr", "?")
+            bcast = variables.get("bcast")
+            # Assemble the fields into a line
+            s = ("%3u %s %s %s %s %s %s %s %s\n    %s\n"
+                 % (i,
+                    formatted['name'],
                     enFlag,
                     formatted['flags'],
                     formatted['rx'],
@@ -1221,9 +1226,9 @@ class IfstatsSummary:
                     formatted['txerr'],
                     formatted['pc'],
                     formatted['up'],
-                    variables.get('addr', '?')))
-            if variables.get("bcast"):
-                s += "    %s\n" % variables['bcast']
+                    address))
+            if bcast:
+                s += "    %s\n" % bcast
         except TypeError:
             # Can happen when ntpd ships a corrupted response
             return ''



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/6643b8e5601305b9d10fbd0bc31a936f366a0e6e...a327911c2edd31854cd9e615928051f2b26967b5

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/6643b8e5601305b9d10fbd0bc31a936f366a0e6e...a327911c2edd31854cd9e615928051f2b26967b5
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/20170825/b8caaf92/attachment.html>


More information about the vc mailing list