From 3bd691a342cbbf513a57d64e38ba82545434ed48 Mon Sep 17 00:00:00 2001 From: Jimmy Brisson Date: Thu, 26 Apr 2018 09:02:18 -0500 Subject: [PATCH] Move terminal notifier to it's own file --- tools/make.py | 2 +- tools/notifier/__init__.py | 72 ----------------------------- tools/notifier/term.py | 89 ++++++++++++++++++++++++++++++++++++ tools/toolchains/__init__.py | 2 +- 4 files changed, 91 insertions(+), 74 deletions(-) create mode 100644 tools/notifier/term.py diff --git a/tools/make.py b/tools/make.py index 0debe8051f..5893445a70 100644 --- a/tools/make.py +++ b/tools/make.py @@ -45,7 +45,7 @@ from tools.targets import TARGET_MAP from tools.options import get_default_options_parser from tools.options import extract_profile from tools.options import extract_mcus -from tools.notifier import TerminalNotifier +from tools.notifier.term import TerminalNotifier from tools.build_api import build_project from tools.build_api import mcu_toolchain_matrix from tools.build_api import mcu_toolchain_list diff --git a/tools/notifier/__init__.py b/tools/notifier/__init__.py index e3cc7c254e..a06809a500 100644 --- a/tools/notifier/__init__.py +++ b/tools/notifier/__init__.py @@ -16,9 +16,6 @@ from __future__ import print_function, division, absolute_import from abc import ABCMeta, abstractmethod -from os.path import basename - -from ..settings import PRINT_COMPILER_OUTPUT_AS_LINK class Notifier(object): @@ -103,72 +100,3 @@ class Notifier(object): Update a UI with a key, value pair """ self.notify({'type': 'var', 'key': key, 'val': value}) - - -class TerminalNotifier(Notifier): - """ - Writes notifications to a terminal based on a silent and verbose flag. - """ - - def __init__(self, verbose=False, silent=False): - self.verbose = verbose - self.silent = silent - self.output = "" - - def get_output(self): - return self.output - - def notify(self, event): - if self.verbose: - msg = self.print_notify_verbose(event) - else: - msg = self.print_notify(event) - if msg: - if not self.silent: - print(msg) - self.output += msg + "\n" - - def print_notify(self, event): - """ Default command line notification - """ - if not self.verbose and event['type'] == 'tool_error': - return event['message'] - - elif event['type'] in ['info']: - return event['message'] - - elif event['type'] == 'cc': - event['severity'] = event['severity'].title() - - if PRINT_COMPILER_OUTPUT_AS_LINK: - event['file'] = getcwd() + event['file'].strip('.') - return '[%(severity)s] %(file)s:%(line)s:%(col)s: %(message)s' % event - else: - event['file'] = basename(event['file']) - return '[%(severity)s] %(file)s@%(line)s,%(col)s: %(message)s' % event - - elif event['type'] == 'progress': - if 'percent' in event: - return '{} [{:>5.1f}%]: {}'.format(event['action'].title(), - event['percent'], - basename(event['file'])) - else: - return '{}: {}'.format(event['action'].title(), - basename(event['file'])) - - def print_notify_verbose(self, event): - """ Default command line notification with more verbose mode - """ - if event['type'] in ['info', 'debug']: - return event['message'] - - elif event['type'] == 'cc': - event['severity'] = event['severity'].title() - event['file'] = basename(event['file']) - event['mcu_name'] = "None" - event['target_name'] = event['target_name'].upper() if event['target_name'] else "Unknown" - event['toolchain_name'] = event['toolchain_name'].upper() if event['toolchain_name'] else "Unknown" - return '[%(severity)s] %(target_name)s::%(toolchain_name)s::%(file)s@%(line)s: %(message)s' % event - - elif event['type'] == 'progress': - return self.print_notify(event) # standard handle diff --git a/tools/notifier/term.py b/tools/notifier/term.py new file mode 100644 index 0000000000..b03c43c774 --- /dev/null +++ b/tools/notifier/term.py @@ -0,0 +1,89 @@ +# mbed SDK +# Copyright (c) 2011-2013 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. + +from __future__ import print_function, division, absolute_import + +from os.path import basename + +from . import Notifier +from ..settings import PRINT_COMPILER_OUTPUT_AS_LINK + +class TerminalNotifier(Notifier): + """ + Writes notifications to a terminal based on a silent and verbose flag. + """ + + def __init__(self, verbose=False, silent=False): + self.verbose = verbose + self.silent = silent + self.output = "" + + def get_output(self): + return self.output + + def notify(self, event): + if self.verbose: + msg = self.print_notify_verbose(event) + else: + msg = self.print_notify(event) + if msg: + if not self.silent: + print(msg) + self.output += msg + "\n" + + def print_notify(self, event): + """ Default command line notification + """ + if not self.verbose and event['type'] == 'tool_error': + return event['message'] + + elif event['type'] in ['info']: + return event['message'] + + elif event['type'] == 'cc': + event['severity'] = event['severity'].title() + + if PRINT_COMPILER_OUTPUT_AS_LINK: + event['file'] = getcwd() + event['file'].strip('.') + return '[%(severity)s] %(file)s:%(line)s:%(col)s: %(message)s' % event + else: + event['file'] = basename(event['file']) + return '[%(severity)s] %(file)s@%(line)s,%(col)s: %(message)s' % event + + elif event['type'] == 'progress': + if 'percent' in event: + return '{} [{:>5.1f}%]: {}'.format(event['action'].title(), + event['percent'], + basename(event['file'])) + else: + return '{}: {}'.format(event['action'].title(), + basename(event['file'])) + + def print_notify_verbose(self, event): + """ Default command line notification with more verbose mode + """ + if event['type'] in ['info', 'debug']: + return event['message'] + + elif event['type'] == 'cc': + event['severity'] = event['severity'].title() + event['file'] = basename(event['file']) + event['mcu_name'] = "None" + event['target_name'] = event['target_name'].upper() if event['target_name'] else "Unknown" + event['toolchain_name'] = event['toolchain_name'].upper() if event['toolchain_name'] else "Unknown" + return '[%(severity)s] %(target_name)s::%(toolchain_name)s::%(file)s@%(line)s: %(message)s' % event + + elif event['type'] == 'progress': + return self.print_notify(event) # standard handle diff --git a/tools/toolchains/__init__.py b/tools/toolchains/__init__.py index 4020336563..7cd8293825 100644 --- a/tools/toolchains/__init__.py +++ b/tools/toolchains/__init__.py @@ -37,7 +37,7 @@ from ..utils import (run_cmd, mkdir, rel_path, ToolException, NotSupportedException, split_path, compile_worker) from ..settings import MBED_ORG_USER, PRINT_COMPILER_OUTPUT_AS_LINK from .. import hooks -from ..notifier import TerminalNotifier +from ..notifier.term import TerminalNotifier from ..memap import MemapParser