[Git][NTPsec/ntpsec][master] Rewrote dump_hex_printable() to be pythonic

Ian Bruene gitlab at mg.gitlab.com
Wed Aug 23 21:35:45 UTC 2017


Ian Bruene pushed to branch master at NTPsec / ntpsec


Commits:
8b911a02 by Ian Bruene at 2017-08-23T16:34:41-05:00
Rewrote dump_hex_printable() to be pythonic

- - - - -


1 changed file:

- pylib/packet.py


Changes:

=====================================
pylib/packet.py
=====================================
--- a/pylib/packet.py
+++ b/pylib/packet.py
@@ -686,27 +686,21 @@ SERR_NOTRUST = "***No trusted keys have been declared"
 
 def dump_hex_printable(xdata, outfp=sys.stdout):
     "Dump a packet in hex, in a familiar hex format"
-    llen = len(xdata)
-    i = 0
-    while llen > 0:
-        rowlen = min(16, llen)
-        restart = i
-        for idx in range(16):
-            if idx < llen:
-                outfp.write("%02x " % polyord(xdata[i]))
-                i += 1
-            else:
-                outfp.write("   ")
-        i = restart
-        for idx in range(rowlen):
-            # Do not use curses.isprint(), netbsd base doesn't install curses
-            if polyord(xdata[i]) >= 32 and polyord(xdata[i]) < 127:
-                outfp.write(polychr(xdata[i]))
-            else:
-                outfp.write('.')
-            i += 1
-        outfp.write("\n")
-        llen -= rowlen
+    rowsize = 16
+    while len(xdata) > 0:
+        # Slice one row off of our data
+        linedata, xdata = ntp.util.slicedata(xdata, rowsize)
+        # Output data in hex form
+        linelen = len(linedata)
+        line = "%02x " * linelen
+        linedata = [polyord(x) for x in linedata]  # Will need this later
+        line %= tuple(linedata)
+        if linelen < rowsize:  # Pad out the line to keep columns neat
+            line += "   " * (rowsize - linelen)
+        # Output printable data in string form
+        linedata = [chr(x) if (32 <= x < 127) else "." for x in linedata]
+        line += "".join(linedata) + "\n"
+        outfp.write(line)
 
 
 class MRUEntry:



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/8b911a02841a0a17261f470fb85564efd9187634

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/8b911a02841a0a17261f470fb85564efd9187634
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/20170823/7c5dec3f/attachment.html>


More information about the vc mailing list