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