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.
This commit is contained in:
Emmanuele Bassi 2017-07-17 10:32:33 +01:00
parent 69515e9f5c
commit 7ee050dc4b

View File

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