mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-03-15 12:24:29 +00:00
Improve adherence to python coding standards and clear up IDE static analysis warnings
This commit is contained in:
@@ -16,62 +16,62 @@ class DXCluster(SpotProvider):
|
||||
"""Spot provider for a DX Cluster. Hostname, port, login_prompt, login_callsign and allow_rbn_spots are provided in config.
|
||||
See config-example.yml for examples."""
|
||||
|
||||
CALLSIGN_PATTERN = "([a-z|0-9|/]+)"
|
||||
FREQUENCY_PATTERN = "([0-9|.]+)"
|
||||
LINE_PATTERN_EXCLUDE_RBN = re.compile(
|
||||
"^DX de " + CALLSIGN_PATTERN + ":\\s+" + FREQUENCY_PATTERN + "\\s+" + CALLSIGN_PATTERN + "\\s+(.*)\\s+(\\d{4}Z)",
|
||||
_CALLSIGN_PATTERN = "([a-z|0-9|/]+)"
|
||||
_FREQUENCY_PATTERN = "([0-9|.]+)"
|
||||
_LINE_PATTERN_EXCLUDE_RBN = re.compile(
|
||||
"^DX de " + _CALLSIGN_PATTERN + ":\\s+" + _FREQUENCY_PATTERN + "\\s+" + _CALLSIGN_PATTERN + "\\s+(.*)\\s+(\\d{4}Z)",
|
||||
re.IGNORECASE)
|
||||
LINE_PATTERN_ALLOW_RBN = re.compile(
|
||||
"^DX de " + CALLSIGN_PATTERN + "-?#?:\\s+" + FREQUENCY_PATTERN + "\\s+" + CALLSIGN_PATTERN + "\\s+(.*)\\s+(\\d{4}Z)",
|
||||
_LINE_PATTERN_ALLOW_RBN = re.compile(
|
||||
"^DX de " + _CALLSIGN_PATTERN + "-?#?:\\s+" + _FREQUENCY_PATTERN + "\\s+" + _CALLSIGN_PATTERN + "\\s+(.*)\\s+(\\d{4}Z)",
|
||||
re.IGNORECASE)
|
||||
|
||||
def __init__(self, provider_config):
|
||||
"""Constructor requires hostname and port"""
|
||||
|
||||
super().__init__(provider_config)
|
||||
self.hostname = provider_config["host"]
|
||||
self.port = provider_config["port"]
|
||||
self.login_prompt = provider_config["login_prompt"] if "login_prompt" in provider_config else "login:"
|
||||
self.login_callsign = provider_config[
|
||||
self._hostname = provider_config["host"]
|
||||
self._port = provider_config["port"]
|
||||
self._login_prompt = provider_config["login_prompt"] if "login_prompt" in provider_config else "login:"
|
||||
self._login_callsign = provider_config[
|
||||
"login_callsign"] if "login_callsign" in provider_config else SERVER_OWNER_CALLSIGN
|
||||
self.allow_rbn_spots = provider_config["allow_rbn_spots"] if "allow_rbn_spots" in provider_config else False
|
||||
self.spot_line_pattern = self.LINE_PATTERN_ALLOW_RBN if self.allow_rbn_spots else self.LINE_PATTERN_EXCLUDE_RBN
|
||||
self.telnet = None
|
||||
self.thread = Thread(target=self.handle)
|
||||
self.thread.daemon = True
|
||||
self.run = True
|
||||
self._allow_rbn_spots = provider_config["allow_rbn_spots"] if "allow_rbn_spots" in provider_config else False
|
||||
self._spot_line_pattern = self._LINE_PATTERN_ALLOW_RBN if self._allow_rbn_spots else self._LINE_PATTERN_EXCLUDE_RBN
|
||||
self._telnet = None
|
||||
self._thread = Thread(target=self._handle)
|
||||
self._thread.daemon = True
|
||||
self._running = True
|
||||
|
||||
def start(self):
|
||||
self.thread.start()
|
||||
self._thread.start()
|
||||
|
||||
def stop(self):
|
||||
self.run = False
|
||||
self.telnet.close()
|
||||
self.thread.join()
|
||||
self._running = False
|
||||
self._telnet.close()
|
||||
self._thread.join()
|
||||
|
||||
def handle(self):
|
||||
while self.run:
|
||||
def _handle(self):
|
||||
while self._running:
|
||||
connected = False
|
||||
while not connected and self.run:
|
||||
while not connected and self._running:
|
||||
try:
|
||||
self.status = "Connecting"
|
||||
logging.info("DX Cluster " + self.hostname + " connecting...")
|
||||
self.telnet = telnetlib3.Telnet(self.hostname, self.port)
|
||||
self.telnet.read_until(self.login_prompt.encode("latin-1"))
|
||||
self.telnet.write((self.login_callsign + "\n").encode("latin-1"))
|
||||
logging.info("DX Cluster " + self._hostname + " connecting...")
|
||||
self._telnet = telnetlib3.Telnet(self._hostname, self._port)
|
||||
self._telnet.read_until(self._login_prompt.encode("latin-1"))
|
||||
self._telnet.write((self._login_callsign + "\n").encode("latin-1"))
|
||||
connected = True
|
||||
logging.info("DX Cluster " + self.hostname + " connected.")
|
||||
except Exception as e:
|
||||
logging.info("DX Cluster " + self._hostname + " connected.")
|
||||
except Exception:
|
||||
self.status = "Error"
|
||||
logging.exception("Exception while connecting to DX Cluster Provider (" + self.hostname + ").")
|
||||
logging.exception("Exception while connecting to DX Cluster Provider (" + self._hostname + ").")
|
||||
sleep(5)
|
||||
|
||||
self.status = "Waiting for Data"
|
||||
while connected and self.run:
|
||||
while connected and self._running:
|
||||
try:
|
||||
# Check new telnet info against regular expression
|
||||
telnet_output = self.telnet.read_until("\n".encode("latin-1"))
|
||||
match = self.spot_line_pattern.match(telnet_output.decode("latin-1"))
|
||||
telnet_output = self._telnet.read_until("\n".encode("latin-1"))
|
||||
match = self._spot_line_pattern.match(telnet_output.decode("latin-1"))
|
||||
if match:
|
||||
spot_time = datetime.strptime(match.group(5), "%H%MZ")
|
||||
spot_datetime = datetime.combine(datetime.today(), spot_time.time()).replace(tzinfo=pytz.UTC)
|
||||
@@ -83,20 +83,20 @@ class DXCluster(SpotProvider):
|
||||
time=spot_datetime.timestamp())
|
||||
|
||||
# Add to our list
|
||||
self.submit(spot)
|
||||
self._submit(spot)
|
||||
|
||||
self.status = "OK"
|
||||
self.last_update_time = datetime.now(pytz.UTC)
|
||||
logging.debug("Data received from DX Cluster " + self.hostname + ".")
|
||||
logging.debug("Data received from DX Cluster " + self._hostname + ".")
|
||||
|
||||
except Exception as e:
|
||||
except Exception:
|
||||
connected = False
|
||||
if self.run:
|
||||
if self._running:
|
||||
self.status = "Error"
|
||||
logging.exception("Exception in DX Cluster Provider (" + self.hostname + ")")
|
||||
logging.exception("Exception in DX Cluster Provider (" + self._hostname + ")")
|
||||
sleep(5)
|
||||
else:
|
||||
logging.info("DX Cluster " + self.hostname + " shutting down...")
|
||||
logging.info("DX Cluster " + self._hostname + " shutting down...")
|
||||
self.status = "Shutting down"
|
||||
|
||||
self.status = "Disconnected"
|
||||
|
||||
Reference in New Issue
Block a user