[Git][NTPsec/ntpsec][master] MIB tree/lists now have callback fields

Ian Bruene gitlab at mg.gitlab.com
Sun Oct 22 17:57:29 UTC 2017


Ian Bruene pushed to branch master at NTPsec / ntpsec


Commits:
4b71a9e8 by Ian Bruene at 2017-10-22T12:56:18-05:00
MIB tree/lists now have callback fields

- - - - -


2 changed files:

- pylib/agentx.py
- tests/pylib/test_agentx.py


Changes:

=====================================
pylib/agentx.py
=====================================
--- a/pylib/agentx.py
+++ b/pylib/agentx.py
@@ -1264,33 +1264,34 @@ def mibTree2List(mibtree, currentPath=()):
     branches = list(mibtree.keys())
     branches.sort()
     for branch in branches:
-        paths.append(OID(currentPath + (branch,)))
+        callback, tree = mibtree[branch]
+        paths.append((callback, OID(currentPath + (branch,))))
         branchPath = currentPath + (branch,)
-        paths += mibTree2List(mibtree[branch], branchPath)
+        paths += mibTree2List(tree, branchPath)
     return tuple(paths)
 
 
 def mibList2Tree(miblist, rootPath=()):
     "Takes a list of OIDs and inflates it into a tree"
     tree = {}
-    for oid in miblist:
-        node = oid.subids
+    for mibnode in miblist:
+        callback, oid = mibnode[0], mibnode[1].subids
         rootlen = len(rootPath)
-        if node[:rootlen] != rootPath:  # OID not decended from the root, bail
+        if oid[:rootlen] != rootPath:  # OID not decended from the root, bail
             raise ValueError("Node %s does not have root %s" %
-                             (node, rootPath))
-        node = node[rootlen:]  # clip the root off for the tree
+                             (oid, rootPath))
+        oid = oid[rootlen:]  # clip the root off for the tree
         branch = tree
-        nodePos = 0
-        nodeSize = len(node)
-        while nodePos < nodeSize:
-            subid = node[nodePos]
+        oidPos = 0
+        oidSize = len(oid)
+        while oidPos < oidSize:
+            subid = oid[oidPos]
             if subid not in branch:  # First time at this position
-                branch[subid] = None  # might be a leaf
-            elif branch[subid] is None:  # It isn't a leaf
-                branch[subid] = {}
-            branch = branch[subid]
-            nodePos += 1
+                branch[subid] = (callback, None)  # might be a leaf
+            elif branch[subid][1] is None:  # It isn't a leaf
+                branch[subid] = (branch[subid][0], {})
+            branch = branch[subid][1]
+            oidPos += 1
     return tree
 
 


=====================================
tests/pylib/test_agentx.py
=====================================
--- a/tests/pylib/test_agentx.py
+++ b/tests/pylib/test_agentx.py
@@ -2460,23 +2460,35 @@ class TestNtpclientsNtpsnmpd(unittest.TestCase):
         # Test empty tree
         self.assertEqual(f({}), ())
         # Test flat tree
-        self.assertEqual(f({0: None, 1: None, 3: None, 4: None}),
-                         (x.OID((0,)), x.OID((1,)), x.OID((3,)), x.OID((4,))))
+        self.assertEqual(f({0: (None, None), 1: (None, None),
+                            3: (None, None), 4: (None, None)}),
+                         ((None, x.OID((0,))), (None, x.OID((1,))),
+                          (None, x.OID((3,))), (None, x.OID((4,)))))
         # Test flat tree with root path
-        self.assertEqual(f({0: None, 1: None, 3: None, 4: None}, (42, 23)),
-                         (x.OID((42, 23, 0)), x.OID((42, 23, 1)),
-                          x.OID((42, 23, 3)), x.OID((42, 23, 4))))
+        self.assertEqual(f({0: (None, None), 1: (None, None),
+                            3: (None, None), 4: (None, None)},
+                           (42, 23)),
+                         ((None, x.OID((42, 23, 0))),
+                          (None, x.OID((42, 23, 1))),
+                          (None, x.OID((42, 23, 3))),
+                          (None, x.OID((42, 23, 4)))))
         # Test nested tree
-        self.assertEqual(f({0: None,
-                            2: {0: None, 1: None},
-                            3: {5: {0: None, 2: None}, 6: None},
-                            4: None}),
-                         (x.OID((0,)),
-                          x.OID((2,)), x.OID((2, 0)), x.OID((2, 1)),
-                          x.OID((3,)),
-                          x.OID((3, 5)), x.OID((3, 5, 0)), x.OID((3, 5, 2)),
-                          x.OID((3, 6)),
-                          x.OID((4,))))
+        self.assertEqual(f({0: (None, None),
+                            2: (None,
+                                {0: (None, None), 1: (None, None)}),
+                            3: (None,
+                                {5: (None,
+                                     {0: (None, None), 2: (None, None)}),
+                                 6: (None, None)}),
+                            4: (None, None)}),
+                         ((None, x.OID((0,))),
+                          (None, x.OID((2,))),
+                          (None, x.OID((2, 0))), (None, x.OID((2, 1))),
+                          (None, x.OID((3,))),
+                          (None, x.OID((3, 5))),
+                          (None, x.OID((3, 5, 0))), (None, x.OID((3, 5, 2))),
+                          (None, x.OID((3, 6))),
+                          (None, x.OID((4,)))))
 
     def test_mibList2Tree(self):
         x = ntp.agentx
@@ -2485,24 +2497,35 @@ class TestNtpclientsNtpsnmpd(unittest.TestCase):
         # Test empty tree
         self.assertEqual(f(tuple()), {})
         # Test flat tree
-        self.assertEqual(f((x.OID((0,)), x.OID((1,)),
-                            x.OID((3,)), x.OID((4,)))),
-                         {0: None, 1: None, 3: None, 4: None})
+        self.assertEqual(f(((None, x.OID((0,))), (None, x.OID((1,))),
+                            (None, x.OID((3,))), (None, x.OID((4,))))),
+                         {0: (None, None), 1: (None, None),
+                          3: (None, None), 4: (None, None)})
         # Test flat tree with root path
-        self.assertEqual(f((x.OID((42, 23, 0)), x.OID((42, 23, 1)),
-                            x.OID((42, 23, 3)), x.OID((42, 23, 4))), (42, 23)),
-                         {0: None, 1: None, 3: None, 4: None})
+        self.assertEqual(f(((None, x.OID((42, 23, 0))),
+                            (None, x.OID((42, 23, 1))),
+                            (None, x.OID((42, 23, 3))),
+                            (None, x.OID((42, 23, 4)))),
+                           (42, 23)),
+                         {0: (None, None), 1: (None, None),
+                          3: (None, None), 4: (None, None)})
         # Test nested tree
-        self.assertEqual(f((x.OID((0,)),
-                            x.OID((2,)), x.OID((2, 0)), x.OID((2, 1)),
-                            x.OID((3,)),
-                            x.OID((3, 5)), x.OID((3, 5, 0)), x.OID((3, 5, 2)),
-                            x.OID((3, 6)),
-                            x.OID((4,)))),
-                         {0: None,
-                          2: {0: None, 1: None},
-                          3: {5: {0: None, 2: None}, 6: None},
-                          4: None})
+        self.assertEqual(f(((None, x.OID((0,))),
+                            (None, x.OID((2,))),
+                            (None, x.OID((2, 0))), (None, x.OID((2, 1))),
+                            (None, x.OID((3,))),
+                            (None, x.OID((3, 5))),
+                            (None, x.OID((3, 5, 0))), (None, x.OID((3, 5, 2))),
+                            (None, x.OID((3, 6))),
+                            (None, x.OID((4,))))),
+                         {0: (None, None),
+                          2: (None,
+                              {0: (None, None), 1: (None, None)}),
+                          3: (None,
+                              {5: (None,
+                                   {0: (None, None), 2: (None, None)}),
+                               6: (None, None)}),
+                          4: (None, None)})
 
 
 if __name__ == "__main__":



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/4b71a9e81754a8e2cc012585c41d0816afa1337e

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/4b71a9e81754a8e2cc012585c41d0816afa1337e
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/20171022/dd3af54e/attachment.html>


More information about the vc mailing list