Format and document colorize

pull/2458/head
Jimmy Brisson 2016-08-15 13:50:00 -05:00
parent cb5e61028d
commit 5fbcc25b46
1 changed files with 41 additions and 32 deletions

View File

@ -1,19 +1,17 @@
"""
mbed SDK
Copyright (c) 2016 ARM Limited
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# mbed SDK
# Copyright (c) 2016 ARM Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" This python file is responsible for generating colorized notifiers.
"""
@ -23,7 +21,7 @@ import re
from colorama import init, Fore, Back, Style
init()
colors = {
COLORS = {
'none' : "",
'default' : Style.RESET_ALL,
@ -46,26 +44,37 @@ colors = {
'on_white' : Back.WHITE,
}
# Convert a color string from a string into an ascii escape code that will print
# that color on the terminal.
color_matcher = re.compile(r"(\w+)(\W+on\W+\w+)?")
COLOR_MATCHER = re.compile(r"(\w+)(\W+on\W+\w+)?")
def colorstring_to_escapecode(color_string):
match = re.match(color_matcher, color_string)
if match:
return colors[match.group(1)] + (colors[match.group(2).strip().replace(" ","_")] if match.group(2) else "")
else:
return corols['default']
""" Convert a color string from a string into an ascii escape code that
will print that color on the terminal.
# Wrap a toolchain notifier in a colorizer. This colorizer will wrap notifications
# in a color if the severity matches a color in the *color_map*.
def print_in_color_notifier (color_map, print_fn):
Positional arguments:
color_string - the string to parse
"""
match = re.match(COLOR_MATCHER, color_string)
if match:
return COLORS[match.group(1)] + \
(COLORS[match.group(2).strip().replace(" ", "_")]
if match.group(2) else "")
else:
return COLORS['default']
def print_in_color_notifier(color_map, print_fn):
""" Wrap a toolchain notifier in a colorizer. This colorizer will wrap
notifications in a color if the severity matches a color in the *color_map*.
"""
def wrap(event, silent=False):
fd = sys.stdout
"""The notification function inself"""
file_desc = sys.stdout
self = event['toolchain']
if fd.isatty() and 'severity' in event and event['severity'] in color_map:
fd.write(colorstring_to_escapecode(color_map[event['severity']]))
if file_desc.isatty() and 'severity' in event and \
event['severity'] in color_map:
file_desc.write(colorstring_to_escapecode(
color_map[event['severity']]))
print_fn(self, event, silent)
fd.write(colorstring_to_escapecode('default'))
file_desc.write(colorstring_to_escapecode('default'))
else:
print_fn(self, event, silent)
return wrap