Clone of official tools
colorize.py@29:1210849dba19, 2016-08-29 (annotated)
- Committer:
- screamer
- Date:
- Mon Aug 29 11:18:36 2016 +0100
- Revision:
- 29:1210849dba19
- Parent:
- 26:ed5e0d4e691e
Port the latest tools patches from https://github.com/ARMmbed/mbed-os
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 29:1210849dba19 | 1 | # mbed SDK |
screamer | 29:1210849dba19 | 2 | # Copyright (c) 2016 ARM Limited |
screamer | 29:1210849dba19 | 3 | # |
screamer | 29:1210849dba19 | 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
screamer | 29:1210849dba19 | 5 | # you may not use this file except in compliance with the License. |
screamer | 29:1210849dba19 | 6 | # You may obtain a copy of the License at |
screamer | 29:1210849dba19 | 7 | # |
screamer | 29:1210849dba19 | 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
screamer | 29:1210849dba19 | 9 | # |
screamer | 29:1210849dba19 | 10 | # Unless required by applicable law or agreed to in writing, software |
screamer | 29:1210849dba19 | 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
screamer | 29:1210849dba19 | 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
screamer | 29:1210849dba19 | 13 | # See the License for the specific language governing permissions and |
screamer | 29:1210849dba19 | 14 | # limitations under the License. |
screamer | 26:ed5e0d4e691e | 15 | |
screamer | 26:ed5e0d4e691e | 16 | """ This python file is responsible for generating colorized notifiers. |
screamer | 26:ed5e0d4e691e | 17 | """ |
screamer | 26:ed5e0d4e691e | 18 | |
screamer | 26:ed5e0d4e691e | 19 | import sys |
screamer | 26:ed5e0d4e691e | 20 | import re |
screamer | 26:ed5e0d4e691e | 21 | from colorama import init, Fore, Back, Style |
screamer | 26:ed5e0d4e691e | 22 | init() |
screamer | 26:ed5e0d4e691e | 23 | |
screamer | 29:1210849dba19 | 24 | COLORS = { |
screamer | 26:ed5e0d4e691e | 25 | 'none' : "", |
screamer | 26:ed5e0d4e691e | 26 | 'default' : Style.RESET_ALL, |
screamer | 26:ed5e0d4e691e | 27 | |
screamer | 26:ed5e0d4e691e | 28 | 'black' : Fore.BLACK, |
screamer | 26:ed5e0d4e691e | 29 | 'red' : Fore.RED, |
screamer | 26:ed5e0d4e691e | 30 | 'green' : Fore.GREEN, |
screamer | 26:ed5e0d4e691e | 31 | 'yellow' : Fore.YELLOW, |
screamer | 26:ed5e0d4e691e | 32 | 'blue' : Fore.BLUE, |
screamer | 26:ed5e0d4e691e | 33 | 'magenta' : Fore.MAGENTA, |
screamer | 26:ed5e0d4e691e | 34 | 'cyan' : Fore.CYAN, |
screamer | 26:ed5e0d4e691e | 35 | 'white' : Fore.WHITE, |
screamer | 26:ed5e0d4e691e | 36 | |
screamer | 26:ed5e0d4e691e | 37 | 'on_black' : Back.BLACK, |
screamer | 26:ed5e0d4e691e | 38 | 'on_red' : Back.RED, |
screamer | 26:ed5e0d4e691e | 39 | 'on_green' : Back.GREEN, |
screamer | 26:ed5e0d4e691e | 40 | 'on_yellow' : Back.YELLOW, |
screamer | 26:ed5e0d4e691e | 41 | 'on_blue' : Back.BLUE, |
screamer | 26:ed5e0d4e691e | 42 | 'on_magenta' : Back.MAGENTA, |
screamer | 26:ed5e0d4e691e | 43 | 'on_cyan' : Back.CYAN, |
screamer | 26:ed5e0d4e691e | 44 | 'on_white' : Back.WHITE, |
screamer | 26:ed5e0d4e691e | 45 | } |
screamer | 26:ed5e0d4e691e | 46 | |
screamer | 29:1210849dba19 | 47 | COLOR_MATCHER = re.compile(r"(\w+)(\W+on\W+\w+)?") |
screamer | 26:ed5e0d4e691e | 48 | def colorstring_to_escapecode(color_string): |
screamer | 29:1210849dba19 | 49 | """ Convert a color string from a string into an ascii escape code that |
screamer | 29:1210849dba19 | 50 | will print that color on the terminal. |
screamer | 29:1210849dba19 | 51 | |
screamer | 29:1210849dba19 | 52 | Positional arguments: |
screamer | 29:1210849dba19 | 53 | color_string - the string to parse |
screamer | 29:1210849dba19 | 54 | """ |
screamer | 29:1210849dba19 | 55 | match = re.match(COLOR_MATCHER, color_string) |
screamer | 26:ed5e0d4e691e | 56 | if match: |
screamer | 29:1210849dba19 | 57 | return COLORS[match.group(1)] + \ |
screamer | 29:1210849dba19 | 58 | (COLORS[match.group(2).strip().replace(" ", "_")] |
screamer | 29:1210849dba19 | 59 | if match.group(2) else "") |
screamer | 26:ed5e0d4e691e | 60 | else: |
screamer | 29:1210849dba19 | 61 | return COLORS['default'] |
screamer | 26:ed5e0d4e691e | 62 | |
screamer | 29:1210849dba19 | 63 | |
screamer | 29:1210849dba19 | 64 | def print_in_color_notifier(color_map, print_fn): |
screamer | 29:1210849dba19 | 65 | """ Wrap a toolchain notifier in a colorizer. This colorizer will wrap |
screamer | 29:1210849dba19 | 66 | notifications in a color if the severity matches a color in the *color_map*. |
screamer | 29:1210849dba19 | 67 | """ |
screamer | 26:ed5e0d4e691e | 68 | def wrap(event, silent=False): |
screamer | 29:1210849dba19 | 69 | """The notification function inself""" |
screamer | 29:1210849dba19 | 70 | file_desc = sys.stdout |
screamer | 26:ed5e0d4e691e | 71 | self = event['toolchain'] |
screamer | 29:1210849dba19 | 72 | if file_desc.isatty() and 'severity' in event and \ |
screamer | 29:1210849dba19 | 73 | event['severity'] in color_map: |
screamer | 29:1210849dba19 | 74 | file_desc.write(colorstring_to_escapecode( |
screamer | 29:1210849dba19 | 75 | color_map[event['severity']])) |
screamer | 26:ed5e0d4e691e | 76 | print_fn(self, event, silent) |
screamer | 29:1210849dba19 | 77 | file_desc.write(colorstring_to_escapecode('default')) |
screamer | 26:ed5e0d4e691e | 78 | else: |
screamer | 26:ed5e0d4e691e | 79 | print_fn(self, event, silent) |
screamer | 26:ed5e0d4e691e | 80 | return wrap |