From 2873e35f14b8a1a088274529808523c7833c7c33 Mon Sep 17 00:00:00 2001 From: Hemna Date: Sun, 13 Dec 2020 21:31:01 -0500 Subject: [PATCH] Fixed the usage string after plugins introduced This patch fixes the Usage string for a call message that isn't matched by any plugin. Plugin object now must impleent a 'command_name' attribute that is the usage string for that plugin. --- aprsd/main.py | 12 +++++++++--- aprsd/plugin.py | 29 ++++++++++++++++++++++++----- examples/plugins/example_plugin.py | 1 + 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/aprsd/main.py b/aprsd/main.py index f0220b4..6058a40 100644 --- a/aprsd/main.py +++ b/aprsd/main.py @@ -890,13 +890,13 @@ def server(loglevel, quiet, config_file): continue # break out of this so we don't ack an ack at the end # call our `myhook` hook + found_command = False results = pm.hook.run(fromcall=fromcall, message=message, ack=ack) - LOG.info("PLUGINS returned {}".format(results)) for reply in results: + found_command = True send_message(fromcall, reply) # it's not an ack, so try and process user input - found_command = False for key in COMMAND_ENVELOPE: if re.search(COMMAND_ENVELOPE[key]["command"], message): # now call the registered function @@ -905,7 +905,13 @@ def server(loglevel, quiet, config_file): found_command = True if not found_command: - reply = "Usage: {}".format(", ".join(COMMAND_ENVELOPE.keys())) + plugins = pm.get_plugins() + names = [x.command_name for x in plugins] + for k in COMMAND_ENVELOPE.keys(): + names.append(k) + names.sort() + + reply = "Usage: {}".format(", ".join(names)) send_message(fromcall, reply) # let any threads do their thing, then ack diff --git a/aprsd/plugin.py b/aprsd/plugin.py index 3303d8d..1a480c8 100644 --- a/aprsd/plugin.py +++ b/aprsd/plugin.py @@ -42,11 +42,11 @@ def setup_plugins(config): plugin_obj = None if enabled_plugins: if p_name in enabled_plugins: - plugin_obj = globals()[p_name]() + plugin_obj = globals()[p_name](config) else: # Enabled plugins isn't set, so we default to loading all of # the core plugins. - plugin_obj = globals()[p_name]() + plugin_obj = globals()[p_name](config) if plugin_obj: LOG.info( @@ -59,7 +59,7 @@ def setup_plugins(config): plugin_dir = config["aprsd"].get("plugin_dir", None) if plugin_dir: LOG.info("Trying to load custom plugins from '{}'".format(plugin_dir)) - cpm = PluginManager() + cpm = PluginManager(config) plugins_list = cpm.load_plugins(plugin_dir) LOG.info("Discovered {} modules to load".format(len(plugins_list))) for o in plugins_list: @@ -93,8 +93,9 @@ def setup_plugins(config): class PluginManager(object): - def __init__(self): + def __init__(self, config): self.obj_list = [] + self.config = config def load_plugins(self, module_path): dir_path = os.path.dirname(os.path.realpath(module_path)) @@ -115,7 +116,9 @@ class PluginManager(object): and inspect.getmodule(obj) is module and self.is_plugin(obj) ): - self.obj_list.append({"name": mem_name, "obj": obj()}) + self.obj_list.append( + {"name": mem_name, "obj": obj(self.config)} + ) return self.obj_list @@ -138,12 +141,23 @@ class APRSDCommandSpec: @six.add_metaclass(abc.ABCMeta) class APRSDPluginBase(object): + def __init__(self, config): + """The aprsd config object is stored.""" + self.config = config + + @property + def command_name(self): + """The usage string help.""" + raise NotImplementedError + @property def command_regex(self): + """The regex to match from the caller""" raise NotImplementedError @property def version(self): + """Version""" raise NotImplementedError @hookimpl @@ -166,6 +180,7 @@ class FortunePlugin(APRSDPluginBase): version = "1.0" command_regex = "^[fF]" + command_name = "fortune" def command(self, fromcall, message, ack): LOG.info("FortunePlugin") @@ -189,6 +204,7 @@ class LocationPlugin(APRSDPluginBase): version = "1.0" command_regex = "^[lL]" + command_name = "location" def command(self, fromcall, message, ack): LOG.info("Location Plugin") @@ -255,6 +271,7 @@ class PingPlugin(APRSDPluginBase): version = "1.0" command_regex = "^[pP]" + command_name = "ping" def command(self, fromcall, message, ack): LOG.info("PINGPlugin") @@ -273,6 +290,7 @@ class TimePlugin(APRSDPluginBase): version = "1.0" command_regex = "^[tT]" + command_name = "time" def command(self, fromcall, message, ack): LOG.info("TIME COMMAND") @@ -291,6 +309,7 @@ class WeatherPlugin(APRSDPluginBase): version = "1.0" command_regex = "^[wW]" + command_name = "weather" def command(self, fromcall, message, ack): LOG.info("Weather Plugin") diff --git a/examples/plugins/example_plugin.py b/examples/plugins/example_plugin.py index 83f5978..c4eb1d0 100644 --- a/examples/plugins/example_plugin.py +++ b/examples/plugins/example_plugin.py @@ -11,6 +11,7 @@ class HelloPlugin(plugin.APRSDPluginBase): version = "1.0" # matches any string starting with h or H command_regex = "^[hH]" + command_name = "hello" def command(self, fromcall, message, ack): LOG.info("HelloPlugin")