From 3faf41b203219fb3df237184e02daf2b9191a0ec Mon Sep 17 00:00:00 2001 From: Hemna Date: Wed, 8 Sep 2021 14:18:49 -0400 Subject: [PATCH] Added enabled flag for every plugin object This allows the admin interface to see which plugins are registered and enabled. Enabled is a flag that is set in the setup() method of the plugin. This gives the plugin developer a chance to disable the plugin if something isn't right at setup time. This allows aprsd to ignore plugins that are registered but not emabled. --- aprsd/plugin.py | 67 ++++++++++++++++++++++--------------- aprsd/stats.py | 1 + aprsd/web/static/js/main.js | 7 ++-- tests/fake.py | 6 ++++ 4 files changed, 52 insertions(+), 29 deletions(-) diff --git a/aprsd/plugin.py b/aprsd/plugin.py index a116a09..15027fe 100644 --- a/aprsd/plugin.py +++ b/aprsd/plugin.py @@ -55,6 +55,8 @@ class APRSDPluginBase(metaclass=abc.ABCMeta): # Holds the list of APRSDThreads that the plugin creates threads = [] + # Set this in setup() + enabled = False def __init__(self, config): self.config = config @@ -67,7 +69,7 @@ class APRSDPluginBase(metaclass=abc.ABCMeta): self.start_threads() def start_threads(self): - if self.threads: + if self.enabled and self.threads: if not isinstance(self.threads, list): self.threads = [self.threads] @@ -99,8 +101,10 @@ class APRSDPluginBase(metaclass=abc.ABCMeta): """Version""" raise NotImplementedError + @abc.abstractmethod def setup(self): """Do any plugin setup here.""" + self.enabled = True def create_threads(self): """Gives the plugin writer the ability start a background thread.""" @@ -139,7 +143,6 @@ class APRSDWatchListPluginBase(APRSDPluginBase, metaclass=abc.ABCMeta): by a particular HAM callsign, write a plugin based off of this class. """ - enabled = False def setup(self): # if we have a watch list enabled, we need to add filtering @@ -162,15 +165,18 @@ class APRSDWatchListPluginBase(APRSDPluginBase, metaclass=abc.ABCMeta): LOG.warning("Watch list enabled, but no callsigns set.") def filter(self, packet): - wl = packets.WatchList() - result = messaging.NULL_MESSAGE - if wl.callsign_in_watchlist(packet["from"]): - # packet is from a callsign in the watch list - self.rx_inc() - result = self.process() - if result: - self.tx_inc() - wl.update_seen(packet) + if self.enabled: + wl = packets.WatchList() + result = messaging.NULL_MESSAGE + if wl.callsign_in_watchlist(packet["from"]): + # packet is from a callsign in the watch list + self.rx_inc() + result = self.process() + if result: + self.tx_inc() + wl.update_seen(packet) + else: + LOG.warning(f"{self.__class__} plugin is not enabled") return result @@ -193,26 +199,33 @@ class APRSDRegexCommandPluginBase(APRSDPluginBase, metaclass=abc.ABCMeta): """The regex to match from the caller""" raise NotImplementedError + def setup(self): + """Do any plugin setup here.""" + self.enabled = True + @hookimpl def filter(self, packet): - result = None + if self.enabled: + result = None - message = packet.get("message_text", None) - msg_format = packet.get("format", None) - tocall = packet.get("addresse", None) + message = packet.get("message_text", None) + msg_format = packet.get("format", None) + tocall = packet.get("addresse", None) - # Only process messages destined for us - # and is an APRS message format and has a message. - if ( - tocall == self.config["aprs"]["login"] - and msg_format == "message" - and message - ): - if re.search(self.command_regex, message): - self.rx_inc() - result = self.process(packet) - if result: - self.tx_inc() + # Only process messages destined for us + # and is an APRS message format and has a message. + if ( + tocall == self.config["aprs"]["login"] + and msg_format == "message" + and message + ): + if re.search(self.command_regex, message): + self.rx_inc() + result = self.process(packet) + if result: + self.tx_inc() + else: + LOG.warning(f"{self.__class__} is not enabled.") return result diff --git a/aprsd/stats.py b/aprsd/stats.py index 144c6bf..c9bd38c 100644 --- a/aprsd/stats.py +++ b/aprsd/stats.py @@ -194,6 +194,7 @@ class APRSDStats: for p in plugins: plugin_stats[full_name_with_qualname(p)] = { + "enabled": p.enabled, "rx": p.rx_count, "tx": p.tx_count, } diff --git a/aprsd/web/static/js/main.js b/aprsd/web/static/js/main.js index f3849b3..fa48fbb 100644 --- a/aprsd/web/static/js/main.js +++ b/aprsd/web/static/js/main.js @@ -60,7 +60,10 @@ function update_watchlist_from_packet(callsign, val) { function update_plugins( data ) { var plugindiv = $("#pluginDiv"); - var html_str = '' + var html_str = '
Plugin NameProcessed PacketsSent Packets
' + html_str += '' + html_str += '' + html_str += '' plugindiv.html('') var plugins = data["stats"]["plugins"]; @@ -69,7 +72,7 @@ function update_plugins( data ) { for (var i=0; i'; + html_str += ''; } html_str += "
Plugin NamePlugin Enabled?Processed PacketsSent Packets
' + val["rx"] + '' + val["tx"] + '
' + key + '' + val["enabled"] + '' + val["rx"] + '' + val["tx"] + '
"; plugindiv.append(html_str); diff --git a/tests/fake.py b/tests/fake.py index a45ead3..71a2ec1 100644 --- a/tests/fake.py +++ b/tests/fake.py @@ -30,6 +30,9 @@ def fake_packet( class FakeBaseNoThreadsPlugin(plugin.APRSDPluginBase): version = "1.0" + def setup(self): + self.enabled = True + def filter(self, packet): return None @@ -48,6 +51,9 @@ class FakeThread(threads.APRSDThread): class FakeBaseThreadsPlugin(plugin.APRSDPluginBase): version = "1.0" + def setup(self): + self.enabled = True + def filter(self, packet): return None