ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers colorize.py Source File

colorize.py

00001 # mbed SDK
00002 # Copyright (c) 2016 ARM Limited
00003 #
00004 # Licensed under the Apache License, Version 2.0 (the "License");
00005 # you may not use this file except in compliance with the License.
00006 # You may obtain a copy of the License at
00007 #
00008 #     http://www.apache.org/licenses/LICENSE-2.0
00009 #
00010 # Unless required by applicable law or agreed to in writing, software
00011 # distributed under the License is distributed on an "AS IS" BASIS,
00012 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 # See the License for the specific language governing permissions and
00014 # limitations under the License.
00015 
00016 """ This python file is responsible for generating colorized notifiers.
00017 """
00018 
00019 import sys
00020 import re
00021 from colorama import init, Fore, Back, Style
00022 init()
00023 
00024 COLORS = {
00025     'none' : "",
00026     'default' : Style.RESET_ALL,
00027 
00028     'black'   : Fore.BLACK,
00029     'red'     : Fore.RED,
00030     'green'   : Fore.GREEN,
00031     'yellow'  : Fore.YELLOW,
00032     'blue'    : Fore.BLUE,
00033     'magenta' : Fore.MAGENTA,
00034     'cyan'    : Fore.CYAN,
00035     'white'   : Fore.WHITE,
00036 
00037     'on_black'   : Back.BLACK,
00038     'on_red'     : Back.RED,
00039     'on_green'   : Back.GREEN,
00040     'on_yellow'  : Back.YELLOW,
00041     'on_blue'    : Back.BLUE,
00042     'on_magenta' : Back.MAGENTA,
00043     'on_cyan'    : Back.CYAN,
00044     'on_white'   : Back.WHITE,
00045 }
00046 
00047 COLOR_MATCHER = re.compile(r"(\w+)(\W+on\W+\w+)?")
00048 def colorstring_to_escapecode(color_string):
00049     """ Convert a color string from a string into an ascii escape code that
00050     will print that color on the terminal.
00051 
00052     Positional arguments:
00053     color_string - the string to parse
00054     """
00055     match = re.match(COLOR_MATCHER, color_string)
00056     if match:
00057         return COLORS[match.group(1)] + \
00058             (COLORS[match.group(2).strip().replace(" ", "_")]
00059              if match.group(2) else "")
00060     else:
00061         return COLORS['default']
00062 
00063 
00064 def print_in_color_notifier(color_map, print_fn):
00065     """ Wrap a toolchain notifier in a colorizer. This colorizer will wrap
00066     notifications in a color if the severity matches a color in the *color_map*.
00067     """
00068     def wrap(event, silent=False):
00069         """The notification function inself"""
00070         file_desc = sys.stdout
00071         self = event['toolchain']
00072         if file_desc.isatty() and 'severity' in event and \
00073            event['severity'] in color_map:
00074             file_desc.write(colorstring_to_escapecode(
00075                 color_map[event['severity']]))
00076             print_fn(self, event, silent)
00077             file_desc.write(colorstring_to_escapecode('default'))
00078         else:
00079             print_fn(self, event, silent)
00080     return wrap