[gi-docgen/class-hierarchy: 1/2] Move class hierarchy generator to the GIR API




commit 1e9c00b64ccd7e3584285e9ca1c4c28562df543d
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Mon May 24 18:37:51 2021 +0100

    Move class hierarchy generator to the GIR API
    
    We want to hide it into the ast.Repository class, so we can access the
    various bits and blobs from the GIR. This commit has no functional
    change.

 gidocgen/gdgenerate.py | 30 +++++-------------------------
 gidocgen/gir/ast.py    | 18 ++++++++++++++++++
 2 files changed, 23 insertions(+), 25 deletions(-)
---
diff --git a/gidocgen/gdgenerate.py b/gidocgen/gdgenerate.py
index 8427ad0..306d78e 100644
--- a/gidocgen/gdgenerate.py
+++ b/gidocgen/gdgenerate.py
@@ -2315,36 +2315,14 @@ def gen_content_images(config, content_dir, output_dir):
 
 
 def gen_types_hierarchy(config, theme_config, output_dir, jinja_env, repository):
-    namespace = repository.namespace
-
-    # Gather all class types and their parent to build a flat list
-    flat_tree = []
-    for cls in namespace.get_classes():
-        name = cls.name
-        if cls.parent is not None:
-            parent = cls.parent.name
-        else:
-            parent = None
-        flat_tree.append((name, parent))
-
-    # A subtly elegant way to rebuild the tree from a flat
-    # list of (name, parent) tuples. See:
-    #
-    #   https://stackoverflow.com/a/43728268/771066
-    def subtree(cls, rel):
-        return {
-            v: subtree(v, rel)
-            for v in [x[0] for x in rel if x[1] == cls]
-        }
-
     # All GObject sub-types
-    objects_tree = subtree('GObject.Object', flat_tree)
+    objects_tree = repository.get_class_hierarchy(root="GObject.Object")
 
     # All GInitiallyUnowned sub-types
-    unowned_tree = subtree('GObject.InitiallyUnowned', flat_tree)
+    unowned_tree = repository.get_class_hierarchy(root="GObject.InitiallyUnowned")
 
     # All GTypeInstance sub-types
-    typed_tree = subtree(None, flat_tree)
+    typed_tree = repository.get_class_hierarchy()
 
     res = ["<h1>Classes Hierarchy</h1>"]
 
@@ -2393,6 +2371,8 @@ def gen_types_hierarchy(config, theme_config, output_dir, jinja_env, repository)
 
     content_tmpl = jinja_env.get_template(theme_config.content_template)
 
+    namespace = repository.namespace
+
     dst_file = os.path.join(output_dir, content["output_file"])
     log.info(f"Generating type hierarchy file: {dst_file}")
     with open(dst_file, "w") as outfile:
diff --git a/gidocgen/gir/ast.py b/gidocgen/gir/ast.py
index d603ed2..6c499e1 100644
--- a/gidocgen/gir/ast.py
+++ b/gidocgen/gir/ast.py
@@ -1108,6 +1108,24 @@ class Repository:
                 symbols[m.identifier] = union
         self.namespace._symbols = symbols
 
+    def get_class_hierarchy(self, root=None):
+        flat_tree = []
+        for cls in self.namespace.get_classes():
+            name = cls.name
+            if cls.parent is not None:
+                parent = cls.parent.name
+            else:
+                parent = None
+            flat_tree.append((name, parent))
+
+        def subtree(cls, rel):
+            return {
+                v: subtree(v, rel)
+                for v in [x[0] for x in rel if x[1] == cls]
+            }
+
+        return subtree(root, flat_tree)
+
     @property
     def namespace(self) -> T.Optional[Namespace]:
         return self._namespaces[0]


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]