[gobject-introspection] giscanner: give message.ERROR a purpose



commit 6d9022311b5d3bfb48895466880c2a15235b63a6
Author: Dieter Verfaillie <dieterv optionexplicit be>
Date:   Wed Jul 24 16:56:20 2013 +0200

    giscanner: give message.ERROR a purpose
    
    It's not yet being used but will be in the future by
    annotationparser.py to signal the difference between
    message.WARNING:
        something is wrong but the comment block can still
        be parsed and serialized back into a comment block
        without information being lost
    message.ERROR:
        something is wrong and the comment block can *not*
        be parsed and serialized back into a comment block
        without information being lost
    
    Different tools can then act accordingly. Nothing will
    change for g-ir-scanner but this will be important for
    the GTK-Doc comment block rewriting tool to prevent
    extremely broken input leading to even more broken
    output...

 giscanner/message.py                          |   49 ++++++++++++++++---------
 giscanner/scannermain.py                      |    3 +-
 tests/scanner/annotationparser/test_parser.py |    4 +-
 tests/warn/warningtester.py                   |    4 +-
 4 files changed, 37 insertions(+), 23 deletions(-)
---
diff --git a/giscanner/message.py b/giscanner/message.py
index 5890835..1a0a6c5 100644
--- a/giscanner/message.py
+++ b/giscanner/message.py
@@ -31,7 +31,8 @@ from . import utils
 
 
 class Position(object):
-    """Represents a position in the source file which we
+    """
+    Represents a position in the source file which we
     want to inform about.
     """
 
@@ -47,10 +48,8 @@ class Position(object):
                    (other.filename, other.line, other.column))
 
     def __repr__(self):
-        return '<Position %s:%d:%d>' % (
-            os.path.basename(self.filename),
-            self.line or -1,
-            self.column or -1)
+        return '<Position %s:%d:%d>' % (os.path.basename(self.filename), self.line or -1,
+                                        self.column or -1)
 
     def format(self, cwd):
         filename = os.path.realpath(self.filename)
@@ -76,8 +75,9 @@ class MessageLogger(object):
         self._cwd = os.getcwd()
         self._output = output
         self._namespace = namespace
-        self._enable_warnings = False
+        self._enable_warnings = []
         self._warning_count = 0
+        self._error_count = 0
 
     @classmethod
     def get(cls, *args, **kwargs):
@@ -85,24 +85,27 @@ class MessageLogger(object):
             cls._instance = cls(*args, **kwargs)
         return cls._instance
 
-    def enable_warnings(self, enable):
-        self._enable_warnings = enable
+    def enable_warnings(self, log_types):
+        self._enable_warnings = log_types
 
     def get_warning_count(self):
         return self._warning_count
 
+    def get_error_count(self):
+        return self._error_count
+
     def log(self, log_type, text, positions=None, prefix=None):
-        """Log a warning, using optional file positioning information.
-If the warning is related to a ast.Node type, see log_node()."""
+        """
+        Log a warning, using optional file positioning information.
+        If the warning is related to a ast.Node type, see log_node().
+        """
         utils.break_on_debug_flag('warning')
 
         self._warning_count += 1
 
-        if not self._enable_warnings and log_type != FATAL:
+        if not log_type in self._enable_warnings:
             return
 
-        # Always drop through on fatal
-
         if type(positions) == set:
             positions = list(positions)
         if isinstance(positions, Position):
@@ -119,8 +122,10 @@ If the warning is related to a ast.Node type, see log_node()."""
             error_type = "Warning"
         elif log_type == ERROR:
             error_type = "Error"
+            self._error_count += 1
         elif log_type == FATAL:
             error_type = "Fatal"
+
         if prefix:
             text = ('%s: %s: %s: %s: %s\n' % (last_position, error_type,
                                               self._namespace.name, prefix, text))
@@ -132,16 +137,19 @@ If the warning is related to a ast.Node type, see log_node()."""
                 text = ('%s: %s: %s\n' % (last_position, error_type, text))
 
         self._output.write(text)
+
         if log_type == FATAL:
             utils.break_on_debug_flag('fatal')
             raise SystemExit(text)
 
     def log_node(self, log_type, node, text, context=None, positions=None):
-        """Log a warning, using information about file positions from
-the given node.  The optional context argument, if given, should be
-another ast.Node type which will also be displayed.  If no file position
-information is available from the node, the position data from the
-context will be used."""
+        """
+        Log a warning, using information about file positions from
+        the given node.  The optional context argument, if given, should be
+        another ast.Node type which will also be displayed.  If no file position
+        information is available from the node, the position data from the
+        context will be used.
+        """
         if positions:
             pass
         elif getattr(node, 'file_positions', None):
@@ -185,6 +193,11 @@ def warn_symbol(symbol, text):
     ml.log_symbol(WARNING, symbol, text)
 
 
+def error(text, positions=None, prefix=None):
+    ml = MessageLogger.get()
+    ml.log(ERROR, text, positions, prefix)
+
+
 def fatal(text, positions=None, prefix=None):
     ml = MessageLogger.get()
     ml.log(FATAL, text, positions, prefix)
diff --git a/giscanner/scannermain.py b/giscanner/scannermain.py
index e4436b8..ab8f5bb 100755
--- a/giscanner/scannermain.py
+++ b/giscanner/scannermain.py
@@ -452,7 +452,8 @@ def scanner_main(args):
     namespace = create_namespace(options)
     logger = message.MessageLogger.get(namespace=namespace)
     if options.warn_all:
-        logger.enable_warnings(True)
+        logger.enable_warnings((message.WARNING, message.ERROR, message.FATAL))
+
     transformer = create_transformer(namespace, options)
 
     packages = set(options.packages)
diff --git a/tests/scanner/annotationparser/test_parser.py b/tests/scanner/annotationparser/test_parser.py
index adf4f65..1b82625 100644
--- a/tests/scanner/annotationparser/test_parser.py
+++ b/tests/scanner/annotationparser/test_parser.py
@@ -35,7 +35,7 @@ import xml.etree.ElementTree as etree
 
 from giscanner.annotationparser import GtkDocCommentBlockParser
 from giscanner.ast import Namespace
-from giscanner.message import MessageLogger
+from giscanner.message import MessageLogger, WARNING, ERROR, FATAL
 
 
 XML_NS = 'http://schemas.gnome.org/gobject-introspection/2013/test'
@@ -358,7 +358,7 @@ if __name__ == '__main__':
     # Initialize message logger
     namespace = Namespace('Test', '1.0')
     logger = MessageLogger.get(namespace=namespace)
-    logger.enable_warnings(True)
+    logger.enable_warnings((WARNING, ERROR, FATAL))
 
     # Load test cases from disc
     tests_dir = os.path.dirname(os.path.abspath(__file__))
diff --git a/tests/warn/warningtester.py b/tests/warn/warningtester.py
index c78f92a..b211791 100644
--- a/tests/warn/warningtester.py
+++ b/tests/warn/warningtester.py
@@ -14,7 +14,7 @@ from giscanner.annotationparser import GtkDocCommentBlockParser
 from giscanner.ast import Include, Namespace
 from giscanner.introspectablepass import IntrospectablePass
 from giscanner.maintransformer import MainTransformer
-from giscanner.message import MessageLogger
+from giscanner.message import MessageLogger, WARNING, ERROR, FATAL
 from giscanner.sourcescanner import SourceScanner
 from giscanner.transformer import Transformer
 from giscanner.scannermain import process_packages
@@ -97,7 +97,7 @@ def check(args):
     output = ChunkedIO()
     namespace = Namespace('Test', '1.0')
     logger = MessageLogger.get(namespace=namespace, output=output)
-    logger.enable_warnings(True)
+    logger.enable_warnings((WARNING, ERROR, FATAL))
 
     transformer = Transformer(namespace)
     transformer.set_include_paths([os.path.join(top_srcdir, 'gir'), top_builddir])


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