|
| 1 | +""" |
| 2 | +mbed SDK |
| 3 | +Copyright (c) 2016 ARM Limited |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +""" |
| 17 | + |
| 18 | +""" This python file is responsible for generating colorized notifiers. |
| 19 | +""" |
| 20 | + |
| 21 | +import sys |
| 22 | +import re |
| 23 | +from colorama import init |
| 24 | +init() |
| 25 | + |
| 26 | +colors = { |
| 27 | + 'none' : "", |
| 28 | + 'default' : "\033[.0m", |
| 29 | + 'bold' : "\033[.1m", |
| 30 | + 'underline' : "\033[.4m", |
| 31 | + 'blink' : "\033[.5m", |
| 32 | + 'reverse' : "\033[.7m", |
| 33 | + 'concealed' : "\033[.8m", |
| 34 | + |
| 35 | + 'black' : "\033[.30m", |
| 36 | + 'red' : "\033[.31m", |
| 37 | + 'green' : "\033[.32m", |
| 38 | + 'yellow' : "\033[.33m", |
| 39 | + 'blue' : "\033[.34m", |
| 40 | + 'magenta' : "\033[.35m", |
| 41 | + 'cyan' : "\033[.36m", |
| 42 | + 'white' : "\033[.37m", |
| 43 | + |
| 44 | + 'on_black' : "\033[.40m", |
| 45 | + 'on_red' : "\033[.41m", |
| 46 | + 'on_green' : "\033[.42m", |
| 47 | + 'on_yellow' : "\033[.43m", |
| 48 | + 'on_blue' : "\033[.44m", |
| 49 | + 'on_magenta' : "\033[.45m", |
| 50 | + 'on_cyan' : "\033[46m", |
| 51 | + 'on_white' : "\033[47m", |
| 52 | +} |
| 53 | + |
| 54 | +# Convert a color string from a string into an ascii escape code that will print |
| 55 | +# that color on the terminal. |
| 56 | +color_matcher = re.compile(r"(\w+)(\W+on\W+\w+)?") |
| 57 | +def colorstring_to_escapecode(color_string): |
| 58 | + match = re.match(color_matcher, color_string) |
| 59 | + if match: |
| 60 | + return colors[match.group(1)] + (colors[match.group(2).strip().replace(" ","_")] if match.group(2) else "") |
| 61 | + else: |
| 62 | + return corols['default'] |
| 63 | + |
| 64 | +# Wrap a toolchain notifier in a colorizer. This colorizer will wrap notifications |
| 65 | +# in a color if the severity matches a color in the *color_map*. |
| 66 | +def print_in_color_notifier (color_map, print_fn): |
| 67 | + def wrap(event, silent=False): |
| 68 | + fd = sys.stdout |
| 69 | + if fd.isatty() and 'severity' in event and event['severity'] in color_map: |
| 70 | + fd.write(colorstring_to_escapecode(color_map[event['severity']])) |
| 71 | + print_fn(event, silent) |
| 72 | + fd.write(colorstring_to_escapecode('default')) |
| 73 | + else: |
| 74 | + print_fn(event, silent) |
| 75 | + return wrap |
0 commit comments