[Git][NTPsec/ntpsec][wip-ntpq-peers-display] Added optional units display to ntpq peers

Ian Bruene gitlab at mg.gitlab.com
Wed Mar 22 23:10:52 UTC 2017


Ian Bruene pushed to branch wip-ntpq-peers-display at NTPsec / ntpsec


Commits:
d3f9f8f0 by Ian Bruene at 2017-03-22T18:08:01-05:00
Added optional units display to ntpq peers

ntpq option -u / --units will display peer times with units instead
of decimals.

In interactive mode the 'units' command will toggle the display setting.

- - - - -


2 changed files:

- ntpclients/ntpq
- pylib/util.py


Changes:

=====================================
ntpclients/ntpq
=====================================
--- a/ntpclients/ntpq
+++ b/ntpclients/ntpq
@@ -153,6 +153,7 @@ class Ntpq(cmd.Cmd):
         self.rawmode = False            # Flag which indicates raw mode output.
         self.directmode = False         # Flag for direct MRU output.
         self.showhostnames = True       # If false, display numeric IPs
+        self.showunits = False          # If False, show old style float
         self.auth_delay = 20            # delay time (default 20msec)
         self.wideremote = False         # show wide remote names?
         self.ccmds = []                 # Queued commands
@@ -240,7 +241,7 @@ usage: help [ command ]
 """)
 
     # Unexposed helper tables and functions begin here
-
+    
     def __dogetassoc(self):
         try:
             self.peers = self.session.readstat()
@@ -293,6 +294,7 @@ usage: help [ command ]
                                       self.pktversion,
                                       self.showhostnames,
                                       self.wideremote,
+                                      self.showunits,
                                       termwidth=termwidth,
                                       debug=interpreter.debug)
         try:
@@ -435,6 +437,10 @@ usage: help [ command ]
         return True
 
     # Unexposed helper tables and functions end here
+    
+    def do_units(self, _unused):
+        "toggle unit display"
+        self.showunits = not self.showunits
 
     def do_EOF(self, _unused):
         "exit ntpq"
@@ -1569,17 +1575,18 @@ USAGE: ntpq [-46dphinOV] [-c str] [-D lvl] [host ...]
                          on a separate line
    -W no  width          force output width to this value instead of
                          querying the terminal size
+   -u no  units          Display time with units.
 '''
 
 if __name__ == '__main__':
     try:
         (options, arguments) = getopt.getopt(sys.argv[1:],
-                                             "46c:dD:hinpVwW:",
+                                             "46c:dD:hinpVwW:u",
                                              ["ipv4", "ipv6", "command=",
                                               "debug", "set-debug-level=",
                                               "help", "interactive", "numeric",
                                               "peers", "version",
-                                              "wide", "width="])
+                                              "wide", "width=", "units"])
     except getopt.GetoptError as e:
         sys.stderr.write("%s\n" % e)
         sys.stderr.write(usage)
@@ -1629,6 +1636,8 @@ if __name__ == '__main__':
                 sys.stderr.write("'%s' is not a recognizable number\n" % val)
                 sys.stderr.write(usage)
                 raise SystemExit(1)
+        elif switch in ("-u", "--units"):
+            interpreter.showunits = True
 
     if interpreter.interactive and len(interpreter.ccmds) > 0:
         interpreter.warn("%s: invalid option combination.\n" % progname)


=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -335,10 +335,11 @@ class PeerSummary:
     "Reusable report generator for peer statistics"
 
     def __init__(self, displaymode, pktversion, showhostnames,
-                 wideremote, termwidth=None, debug=0):
+                 wideremote, showunits=False, termwidth=None, debug=0):
         self.displaymode = displaymode          # peers/apeers/opeers
         self.pktversion = pktversion            # interpretation of flash bits
         self.showhostnames = showhostnames      # If false, display numeric IPs
+        self.showunits = showunits              # If False show old style float
         self.wideremote = wideremote            # show wide remote names?
         self.debug = debug
         self.termwidth = termwidth
@@ -559,15 +560,17 @@ class PeerSummary:
                     else int(now - ntp.ntpc.lfptofloat(last_sync))),
                    PeerSummary.prettyinterval(poll_sec), reach))
             if saw6:
+                renderer = (i8unit if self.showunits else f8dot4)
                 line += (
                     " %s %s %s" %
-                    (i8unit(estdelay), i8unit(estoffset), i8unit(jd)))
+                    (renderer(estdelay), renderer(estoffset), renderer(jd)))
             else:
                 # old servers only have 3 digits of fraction
                 # don't print a fake 4th digit
+                renderer = (i8unit if self.showunits else f8dot3)
                 line += (
                     " %s %s %s" %
-                    (i8unit(estdelay), i8unit(estoffset), i8unit(jd)))
+                    (renderer(estdelay), renderer(estoffset), renderer(jd)))
             line += "\n"
             return line
         except TypeError:



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/d3f9f8f02ded2d88e6a585aadcaf9bbc28152400
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170322/5682c99b/attachment.html>


More information about the vc mailing list