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