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