[Git][NTPsec/ntpsec][master] 2 commits: Added option to put a space between the value and unit in unitify()

Ian Bruene gitlab at mg.gitlab.com
Fri Nov 3 20:42:57 UTC 2017


Ian Bruene pushed to branch master at NTPsec / ntpsec


Commits:
fb45d3b0 by Ian Bruene at 2017-11-03T15:42:35-05:00
Added option to put a space between the value and unit in unitify()

- - - - -
13398c9e by Ian Bruene at 2017-11-03T15:42:35-05:00
De-dummyified several OIDs

- - - - -


3 changed files:

- ntpclients/ntpsnmpd
- pylib/util.py
- tests/pylib/test_util.py


Changes:

=====================================
ntpclients/ntpsnmpd
=====================================
--- a/ntpclients/ntpsnmpd
+++ b/ntpclients/ntpsnmpd
@@ -294,9 +294,10 @@ class DataSource:  # This may be broken up in future to be less NTP-specific
         if write is None:
             return ax.Varbind(ax.VALUE_GAUGE32, oid, 42)
 
-    def cb_timePrecision(self, oid, write=None):  # DUMMY
+    def cb_timePrecision(self, oid, write=None):
         if write is None:
-            return ax.Varbind(ax.VALUE_INTEGER, oid, 23)
+            data = self.session.readvar(0, ["precision"])
+            return ax.Varbind(ax.VALUE_INTEGER, oid, data["precision"])
 
     def cb_timeDistance(self, oid, write=None):  # DUMMY
         # Displaystring
@@ -310,7 +311,7 @@ class DataSource:  # This may be broken up in future to be less NTP-specific
         if write is None:
             return ax.Varbind(ax.VALUE_INTEGER, oid, 15)
 
-    def cb_statusStratum(self, oid, write=None):  # DUMMY
+    def cb_statusStratum(self, oid, write=None):
         # NTPstratum
         if write is None:
             data = self.session.readvar(0, ["stratum"])
@@ -321,20 +322,25 @@ class DataSource:  # This may be broken up in future to be less NTP-specific
         if write is None:
             return ax.Varbind(ax.VALUE_GAUGE32, oid, 1024)
 
-    def cb_statusActiveRefSourceName(self, oid, write=None):  # DUMMY
+    def cb_statusActiveRefSourceName(self, oid, write=None):
         # utf8
-        if write is None:
-            return ax.Varbind(ax.VALUE_OCTET_STR, oid, "bar")
+        if write is None:  # TODO: pretty sure this is right, just need DNS
+            data = self.session.readvar(0, ["peeradr"])
+            return ax.Varbind(ax.VALUE_OCTET_STR, oid, data["peeradr"])
 
-    def cb_statusActiveOffset(self, oid, write=None):  # DUMMY
+    def cb_statusActiveOffset(self, oid, write=None):
         # DisplayString
         if write is None:
-            return ax.Varbind(ax.VALUE_OCTET_STR, oid, "baz")
+            data = self.session.readvar(0, ["koffset"], raw=True)
+            data = ntp.util.unitifyvar(data["koffset"][1], "koffset",
+                                       width=None, unitSpace=True)
+            return ax.Varbind(ax.VALUE_OCTET_STR, oid, data)
 
-    def cb_statusNumRefSources(self, oid, write=None):  # DUMMY
+    def cb_statusNumRefSources(self, oid, write=None):
         # range of uint32
         if write is None:
-            return ax.Varbind(ax.VALUE_GAUGE32, oid, 50)
+            data = self.session.readstat()
+            return ax.Varbind(ax.VALUE_GAUGE32, oid, len(data))
 
     def cb_statusDispersion(self, oid, write=None):  # DUMMY
         # DisplayString
@@ -361,17 +367,20 @@ class DataSource:  # This may be broken up in future to be less NTP-specific
         if write is None:
             return ax.Varbind(ax.VALUE_INTEGER, oid, -1)
 
-    def cb_statusInPkts(self, oid, write=None):  # DUMMY
+    def cb_statusInPkts(self, oid, write=None):
         if write is None:
-            return ax.Varbind(ax.VALUE_COUNTER32, oid, 100)
+            data = self.session.readvar(0, ["io_received"])
+            return ax.Varbind(ax.VALUE_COUNTER32, oid, data["io_received"])
 
-    def cb_statusOutPkts(self, oid, write=None):  # DUMMY
+    def cb_statusOutPkts(self, oid, write=None):
         if write is None:
-            return ax.Varbind(ax.VALUE_COUNTER32, oid, 200)
+            data = self.session.readvar(0, ["io_sent"])
+            return ax.Varbind(ax.VALUE_COUNTER32, oid, data["io_sent"])
 
-    def cb_statusBadVersion(self, oid, write=None):  # DUMMY
+    def cb_statusBadVersion(self, oid, write=None):
         if write is None:
-            return ax.Varbind(ax.VALUE_COUNTER32, oid, 300)
+            data = self.session.readvar(0, ["ss_oldver"])
+            return ax.Varbind(ax.VALUE_COUNTER32, oid, data["ss_oldver"])
 
     def cb_statusProtocolError(self, oid, write=None):  # DUMMY
         if write is None:


=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -397,7 +397,7 @@ def unitrelativeto(unit, move):
     return None  # couldn't find anything
 
 
-def unitifyvar(value, varname, baseunit=None, width=8):
+def unitifyvar(value, varname, baseunit=None, width=8, unitSpace=False):
     "Call unitify() with the correct units for varname"
     if varname in S_VARS:
         start = UNIT_S
@@ -407,10 +407,10 @@ def unitifyvar(value, varname, baseunit=None, width=8):
         start = UNIT_PPM
     else:
         return value
-    return unitify(value, start, baseunit, width)
+    return unitify(value, start, baseunit, width, unitSpace)
 
 
-def unitify(value, startingunit, baseunit=None, width=8):
+def unitify(value, startingunit, baseunit=None, width=8, unitSpace=False):
     "Formats a numberstring with relevant units. Attemps to fit in width."
     if baseunit is None:
         baseunit = getunitgroup(startingunit)[0]
@@ -421,14 +421,20 @@ def unitify(value, startingunit, baseunit=None, width=8):
         newvalue = cropprecision(value, ooms)
         newvalue, unitsmoved = scalestring(newvalue)
     unitget = unitrelativeto(startingunit, unitsmoved)
+    if unitSpace is True:
+        spaceWidthAdjustment = 1
+        spacer = " "
+    else:
+        spaceWidthAdjustment = 0
+        spacer = ""
     if unitget is not None:  # We have a unit
         if width is None:
             realwidth = None
         else:
-            realwidth = width - len(unitget)
-        newvalue = fitinfield(newvalue, realwidth) + unitget
+            realwidth = width - (len(unitget) + spaceWidthAdjustment)
+        newvalue = fitinfield(newvalue, realwidth) + spacer + unitget
     else:  # don't have a replacement unit, use original
-        newvalue = value + startingunit
+        newvalue = value + spacer + startingunit
     if width is None:
         newvalue = newvalue.strip()
     return newvalue


=====================================
tests/pylib/test_util.py
=====================================
--- a/tests/pylib/test_util.py
+++ b/tests/pylib/test_util.py
@@ -278,6 +278,8 @@ class TestPylibUtilMethods(unittest.TestCase):
 
         # Zero
         self.assertEqual(f("0.000", nu.UNIT_MS), u"     0µs")
+        # Zero, spaced
+        self.assertEqual(f("0.000", nu.UNIT_MS, unitSpace=True), u"    0 µs")
         # Standard, width=8
         self.assertEqual(f("1.234", nu.UNIT_MS), " 1.234ms")
         # ditto, negative



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/a916564f90b40a364d5a86bca731a17fa51614c1...13398c9eac97944667b1dff36049b0a14aec27bf

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/a916564f90b40a364d5a86bca731a17fa51614c1...13398c9eac97944667b1dff36049b0a14aec27bf
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/20171103/4f8a9f09/attachment.html>


More information about the vc mailing list