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
This commit is contained in:
Iñigo Martínez 2018-01-12 08:55:36 +01:00
parent e2054240c2
commit dcc1fe09d0
5 changed files with 59 additions and 16 deletions

View File

@ -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'

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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 = ''