From 7787dc1be4f1115c9b7e6ef6804e1e41eea777d2 Mon Sep 17 00:00:00 2001 From: Hemna Date: Wed, 10 Nov 2021 11:01:10 -0500 Subject: [PATCH] Removed requirement for aprs.fi key This removed the requirement of running APRSD for specifying the aprs.fi key in the config file. The plugins that need the key have been updated to set enabled = False when the key is missing. --- aprsd/config.py | 6 ------ aprsd/plugin.py | 15 ++++++++++++++- aprsd/plugins/fortune.py | 14 ++++++++------ aprsd/plugins/location.py | 12 ++++-------- aprsd/plugins/time.py | 24 ++++++++---------------- 5 files changed, 34 insertions(+), 37 deletions(-) diff --git a/aprsd/config.py b/aprsd/config.py index ebcc5cb..9650604 100644 --- a/aprsd/config.py +++ b/aprsd/config.py @@ -327,12 +327,6 @@ def parse_config(config_file): "ham.callsign", default_fail=DEFAULT_CONFIG_DICT["ham"]["callsign"], ) - - check_option( - config, - ["services", "aprs.fi", "apiKey"], - default_fail=DEFAULT_CONFIG_DICT["services"]["aprs.fi"]["apiKey"], - ) check_option( config, "aprs.login", diff --git a/aprsd/plugin.py b/aprsd/plugin.py index 2257c8b..cc3b85c 100644 --- a/aprsd/plugin.py +++ b/aprsd/plugin.py @@ -241,11 +241,24 @@ class APRSDRegexCommandPluginBase(APRSDPluginBase, metaclass=abc.ABCMeta): if result: self.tx_inc() else: - LOG.warning(f"{self.__class__} isn't enabled.") + result = f"{self.__class__.__name__} isn't enabled" + LOG.warning(result) return result +class APRSFIKEYMixin: + """Mixin class to enable checking the existence of the aprs.fi apiKey.""" + + def ensure_aprs_fi_key(self): + try: + self.config.check_option(["services", "aprs.fi", "apiKey"]) + self.enabled = True + except Exception as ex: + LOG.error(f"Failed to find config aprs.fi:apikey {ex}") + self.enabled = False + + class HelpPlugin(APRSDRegexCommandPluginBase): """Help Plugin that is always enabled. diff --git a/aprsd/plugins/fortune.py b/aprsd/plugins/fortune.py index e6ac01a..56c5013 100644 --- a/aprsd/plugins/fortune.py +++ b/aprsd/plugins/fortune.py @@ -15,6 +15,13 @@ class FortunePlugin(plugin.APRSDRegexCommandPluginBase): command_regex = "^[fF]" command_name = "fortune" + fortune_path = None + + def setup(self): + self.fortune_path = shutil.which("fortune") + if not self.fortune_path: + self.enabled = False + @trace.trace def process(self, packet): LOG.info("FortunePlugin") @@ -25,13 +32,8 @@ class FortunePlugin(plugin.APRSDRegexCommandPluginBase): reply = None - fortune_path = shutil.which("fortune") - if not fortune_path: - reply = "Fortune command not installed" - return reply - try: - cmnd = [fortune_path, "-s", "-n 60"] + cmnd = [self.fortune_path, "-s", "-n 60"] command = " ".join(cmnd) output = subprocess.check_output( command, diff --git a/aprsd/plugins/location.py b/aprsd/plugins/location.py index 6b9835e..a0381d9 100644 --- a/aprsd/plugins/location.py +++ b/aprsd/plugins/location.py @@ -8,13 +8,16 @@ from aprsd import plugin, plugin_utils, trace LOG = logging.getLogger("APRSD") -class LocationPlugin(plugin.APRSDRegexCommandPluginBase): +class LocationPlugin(plugin.APRSDRegexCommandPluginBase, plugin.APRSFIKEYMixin): """Location!""" version = "1.0" command_regex = "^[lL]" command_name = "location" + def setup(self): + self.ensure_aprs_fi_key() + @trace.trace def process(self, packet): LOG.info("Location Plugin") @@ -22,13 +25,6 @@ class LocationPlugin(plugin.APRSDRegexCommandPluginBase): message = packet.get("message_text", None) # ack = packet.get("msgNo", "0") - # get last location of a callsign, get descriptive name from weather service - try: - self.config.check_option(["services", "aprs.fi", "apiKey"]) - except Exception as ex: - LOG.error(f"Failed to find config aprs.fi:apikey {ex}") - return "No aprs.fi apikey found" - api_key = self.config["services"]["aprs.fi"]["apiKey"] # optional second argument is a callsign to search diff --git a/aprsd/plugins/time.py b/aprsd/plugins/time.py index 61e9f4b..faf9a5a 100644 --- a/aprsd/plugins/time.py +++ b/aprsd/plugins/time.py @@ -49,26 +49,22 @@ class TimePlugin(plugin.APRSDRegexCommandPluginBase): return self.build_date_str(localzone) -class TimeOpenCageDataPlugin(TimePlugin): +class TimeOpenCageDataPlugin(TimePlugin, plugin.APRSFIKEYMixin): """geocage based timezone fetching.""" version = "1.0" command_regex = "^[tT]" command_name = "time" + def setup(self): + self.ensure_aprs_fi_key() + @trace.trace def process(self, packet): fromcall = packet.get("from") message = packet.get("message_text", None) # ack = packet.get("msgNo", "0") - # get last location of a callsign, get descriptive name from weather service - try: - self.config.exists(["services", "aprs.fi", "apiKey"]) - except Exception as ex: - LOG.error(f"Failed to find config aprs.fi:apikey {ex}") - return "No aprs.fi apikey found" - api_key = self.config["services"]["aprs.fi"]["apiKey"] # optional second argument is a callsign to search @@ -115,26 +111,22 @@ class TimeOpenCageDataPlugin(TimePlugin): return self.build_date_str(localzone) -class TimeOWMPlugin(TimePlugin): +class TimeOWMPlugin(TimePlugin, plugin.APRSFIKEYMixin): """OpenWeatherMap based timezone fetching.""" version = "1.0" command_regex = "^[tT]" command_name = "time" + def setup(self): + self.ensure_aprs_fi_key() + @trace.trace def process(self, packet): fromcall = packet.get("from") message = packet.get("message_text", None) # ack = packet.get("msgNo", "0") - # get last location of a callsign, get descriptive name from weather service - try: - self.config.exists(["services", "aprs.fi", "apiKey"]) - except Exception as ex: - LOG.error(f"Failed to find config aprs.fi:apikey {ex}") - return "No aprs.fi apikey found" - # optional second argument is a callsign to search a = re.search(r"^.*\s+(.*)", message) if a is not None: