summaryrefslogtreecommitdiff
path: root/tools/orderedtree.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/orderedtree.py')
-rw-r--r--tools/orderedtree.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/tools/orderedtree.py b/tools/orderedtree.py
new file mode 100644
index 0000000..def8928
--- /dev/null
+++ b/tools/orderedtree.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python
+
+# Copyright (c) 2017, NORDUnet A/S.
+# See LICENSE for licensing information.
+
+class OrderDict(dict):
+ def __init__(self):
+ self._order = []
+ dict.__init__({})
+ def __setitem__(self, key, value):
+ if key not in self:
+ self._order.append(key)
+ super(OrderDict, self).__setitem__(key, value)
+ def iterkeys(self):
+ return iter(self._order)
+
+class TreeNode():
+ def __init__(self):
+ self.entry = None
+ self._children = OrderDict()
+ def add(self, k, e, subtree=None):
+ if subtree != None:
+ self._children[k] = subtree
+ else:
+ self._children[k] = TreeNode()
+ self._children[k].entry = e
+ def __getitem__(self, key):
+ return self._children[key]
+ def iterkeys(self):
+ return self._children.iterkeys()
+ def __contains__(self, key):
+ return key in self._children
+ def walk(self, keys):
+ node = self
+ for k in keys:
+ node = node[k]
+ return node
+ def __str__(self):
+ s = str(self.entry) + "\n"
+ for k in self.iterkeys():
+ s += str(k) + ":\n"
+ for row in str(self._children[k]).split("\n"):
+ if row:
+ s += " " + row + "\n"
+ return s