[gtk-doc/wip/smcv/internal-tree] Implement a simple tree structure without using anytree
- From: Simon McVittie <smcv src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk-doc/wip/smcv/internal-tree] Implement a simple tree structure without using anytree
- Date: Tue, 25 Aug 2020 09:35:36 +0000 (UTC)
commit 9e289cd74056c42ac6ea824809efcb65bd265a85
Author: Simon McVittie <smcv debian org>
Date: Thu Aug 22 09:31:19 2019 +0100
Implement a simple tree structure without using anytree
anytree isn't currently available in Debian or Ubuntu, and seems like
a lot of code just to get a tree data structure. Reimplement just the
bits we need, with a compatible API.
Signed-off-by: Simon McVittie <smcv debian org>
gtkdoc/mkhtml2.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++-
tests/mkhtml2.py | 2 +-
2 files changed, 52 insertions(+), 2 deletions(-)
---
diff --git a/gtkdoc/mkhtml2.py b/gtkdoc/mkhtml2.py
index 2eb5c73..093deb3 100644
--- a/gtkdoc/mkhtml2.py
+++ b/gtkdoc/mkhtml2.py
@@ -101,7 +101,6 @@ import os
import shutil
import sys
-from anytree import Node, PreOrderIter
from copy import deepcopy
from glob import glob
from lxml import etree
@@ -254,6 +253,57 @@ def get_chunk_titles(module, node, tree_node):
return result
+class PreOrderIter:
+ def __init__(self, node):
+ self.__node = node
+
+ def __iter__(self):
+ yield self.__node
+
+ for child in self.__node.descendants:
+ yield child
+
+
+class Node:
+ def __init__(self, name, parent=None, **kwargs):
+ self.name = name
+ self.__root = None
+ self.__attrs = kwargs
+ self.children = []
+
+ assert parent is None or isinstance(parent, Node)
+ self.parent = parent
+ if parent is not None:
+ self.__root = parent.root
+ parent.children.append(self)
+
+ @property
+ def root(self):
+ return self.__root or self
+
+ @property
+ def descendants(self):
+ ret = []
+
+ for child in self.children:
+ ret.append(child)
+
+ for other in child.descendants:
+ ret.append(other)
+
+ return ret
+
+ def __iter__(self):
+ for child in self.children:
+ yield child
+
+ def __getattr__(self, name):
+ try:
+ return self.__attrs[name]
+ except KeyError as e:
+ raise AttributeError(str(e))
+
+
def chunk(xml_node, module, depth=0, idx=0, parent=None):
"""Chunk the tree.
diff --git a/tests/mkhtml2.py b/tests/mkhtml2.py
index c61ee27..439e7fe 100755
--- a/tests/mkhtml2.py
+++ b/tests/mkhtml2.py
@@ -22,11 +22,11 @@ import logging
import textwrap
import unittest
-from anytree import PreOrderIter
from lxml import etree
from parameterized import parameterized
from gtkdoc import mkhtml2
+from gtkdoc.mkhtml2 import PreOrderIter
class TestChunking(unittest.TestCase):
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]