From 968b93a88e24c99b3643db5b1942f1d9163b79e7 Mon Sep 17 00:00:00 2001
From: Greg Albrecht <gba@onbeep.com>
Date: Fri, 14 Aug 2015 16:15:06 -0700
Subject: [PATCH] cleaned up code, fixed copyright and author info, added phil
 and john.

---
 CONTRIBUTORS            |   3 +-
 COPYRIGHT.txt           |   2 +-
 LICENSE                 |   2 +-
 Makefile                |  10 ++--
 README.rst              |   7 ++-
 examples/socketread.py  |  41 +++++++++++++---
 kiss/__init__.py        |   8 +--
 kiss/classes.py         | 105 ++++++++++++++++++++++------------------
 kiss/constants.py       |   4 +-
 kiss/util.py            |   4 +-
 requirements.txt        |   4 +-
 setup.cfg               |   4 +-
 setup.py                |   6 +--
 tests/constants.py      |   4 +-
 tests/context.py        |   4 ++
 tests/test_kiss_util.py |   4 +-
 16 files changed, 128 insertions(+), 84 deletions(-)

diff --git a/CONTRIBUTORS b/CONTRIBUTORS
index d18197f..2fef741 100644
--- a/CONTRIBUTORS
+++ b/CONTRIBUTORS
@@ -1 +1,2 @@
-Paul McMillan
+- Paul McMillan N1HHG - https://github.com/PaulMcMillan
+- John Hogenmiller KB3DFZ - https://github.com/ytjohn
diff --git a/COPYRIGHT.txt b/COPYRIGHT.txt
index 11c15be..51d1495 100644
--- a/COPYRIGHT.txt
+++ b/COPYRIGHT.txt
@@ -1 +1 @@
-Copyright 2013, OnBeep, Inc. and Contributors
+Copyright 2015, Orion Labs, Inc. and Contributors
diff --git a/LICENSE b/LICENSE
index 27bf606..79e6956 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,4 +1,4 @@
-Copyright 2013 OnBeep, Inc. and Contributors
+Copyright 2015 Orion Labs, Inc. and Contributors
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
diff --git a/Makefile b/Makefile
index a2d6bb1..9ec0093 100644
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,8 @@
 # Makefile for KISS Python Module.
 #
 # Source:: https://github.com/ampledata/kiss
-# Author:: Greg Albrecht W2GMD <gba@onbeep.com>
-# Copyright:: Copyright 2013 OnBeep, Inc. and Contributors
+# Author:: Greg Albrecht W2GMD <gba@orionlabs.co>
+# Copyright:: Copyright 2015 Orion Labs, Inc. and Contributors
 # License:: Apache License, Version 2.0
 #
 
@@ -19,11 +19,7 @@ lint:
 	pylint -f colorized -r n kiss/*.py tests/*.py *.py
 
 flake8:
-	flake8 --exit-zero  --max-complexity 12 kiss/*.py tests/*.py *.py | \
-		awk -F\: '{printf "%s:%s: [E]%s\n", $$1, $$2, $$3}' | tee flake8.log
-
-cli_flake8:
-	flake8 --max-complexity 12 kiss/*.py tests/*.py *.py
+	flake8 --exit-zero  --max-complexity 12 kiss/*.py tests/*.py *.py
 
 pep8: flake8
 
diff --git a/README.rst b/README.rst
index cb94bfc..438ad38 100644
--- a/README.rst
+++ b/README.rst
@@ -21,6 +21,9 @@ Read & print frames from a TNC connected to '/dev/ttyUSB0' at 1200 baud::
     k.read(callback=print)
 
 
+See also: examples/ directory.
+
+
 Testing
 =======
 Run nosetests from a Makefile target::
@@ -51,14 +54,14 @@ https://github.com/ampledata/kiss
 
 Author
 ======
-Greg Albrecht W2GMD gba@onbeep.com
+Greg Albrecht W2GMD gba@orionlabs.co
 
 http://ampledata.org/
 
 
 Copyright
 =========
-Copyright 2013 OnBeep, Inc. and Contributors
+Copyright 2015 Orion Labs, Inc. and Contributors
 
 
 License
diff --git a/examples/socketread.py b/examples/socketread.py
index da0eda1..ba5fd23 100644
--- a/examples/socketread.py
+++ b/examples/socketread.py
@@ -1,12 +1,39 @@
+#!/usr/bin/env python
+"""
+Reads & Prints KISS frames from a TCP Socket.
 
+For use with programs like Dire Wolf.
+"""
+
+
+import aprs
 import kiss
+import logging
+
+
+def print_frame(frame):
+    try:
+        # Decode raw APRS frame into dictionary of separate sections
+        decoded_frame = aprs.util.decode_frame(frame)
+
+        # Format the APRS frame (in Raw ASCII Text) as a human readable frame
+        formatted_aprs = aprs.util.format_aprs_frame(decoded_frame)
+
+        # This is the human readable APRS output:
+        print formatted_aprs
+
+    except Exception as ex:
+        print ex
+        print "Error decoding frame:"
+        print "\t%s" % frame
+
 
-k = kiss.KISS(host='10.10.0.214', speed=115200, port=8001)
-k.logger.LOG_LEVEL = "DEBUG"
-k.start()  # inits the TNC, optionally passes KISS config flags.
-# k.write("testing")
-# k.read()
-# k.write('KB3DFZ-5>APDW10,WIDE1-1,WIDE2-1:testing')
-k.read()
+def main():
+    ki = kiss.KISS(host='10.42.10.170', tcp_port=8001)
+    ki.logger.setLevel(logging.INFO)
+    ki.start()
+    ki.read(callback=print_frame)
 
 
+if __name__ == '__main__':
+    main()
diff --git a/kiss/__init__.py b/kiss/__init__.py
index 52f7438..5d09f1f 100644
--- a/kiss/__init__.py
+++ b/kiss/__init__.py
@@ -8,16 +8,16 @@ KISS Python Module.
 ~~~~
 
 
-:author: Greg Albrecht W2GMD <gba@onbeep.com>
-:copyright: Copyright 2013 OnBeep, Inc. and Contributors
+:author: Greg Albrecht W2GMD <gba@orionlabs.co>
+:copyright: Copyright 2015 Orion Labs, Inc. and Contributors
 :license: Apache License, Version 2.0
 :source: <https://github.com/ampledata/kiss>
 
 """
 
-__author__ = 'Greg Albrecht W2GMD <gba@onbeep.com>'
+__author__ = 'Greg Albrecht W2GMD <gba@orionlabs.co>'
+__copyright__ = 'Copyright 2015 Orion Labs, Inc. and Contributors'
 __license__ = 'Apache License, Version 2.0'
-__copyright__ = 'Copyright 2013 OnBeep, Inc. and Contributors'
 
 
 import logging
diff --git a/kiss/classes.py b/kiss/classes.py
index dbff065..029caa4 100644
--- a/kiss/classes.py
+++ b/kiss/classes.py
@@ -3,8 +3,8 @@
 
 """KISS Core Classes."""
 
-__author__ = 'Greg Albrecht W2GMD <gba@onbeep.com>'
-__copyright__ = 'Copyright 2013 OnBeep, Inc. and Contributors'
+__author__ = 'Greg Albrecht W2GMD <gba@orionlabs.co>'
+__copyright__ = 'Copyright 2015 Orion Labs, Inc. and Contributors'
 __license__ = 'Apache License, Version 2.0'
 
 
@@ -30,24 +30,35 @@ class KISS(object):
     logger.addHandler(console_handler)
     logger.propagate = False
 
-    def __init__(self, port=None, speed=None, host=None, tcpport=None):
+    def __init__(self, port=None, speed=None, host=None, tcp_port=None):
         self.port = port
         self.speed = speed
         self.host = host
-        self.serial_int = None
+        self.tcp_port = tcp_port
+        self.interface = None
+        self.interface_mode = None
+
+        if self.port is not None and self.speed is not None:
+            self.interface_mode = 'serial'
+        elif self.host is not None and self.tcp_port is not None:
+            self.interface_mode = 'tcp'
+        if self.interface_mode is None:
+            raise Exception('Must set port/speed or host/tcp_port.')
+
+        self.logger.info('Using interface_mode=%s', self.interface_mode)
 
     def __enter__(self):
         return self
 
     def __exit__(self, exc_type, exc_val, exc_tb):
-        if self.host:
-            self.serial_int.shutdown()
-        elif self.serial_int and self.serial_int.isOpen():
-            self.serial_int.close()
+        if 'tcp' in self.interface_mode:
+            self.interface.shutdown()
+        elif self.interface and self.interface.isOpen():
+            self.interface.close()
 
     def __del__(self):
-        if self.serial_int and self.serial_int.isOpen():
-            self.serial_int.close()
+        if self.interface and self.interface.isOpen():
+            self.interface.close()
 
     def start(self, **kwargs):
         """
@@ -59,23 +70,23 @@ class KISS(object):
         :param **kwargs: name/value pairs to use as initial config values.
         """
         self.logger.debug("kwargs=%s", kwargs)
-        if self.host:
-            address = (self.host, self.port)
-            self.serial_int = socket.create_connection(address)
-        else:
-            self.serial_int = serial.Serial(self.port, self.speed)
-            self.serial_int.timeout = kiss.constants.SERIAL_TIMEOUT
+
+        if 'tcp' in self.interface_mode:
+            address = (self.host, self.tcp_port)
+            self.interface = socket.create_connection(address)
+        elif 'serial' in self.interface_mode:
+            self.interface = serial.Serial(self.port, self.speed)
+            self.interface.timeout = kiss.constants.SERIAL_TIMEOUT
 
         # If no settings specified, default to config values similar
-        # to those that ship with xastir.
+        # to those that ship with Xastir.
         if not kwargs:
             kwargs = kiss.constants.DEFAULT_KISS_CONFIG_VALUES
 
-        if not self.host:
+        if 'serial' in self.interface_mode:
             for name, value in kwargs.items():
                 self.write_setting(name, value)
 
-    
     def write_setting(self, name, value):
         """
         Writes KISS Command Codes to attached device.
@@ -88,10 +99,10 @@ class KISS(object):
         self.logger.debug('Configuring %s = %s', name, repr(value))
 
         # Do the reasonable thing if a user passes an int
-        if type(value) == int:
+        if isinstance(value, int):
             value = chr(value)
 
-        return self.serial_int.write(
+        return self.interface.write(
             kiss.constants.FEND +
             getattr(kiss.constants, name.upper()) +
             kiss.util.escape_special_codes(value) +
@@ -105,23 +116,21 @@ class KISS(object):
         :param callback: Callback to call with decoded data.
         """
         self.logger.debug('callback=%s', callback)
+
         read_buffer = ''
 
         while 1:
-            if self.host:
-                read_data = self.serial_int.recv(kiss.constants.READ_BYTES)
-                print read_data
-                waiting_data = None
-            else:
-                read_data = self.serial_int.read(kiss.constants.READ_BYTES)
-                waiting_data = self.serial_int.inWaiting()
-
-
-            if waiting_data:
-                read_data = ''.join([
-                    read_data, self.serial_int.read(waiting_data)])
-
-            if read_data:
+            read_data = None
+            if 'tcp' in self.interface_mode:
+                read_data = self.interface.recv(kiss.constants.READ_BYTES)
+            elif 'serial' in self.interface_mode:
+                read_data = self.interface.read(kiss.constants.READ_BYTES)
+                waiting_data = self.interface.inWaiting()
+                if waiting_data:
+                    read_data = ''.join([
+                        read_data, self.interface.read(waiting_data)])
+
+            if read_data is not None:
                 frames = []
 
                 split_data = read_data.split(kiss.constants.FEND)
@@ -157,24 +166,28 @@ class KISS(object):
                     if len(frame) and ord(frame[0]) == 0:
                         self.logger.debug('frame=%s', frame)
                         if callback:
-                            callback(frame)
+                            if 'tcp' in self.interface_mode:
+                                callback(
+                                    frame.lstrip(
+                                        kiss.constants.DATA_FRAME).strip())
+                            elif 'serial' in self.interface_mode:
+                                callback(frame)
 
     def write(self, frame):
         """
-        Writes frame to KISS device.
+        Writes frame to KISS interface.
 
         :param frame: Frame to write.
         """
+        interface_handler = None
 
-        if self.host:
-            return self.serial_int.send(''.join([
-                kiss.constants.FEND,
-                kiss.constants.DATA_FRAME,
-                kiss.util.escape_special_codes(frame),
-                kiss.constants.FEND
-            ]))
-        else:
-            return self.serial_int.write(''.join([
+        if 'tcp' in self.interface_mode:
+            interface_handler = self.interface.send
+        elif 'serial' in self.interface_mode:
+            interface_handler = self.interface.write
+
+        if interface_handler is not None:
+            return interface_handler(''.join([
                 kiss.constants.FEND,
                 kiss.constants.DATA_FRAME,
                 kiss.util.escape_special_codes(frame),
diff --git a/kiss/constants.py b/kiss/constants.py
index 7a3a45b..393c517 100644
--- a/kiss/constants.py
+++ b/kiss/constants.py
@@ -3,8 +3,8 @@
 
 """Constants for KISS Python Module."""
 
-__author__ = 'Greg Albrecht W2GMD <gba@onbeep.com>'
-__copyright__ = 'Copyright 2013 OnBeep, Inc. and Contributors'
+__author__ = 'Greg Albrecht W2GMD <gba@orionlabs.co>'
+__copyright__ = 'Copyright 2015 Orion Labs, Inc. and Contributors'
 __license__ = 'Apache License, Version 2.0'
 
 
diff --git a/kiss/util.py b/kiss/util.py
index 13eabbc..e298135 100644
--- a/kiss/util.py
+++ b/kiss/util.py
@@ -3,8 +3,8 @@
 
 """Utilities for the KISS Python Module."""
 
-__author__ = 'Greg Albrecht W2GMD <gba@onbeep.com>'
-__copyright__ = 'Copyright 2013 OnBeep, Inc. and Contributors'
+__author__ = 'Greg Albrecht W2GMD <gba@orionlabs.co>'
+__copyright__ = 'Copyright 2015 Orion Labs, Inc. and Contributors'
 __license__ = 'Apache License, Version 2.0'
 
 
diff --git a/requirements.txt b/requirements.txt
index 818474d..b65e6c2 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,8 +1,8 @@
 # Python Distribution Package Requirements for KISS.
 #
 # Source:: https://github.com/ampledata/kiss
-# Author:: Greg Albrecht W2GMD <gba@onbeep.com>
-# Copyright:: Copyright 2013 OnBeep, Inc. and Contributors
+# Author:: Greg Albrecht W2GMD <gba@orionlabs.co>
+# Copyright:: Copyright 2015 Orion Labs, Inc. and Contributors
 # License:: Apache License, Version 2.0
 
 
diff --git a/setup.cfg b/setup.cfg
index 7c056b0..12f7acf 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -1,8 +1,8 @@
 # Nosetests configuration for KISS.
 #
 # Source:: https://github.com/ampledata/kiss
-# Author:: Greg Albrecht W2GMD <gba@onbeep.com>
-# Copyright:: Copyright 2013 OnBeep, Inc. and Contributors
+# Author:: Greg Albrecht W2GMD <gba@orionlabs.co>
+# Copyright:: Copyright 2015 Orion Labs, Inc. and Contributors
 # License:: Apache License, Version 2.0
 #
 
diff --git a/setup.py b/setup.py
index b3b5e66..f2f7770 100644
--- a/setup.py
+++ b/setup.py
@@ -10,8 +10,8 @@ Source:: https://github.com/ampledata/kiss
 __title__ = 'kiss'
 __version__ = '2.0.2'
 __build__ = '0x020002'
-__author__ = 'Greg Albrecht W2GMD <gba@onbeep.com>'
-__copyright__ = 'Copyright 2013 OnBeep, Inc. and Contributors'
+__author__ = 'Greg Albrecht W2GMD <gba@orionlabs.co>'
+__copyright__ = 'Copyright 2015 Orion Labs, Inc. and Contributors'
 __license__ = 'Apache License, Version 2.0'
 
 
@@ -42,7 +42,7 @@ setup(
     description='KISS Python Module.',
     long_description=open('README.rst').read(),
     author='Greg Albrecht',
-    author_email='gba@onbeep.com',
+    author_email='gba@orionlabs.co',
     license=open('LICENSE').read(),
     url='https://github.com/ampledata/kiss',
     setup_requires=['nose'],
diff --git a/tests/constants.py b/tests/constants.py
index 2877af2..6021737 100644
--- a/tests/constants.py
+++ b/tests/constants.py
@@ -3,8 +3,8 @@
 
 """Constants for KISS Module Tests."""
 
-__author__ = 'Greg Albrecht W2GMD <gba@onbeep.com>'
-__copyright__ = 'Copyright 2013 OnBeep, Inc. and Contributors'
+__author__ = 'Greg Albrecht W2GMD <gba@orionlabs.co>'
+__copyright__ = 'Copyright 2015 Orion Labs, Inc. and Contributors'
 __license__ = 'Apache License, Version 2.0'
 
 
diff --git a/tests/context.py b/tests/context.py
index 6654f6e..54a2d00 100644
--- a/tests/context.py
+++ b/tests/context.py
@@ -3,6 +3,10 @@
 
 """Context for tests for KISS Python Module."""
 
+__author__ = 'Greg Albrecht W2GMD <gba@orionlabs.co>'
+__copyright__ = 'Copyright 2015 Orion Labs, Inc. and Contributors'
+__license__ = 'Apache License, Version 2.0'
+
 
 import os
 import sys
diff --git a/tests/test_kiss_util.py b/tests/test_kiss_util.py
index c5e91c8..8e1d18e 100644
--- a/tests/test_kiss_util.py
+++ b/tests/test_kiss_util.py
@@ -3,8 +3,8 @@
 
 """Tests for KISS Util Module."""
 
-__author__ = 'Greg Albrecht W2GMD <gba@onbeep.com>'
-__copyright__ = 'Copyright 2013 OnBeep, Inc. and Contributors'
+__author__ = 'Greg Albrecht W2GMD <gba@orionlabs.co>'
+__copyright__ = 'Copyright 2015 Orion Labs, Inc. and Contributors'
 __license__ = 'Apache License, Version 2.0'
 
 
-- 
GitLab