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.
This commit is contained in:
Hemna 2020-12-13 21:31:01 -05:00
parent ccad85a8bf
commit 2873e35f14
3 changed files with 34 additions and 8 deletions

View File

@ -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

View File

@ -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")

View File

@ -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")