From 95524e5fc70021285b467483584a9edbf9df90ea Mon Sep 17 00:00:00 2001 From: Jeffrey Phillips Freeman <jeffrey.freeman@syncleus.com> Date: Wed, 28 Sep 2016 14:16:09 -0400 Subject: [PATCH] changed print_colorized_frame to echo_colorized_frame, simplified some of the error and warning messages to functions as well. --- src/apex/cli.py | 24 +++++++++-------------- src/apex/plugins/apexparadigm/__init__.py | 14 ++++++------- src/apex/plugins/beacon/__init__.py | 2 +- src/apex/plugins/id/__init__.py | 2 +- src/apex/plugins/status/__init__.py | 2 +- src/apex/util.py | 8 +++++++- 6 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/apex/cli.py b/src/apex/cli.py index 266bb2c..edc70a0 100644 --- a/src/apex/cli.py +++ b/src/apex/cli.py @@ -96,8 +96,7 @@ def main(verbose, configfile): config = find_config(configfile, verbose) if config is None: - click.echo(click.style('Error: ', fg='red', bold=True, blink=True) + - click.style('No apex configuration found, can not continue.', bold=True)) + echo_colorized_error('No apex configuration found, can not continue.') return for section in config.sections(): if section.startswith("TNC "): @@ -111,9 +110,8 @@ def main(verbose, configfile): tcp_port = config.get(section, 'tcp_port') kiss_tnc = apex.kiss.KissTcp(host=tcp_host, tcp_port=tcp_port) else: - click.echo(click.style('Error: ', fg='red', bold=True, blink=True) + - click.style("""Invalid configuration, must have both com_port and baud set or tcp_host and - tcp_port set in TNC sections of configuration file""", bold=True)) + echo_colorized_error("""Invalid configuration, must have both com_port and baud set or tcp_host and + tcp_port set in TNC sections of configuration file""") return if not config.has_option(section, 'kiss_init'): @@ -129,9 +127,8 @@ def main(verbose, configfile): elif kiss_init_string == 'NONE': kiss_tnc.connect() else: - click.echo(click.style('Error: ', fg='red', bold=True, blink=True) + - click.style('Invalid configuration, value assigned to kiss_init was not recognized: %s' - % kiss_init_string, bold=True)) + echo_colorized_error('Invalid configuration, value assigned to kiss_init was not recognized: %s' + % kiss_init_string) return aprs_tnc = apex.aprs.Aprs(data_stream=kiss_tnc) @@ -164,8 +161,7 @@ def main(verbose, configfile): try: plugin_loaders = get_plugins() if not len(plugin_loaders): - click.echo(click.style('Warning: ', fg='yellow') + - click.style('No plugins were able to be discovered, will only display incoming messages.')) + echo_colorized_warning('No plugins were able to be discovered, will only display incoming messages.') for plugin_loader in plugin_loaders: if verbose: click.echo('Plugin found at the following location: %s' % repr(plugin_loader)) @@ -175,8 +171,7 @@ def main(verbose, configfile): new_thread.start() plugin_threads.append(new_thread) except IOError: - click.echo(click.style('Warning: ', fg='yellow') + - click.style('plugin directory not found, will only display incoming messages.')) + echo_colorized_warning('plugin directory not found, will only display incoming messages.') def sigint_handler(signal, frame): global running @@ -210,7 +205,7 @@ def main(verbose, configfile): port = port_map[port_name] frame = port['tnc'].read() if frame: - print_colorized_frame(frame, port_name, True) + echo_colorized_frame(frame, port_name, True) for plugin_module in plugin_modules: something_read = True @@ -218,8 +213,7 @@ def main(verbose, configfile): except Exception as ex: # We want to keep this thread alive so long as the application runs. traceback.print_exc(file=sys.stdout) - click.echo(click.style('Error: ', fg='red', bold=True, blink=True) + - click.style('Caught exception while reading packet: %s' % str(ex), bold=True)) + echo_colorized_error('Caught exception while reading packet: %s' % str(ex)) if something_read is False: time.sleep(1) diff --git a/src/apex/plugins/apexparadigm/__init__.py b/src/apex/plugins/apexparadigm/__init__.py index 57c2ecd..386aa49 100644 --- a/src/apex/plugins/apexparadigm/__init__.py +++ b/src/apex/plugins/apexparadigm/__init__.py @@ -91,7 +91,7 @@ class ApexParadigmPlugin(object): self.packet_cache[str(frame_hash)] = frame_hash port['tnc'].write(frame, port['tnc_port']) self.aprsis.write(frame) - apex.print_colorized_frame(frame, port_name, False) + apex.echo_colorized_frame(frame, port_name, False) return else: if port['net'].startswith(node): @@ -102,7 +102,7 @@ class ApexParadigmPlugin(object): self.packet_cache[str(frame_hash)] = frame_hash port['tnc'].write(frame, port['tnc_port']) self.aprsis.write(frame) - apex.print_colorized_frame(frame, port_name, False) + apex.echo_colorized_frame(frame, port_name, False) return if node == port_callsign and ssid == port_ssid: if ssid is 0: @@ -114,7 +114,7 @@ class ApexParadigmPlugin(object): self.packet_cache[str(frame_hash)] = frame_hash port['tnc'].write(frame, port['tnc_port']) self.aprsis.write(frame) - apex.print_colorized_frame(frame, port_name, False) + apex.echo_colorized_frame(frame, port_name, False) return elif node == 'GATE' and port['net'].startswith('2M'): frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] + [node + '*'] +\ @@ -124,7 +124,7 @@ class ApexParadigmPlugin(object): self.packet_cache[str(frame_hash)] = frame_hash port['tnc'].write(frame, port['tnc_port']) self.aprsis.write(frame) - apex.print_colorized_frame(frame, port_name, False) + apex.echo_colorized_frame(frame, port_name, False) return if node.startswith('WIDE') and ssid > 1: frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] +\ @@ -134,7 +134,7 @@ class ApexParadigmPlugin(object): self.packet_cache[str(frame_hash)] = frame_hash recv_port['tnc'].write(frame, recv_port['tnc_port']) self.aprsis.write(frame) - apex.print_colorized_frame(frame, port_name, False) + apex.echo_colorized_frame(frame, port_name, False) return elif node.startswith('WIDE') and ssid is 1: frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] + [node + '*'] + frame['path'][hop_index+1:] @@ -143,7 +143,7 @@ class ApexParadigmPlugin(object): self.packet_cache[str(frame_hash)] = frame_hash recv_port['tnc'].write(frame, recv_port['tnc_port']) self.aprsis.write(frame) - apex.print_colorized_frame(frame, port_name, False) + apex.echo_colorized_frame(frame, port_name, False) return elif node.startswith('WIDE') and ssid is 0: frame['path'][hop_index] = node + '*' @@ -275,7 +275,7 @@ class ApexParadigmPlugin(object): self.packet_cache[str(frame_hash)] = frame_hash selected_hop['port']['tnc'].write(frame, selected_hop['port']['tnc_port']) self.aprsis.write(frame) - apex.print_colorized_frame(frame, port_name, False) + apex.echo_colorized_frame(frame, port_name, False) return def stop(self): diff --git a/src/apex/plugins/beacon/__init__.py b/src/apex/plugins/beacon/__init__.py index dafbabc..a17bf53 100644 --- a/src/apex/plugins/beacon/__init__.py +++ b/src/apex/plugins/beacon/__init__.py @@ -77,6 +77,6 @@ class BeaconPlugin(object): if frame_hash not in self.packet_cache.values(): self.packet_cache[str(frame_hash)] = frame_hash port['tnc'].write(beacon_frame, port['tnc_port']) - apex.print_colorized_frame(beacon_frame, port_name, False) + apex.echo_colorized_frame(beacon_frame, port_name, False) else: time.sleep(1) diff --git a/src/apex/plugins/id/__init__.py b/src/apex/plugins/id/__init__.py index 362a686..408d0cd 100644 --- a/src/apex/plugins/id/__init__.py +++ b/src/apex/plugins/id/__init__.py @@ -77,6 +77,6 @@ class IdPlugin(object): if frame_hash not in self.packet_cache.values(): self.packet_cache[str(frame_hash)] = frame_hash port['tnc'].write(id_frame, port['tnc_port']) - apex.print_colorized_frame(id_frame, port_name, False) + apex.echo_colorized_frame(id_frame, port_name, False) else: time.sleep(1) diff --git a/src/apex/plugins/status/__init__.py b/src/apex/plugins/status/__init__.py index 25f12cb..2391df5 100644 --- a/src/apex/plugins/status/__init__.py +++ b/src/apex/plugins/status/__init__.py @@ -79,6 +79,6 @@ class StatusPlugin(object): if frame_hash not in self.packet_cache.values(): self.packet_cache[str(frame_hash)] = frame_hash port['tnc'].write(status_frame, port['tnc_port']) - apex.print_colorized_frame(status_frame, port_name, False) + apex.echo_colorized_frame(status_frame, port_name, False) else: time.sleep(1) diff --git a/src/apex/util.py b/src/apex/util.py index 5a11539..fb90d14 100644 --- a/src/apex/util.py +++ b/src/apex/util.py @@ -6,7 +6,7 @@ from __future__ import print_function import click -def print_colorized_frame(frame, port_name, direction_in): +def echo_colorized_frame(frame, port_name, direction_in): formatted_aprs = '>'.join([click.style(frame['source'], fg='green'), click.style(frame['destination'], fg='blue')]) paths = [] for path in frame['path']: @@ -20,3 +20,9 @@ def print_colorized_frame(frame, port_name, direction_in): click.echo(click.style(port_name + ' << ', fg='magenta') + formatted_aprs) else: click.echo(click.style(port_name + ' >> ', fg='magenta', bold=True, blink=True) + formatted_aprs) + +def echo_colorized_error(text): + click.echo(click.style('Error: ', fg='red', bold=True, blink=True) + click.style(text, bold=True)) + +def echo_colorized_warning(text): + click.echo(click.style('Error: ', fg='yellow') + click.style(text)) -- GitLab