[glib] gdbus-codegen: Use Color's print_* methods



commit dcc1fe09d01bb1a6141b88e44ad63f79e9edf2de
Author: Iñigo Martínez <inigomartinez gmail com>
Date:   Fri Jan 12 08:55:36 2018 +0100

    gdbus-codegen: Use Color's print_* methods
    
    `glib-genmarshal` and `glib-mkenums` use a `Color` class which
    implements a number of print_* methods to print colored messages
    to the standard error output.
    
    In order to be consistent with those programs' output,
    `gdbus-codegen` has also started using that same class and methods.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=791015

 gio/gdbus-2.0/codegen/codegen.py      |   11 +++++----
 gio/gdbus-2.0/codegen/codegen_main.py |   14 +++++-----
 gio/gdbus-2.0/codegen/dbustypes.py    |    5 ++-
 gio/gdbus-2.0/codegen/parser.py       |    5 ++-
 gio/gdbus-2.0/codegen/utils.py        |   40 +++++++++++++++++++++++++++++++++
 5 files changed, 59 insertions(+), 16 deletions(-)
---
diff --git a/gio/gdbus-2.0/codegen/codegen.py b/gio/gdbus-2.0/codegen/codegen.py
index fd5c1cc..e508da1 100644
--- a/gio/gdbus-2.0/codegen/codegen.py
+++ b/gio/gdbus-2.0/codegen/codegen.py
@@ -24,6 +24,7 @@ import sys
 from . import config
 from . import utils
 from . import dbustypes
+from .utils import print_error
 
 LICENSE_STR = '''/*
  * Generated by gdbus-codegen {!s}. DO NOT EDIT.
@@ -1156,7 +1157,7 @@ class CodeGenerator:
                 elif p.writable:
                     hint = 'Since the D-Bus property for this #GObject property is writable but not 
readable, it is meaningful to write to it on both the client- and service-side. It is only meaningful, 
however, to read from it on the service-side.'
                 else:
-                    raise RuntimeError('Cannot handle property %s that neither readable nor 
writable'%(p.name))
+                    print_error('Cannot handle property "{}" that neither readable nor 
writable'.format(p.name))
                 self.c.write(self.docbook_gen.expand(
                         '  /**\n'
                         '   * %s:%s:\n'
@@ -1202,7 +1203,7 @@ class CodeGenerator:
                 elif p.arg.signature == 'aay':
                     s = 'g_param_spec_boxed ("%s", "%s", "%s", G_TYPE_STRV'%(p.name_hyphen, p.name, p.name)
                 else:
-                    raise RuntimeError('Unsupported gtype %s for GParamSpec'%(p.arg.gtype))
+                    print_error('Unsupported gtype "{}" for GParamSpec'.format(p.arg.gtype))
                 self.c.write('    %s, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));'%s);
                 self.c.write('\n')
 
@@ -1221,7 +1222,7 @@ class CodeGenerator:
             elif p.writable:
                 hint = 'Since this D-Bus property is not readable, it is only meaningful to use this 
function on the service-side.'
             else:
-                raise RuntimeError('Cannot handle property %s that neither readable nor writable'%(p.name))
+                print_error('Cannot handle property "{}" that neither readable nor writable'.format(p.name))
             self.c.write(self.docbook_gen.expand(
                     '/**\n'
                     ' * %s_get_%s: (skip)\n'
@@ -1277,7 +1278,7 @@ class CodeGenerator:
             elif p.writable:
                 hint = 'Since this D-Bus property is writable, it is meaningful to use this function on both 
the client- and service-side.'
             else:
-                raise RuntimeError('Cannot handle property %s that neither readable nor writable'%(p.name))
+                print_error('Cannot handle property "{}" that neither readable nor writable'.format(p.name))
             self.c.write(self.docbook_gen.expand(
                     '/**\n'
                     ' * %s_set_%s: (skip)\n'
@@ -3397,7 +3398,7 @@ class CodeGenerator:
             elif isinstance(obj, dbustypes.Property):
                 thing = 'The D-Bus property'
             else:
-                raise RuntimeError('Cannot handle object ', obj)
+                print_error('Cannot handle object "{}"'.format(obj))
             f.write(self.docbook_gen.expand(
                     '%*s *\n'
                     '%*s * Deprecated: %s has been deprecated.\n'
diff --git a/gio/gdbus-2.0/codegen/codegen_main.py b/gio/gdbus-2.0/codegen/codegen_main.py
index 0f26f11..eb387ef 100755
--- a/gio/gdbus-2.0/codegen/codegen_main.py
+++ b/gio/gdbus-2.0/codegen/codegen_main.py
@@ -24,11 +24,11 @@ import optparse
 from os import path
 
 from . import config
-from . import utils
 from . import dbustypes
 from . import parser
 from . import codegen
 from . import codegen_docbook
+from .utils import print_error
 
 def find_arg(arg_list, arg_name):
     for a in arg_list:
@@ -62,38 +62,38 @@ def apply_annotation(iface_list, iface, method, signal, prop, arg, key, value):
             break
 
     if iface_obj == None:
-        raise RuntimeError('No interface %s'%iface)
+        print_error('No interface "{}"'.format(iface))
 
     target_obj = None
 
     if method:
         method_obj = find_method(iface_obj, method)
         if method_obj == None:
-            raise RuntimeError('No method %s on interface %s'%(method, iface))
+            print_error('No method "{}" on interface "{}"'.format(method, iface))
         if arg:
             arg_obj = find_arg(method_obj.in_args, arg)
             if (arg_obj == None):
                 arg_obj = find_arg(method_obj.out_args, arg)
                 if (arg_obj == None):
-                    raise RuntimeError('No arg %s on method %s on interface %s'%(arg, method, iface))
+                    print_error('No arg "{}" on method "{}" on interface "{}"'.format(arg, method, iface))
             target_obj = arg_obj
         else:
             target_obj = method_obj
     elif signal:
         signal_obj = find_signal(iface_obj, signal)
         if signal_obj == None:
-            raise RuntimeError('No signal %s on interface %s'%(signal, iface))
+            print_error('No signal "{}" on interface "{}"'.format(signal, iface))
         if arg:
             arg_obj = find_arg(signal_obj.args, arg)
             if (arg_obj == None):
-                raise RuntimeError('No arg %s on signal %s on interface %s'%(arg, signal, iface))
+                print_error('No arg "{}" on signal "{}" on interface "{}"'.format(arg, signal, iface))
             target_obj = arg_obj
         else:
             target_obj = signal_obj
     elif prop:
         prop_obj = find_prop(iface_obj, prop)
         if prop_obj == None:
-            raise RuntimeError('No property %s on interface %s'%(prop, iface))
+            print_error('No property "{}" on interface "{}"'.format(prop, iface))
         target_obj = prop_obj
     else:
         target_obj = iface_obj
diff --git a/gio/gdbus-2.0/codegen/dbustypes.py b/gio/gdbus-2.0/codegen/dbustypes.py
index 2dc8e11..bfc69f5 100644
--- a/gio/gdbus-2.0/codegen/dbustypes.py
+++ b/gio/gdbus-2.0/codegen/dbustypes.py
@@ -20,6 +20,7 @@
 # Author: David Zeuthen <davidz redhat com>
 
 from . import utils
+from .utils import print_error
 
 class Annotation:
     def __init__(self, key, value):
@@ -322,7 +323,7 @@ class Property:
         elif self.access == 'write':
             self.writable = True
         else:
-            raise RuntimeError('Invalid access type %s'%self.access)
+            print_error('Invalid access type "{}"'.format(self.access))
         self.doc_string = ''
         self.since = ''
         self.deprecated = False
@@ -399,7 +400,7 @@ class Interface:
             self.name_lower = cns_lower + overridden_name.lower()
             self.name_upper = overridden_name.upper()
 
-            #raise RuntimeError('handle Ugly_Case ', overridden_name)
+            #print_error('handle Ugly_Case "{}"'.format(overridden_name))
         else:
             if overridden_name:
                 name = overridden_name
diff --git a/gio/gdbus-2.0/codegen/parser.py b/gio/gdbus-2.0/codegen/parser.py
index df8f845..f49136d 100644
--- a/gio/gdbus-2.0/codegen/parser.py
+++ b/gio/gdbus-2.0/codegen/parser.py
@@ -23,6 +23,7 @@ import sys
 import xml.parsers.expat
 
 from . import dbustypes
+from .utils import print_error
 
 class DBusXMLParser:
     STATE_TOP = 'top'
@@ -203,7 +204,7 @@ class DBusXMLParser:
                 elif direction == 'out':
                     self._cur_object.out_args.append(arg)
                 else:
-                    raise RuntimeError('Invalid direction "%s"'%(direction))
+                    print_error('Invalid direction "{}"'.format(direction))
                 self._cur_object = arg
             elif name == DBusXMLParser.STATE_ANNOTATION:
                 self.state = DBusXMLParser.STATE_ANNOTATION
@@ -278,7 +279,7 @@ class DBusXMLParser:
                 self.state = DBusXMLParser.STATE_IGNORED
 
         else:
-            raise RuntimeError('Unhandled state "%s" while entering element with name "%s"'%(self.state, 
name))
+            print_error('Unhandled state "{}" while entering element with name "{}"'.format(self.state, 
name))
 
         self.state_stack.append(old_state)
         self._cur_object_stack.append(old_cur_object)
diff --git a/gio/gdbus-2.0/codegen/utils.py b/gio/gdbus-2.0/codegen/utils.py
index 39e0463..bdfa703 100644
--- a/gio/gdbus-2.0/codegen/utils.py
+++ b/gio/gdbus-2.0/codegen/utils.py
@@ -20,6 +20,46 @@
 # Author: David Zeuthen <davidz redhat com>
 
 import distutils.version
+import os
+import sys
+
+# 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
+    sys.stderr.write('{prefix}: {msg}\n'.format(prefix=real_prefix, msg=msg))
+
+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 strip_dots(s):
     ret = ''


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