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