[gi-docgen/ebassi/regression-gir: 2/3] gir: Add an exception mode to the parser




commit cbf893d8e117646a099745960dbb0029744be806
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Tue Nov 9 23:46:31 2021 +0000

    gir: Add an exception mode to the parser
    
    We may want to raise an exception on parsing failure, instead of using
    the default logging system.

 gidocgen/gir/parser.py | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
---
diff --git a/gidocgen/gir/parser.py b/gidocgen/gir/parser.py
index f47ff94..823b111 100644
--- a/gidocgen/gir/parser.py
+++ b/gidocgen/gir/parser.py
@@ -69,12 +69,13 @@ def _cns(tag: str) -> str:
 
 
 class GirParser:
-    def __init__(self, search_paths=[]):
+    def __init__(self, search_paths=[], error=True):
         self._search_paths = search_paths
         self._repository = None
         self._dependencies = {}
         self._seen_types = {}
         self._current_namespace = []
+        self._error = error
 
     def append_search_path(self, path: str) -> None:
         """Append a path to the list of search paths"""
@@ -90,7 +91,10 @@ class GirParser:
         tree = ET.parse(girfile)
         repository = self._parse_tree(tree.getroot())
         if repository is None:
-            log.error(f"Could not parse GIR {girfile}")
+            if self._error:
+                log.error(f"Could not parse GIR {girfile}")
+            else:
+                raise RuntimeError(f"Invalid GIR file {girfile}")
         else:
             if isinstance(girfile, str):
                 repository.girfile = girfile
@@ -194,7 +198,10 @@ class GirParser:
                     found = True
                     break
         if not found:
-            log.error(f"Could not find GIR dependency in the search paths: {include}")
+            if self._error:
+                log.error(f"Could not find GIR dependency in the search paths: {include}")
+            else:
+                raise RuntimeError(f"No {include} found in search paths {self._search_paths}")
 
     def _parse_tree(self, root: ET.Element) -> ast.Repository:
         assert root.tag == _corens('repository')


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