[gi-docgen/filter-hidden: 11/11] config: Allow pattern matching the object name




commit bb77f518f3fc2d4b5dd23315d3ef12807f14a4f2
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Thu Jun 10 11:37:05 2021 +0100

    config: Allow pattern matching the object name
    
    In some cases, we might want to hide a bunch of symbols that follow the
    same pattern; specifying them all makes the project configuration file
    balloon up in size.

 docs/project-configuration.rst | 10 ++++++----
 gidocgen/config.py             | 24 +++++++++++++++++-------
 2 files changed, 23 insertions(+), 11 deletions(-)
---
diff --git a/docs/project-configuration.rst b/docs/project-configuration.rst
index c1a810f..cda882e 100644
--- a/docs/project-configuration.rst
+++ b/docs/project-configuration.rst
@@ -182,8 +182,10 @@ This annotation applies to all possible top-level types:
  - unions
 
 The ``object`` key is always an array of dictionaries; each element in the array
-has a ``name`` key, used to match it. Each element can also have the following
-keys:
+can have a ``name`` key, used to match the object name exactly; or a ``pattern``
+key, which uses a regular expression to match the object name.
+
+Each element can also have the following keys:
 
  - ``property``
  - ``signal``
@@ -191,8 +193,8 @@ keys:
  - ``method``
  - ``function``
 
-Each one of these keys can contain array of dictionaries with a ``name`` key and
-the ``hidden`` key.
+Each one of these keys can contain array of dictionaries with a ``name`` or
+``pattern`` keys, and a ``hidden`` key.
 
 The following example will hide the ``backend`` property on the ``Printer`` type:
 
diff --git a/gidocgen/config.py b/gidocgen/config.py
index eba2caf..5647d64 100644
--- a/gidocgen/config.py
+++ b/gidocgen/config.py
@@ -2,6 +2,7 @@
 # SPDX-License-Identifier: Apache-2.0 OR GPL-3.0-or-later
 
 import os
+import re
 import toml
 
 from urllib.parse import urljoin
@@ -155,17 +156,26 @@ class GIDocConfig:
         return self._config.get('object', {})
 
     def is_hidden(self, name, category=None, key=None):
+        def obj_matches(obj, name):
+            n = obj.get('name')
+            p = obj.get('pattern')
+            if n is not None and n == name:
+                return True
+            elif p is not None and re.match(p, name):
+                return True
+            return False
         for obj in self.objects:
-            if obj['name'] == name:
+            if obj_matches(obj, name):
                 if category is None:
                     return obj.get('hidden', False)
                 else:
-                    obj_category = obj.get(category)
-                    if obj_category is None:
-                        return False
-                    for c in obj_category:
-                        if c['name'] == key:
-                            return c.get('hidden', False)
+                    assert key is not None
+                obj_category = obj.get(category)
+                if obj_category is None:
+                    return False
+                for c in obj_category:
+                    if obj_matches(c, key):
+                        return c.get('hidden', False)
         return False
 
 


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