From bb67c23918ba22be498537a29c01b696732d5b3b Mon Sep 17 00:00:00 2001 From: Magnus Ahltorp Date: Mon, 3 Jul 2017 00:21:02 +0200 Subject: Automatic generation of config man page skeleton --- tools/orderedtree.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 tools/orderedtree.py (limited to 'tools/orderedtree.py') 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 -- cgit v1.1