From 3280d071d61e9e6bdbfb85a8b4952cb218e60aa0 Mon Sep 17 00:00:00 2001 From: Jeffrey Phillips Freeman <jeffrey.freeman@syncleus.com> Date: Tue, 20 Sep 2016 21:43:43 -0400 Subject: [PATCH] Fixed many of the linting errors. --- src/apex/__init__.py | 1 - src/apex/aprs/__init__.py | 2 -- src/apex/aprs/aprs_internet_service.py | 6 ++-- src/apex/aprs/aprs_kiss.py | 3 +- src/apex/aprs/util.py | 6 ++-- src/apex/cli.py | 8 ++--- src/apex/kiss/__init__.py | 2 +- src/apex/kiss/constants.py | 6 ++-- src/apex/kiss/kiss.py | 23 ++++++++----- src/apex/kiss/util.py | 3 +- src/apex/pluginloader.py | 2 ++ src/apex/plugins/apexparadigm/__init__.py | 39 +++++++++++++---------- src/apex/plugins/beacon/__init__.py | 12 ++++--- src/apex/plugins/id/__init__.py | 10 ++++-- src/apex/plugins/status/__init__.py | 15 ++++++--- tests/test_kiss.py | 2 +- 16 files changed, 82 insertions(+), 58 deletions(-) diff --git a/src/apex/__init__.py b/src/apex/__init__.py index e67437e..266fc11 100644 --- a/src/apex/__init__.py +++ b/src/apex/__init__.py @@ -8,4 +8,3 @@ __email__ = "jeffrey.freeman@syncleus.com" __license__ = 'Apache License, Version 2.0' __copyright__ = 'Copyright 2016, Syncleus, Inc. and contributors' __credits__ = [] - diff --git a/src/apex/aprs/__init__.py b/src/apex/aprs/__init__.py index 859d84f..043e6c7 100644 --- a/src/apex/aprs/__init__.py +++ b/src/apex/aprs/__init__.py @@ -20,8 +20,6 @@ from __future__ import print_function, unicode_literals from __future__ import absolute_import, division import logging -from apex.aprs.aprs_kiss import AprsKiss -from apex.aprs.aprs_internet_service import AprsInternetService __author__ = 'Jeffrey Phillips Freeman (WI2ARD)' __maintainer__ = "Jeffrey Phillips Freeman (WI2ARD)" diff --git a/src/apex/aprs/aprs_internet_service.py b/src/apex/aprs/aprs_internet_service.py index e9aac33..03728a6 100644 --- a/src/apex/aprs/aprs_internet_service.py +++ b/src/apex/aprs/aprs_internet_service.py @@ -11,6 +11,7 @@ import logging import socket import requests import time +from apex.aprs import constants as aprsConstants, util as aprsUtil __author__ = 'Jeffrey Phillips Freeman (WI2ARD)' __maintainer__ = "Jeffrey Phillips Freeman (WI2ARD)" @@ -19,8 +20,6 @@ __license__ = 'Apache License, Version 2.0' __copyright__ = 'Copyright 2016, Syncleus, Inc. and contributors' __credits__ = [] -from apex.aprs import constants as aprsConstants, util as aprsUtil - class AprsInternetService(object): @@ -84,7 +83,6 @@ class AprsInternetService(object): if 'TCP' in protocol: self.logger.debug('sending message=%s', str(frame)) - # message = frame['source'].encode('ascii') + b">" + frame['destination'].encode('ascii') + aprs.util.format_path(frame['path']).encode('ascii') + b":" + frame['text'] + b'\n\r' # TODO: simplify this message = bytearray() for frame_chr in aprsUtil.format_aprs_frame(frame): @@ -95,7 +93,7 @@ class AprsInternetService(object): self.aprsis_sock.sendall(message) message_sent = True except (ConnectionResetError, BrokenPipeError) as ex: - #connection reset wait a second then try again + # connection reset wait a second then try again time.sleep(1) self.aprsis_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.aprsis_sock.connect((self.server, self.port)) diff --git a/src/apex/aprs/aprs_kiss.py b/src/apex/aprs/aprs_kiss.py index b270162..d2e69fd 100644 --- a/src/apex/aprs/aprs_kiss.py +++ b/src/apex/aprs/aprs_kiss.py @@ -117,7 +117,8 @@ class AprsKiss(apex.kiss.Kiss): :return: KISS-encoded APRS frame. :rtype: list """ - enc_frame = AprsKiss.__encode_callsign(AprsKiss.__parse_identity_string(frame['destination'])) + AprsKiss.__encode_callsign(AprsKiss.__parse_identity_string(frame['source'])) + enc_frame = AprsKiss.__encode_callsign(AprsKiss.__parse_identity_string(frame['destination'])) +\ + AprsKiss.__encode_callsign(AprsKiss.__parse_identity_string(frame['source'])) for p in frame['path']: enc_frame += AprsKiss.__encode_callsign(AprsKiss.__parse_identity_string(p)) diff --git a/src/apex/aprs/util.py b/src/apex/aprs/util.py index 8d9e6ce..ebb2940 100755 --- a/src/apex/aprs/util.py +++ b/src/apex/aprs/util.py @@ -108,6 +108,7 @@ def decode_aprs_ascii_frame(ascii_frame): return decoded_frame + def format_path(path_list): """ Formats path from raw APRS KISS frame. @@ -139,6 +140,7 @@ def format_aprs_frame(frame): formatted_frame += chr(frame_byte) return formatted_frame + def valid_callsign(callsign): """ Validates callsign. @@ -196,10 +198,10 @@ def hash_frame(frame): index = 0 frame_string_prefix = frame['source'] + ">" + frame['destination'] + ":" for frame_chr in frame_string_prefix: - hash = ord(frame_chr)<<(8*(index%4)) ^ hash + hash = ord(frame_chr) << (8*(index % 4)) ^ hash index += 1 for byte in frame['text']: - hash = byte<<(8*(index%4)) ^ hash + hash = byte << (8*(index % 4)) ^ hash index += 1 return hash diff --git a/src/apex/cli.py b/src/apex/cli.py index 2b7e77b..bf38743 100644 --- a/src/apex/cli.py +++ b/src/apex/cli.py @@ -18,7 +18,6 @@ Why does this file exist, and why not put this in __main__? from __future__ import print_function, unicode_literals from __future__ import absolute_import, division -import sys import click import time import signal @@ -27,9 +26,11 @@ import threading import cachetools import traceback from apex.pluginloader import getPlugins, loadPlugin +from apex.kiss import constants as kissConstants +import apex.aprs if sys.version_info < (3, 0): - import ConfigParser + import ConfigParser # noqa: F401 elif sys.version_info >= (3, 0): import configparser @@ -40,9 +41,6 @@ __license__ = 'Apache License, Version 2.0' __copyright__ = 'Copyright 2016, Syncleus, Inc. and contributors' __credits__ = [] -from apex.kiss import constants as kissConstants -import apex.aprs - @click.command() @click.argument('names', nargs=-1) diff --git a/src/apex/kiss/__init__.py b/src/apex/kiss/__init__.py index 9dd948e..2332c13 100644 --- a/src/apex/kiss/__init__.py +++ b/src/apex/kiss/__init__.py @@ -20,7 +20,7 @@ from __future__ import print_function, unicode_literals from __future__ import absolute_import, division import logging -from .kiss import Kiss +from .kiss import Kiss # noqa: F401 __author__ = 'Jeffrey Phillips Freeman (WI2ARD)' __maintainer__ = "Jeffrey Phillips Freeman (WI2ARD)" diff --git a/src/apex/kiss/constants.py b/src/apex/kiss/constants.py index 3b31659..b0c0cf6 100644 --- a/src/apex/kiss/constants.py +++ b/src/apex/kiss/constants.py @@ -56,10 +56,10 @@ DEFAULT_KISS_CONFIG_VALUES = { 'FULL_DUPLEX': 0, } -#This command will exit KISS mode +# This command will exit KISS mode MODE_END = [192, 255, 192, 13] # This will start kiss on a W8DED or LINK>.<NORD firmware MODE_INIT_W8DED = [13, 27, 64, 75, 13] MODE_INIT_LINKNORD = MODE_INIT_W8DED -#This will work for any Kenwood D710 -MODE_INIT_KENWOOD_D710 = [72,66,32,49,50,48,48,13,75,73,83,83,32,79,78,13,82,69,83,84,65,82,84,13] +# This will work for any Kenwood D710 +MODE_INIT_KENWOOD_D710 = [72, 66, 32, 49, 50, 48, 48, 13, 75, 73, 83, 83, 32, 79, 78, 13, 82, 69, 83, 84, 65, 82, 84, 13] diff --git a/src/apex/kiss/kiss.py b/src/apex/kiss/kiss.py index 035d00e..bdd6709 100644 --- a/src/apex/kiss/kiss.py +++ b/src/apex/kiss/kiss.py @@ -35,7 +35,14 @@ class Kiss(object): frame_buffer = [] - def __init__(self, com_port=None, baud=38400, parity=serial.PARITY_NONE, stop_bits=serial.STOPBITS_ONE, byte_size=serial.EIGHTBITS, host=None, tcp_port=8000, strip_df_start=True): + def __init__(self, com_port=None, + baud=38400, + parity=serial.PARITY_NONE, + stop_bits=serial.STOPBITS_ONE, + byte_size=serial.EIGHTBITS, + host=None, + tcp_port=8000, + strip_df_start=True): self.com_port = com_port self.baud = baud self.parity = parity @@ -115,7 +122,7 @@ class Kiss(object): elif raw_code_byte is kissConstants.FEND: encoded_bytes += kissConstants.FESC_TFEND else: - encoded_bytes += [raw_code_byte]; + encoded_bytes += [raw_code_byte] return encoded_bytes @staticmethod @@ -132,7 +139,7 @@ class Kiss(object): raise Exception("port out of range") elif command_code > 127 or command_code < 0: raise Exception("command_Code out of range") - return (port<<4) & command_code + return (port << 4) & command_code def start(self, mode_init=None, **kwargs): """ @@ -149,7 +156,8 @@ class Kiss(object): address = (self.host, self.tcp_port) self.interface = socket.create_connection(address) elif 'serial' in self.interface_mode: - self.interface = serial.Serial(port=self.com_port, baudrate=self.baud, parity=self.parity, stopbits=self.stop_bits, bytesize=self.byte_size) + self.interface = serial.Serial(port=self.com_port, baudrate=self.baud, parity=self.parity, + stopbits=self.stop_bits, bytesize=self.byte_size) self.interface.timeout = kissConstants.SERIAL_TIMEOUT if mode_init is not None: self.interface.write(mode_init) @@ -163,14 +171,13 @@ class Kiss(object): # If no settings specified, default to config values similar # to those that ship with Xastir. - #if not kwargs: + # if not kwargs: # kwargs = kiss.constants.DEFAULT_KISS_CONFIG_VALUES def close(self): if self.exit_kiss is True: self.interface.write(kissConstants.MODE_END) - def write_setting(self, name, value): """ Writes KISS Command Codes to attached device. @@ -205,7 +212,6 @@ class Kiss(object): split_data = [[]] for read_byte in read_data: if read_byte is kissConstants.FEND: - #split_data_index += 1 split_data.append([]) else: split_data[-1].append(read_byte) @@ -260,7 +266,8 @@ class Kiss(object): :param frame: Frame to write. """ - kiss_packet = [kissConstants.FEND] + [Kiss.__command_byte_combine(port, kissConstants.DATA_FRAME)] + Kiss.__escape_special_codes(frame_bytes) + [kissConstants.FEND] + kiss_packet = [kissConstants.FEND] + [Kiss.__command_byte_combine(port, kissConstants.DATA_FRAME)] +\ + Kiss.__escape_special_codes(frame_bytes) + [kissConstants.FEND] if 'tcp' in self.interface_mode: return self.interface.send(bytearray(kiss_packet)) diff --git a/src/apex/kiss/util.py b/src/apex/kiss/util.py index 7a274d3..52a0b09 100644 --- a/src/apex/kiss/util.py +++ b/src/apex/kiss/util.py @@ -16,6 +16,7 @@ __license__ = 'Apache License, Version 2.0' __copyright__ = 'Copyright 2016, Syncleus, Inc. and contributors' __credits__ = [] + def extract_ui(frame): """ Extracts the UI component of an individual frame. @@ -29,5 +30,3 @@ def extract_ui(frame): ''.join([apex.kiss.constants.FEND, apex.kiss.constants.DATA_FRAME])) end_ui = start_ui[0].split(''.join([apex.kiss.constants.SLOT_TIME, chr(0xF0)])) return ''.join([chr(ord(x) >> 1) for x in end_ui[0]]) - - diff --git a/src/apex/pluginloader.py b/src/apex/pluginloader.py index 6d8b048..9209883 100644 --- a/src/apex/pluginloader.py +++ b/src/apex/pluginloader.py @@ -15,6 +15,7 @@ __credits__ = [] PluginFolder = "./plugins" MainModule = "__init__" + def getPlugins(): plugins = [] possibleplugins = os.listdir(PluginFolder) @@ -26,5 +27,6 @@ def getPlugins(): plugins.append(i) return plugins + def loadPlugin(plugin): return importlib.import_module("plugins." + plugin) diff --git a/src/apex/plugins/apexparadigm/__init__.py b/src/apex/plugins/apexparadigm/__init__.py index 1aaf122..2fb8955 100644 --- a/src/apex/plugins/apexparadigm/__init__.py +++ b/src/apex/plugins/apexparadigm/__init__.py @@ -16,15 +16,18 @@ __credits__ = [] plugin = None + def start(config, port_map, packet_cache, aprsis): global plugin plugin = ApexParadigmPlugin(config, port_map, packet_cache, aprsis) plugin.run() + def handle_packet(frame, recv_port, recv_port_name): global plugin plugin.handle_packet(frame, recv_port, recv_port_name) + class ApexParadigmPlugin(object): BAND_PATH_REGEX = re.compile(r'(\d{1,4})M(\d{0,3})') @@ -45,7 +48,7 @@ class ApexParadigmPlugin(object): if hop.startswith('WI2ARD') and hop.endswith('*'): return - for hop_index in range(0,len(frame['path'])): + for hop_index in range(0, len(frame['path'])): hop = frame['path'][hop_index] if hop[-1] is not '*': split_hop = hop.split('-') @@ -74,9 +77,10 @@ class ApexParadigmPlugin(object): if band_path: if band_path_net: if node == port['net']: - frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] + [hop + "*"] + frame['path'][hop_index+1:] + frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] +\ + [hop + "*"] + frame['path'][hop_index+1:] frame_hash = apex.aprs.util.hash_frame(frame) - if not frame_hash in self.packet_cache.values(): + if frame_hash not in self.packet_cache.values(): self.packet_cache[str(frame_hash)] = frame_hash port['tnc'].write(frame, port['tnc_port']) self.aprsis.send(frame) @@ -84,9 +88,10 @@ class ApexParadigmPlugin(object): return else: if port['net'].startswith(node): - frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] + [hop + "*"] + frame['path'][hop_index+1:] + frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] +\ + [hop + "*"] + frame['path'][hop_index+1:] frame_hash = apex.aprs.util.hash_frame(frame) - if not frame_hash in self.packet_cache.values(): + if frame_hash not in self.packet_cache.values(): self.packet_cache[str(frame_hash)] = frame_hash port['tnc'].write(frame, port['tnc_port']) self.aprsis.send(frame) @@ -98,25 +103,27 @@ class ApexParadigmPlugin(object): else: frame['path'][hop_index] = port['identifier'] + '*' frame_hash = apex.aprs.util.hash_frame(frame) - if not frame_hash in self.packet_cache.values(): + if frame_hash not in self.packet_cache.values(): self.packet_cache[str(frame_hash)] = frame_hash port['tnc'].write(frame, port['tnc_port']) self.aprsis.send(frame) print(port_name + " >> " + apex.aprs.util.format_aprs_frame(frame)) return elif node == "GATE" and port['net'].startswith("2M"): - frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] + [node + "*"] + frame['path'][hop_index+1:] + frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] + [node + "*"] +\ + frame['path'][hop_index+1:] frame_hash = apex.aprs.util.hash_frame(frame) - if not frame_hash in self.packet_cache.values(): + if frame_hash not in self.packet_cache.values(): self.packet_cache[str(frame_hash)] = frame_hash port['tnc'].write(frame, port['tnc_port']) self.aprsis.send(frame) print(port_name + " >> " + apex.aprs.util.format_aprs_frame(frame)) return if node.startswith('WIDE') and ssid > 1: - frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] + [node + "-" + str(ssid-1)] + frame['path'][hop_index+1:] + frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] +\ + [node + "-" + str(ssid-1)] + frame['path'][hop_index+1:] frame_hash = apex.aprs.util.hash_frame(frame) - if not frame_hash in self.packet_cache.values(): + if frame_hash not in self.packet_cache.values(): self.packet_cache[str(frame_hash)] = frame_hash recv_port['tnc'].write(frame, recv_port['tnc_port']) self.aprsis.send(frame) @@ -125,7 +132,7 @@ class ApexParadigmPlugin(object): elif node.startswith('WIDE') and ssid is 1: frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] + [node + "*"] + frame['path'][hop_index+1:] frame_hash = apex.aprs.util.hash_frame(frame) - if not frame_hash in self.packet_cache.values(): + if frame_hash not in self.packet_cache.values(): self.packet_cache[str(frame_hash)] = frame_hash recv_port['tnc'].write(frame, recv_port['tnc_port']) self.aprsis.send(frame) @@ -135,7 +142,7 @@ class ApexParadigmPlugin(object): frame['path'][hop_index] = node + "*" # no return else: - #If we didnt digipeat it then we didn't modify the frame, send it to aprsis as-is + # If we didnt digipeat it then we didn't modify the frame, send it to aprsis as-is self.aprsis.send(frame) return @@ -171,7 +178,7 @@ class ApexParadigmPlugin(object): band_path_net = band_match.group(2) if not band_path: - continue; + continue for port_name in self.port_map.keys(): port = self.port_map[port_name] @@ -241,8 +248,8 @@ class ApexParadigmPlugin(object): if not selected_hop: return - #now lets digipeat this packet - new_path=[] + # now lets digipeat this packet + new_path = [] for hop_index in range(0, len(frame['path'])): hop = frame['path'][hop_index] if hop[-1] != '*': @@ -257,7 +264,7 @@ class ApexParadigmPlugin(object): new_path += [hop] frame['path'] = new_path frame_hash = apex.aprs.util.hash_frame(frame) - if not frame_hash in self.packet_cache.values(): + if frame_hash not in self.packet_cache.values(): self.packet_cache[str(frame_hash)] = frame_hash selected_hop['port']['tnc'].write(frame, selected_hop['port']['tnc_port']) self.aprsis.send(frame) diff --git a/src/apex/plugins/beacon/__init__.py b/src/apex/plugins/beacon/__init__.py index 4f74e6c..9319360 100644 --- a/src/apex/plugins/beacon/__init__.py +++ b/src/apex/plugins/beacon/__init__.py @@ -14,15 +14,18 @@ __credits__ = [] plugin = None + def start(config, port_map, packet_cache, aprsis): global plugin plugin = BeaconPlugin(config, port_map, packet_cache, aprsis) plugin.run() + def handle_packet(frame, recv_port, recv_port_name): return -class BeaconPlugin( object ): + +class BeaconPlugin(object): def __init__(self, config, port_map, packet_cache, aprsis): self.port_map = port_map @@ -40,13 +43,14 @@ class BeaconPlugin( object ): port['beacon_path'] = config.get(port_section, 'beacon_path') def run(self): - while 1 : + while 1: for port_name in self.port_map.keys(): port = self.port_map[port_name] - beacon_frame = {'source':port['identifier'], 'destination': 'APRS', 'path':port['beacon_path'].split(','), 'text': list(port['beacon_text'].encode('ascii'))} + beacon_frame = {'source': port['identifier'], 'destination': 'APRS', + 'path': port['beacon_path'].split(','), 'text': list(port['beacon_text'].encode('ascii'))} frame_hash = apex.aprs.util.hash_frame(beacon_frame) - if not frame_hash in self.packet_cache.values(): + 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']) print(port_name + " >> " + apex.aprs.util.format_aprs_frame(beacon_frame)) diff --git a/src/apex/plugins/id/__init__.py b/src/apex/plugins/id/__init__.py index bae924e..879efe3 100644 --- a/src/apex/plugins/id/__init__.py +++ b/src/apex/plugins/id/__init__.py @@ -14,14 +14,17 @@ __credits__ = [] plugin = None + def start(config, port_map, packet_cache, aprsis): global plugin plugin = IdPlugin(config, port_map, packet_cache, aprsis) plugin.run() + def handle_packet(frame, recv_port, recv_port_name): return + class IdPlugin(object): def __init__(self, config, port_map, packet_cache, aprsis): @@ -41,13 +44,14 @@ class IdPlugin(object): def run(self): time.sleep(30) - while 1 : + while 1: for port_name in self.port_map.keys(): port = self.port_map[port_name] - id_frame = {'source':port['identifier'], 'destination': 'ID', 'path':port['id_path'].split(','), 'text': list(port['id_text'].encode('ascii'))} + id_frame = {'source': port['identifier'], 'destination': 'ID', 'path': port['id_path'].split(','), + 'text': list(port['id_text'].encode('ascii'))} frame_hash = apex.aprs.util.hash_frame(id_frame) - if not frame_hash in self.packet_cache.values(): + 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']) print(port_name + " >> " + apex.aprs.util.format_aprs_frame(id_frame)) diff --git a/src/apex/plugins/status/__init__.py b/src/apex/plugins/status/__init__.py index 4ce4fc9..4bdefa5 100644 --- a/src/apex/plugins/status/__init__.py +++ b/src/apex/plugins/status/__init__.py @@ -14,14 +14,17 @@ __credits__ = [] plugin = None + def start(config, port_map, packet_cache, aprsis): global plugin plugin = StatusPlugin(config, port_map, packet_cache, aprsis) plugin.run() + def handle_packet(frame, recv_port, recv_port_name): return + class StatusPlugin(object): def __init__(self, config, port_map, packet_cache, aprsis): @@ -32,7 +35,6 @@ class StatusPlugin(object): for section in config.sections(): if section.startswith("TNC "): tnc_name = section.split(" ")[1] - kiss_tnc = None for port_id in range(1, 1+int(config.get(section, 'port_count'))): port_name = tnc_name + '-' + str(port_id) port = port_map[port_name] @@ -40,16 +42,19 @@ class StatusPlugin(object): port['status_text'] = config.get(port_section, 'status_text') port['status_path'] = config.get(port_section, 'status_path') - def run(self): time.sleep(60) - while 1 : + while 1: for port_name in self.port_map.keys(): port = self.port_map[port_name] - status_frame = {'source':port['identifier'], 'destination': 'APRS', 'path':port['status_path'].split(','), 'text': list(port['status_text'].encode('ascii'))} + status_frame = { + 'source': port['identifier'], + 'destination': 'APRS', + 'path': port['status_path'].split(','), + 'text': list(port['status_text'].encode('ascii'))} frame_hash = apex.aprs.util.hash_frame(status_frame) - if not frame_hash in self.packet_cache.values(): + 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.format_aprs_frame(status_frame)) diff --git a/tests/test_kiss.py b/tests/test_kiss.py index d4dfb97..286111c 100644 --- a/tests/test_kiss.py +++ b/tests/test_kiss.py @@ -12,7 +12,7 @@ import unittest from . import constants -import apex +import apex.kiss import apex.kiss.constants import apex.kiss.util -- GitLab