From 5669a378c6b2049f0a7d8dec5098cd42d672b8d7 Mon Sep 17 00:00:00 2001
From: Jeffrey Phillips Freeman <jeffrey.freeman@syncleus.com>
Date: Wed, 28 Sep 2016 13:59:32 -0400
Subject: [PATCH] Colorize the output for the plugins.

Issue: #3
---
 src/apex/__init__.py                      |  2 ++
 src/apex/cli.py                           | 12 ++----------
 src/apex/plugins/apexparadigm/__init__.py | 15 ++++++++-------
 src/apex/plugins/beacon/__init__.py       |  3 ++-
 src/apex/plugins/id/__init__.py           |  3 ++-
 src/apex/plugins/status/__init__.py       |  3 ++-
 src/apex/util.py                          | 22 ++++++++++++++++++++++
 7 files changed, 40 insertions(+), 20 deletions(-)
 create mode 100644 src/apex/util.py

diff --git a/src/apex/__init__.py b/src/apex/__init__.py
index 0d34504..4c9436d 100644
--- a/src/apex/__init__.py
+++ b/src/apex/__init__.py
@@ -7,6 +7,8 @@ from __future__ import absolute_import
 from __future__ import division
 from __future__ import print_function
 
+from .util import *
+
 __author__ = 'Jeffrey Phillips Freeman (WI2ARD)'
 __maintainer__ = 'Jeffrey Phillips Freeman (WI2ARD)'
 __email__ = 'jeffrey.freeman@syncleus.com'
diff --git a/src/apex/cli.py b/src/apex/cli.py
index 501cfa4..266bb2c 100644
--- a/src/apex/cli.py
+++ b/src/apex/cli.py
@@ -30,6 +30,7 @@ import cachetools
 import click
 
 import apex.aprs
+from .util import *
 from apex.kiss import constants as kissConstants
 from apex.plugin_loader import get_plugins
 from apex.plugin_loader import load_plugin
@@ -209,16 +210,7 @@ def main(verbose, configfile):
                 port = port_map[port_name]
                 frame = port['tnc'].read()
                 if frame:
-                    formatted_aprs = '>'.join([click.style(frame['source'], fg='green'), click.style(frame['destination'], fg='blue')])
-                    paths = []
-                    for path in frame['path']:
-                        paths.append(click.style(path, fg='cyan'))
-                    paths = ','.join(paths)
-                    if frame['path']:
-                        formatted_aprs = ','.join([formatted_aprs, paths])
-                    formatted_aprs += ':'
-                    formatted_aprs += frame['text']
-                    click.echo(click.style(port_name + ' << ', fg='magenta') + formatted_aprs)
+                    print_colorized_frame(frame, port_name, True)
 
                     for plugin_module in plugin_modules:
                         something_read = True
diff --git a/src/apex/plugins/apexparadigm/__init__.py b/src/apex/plugins/apexparadigm/__init__.py
index 4fd702c..4df8fe6 100644
--- a/src/apex/plugins/apexparadigm/__init__.py
+++ b/src/apex/plugins/apexparadigm/__init__.py
@@ -7,6 +7,7 @@ import copy
 import re
 import click
 
+import apex
 import apex.aprs.util
 
 __author__ = 'Jeffrey Phillips Freeman (WI2ARD)'
@@ -90,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)
-                                    click.echo(port_name + ' >> ' + apex.aprs.util.encode_frame(frame))
+                                    apex.print_colorized_frame(frame, port_name, False)
                                 return
                         else:
                             if port['net'].startswith(node):
@@ -101,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)
-                                    click.echo(port_name + ' >> ' + apex.aprs.util.encode_frame(frame))
+                                    apex.print_colorized_frame(frame, port_name, False)
                                 return
                     if node == port_callsign and ssid == port_ssid:
                         if ssid is 0:
@@ -113,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)
-                            click.echo(port_name + ' >> ' + apex.aprs.util.encode_frame(frame))
+                            apex.print_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 + '*'] +\
@@ -123,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)
-                            click.echo(port_name + ' >> ' + apex.aprs.util.encode_frame(frame))
+                            apex.print_colorized_frame(frame, port_name, False)
                         return
                 if node.startswith('WIDE') and ssid > 1:
                     frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] +\
@@ -133,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)
-                        click.echo(recv_port_name + ' >> ' + apex.aprs.util.encode_frame(frame))
+                        apex.print_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:]
@@ -142,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)
-                        click.echo(recv_port_name + ' >> ' + apex.aprs.util.encode_frame(frame))
+                        apex.print_colorized_frame(frame, port_name, False)
                     return
                 elif node.startswith('WIDE') and ssid is 0:
                     frame['path'][hop_index] = node + '*'
@@ -274,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)
-            click.echo(selected_hop['port_name'] + ' >> ' + apex.aprs.util.encode_frame(frame))
+            apex.print_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 d7e9500..1865177 100644
--- a/src/apex/plugins/beacon/__init__.py
+++ b/src/apex/plugins/beacon/__init__.py
@@ -6,6 +6,7 @@ from __future__ import print_function
 import time
 import click
 
+import apex
 import apex.aprs.util
 
 __author__ = 'Jeffrey Phillips Freeman (WI2ARD)'
@@ -76,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'])
-                        click.echo(port_name + ' >> ' + apex.aprs.util.encode_frame(beacon_frame))
+                        apex.print_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 2e508cf..083540e 100644
--- a/src/apex/plugins/id/__init__.py
+++ b/src/apex/plugins/id/__init__.py
@@ -6,6 +6,7 @@ from __future__ import print_function
 import time
 import click
 
+import apex
 import apex.aprs.util
 
 __author__ = 'Jeffrey Phillips Freeman (WI2ARD)'
@@ -76,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'])
-                        click.echo(port_name + ' >> ' + apex.aprs.util.encode_frame(id_frame))
+                        apex.print_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 c5278c3..ec695c6 100644
--- a/src/apex/plugins/status/__init__.py
+++ b/src/apex/plugins/status/__init__.py
@@ -5,6 +5,7 @@ from __future__ import print_function
 
 import time
 
+import apex
 import apex.aprs.util
 
 __author__ = 'Jeffrey Phillips Freeman (WI2ARD)'
@@ -78,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'])
-                        print(port_name + ' >> ' + apex.aprs.util.encode_frame(status_frame))
+                        apex.print_colorized_frame(status_frame, port_name, False)
             else:
                 time.sleep(1)
diff --git a/src/apex/util.py b/src/apex/util.py
new file mode 100644
index 0000000..5a11539
--- /dev/null
+++ b/src/apex/util.py
@@ -0,0 +1,22 @@
+# These imports are for python3 compatibility inside python2
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
+import click
+
+
+def print_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']:
+        paths.append(click.style(path, fg='cyan'))
+    paths = ','.join(paths)
+    if frame['path']:
+        formatted_aprs = ','.join([formatted_aprs, paths])
+    formatted_aprs += ':'
+    formatted_aprs += frame['text']
+    if 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)
-- 
GitLab