From 259c2aa397d776575592fcac780f8c10f9098660 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Wed, 1 Dec 2021 13:39:51 +0100 Subject: [PATCH] Conditional imports for serial-based interfaces --- RNS/Interfaces/AX25KISSInterface.py | 8 +++++++- RNS/Interfaces/KISSInterface.py | 8 +++++++- RNS/Interfaces/RNodeInterface.py | 8 +++++++- RNS/Interfaces/SerialInterface.py | 8 +++++++- setup.py | 2 +- 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/RNS/Interfaces/AX25KISSInterface.py b/RNS/Interfaces/AX25KISSInterface.py index 112817e..da7b218 100644 --- a/RNS/Interfaces/AX25KISSInterface.py +++ b/RNS/Interfaces/AX25KISSInterface.py @@ -2,7 +2,6 @@ from .Interface import Interface from time import sleep import sys -import serial import threading import time import RNS @@ -48,6 +47,13 @@ class AX25KISSInterface(Interface): serial = None def __init__(self, owner, name, callsign, ssid, port, speed, databits, parity, stopbits, preamble, txtail, persistence, slottime, flow_control): + if importlib.util.find_spec('serial') != None: + import serial + else: + RNS.log("Using the AX.25 KISS interface requires a serial communication module to be installed.", RNS.LOG_CRITICAL) + RNS.log("You can install one with the command: python3 -m pip install pyserial", RNS.LOG_CRITICAL) + RNS.panic() + self.rxb = 0 self.txb = 0 diff --git a/RNS/Interfaces/KISSInterface.py b/RNS/Interfaces/KISSInterface.py index 810399a..4c63f26 100644 --- a/RNS/Interfaces/KISSInterface.py +++ b/RNS/Interfaces/KISSInterface.py @@ -1,7 +1,6 @@ from .Interface import Interface from time import sleep import sys -import serial import threading import time import RNS @@ -40,6 +39,13 @@ class KISSInterface(Interface): serial = None def __init__(self, owner, name, port, speed, databits, parity, stopbits, preamble, txtail, persistence, slottime, flow_control, beacon_interval, beacon_data): + if importlib.util.find_spec('serial') != None: + import serial + else: + RNS.log("Using the KISS interface requires a serial communication module to be installed.", RNS.LOG_CRITICAL) + RNS.log("You can install one with the command: python3 -m pip install pyserial", RNS.LOG_CRITICAL) + RNS.panic() + self.rxb = 0 self.txb = 0 diff --git a/RNS/Interfaces/RNodeInterface.py b/RNS/Interfaces/RNodeInterface.py index 86334d2..eae0bcd 100644 --- a/RNS/Interfaces/RNodeInterface.py +++ b/RNS/Interfaces/RNodeInterface.py @@ -2,7 +2,6 @@ from .Interface import Interface from time import sleep import sys -import serial import threading import time import math @@ -72,6 +71,13 @@ class RNodeInterface(Interface): CALLSIGN_MAX_LEN = 32 def __init__(self, owner, name, port, frequency = None, bandwidth = None, txpower = None, sf = None, cr = None, flow_control = False, id_interval = None, id_callsign = None): + if importlib.util.find_spec('serial') != None: + import serial + else: + RNS.log("Using the RNode interface requires a serial communication module to be installed.", RNS.LOG_CRITICAL) + RNS.log("You can install one with the command: python3 -m pip install pyserial", RNS.LOG_CRITICAL) + RNS.panic() + self.rxb = 0 self.txb = 0 diff --git a/RNS/Interfaces/SerialInterface.py b/RNS/Interfaces/SerialInterface.py index 8ce02c7..34e7a62 100755 --- a/RNS/Interfaces/SerialInterface.py +++ b/RNS/Interfaces/SerialInterface.py @@ -1,7 +1,6 @@ from .Interface import Interface from time import sleep import sys -import serial import threading import time import RNS @@ -31,6 +30,13 @@ class SerialInterface(Interface): serial = None def __init__(self, owner, name, port, speed, databits, parity, stopbits): + if importlib.util.find_spec('serial') != None: + import serial + else: + RNS.log("Using the Serial interface requires a serial communication module to be installed.", RNS.LOG_CRITICAL) + RNS.log("You can install one with the command: python3 -m pip install pyserial", RNS.LOG_CRITICAL) + RNS.panic() + self.rxb = 0 self.txb = 0 diff --git a/setup.py b/setup.py index 0c10f6f..91af795 100644 --- a/setup.py +++ b/setup.py @@ -29,6 +29,6 @@ setuptools.setup( ] }, - install_requires=['cryptography>=3.4.7', 'pyserial', 'netifaces>=0.10.4'], + install_requires=['cryptography>=3.4.7', 'pyserial'], python_requires='>=3.6', )