takashi kadono
/
Nucleo_446
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/tools/notifier/term.py@0:8fdf9a60065b, 2018-10-10 (annotated)
- Committer:
- kadonotakashi
- Date:
- Wed Oct 10 00:33:53 2018 +0000
- Revision:
- 0:8fdf9a60065b
how to make mbed librry
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | # mbed SDK |
kadonotakashi | 0:8fdf9a60065b | 2 | # Copyright (c) 2011-2013 ARM Limited |
kadonotakashi | 0:8fdf9a60065b | 3 | # |
kadonotakashi | 0:8fdf9a60065b | 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
kadonotakashi | 0:8fdf9a60065b | 5 | # you may not use this file except in compliance with the License. |
kadonotakashi | 0:8fdf9a60065b | 6 | # You may obtain a copy of the License at |
kadonotakashi | 0:8fdf9a60065b | 7 | # |
kadonotakashi | 0:8fdf9a60065b | 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
kadonotakashi | 0:8fdf9a60065b | 9 | # |
kadonotakashi | 0:8fdf9a60065b | 10 | # Unless required by applicable law or agreed to in writing, software |
kadonotakashi | 0:8fdf9a60065b | 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
kadonotakashi | 0:8fdf9a60065b | 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kadonotakashi | 0:8fdf9a60065b | 13 | # See the License for the specific language governing permissions and |
kadonotakashi | 0:8fdf9a60065b | 14 | # limitations under the License. |
kadonotakashi | 0:8fdf9a60065b | 15 | |
kadonotakashi | 0:8fdf9a60065b | 16 | from __future__ import print_function, division, absolute_import |
kadonotakashi | 0:8fdf9a60065b | 17 | from past.builtins import basestring |
kadonotakashi | 0:8fdf9a60065b | 18 | |
kadonotakashi | 0:8fdf9a60065b | 19 | import re |
kadonotakashi | 0:8fdf9a60065b | 20 | import sys |
kadonotakashi | 0:8fdf9a60065b | 21 | from os import getcwd |
kadonotakashi | 0:8fdf9a60065b | 22 | from os.path import (basename, abspath) |
kadonotakashi | 0:8fdf9a60065b | 23 | |
kadonotakashi | 0:8fdf9a60065b | 24 | from . import Notifier |
kadonotakashi | 0:8fdf9a60065b | 25 | from ..settings import (PRINT_COMPILER_OUTPUT_AS_LINK, |
kadonotakashi | 0:8fdf9a60065b | 26 | CLI_COLOR_MAP, COLOR) |
kadonotakashi | 0:8fdf9a60065b | 27 | |
kadonotakashi | 0:8fdf9a60065b | 28 | class TerminalNotifier(Notifier): |
kadonotakashi | 0:8fdf9a60065b | 29 | """ |
kadonotakashi | 0:8fdf9a60065b | 30 | Writes notifications to a terminal based on silent, verbose and color flags. |
kadonotakashi | 0:8fdf9a60065b | 31 | """ |
kadonotakashi | 0:8fdf9a60065b | 32 | |
kadonotakashi | 0:8fdf9a60065b | 33 | def __init__(self, verbose=False, silent=False, color=False): |
kadonotakashi | 0:8fdf9a60065b | 34 | self.verbose = verbose |
kadonotakashi | 0:8fdf9a60065b | 35 | self.silent = silent |
kadonotakashi | 0:8fdf9a60065b | 36 | self.output = "" |
kadonotakashi | 0:8fdf9a60065b | 37 | self.color = color or COLOR |
kadonotakashi | 0:8fdf9a60065b | 38 | if self.color: |
kadonotakashi | 0:8fdf9a60065b | 39 | from colorama import init, Fore, Back, Style |
kadonotakashi | 0:8fdf9a60065b | 40 | init() |
kadonotakashi | 0:8fdf9a60065b | 41 | self.COLORS = { |
kadonotakashi | 0:8fdf9a60065b | 42 | 'none' : "", |
kadonotakashi | 0:8fdf9a60065b | 43 | 'default' : Style.RESET_ALL, |
kadonotakashi | 0:8fdf9a60065b | 44 | |
kadonotakashi | 0:8fdf9a60065b | 45 | 'black' : Fore.BLACK, |
kadonotakashi | 0:8fdf9a60065b | 46 | 'red' : Fore.RED, |
kadonotakashi | 0:8fdf9a60065b | 47 | 'green' : Fore.GREEN, |
kadonotakashi | 0:8fdf9a60065b | 48 | 'yellow' : Fore.YELLOW, |
kadonotakashi | 0:8fdf9a60065b | 49 | 'blue' : Fore.BLUE, |
kadonotakashi | 0:8fdf9a60065b | 50 | 'magenta' : Fore.MAGENTA, |
kadonotakashi | 0:8fdf9a60065b | 51 | 'cyan' : Fore.CYAN, |
kadonotakashi | 0:8fdf9a60065b | 52 | 'white' : Fore.WHITE, |
kadonotakashi | 0:8fdf9a60065b | 53 | |
kadonotakashi | 0:8fdf9a60065b | 54 | 'on_black' : Back.BLACK, |
kadonotakashi | 0:8fdf9a60065b | 55 | 'on_red' : Back.RED, |
kadonotakashi | 0:8fdf9a60065b | 56 | 'on_green' : Back.GREEN, |
kadonotakashi | 0:8fdf9a60065b | 57 | 'on_yellow' : Back.YELLOW, |
kadonotakashi | 0:8fdf9a60065b | 58 | 'on_blue' : Back.BLUE, |
kadonotakashi | 0:8fdf9a60065b | 59 | 'on_magenta' : Back.MAGENTA, |
kadonotakashi | 0:8fdf9a60065b | 60 | 'on_cyan' : Back.CYAN, |
kadonotakashi | 0:8fdf9a60065b | 61 | 'on_white' : Back.WHITE, |
kadonotakashi | 0:8fdf9a60065b | 62 | } |
kadonotakashi | 0:8fdf9a60065b | 63 | |
kadonotakashi | 0:8fdf9a60065b | 64 | def get_output(self): |
kadonotakashi | 0:8fdf9a60065b | 65 | return self.output |
kadonotakashi | 0:8fdf9a60065b | 66 | |
kadonotakashi | 0:8fdf9a60065b | 67 | def notify(self, event): |
kadonotakashi | 0:8fdf9a60065b | 68 | if self.verbose: |
kadonotakashi | 0:8fdf9a60065b | 69 | msg = self.print_notify_verbose(event) |
kadonotakashi | 0:8fdf9a60065b | 70 | else: |
kadonotakashi | 0:8fdf9a60065b | 71 | msg = self.print_notify(event) |
kadonotakashi | 0:8fdf9a60065b | 72 | if msg: |
kadonotakashi | 0:8fdf9a60065b | 73 | if not self.silent: |
kadonotakashi | 0:8fdf9a60065b | 74 | if self.color: |
kadonotakashi | 0:8fdf9a60065b | 75 | self.print_in_color(event, msg) |
kadonotakashi | 0:8fdf9a60065b | 76 | else: |
kadonotakashi | 0:8fdf9a60065b | 77 | print(msg) |
kadonotakashi | 0:8fdf9a60065b | 78 | self.output += msg + "\n" |
kadonotakashi | 0:8fdf9a60065b | 79 | |
kadonotakashi | 0:8fdf9a60065b | 80 | def print_notify(self, event): |
kadonotakashi | 0:8fdf9a60065b | 81 | """ Command line notification |
kadonotakashi | 0:8fdf9a60065b | 82 | """ |
kadonotakashi | 0:8fdf9a60065b | 83 | if event['type'] in ('tool_error', 'info'): |
kadonotakashi | 0:8fdf9a60065b | 84 | return event['message'] |
kadonotakashi | 0:8fdf9a60065b | 85 | |
kadonotakashi | 0:8fdf9a60065b | 86 | elif event['type'] == 'cc' and event['severity'] != 'verbose': |
kadonotakashi | 0:8fdf9a60065b | 87 | event['severity'] = event['severity'].title() |
kadonotakashi | 0:8fdf9a60065b | 88 | |
kadonotakashi | 0:8fdf9a60065b | 89 | if PRINT_COMPILER_OUTPUT_AS_LINK: |
kadonotakashi | 0:8fdf9a60065b | 90 | event['file'] = abspath(event['file']) |
kadonotakashi | 0:8fdf9a60065b | 91 | return '[{severity}] {file}:{line}:{col}: {message}'.format( |
kadonotakashi | 0:8fdf9a60065b | 92 | **event) |
kadonotakashi | 0:8fdf9a60065b | 93 | else: |
kadonotakashi | 0:8fdf9a60065b | 94 | event['file'] = basename(event['file']) |
kadonotakashi | 0:8fdf9a60065b | 95 | return '[{severity}] {file}@{line},{col}: {message}'.format( |
kadonotakashi | 0:8fdf9a60065b | 96 | **event) |
kadonotakashi | 0:8fdf9a60065b | 97 | |
kadonotakashi | 0:8fdf9a60065b | 98 | elif event['type'] == 'progress': |
kadonotakashi | 0:8fdf9a60065b | 99 | event['action'] = event['action'].title() |
kadonotakashi | 0:8fdf9a60065b | 100 | event['file'] = basename(event['file']) |
kadonotakashi | 0:8fdf9a60065b | 101 | if 'percent' in event: |
kadonotakashi | 0:8fdf9a60065b | 102 | format_string = '{action} [{percent:>5.1f}%]: {file}' |
kadonotakashi | 0:8fdf9a60065b | 103 | else: |
kadonotakashi | 0:8fdf9a60065b | 104 | format_string = '{action}: {file}' |
kadonotakashi | 0:8fdf9a60065b | 105 | return format_string.format(**event) |
kadonotakashi | 0:8fdf9a60065b | 106 | |
kadonotakashi | 0:8fdf9a60065b | 107 | def print_notify_verbose(self, event): |
kadonotakashi | 0:8fdf9a60065b | 108 | """ Command line notification with more verbose mode |
kadonotakashi | 0:8fdf9a60065b | 109 | """ |
kadonotakashi | 0:8fdf9a60065b | 110 | if event['type'] == 'info' or (event['type'] == 'cc' and |
kadonotakashi | 0:8fdf9a60065b | 111 | event['severity'] == 'verbose'): |
kadonotakashi | 0:8fdf9a60065b | 112 | return event['message'] |
kadonotakashi | 0:8fdf9a60065b | 113 | elif event['type'] == 'debug': |
kadonotakashi | 0:8fdf9a60065b | 114 | return "[DEBUG] {message}".format(**event) |
kadonotakashi | 0:8fdf9a60065b | 115 | elif event['type'] in ('progress', 'cc'): |
kadonotakashi | 0:8fdf9a60065b | 116 | return self.print_notify(event) |
kadonotakashi | 0:8fdf9a60065b | 117 | |
kadonotakashi | 0:8fdf9a60065b | 118 | COLOR_MATCHER = re.compile(r"(\w+)(\W+on\W+\w+)?") |
kadonotakashi | 0:8fdf9a60065b | 119 | def colorstring_to_escapecode(self, color_string): |
kadonotakashi | 0:8fdf9a60065b | 120 | """ Convert a color string from a string into an ascii escape code that |
kadonotakashi | 0:8fdf9a60065b | 121 | will print that color on the terminal. |
kadonotakashi | 0:8fdf9a60065b | 122 | |
kadonotakashi | 0:8fdf9a60065b | 123 | Positional arguments: |
kadonotakashi | 0:8fdf9a60065b | 124 | color_string - the string to parse |
kadonotakashi | 0:8fdf9a60065b | 125 | """ |
kadonotakashi | 0:8fdf9a60065b | 126 | match = re.match(self.COLOR_MATCHER, color_string) |
kadonotakashi | 0:8fdf9a60065b | 127 | if match: |
kadonotakashi | 0:8fdf9a60065b | 128 | return self.COLORS[match.group(1)] + \ |
kadonotakashi | 0:8fdf9a60065b | 129 | (self.COLORS[match.group(2).strip().replace(" ", "_")] |
kadonotakashi | 0:8fdf9a60065b | 130 | if match.group(2) else "") |
kadonotakashi | 0:8fdf9a60065b | 131 | else: |
kadonotakashi | 0:8fdf9a60065b | 132 | return self.COLORS['default'] |
kadonotakashi | 0:8fdf9a60065b | 133 | |
kadonotakashi | 0:8fdf9a60065b | 134 | def print_in_color(self, event, msg): |
kadonotakashi | 0:8fdf9a60065b | 135 | """ Wrap a toolchain notifier in a colorizer. This colorizer will wrap |
kadonotakashi | 0:8fdf9a60065b | 136 | notifications in a color if the severity matches a color in the |
kadonotakashi | 0:8fdf9a60065b | 137 | CLI_COLOR_MAP. |
kadonotakashi | 0:8fdf9a60065b | 138 | """ |
kadonotakashi | 0:8fdf9a60065b | 139 | """The notification function inself""" |
kadonotakashi | 0:8fdf9a60065b | 140 | if sys.stdout.isatty() and event.get('severity', None) in CLI_COLOR_MAP: |
kadonotakashi | 0:8fdf9a60065b | 141 | sys.stdout.write(self.colorstring_to_escapecode( |
kadonotakashi | 0:8fdf9a60065b | 142 | CLI_COLOR_MAP[event['severity']])) |
kadonotakashi | 0:8fdf9a60065b | 143 | print(msg) |
kadonotakashi | 0:8fdf9a60065b | 144 | sys.stdout.write(self.colorstring_to_escapecode('default')) |
kadonotakashi | 0:8fdf9a60065b | 145 | else: |
kadonotakashi | 0:8fdf9a60065b | 146 | print(msg) |