|
| 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, Fore, Back, Style |
| 24 | +init() |
| 25 | + |
| 26 | +colors = { |
| 27 | + 'none' : "", |
| 28 | + 'default' : Style.RESET_ALL, |
| 29 | + |
| 30 | + 'black' : Fore.BLACK, |
| 31 | + 'red' : Fore.RED, |
| 32 | + 'green' : Fore.GREEN, |
| 33 | + 'yellow' : Fore.YELLOW, |
| 34 | + 'blue' : Fore.BLUE, |
| 35 | + 'magenta' : Fore.MAGENTA, |
| 36 | + 'cyan' : Fore.CYAN, |
| 37 | + 'white' : Fore.WHITE, |
| 38 | + |
| 39 | + 'on_black' : Back.BLACK, |
| 40 | + 'on_red' : Back.RED, |
| 41 | + 'on_green' : Back.GREEN, |
| 42 | + 'on_yellow' : Back.YELLOW, |
| 43 | + 'on_blue' : Back.BLUE, |
| 44 | + 'on_magenta' : Back.MAGENTA, |
| 45 | + 'on_cyan' : Back.CYAN, |
| 46 | + 'on_white' : Back.WHITE, |
| 47 | +} |
| 48 | + |
| 49 | +# Convert a color string from a string into an ascii escape code that will print |
| 50 | +# that color on the terminal. |
| 51 | +color_matcher = re.compile(r"(\w+)(\W+on\W+\w+)?") |
| 52 | +def colorstring_to_escapecode(color_string): |
| 53 | + match = re.match(color_matcher, color_string) |
| 54 | + if match: |
| 55 | + return colors[match.group(1)] + (colors[match.group(2).strip().replace(" ","_")] if match.group(2) else "") |
| 56 | + else: |
| 57 | + return corols['default'] |
| 58 | + |
| 59 | +# Wrap a toolchain notifier in a colorizer. This colorizer will wrap notifications |
| 60 | +# in a color if the severity matches a color in the *color_map*. |
| 61 | +def print_in_color_notifier (color_map, print_fn): |
| 62 | + def wrap(event, silent=False): |
| 63 | + fd = sys.stdout |
| 64 | + self = event['toolchain'] |
| 65 | + if fd.isatty() and 'severity' in event and event['severity'] in color_map: |
| 66 | + fd.write(colorstring_to_escapecode(color_map[event['severity']])) |
| 67 | + print_fn(self, event, silent) |
| 68 | + fd.write(colorstring_to_escapecode('default')) |
| 69 | + else: |
| 70 | + print_fn(self, event, silent) |
| 71 | + return wrap |
0 commit comments