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