Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
term.py
00001 # mbed SDK 00002 # Copyright (c) 2011-2013 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 from __future__ import print_function, division, absolute_import 00017 from past.builtins import basestring 00018 00019 import re 00020 import sys 00021 from os import getcwd 00022 from os.path import (basename, abspath) 00023 00024 from . import Notifier 00025 from ..settings import (PRINT_COMPILER_OUTPUT_AS_LINK, 00026 CLI_COLOR_MAP, COLOR) 00027 00028 class TerminalNotifier (Notifier): 00029 """ 00030 Writes notifications to a terminal based on silent, verbose and color flags. 00031 """ 00032 00033 def __init__(self, verbose=False, silent=False, color=False): 00034 self.verbose = verbose 00035 self.silent = silent 00036 self.output = "" 00037 self.color = color or COLOR 00038 if self.color : 00039 from colorama import init, Fore, Back, Style 00040 init() 00041 self.COLORS = { 00042 'none' : "", 00043 'default' : Style.RESET_ALL, 00044 00045 'black' : Fore.BLACK, 00046 'red' : Fore.RED, 00047 'green' : Fore.GREEN, 00048 'yellow' : Fore.YELLOW, 00049 'blue' : Fore.BLUE, 00050 'magenta' : Fore.MAGENTA, 00051 'cyan' : Fore.CYAN, 00052 'white' : Fore.WHITE, 00053 00054 'on_black' : Back.BLACK, 00055 'on_red' : Back.RED, 00056 'on_green' : Back.GREEN, 00057 'on_yellow' : Back.YELLOW, 00058 'on_blue' : Back.BLUE, 00059 'on_magenta' : Back.MAGENTA, 00060 'on_cyan' : Back.CYAN, 00061 'on_white' : Back.WHITE, 00062 } 00063 00064 def get_output(self): 00065 return self.output 00066 00067 def notify(self, event): 00068 if self.verbose : 00069 msg = self.print_notify_verbose (event) 00070 else: 00071 msg = self.print_notify (event) 00072 if msg: 00073 if not self.silent : 00074 if self.color : 00075 self.print_in_color (event, msg) 00076 else: 00077 print(msg) 00078 self.output += msg + "\n" 00079 00080 def print_notify (self, event): 00081 """ Command line notification 00082 """ 00083 if event['type'] in ('tool_error', 'info'): 00084 return event['message'] 00085 00086 elif event['type'] == 'cc' and event['severity'] != 'verbose': 00087 event['severity'] = event['severity'].title() 00088 00089 if PRINT_COMPILER_OUTPUT_AS_LINK: 00090 event['file'] = abspath(event['file']) 00091 return '[{severity}] {file}:{line}:{col}: {message}'.format( 00092 **event) 00093 else: 00094 event['file'] = basename(event['file']) 00095 return '[{severity}] {file}@{line},{col}: {message}'.format( 00096 **event) 00097 00098 elif event['type'] == 'progress': 00099 event['action'] = event['action'].title() 00100 event['file'] = basename(event['file']) 00101 if 'percent' in event: 00102 format_string = '{action} [{percent:>5.1f}%]: {file}' 00103 else: 00104 format_string = '{action}: {file}' 00105 return format_string.format(**event) 00106 00107 def print_notify_verbose (self, event): 00108 """ Command line notification with more verbose mode 00109 """ 00110 if event['type'] == 'info' or (event['type'] == 'cc' and 00111 event['severity'] == 'verbose'): 00112 return event['message'] 00113 elif event['type'] == 'debug': 00114 return "[DEBUG] {message}".format(**event) 00115 elif event['type'] in ('progress', 'cc'): 00116 return self.print_notify (event) 00117 00118 COLOR_MATCHER = re.compile(r"(\w+)(\W+on\W+\w+)?") 00119 def colorstring_to_escapecode (self, color_string): 00120 """ Convert a color string from a string into an ascii escape code that 00121 will print that color on the terminal. 00122 00123 Positional arguments: 00124 color_string - the string to parse 00125 """ 00126 match = re.match(self.COLOR_MATCHER , color_string) 00127 if match: 00128 return self.COLORS [match.group(1)] + \ 00129 (self.COLORS [match.group(2).strip().replace(" ", "_")] 00130 if match.group(2) else "") 00131 else: 00132 return self.COLORS ['default'] 00133 00134 def print_in_color (self, event, msg): 00135 """ Wrap a toolchain notifier in a colorizer. This colorizer will wrap 00136 notifications in a color if the severity matches a color in the 00137 CLI_COLOR_MAP. 00138 """ 00139 """The notification function inself""" 00140 if sys.stdout.isatty() and event.get('severity', None) in CLI_COLOR_MAP: 00141 sys.stdout.write(self.colorstring_to_escapecode ( 00142 CLI_COLOR_MAP[event['severity']])) 00143 print(msg) 00144 sys.stdout.write(self.colorstring_to_escapecode ('default')) 00145 else: 00146 print(msg)
Generated on Tue Jul 12 2022 12:45:51 by
1.7.2