Clone of official tools

Revision:
26:ed5e0d4e691e
Child:
29:1210849dba19
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/colorize.py	Thu Aug 04 10:32:55 2016 +0100
@@ -0,0 +1,71 @@
+"""
+mbed SDK
+Copyright (c) 2016 ARM Limited
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+"""
+
+""" This python file is responsible for generating colorized notifiers.
+"""
+
+import sys
+import re
+from colorama import init, Fore, Back, Style
+init()
+
+colors = {
+    'none' : "",
+    'default' : Style.RESET_ALL,
+
+    'black'   : Fore.BLACK,
+    'red'     : Fore.RED,
+    'green'   : Fore.GREEN,
+    'yellow'  : Fore.YELLOW,
+    'blue'    : Fore.BLUE,
+    'magenta' : Fore.MAGENTA,
+    'cyan'    : Fore.CYAN,
+    'white'   : Fore.WHITE,
+
+    'on_black'   : Back.BLACK,
+    'on_red'     : Back.RED,
+    'on_green'   : Back.GREEN,
+    'on_yellow'  : Back.YELLOW,
+    'on_blue'    : Back.BLUE,
+    'on_magenta' : Back.MAGENTA,
+    'on_cyan'    : Back.CYAN,
+    'on_white'   : Back.WHITE,
+}
+
+# Convert a color string from a string into an ascii escape code that will print
+# that color on the terminal.
+color_matcher = re.compile(r"(\w+)(\W+on\W+\w+)?")
+def colorstring_to_escapecode(color_string):
+    match = re.match(color_matcher, color_string)
+    if match:
+        return colors[match.group(1)] + (colors[match.group(2).strip().replace(" ","_")] if match.group(2) else "")
+    else:
+        return corols['default']
+
+# Wrap a toolchain notifier in a colorizer. This colorizer will wrap notifications
+# in a color if the severity matches a color in the *color_map*.
+def print_in_color_notifier (color_map, print_fn):
+    def wrap(event, silent=False):
+        fd = sys.stdout
+        self = event['toolchain']
+        if fd.isatty() and 'severity' in event and event['severity'] in color_map:
+            fd.write(colorstring_to_escapecode(color_map[event['severity']]))
+            print_fn(self, event, silent)
+            fd.write(colorstring_to_escapecode('default'))
+        else:
+            print_fn(self, event, silent)
+    return wrap