[glib] mkenums: Use the same reporting functions from genmarshal



commit 7ee050dc4bf5187842f656889017414aa1c9a729
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Mon Jul 17 10:32:33 2017 +0100

    mkenums: Use the same reporting functions from genmarshal
    
    We can reuse the same code to make error reporting stand out a bit more,
    with colors and potentially with the ability to make warnings fatal.

 gobject/glib-mkenums.in |   49 ++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 46 insertions(+), 3 deletions(-)
---
diff --git a/gobject/glib-mkenums.in b/gobject/glib-mkenums.in
index c59d17c..6242561 100755
--- a/gobject/glib-mkenums.in
+++ b/gobject/glib-mkenums.in
@@ -17,6 +17,49 @@ import tempfile
 
 output_stream = sys.stdout
 
+# pylint: disable=too-few-public-methods
+class Color:
+    '''ANSI Terminal colors'''
+    GREEN = '\033[1;32m'
+    BLUE = '\033[1;34m'
+    YELLOW = '\033[1;33m'
+    RED = '\033[1;31m'
+    END = '\033[0m'
+
+
+def print_color(msg, color=Color.END, prefix='MESSAGE'):
+    '''Print a string with a color prefix'''
+    if os.isatty(sys.stderr.fileno()):
+        real_prefix = '{start}{prefix}{end}'.format(start=color, prefix=prefix, end=Color.END)
+    else:
+        real_prefix = prefix
+    print('{prefix}: {msg}'.format(prefix=real_prefix, msg=msg), file=sys.stderr)
+
+
+def print_error(msg):
+    '''Print an error, and terminate'''
+    print_color(msg, color=Color.RED, prefix='ERROR')
+    sys.exit(1)
+
+
+def print_warning(msg, fatal=False):
+    '''Print a warning, and optionally terminate'''
+    if fatal:
+        color = Color.RED
+        prefix = 'ERROR'
+    else:
+        color = Color.YELLOW
+        prefix = 'WARNING'
+    print_color(msg, color, prefix)
+    if fatal:
+        sys.exit(1)
+
+
+def print_info(msg):
+    '''Print a message'''
+    print_color(msg, color=Color.GREEN, prefix='INFO')
+
+
 def write_output(output):
     global output_stream
     print(output, file=output_stream)
@@ -352,7 +395,7 @@ def process_file(curfilename):
     try:
         curfile = open(curfilename)
     except FileNotFoundError:
-        sys.stderr.write('WARNING: No file "{}" found.'.format(curfilename))
+        print_warning('No file "{}" found.'.format(curfilename))
         return
 
     for line in curfile:
@@ -393,10 +436,10 @@ def process_file(curfilename):
 
             if option_lowercase_name is not None:
                 if option_underscore_name is not None:
-                    print("$0: $ARGV:$.: lowercase_name overriden with underscore_name", file=sys.stderr)
+                    print_warning("lowercase_name overriden with underscore_name")
                     option_lowercase_name = None
                 else:
-                    print("$0: $ARGV:$.: lowercase_name is deprecated, use underscore_name", file=sys.stderr)
+                    print_warning("lowercase_name is deprecated, use underscore_name")
 
             # Didn't have trailing '{' look on next lines
             if groups[0] is None and (len(groups) < 4 or groups[3] is None):


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