[gobject-introspection] [scanner] Simplify strip_identifer/strip_symbol



commit 5c25cabe5aa0385c224d519eedfd1df9e7e42187
Author: Johan Dahlin <johan gnome org>
Date:   Thu Sep 2 11:07:01 2010 -0300

    [scanner] Simplify strip_identifer/strip_symbol
    
    The strip_* functions are problematic since they
    have a fatal switch which determines if the message should
    kill the scanner. Change the api to make it easier to extend
    with other logging categories and move over the callsites to
    use the new message module

 giscanner/gdumpparser.py     |   45 +++++++++++++-------
 giscanner/maintransformer.py |    9 ++++-
 giscanner/transformer.py     |   90 ++++++++++++++++++++++++++----------------
 3 files changed, 93 insertions(+), 51 deletions(-)
---
diff --git a/giscanner/gdumpparser.py b/giscanner/gdumpparser.py
index 7f1d229..d93a464 100644
--- a/giscanner/gdumpparser.py
+++ b/giscanner/gdumpparser.py
@@ -27,6 +27,8 @@ from xml.etree.cElementTree import parse
 
 from . import ast
 from . import glibast
+from . import message
+from .transformer import TransformerException
 
 # GParamFlags
 G_PARAM_READABLE = 1 << 0
@@ -254,7 +256,10 @@ blob containing data gleaned from GObject's primitive introspection."""
 
         klass = (glibast.GLibFlags if node.tag == 'flags' else glibast.GLibEnum)
         type_name = node.attrib['name']
-        enum_name = self._transformer.strip_identifier_or_warn(type_name, fatal=True)
+        try:
+            enum_name = self._transformer.strip_identifier(type_name)
+        except TransformerException, e:
+            message.fatal(e)
         node = klass(enum_name, type_name, members, node.attrib['get-type'])
         self._namespace.append(node, replace=True)
 
@@ -275,11 +280,12 @@ blob containing data gleaned from GObject's primitive introspection."""
             return
         is_abstract = bool(xmlnode.attrib.get('abstract', False))
         (get_type, c_symbol_prefix) = self._split_type_and_symbol_prefix(xmlnode)
-        node = glibast.GLibObject(
-            self._transformer.strip_identifier_or_warn(type_name, fatal=True),
-            None,
-            type_name,
-            get_type, c_symbol_prefix, is_abstract)
+        try:
+            object_name = self._transformer.strip_identifier(type_name)
+        except TransformerException, e:
+            message.fatal(e)
+        node = glibast.GLibObject(object_name, None, type_name,
+                                  get_type, c_symbol_prefix, is_abstract)
         self._parse_parents(xmlnode, node)
         self._introspect_properties(node, xmlnode)
         self._introspect_signals(node, xmlnode)
@@ -291,10 +297,12 @@ blob containing data gleaned from GObject's primitive introspection."""
     def _introspect_interface(self, xmlnode):
         type_name = xmlnode.attrib['name']
         (get_type, c_symbol_prefix) = self._split_type_and_symbol_prefix(xmlnode)
-        node = glibast.GLibInterface(
-            self._transformer.strip_identifier_or_warn(type_name, fatal=True),
-            None,
-            type_name, get_type, c_symbol_prefix)
+        try:
+            interface_name = self._transformer.strip_identifier(type_name)
+        except TransformerException, e:
+            message.fatal(e)
+        node = glibast.GLibInterface(interface_name, None, type_name,
+                                     get_type, c_symbol_prefix)
         self._introspect_properties(node, xmlnode)
         self._introspect_signals(node, xmlnode)
         for child in xmlnode.findall('prerequisite'):
@@ -379,11 +387,13 @@ blob containing data gleaned from GObject's primitive introspection."""
         type_name = xmlnode.attrib['name']
         is_abstract = bool(xmlnode.attrib.get('abstract', False))
         (get_type, c_symbol_prefix) = self._split_type_and_symbol_prefix(xmlnode)
-        node = glibast.GLibObject(
-            self._transformer.strip_identifier_or_warn(type_name, fatal=True),
-            None,
-            type_name,
-            get_type, c_symbol_prefix, is_abstract)
+        try:
+            fundamental_name = self._transformer.strip_identifier(type_name)
+        except TransformerException, e:
+            message.fatal(e)
+
+        node = glibast.GLibObject(fundamental_name, None, type_name,
+                                  get_type, c_symbol_prefix, is_abstract)
         self._parse_parents(xmlnode, node)
         node.fundamental = True
         self._introspect_implemented_interfaces(node, xmlnode)
@@ -404,7 +414,10 @@ blob containing data gleaned from GObject's primitive introspection."""
                 field.writable = False
 
     def _pair_boxed_type(self, boxed):
-        name = self._transformer.strip_identifier_or_warn(boxed.type_name, fatal=True)
+        try:
+            name = self._transformer.strip_identifier(boxed.type_name)
+        except TransformerException, e:
+            message.fatal(e)
         pair_node = self._namespace.get(name)
         if not pair_node:
             boxed_item = glibast.GLibBoxedOther(name, boxed.type_name,
diff --git a/giscanner/maintransformer.py b/giscanner/maintransformer.py
index b529ee8..867a6c7 100644
--- a/giscanner/maintransformer.py
+++ b/giscanner/maintransformer.py
@@ -21,6 +21,7 @@ import re
 
 from . import ast
 from . import glibast
+from . import message
 from .annotationparser import (TAG_VFUNC, TAG_SINCE, TAG_DEPRECATED, TAG_RETURNS,
                                TAG_ATTRIBUTES, TAG_RENAME_TO, TAG_TYPE, TAG_TRANSFER,
                                TAG_UNREF_FUNC, TAG_REF_FUNC, TAG_SET_VALUE_FUNC,
@@ -32,6 +33,7 @@ from .annotationparser import (OPT_ALLOW_NONE,
                                OPT_FOREIGN, OPT_ARRAY_FIXED_SIZE,
                                OPT_ARRAY_LENGTH, OPT_ARRAY_ZERO_TERMINATED)
 from .annotationparser import AnnotationParser
+from .transformer import TransformerException
 from .utils import to_underscores, to_underscores_noprefix
 
 class MainTransformer(object):
@@ -688,7 +690,12 @@ the ones that failed to resolve removed."""
 
             uscore_enums[uscored] = enum
 
-            no_uscore_prefixed = self._transformer.strip_identifier_or_warn(type_name)
+            try:
+                no_uscore_prefixed = self._transformer.strip_identifier(type_name)
+            except TransformerException, e:
+                message.warn(e)
+                no_uscore_prefixed = None
+
             if no_uscore_prefixed not in uscore_enums:
                 uscore_enums[no_uscore_prefixed] = enum
 
diff --git a/giscanner/transformer.py b/giscanner/transformer.py
index 9b061a3..30f7bbf 100644
--- a/giscanner/transformer.py
+++ b/giscanner/transformer.py
@@ -24,6 +24,7 @@ import re
 
 from . import ast
 from . import glibast
+from . import message
 from .cachestore import CacheStore
 from .config import DATADIR, GIR_DIR, GIR_SUFFIX
 from .girparser import GIRParser
@@ -36,6 +37,10 @@ from .sourcescanner import (
     CSYMBOL_TYPE_MEMBER, CSYMBOL_TYPE_ELLIPSIS, CSYMBOL_TYPE_CONST,
     TYPE_QUALIFIER_CONST)
 
+class TransformerException(Exception):
+    pass
+
+
 _xdg_data_dirs = [x for x in os.environ.get('XDG_DATA_DIRS', '').split(':') \
                       + [DATADIR, '/usr/share'] if x]
 
@@ -317,26 +322,26 @@ raise ValueError."""
         matches = self._split_c_string_for_namespace_matches(symbol, is_identifier=False)
         return matches[-1]
 
-    def strip_identifier_or_warn(self, ident, fatal=False):
+    def strip_identifier(self, ident):
         hidden = ident.startswith('_')
         if hidden:
             ident = ident[1:]
         try:
             matches = self.split_ctype_namespaces(ident)
         except ValueError, e:
-            self.log_warning(str(e), fatal=fatal)
-            return None
+            raise TransformerException(str(e))
         for ns, name in matches:
             if ns is self._namespace:
                 if hidden:
                     return '_' + name
                 return name
         (ns, name) = matches[-1]
-        self.log_warning("Skipping foreign identifier %r from namespace %s" % (ident, ns.name, ),
-                         fatal=fatal)
+        raise TransformerException(
+            "Skipping foreign identifier %r from namespace %s" % (
+            ident, ns.name, ))
         return None
 
-    def _strip_symbol_or_warn(self, symbol, is_constant=False, fatal=False):
+    def _strip_symbol(self, symbol, is_constant=False):
         ident = symbol.ident
         if is_constant:
             # Temporarily lowercase
@@ -347,13 +352,10 @@ raise ValueError."""
         try:
             (ns, name) = self.split_csymbol(ident)
         except ValueError, e:
-            self.log_symbol_warning(symbol, "Unknown namespace", fatal=fatal)
-            return None
+            raise TransformerException("Unknown namespace")
         if ns != self._namespace:
-            self.log_symbol_warning(symbol,
-"Skipping foreign symbol from namespace %s" % (ns.name, ),
-                                    fatal=fatal)
-            return None
+            raise TransformerException(
+                "Skipping foreign symbol from namespace %s" % (ns.name, ))
         if is_constant:
             name = name.upper()
         if hidden:
@@ -421,15 +423,19 @@ raise ValueError."""
                 # Ok, the enum members don't have a consistent prefix
                 # among them, so let's just remove the global namespace
                 # prefix.
-                name = self._strip_symbol_or_warn(child, is_constant=True)
-                if name is None:
+                try:
+                    name = self._strip_symbol(child, is_constant=True)
+                except TransformerException, e:
+                    message.warn_symbol(child, e)
                     return None
             members.append(ast.Member(name.lower(),
-                                  child.const_int,
-                                  child.ident))
+                                      child.const_int,
+                                      child.ident))
 
-        enum_name = self.strip_identifier_or_warn(symbol.ident)
-        if not enum_name:
+        try:
+            enum_name = self.strip_identifier(symbol.ident)
+        except TransformerException, e:
+            message.warn(e)
             return None
         if symbol.base_type.is_bitfield:
             klass = ast.Bitfield
@@ -442,8 +448,10 @@ raise ValueError."""
     def _create_function(self, symbol):
         parameters = list(self._create_parameters(symbol.base_type))
         return_ = self._create_return(symbol.base_type.base_type)
-        name = self._strip_symbol_or_warn(symbol)
-        if not name:
+        try:
+            name = self._strip_symbol(symbol)
+        except TransformerException, e:
+            message.warn_symbol(symbol, e)
             return None
         func = ast.Function(name, return_, parameters, False, symbol.ident)
         func.add_symbol_reference(symbol)
@@ -523,8 +531,10 @@ raise ValueError."""
                        CTYPE_POINTER,
                        CTYPE_BASIC_TYPE,
                        CTYPE_VOID):
-            name = self.strip_identifier_or_warn(symbol.ident)
-            if not name:
+            try:
+                name = self.strip_identifier(symbol.ident)
+            except TransformerException, e:
+                message.warn(e)
                 return None
             if symbol.base_type.name:
                 target = self.create_type_from_ctype_string(symbol.base_type.name)
@@ -648,8 +658,10 @@ raise ValueError."""
         # ignore non-uppercase defines
         if not self.UCASE_CONSTANT_RE.match(symbol.ident):
             return None
-        name = self._strip_symbol_or_warn(symbol, is_constant=True)
-        if not name:
+        try:
+            name = self._strip_symbol(symbol, is_constant=True)
+        except TransformerException, e:
+            message.warn_symbol(symbol, e)
             return None
         if symbol.const_string is not None:
             typeval = ast.TYPE_STRING
@@ -668,8 +680,10 @@ raise ValueError."""
         return const
 
     def _create_typedef_struct(self, symbol, disguised=False):
-        name = self.strip_identifier_or_warn(symbol.ident)
-        if not name:
+        try:
+            name = self.strip_identifier(symbol.ident)
+        except TransformerException, e:
+            message.warn(e)
             return None
         struct = ast.Record(name, symbol.ident, disguised)
         self._parse_fields(symbol, struct)
@@ -678,8 +692,10 @@ raise ValueError."""
         return None
 
     def _create_typedef_union(self, symbol):
-        name = self.strip_identifier_or_warn(symbol.ident)
-        if not name:
+        try:
+            name = self.strip_identifier(symbol.ident)
+        except TransformerException, e:
+            message.warn(e)
             return None
         union = ast.Union(name, symbol.ident)
         self._parse_fields(symbol, union)
@@ -724,8 +740,10 @@ raise ValueError."""
                 if anonymous:
                     name = symbol.ident
                 else:
-                    name = self.strip_identifier_or_warn(symbol.ident)
-                    if not name:
+                    try:
+                        name = self.strip_identifier(symbol.ident)
+                    except TransformerException, e:
+                        message.warn(e)
                         return None
                 compound = klass(name, symbol.ident)
 
@@ -752,12 +770,16 @@ raise ValueError."""
         if member:
             name = symbol.ident
         elif symbol.ident.find('_') > 0:
-            name = self._strip_symbol_or_warn(symbol)
-            if not name:
+            try:
+                name = self._strip_symbol(symbol)
+            except TransformerException, e:
+                message.warn_symbol(symbol, e)
                 return None
         else:
-            name = self.strip_identifier_or_warn(symbol.ident)
-            if not name:
+            try:
+                name = self.strip_identifier(symbol.ident)
+            except TransformerException, e:
+                message.warn(e)
                 return None
         callback = ast.Callback(name, retval, parameters, False)
         callback.add_symbol_reference(symbol)



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