mirror of
https://github.com/craigerl/aprsd.git
synced 2024-11-17 22:01:49 -05:00
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:
parent
ccad85a8bf
commit
2873e35f14
@ -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
|
continue # break out of this so we don't ack an ack at the end
|
||||||
|
|
||||||
# call our `myhook` hook
|
# call our `myhook` hook
|
||||||
|
found_command = False
|
||||||
results = pm.hook.run(fromcall=fromcall, message=message, ack=ack)
|
results = pm.hook.run(fromcall=fromcall, message=message, ack=ack)
|
||||||
LOG.info("PLUGINS returned {}".format(results))
|
|
||||||
for reply in results:
|
for reply in results:
|
||||||
|
found_command = True
|
||||||
send_message(fromcall, reply)
|
send_message(fromcall, reply)
|
||||||
|
|
||||||
# it's not an ack, so try and process user input
|
# it's not an ack, so try and process user input
|
||||||
found_command = False
|
|
||||||
for key in COMMAND_ENVELOPE:
|
for key in COMMAND_ENVELOPE:
|
||||||
if re.search(COMMAND_ENVELOPE[key]["command"], message):
|
if re.search(COMMAND_ENVELOPE[key]["command"], message):
|
||||||
# now call the registered function
|
# now call the registered function
|
||||||
@ -905,7 +905,13 @@ def server(loglevel, quiet, config_file):
|
|||||||
found_command = True
|
found_command = True
|
||||||
|
|
||||||
if not found_command:
|
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)
|
send_message(fromcall, reply)
|
||||||
|
|
||||||
# let any threads do their thing, then ack
|
# let any threads do their thing, then ack
|
||||||
|
@ -42,11 +42,11 @@ def setup_plugins(config):
|
|||||||
plugin_obj = None
|
plugin_obj = None
|
||||||
if enabled_plugins:
|
if enabled_plugins:
|
||||||
if p_name in enabled_plugins:
|
if p_name in enabled_plugins:
|
||||||
plugin_obj = globals()[p_name]()
|
plugin_obj = globals()[p_name](config)
|
||||||
else:
|
else:
|
||||||
# Enabled plugins isn't set, so we default to loading all of
|
# Enabled plugins isn't set, so we default to loading all of
|
||||||
# the core plugins.
|
# the core plugins.
|
||||||
plugin_obj = globals()[p_name]()
|
plugin_obj = globals()[p_name](config)
|
||||||
|
|
||||||
if plugin_obj:
|
if plugin_obj:
|
||||||
LOG.info(
|
LOG.info(
|
||||||
@ -59,7 +59,7 @@ def setup_plugins(config):
|
|||||||
plugin_dir = config["aprsd"].get("plugin_dir", None)
|
plugin_dir = config["aprsd"].get("plugin_dir", None)
|
||||||
if plugin_dir:
|
if plugin_dir:
|
||||||
LOG.info("Trying to load custom plugins from '{}'".format(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)
|
plugins_list = cpm.load_plugins(plugin_dir)
|
||||||
LOG.info("Discovered {} modules to load".format(len(plugins_list)))
|
LOG.info("Discovered {} modules to load".format(len(plugins_list)))
|
||||||
for o in plugins_list:
|
for o in plugins_list:
|
||||||
@ -93,8 +93,9 @@ def setup_plugins(config):
|
|||||||
|
|
||||||
|
|
||||||
class PluginManager(object):
|
class PluginManager(object):
|
||||||
def __init__(self):
|
def __init__(self, config):
|
||||||
self.obj_list = []
|
self.obj_list = []
|
||||||
|
self.config = config
|
||||||
|
|
||||||
def load_plugins(self, module_path):
|
def load_plugins(self, module_path):
|
||||||
dir_path = os.path.dirname(os.path.realpath(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 inspect.getmodule(obj) is module
|
||||||
and self.is_plugin(obj)
|
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
|
return self.obj_list
|
||||||
|
|
||||||
@ -138,12 +141,23 @@ class APRSDCommandSpec:
|
|||||||
|
|
||||||
@six.add_metaclass(abc.ABCMeta)
|
@six.add_metaclass(abc.ABCMeta)
|
||||||
class APRSDPluginBase(object):
|
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
|
@property
|
||||||
def command_regex(self):
|
def command_regex(self):
|
||||||
|
"""The regex to match from the caller"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def version(self):
|
def version(self):
|
||||||
|
"""Version"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
@hookimpl
|
@hookimpl
|
||||||
@ -166,6 +180,7 @@ class FortunePlugin(APRSDPluginBase):
|
|||||||
|
|
||||||
version = "1.0"
|
version = "1.0"
|
||||||
command_regex = "^[fF]"
|
command_regex = "^[fF]"
|
||||||
|
command_name = "fortune"
|
||||||
|
|
||||||
def command(self, fromcall, message, ack):
|
def command(self, fromcall, message, ack):
|
||||||
LOG.info("FortunePlugin")
|
LOG.info("FortunePlugin")
|
||||||
@ -189,6 +204,7 @@ class LocationPlugin(APRSDPluginBase):
|
|||||||
|
|
||||||
version = "1.0"
|
version = "1.0"
|
||||||
command_regex = "^[lL]"
|
command_regex = "^[lL]"
|
||||||
|
command_name = "location"
|
||||||
|
|
||||||
def command(self, fromcall, message, ack):
|
def command(self, fromcall, message, ack):
|
||||||
LOG.info("Location Plugin")
|
LOG.info("Location Plugin")
|
||||||
@ -255,6 +271,7 @@ class PingPlugin(APRSDPluginBase):
|
|||||||
|
|
||||||
version = "1.0"
|
version = "1.0"
|
||||||
command_regex = "^[pP]"
|
command_regex = "^[pP]"
|
||||||
|
command_name = "ping"
|
||||||
|
|
||||||
def command(self, fromcall, message, ack):
|
def command(self, fromcall, message, ack):
|
||||||
LOG.info("PINGPlugin")
|
LOG.info("PINGPlugin")
|
||||||
@ -273,6 +290,7 @@ class TimePlugin(APRSDPluginBase):
|
|||||||
|
|
||||||
version = "1.0"
|
version = "1.0"
|
||||||
command_regex = "^[tT]"
|
command_regex = "^[tT]"
|
||||||
|
command_name = "time"
|
||||||
|
|
||||||
def command(self, fromcall, message, ack):
|
def command(self, fromcall, message, ack):
|
||||||
LOG.info("TIME COMMAND")
|
LOG.info("TIME COMMAND")
|
||||||
@ -291,6 +309,7 @@ class WeatherPlugin(APRSDPluginBase):
|
|||||||
|
|
||||||
version = "1.0"
|
version = "1.0"
|
||||||
command_regex = "^[wW]"
|
command_regex = "^[wW]"
|
||||||
|
command_name = "weather"
|
||||||
|
|
||||||
def command(self, fromcall, message, ack):
|
def command(self, fromcall, message, ack):
|
||||||
LOG.info("Weather Plugin")
|
LOG.info("Weather Plugin")
|
||||||
|
@ -11,6 +11,7 @@ class HelloPlugin(plugin.APRSDPluginBase):
|
|||||||
version = "1.0"
|
version = "1.0"
|
||||||
# matches any string starting with h or H
|
# matches any string starting with h or H
|
||||||
command_regex = "^[hH]"
|
command_regex = "^[hH]"
|
||||||
|
command_name = "hello"
|
||||||
|
|
||||||
def command(self, fromcall, message, ack):
|
def command(self, fromcall, message, ack):
|
||||||
LOG.info("HelloPlugin")
|
LOG.info("HelloPlugin")
|
||||||
|
Loading…
Reference in New Issue
Block a user