2021-11-05 13:36:33 -04:00
|
|
|
#
|
|
|
|
# Dev.py is used to help develop plugins
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# python included libs
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import click
|
2022-12-27 14:30:03 -05:00
|
|
|
from oslo_config import cfg
|
2021-11-05 13:36:33 -04:00
|
|
|
|
|
|
|
# local imports here
|
2022-12-27 14:30:03 -05:00
|
|
|
from aprsd import cli_helper, client, conf, packets, plugin
|
2021-12-08 17:03:12 -05:00
|
|
|
from aprsd.aprsd import cli
|
2022-07-07 10:47:34 -04:00
|
|
|
from aprsd.utils import trace
|
2021-11-05 13:36:33 -04:00
|
|
|
|
|
|
|
|
2022-12-27 14:30:03 -05:00
|
|
|
CONF = cfg.CONF
|
2021-11-05 13:36:33 -04:00
|
|
|
LOG = logging.getLogger("APRSD")
|
|
|
|
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
|
|
|
|
|
|
|
|
|
2021-11-08 11:52:41 -05:00
|
|
|
@cli.group(help="Development type subcommands", context_settings=CONTEXT_SETTINGS)
|
|
|
|
@click.pass_context
|
|
|
|
def dev(ctx):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@dev.command()
|
|
|
|
@cli_helper.add_options(cli_helper.common_options)
|
2021-11-05 14:05:24 -04:00
|
|
|
@click.option(
|
|
|
|
"--aprs-login",
|
|
|
|
envvar="APRS_LOGIN",
|
|
|
|
show_envvar=True,
|
|
|
|
help="What callsign to send the message from.",
|
|
|
|
)
|
2021-11-05 13:36:33 -04:00
|
|
|
@click.option(
|
|
|
|
"-p",
|
|
|
|
"--plugin",
|
|
|
|
"plugin_path",
|
|
|
|
show_default=True,
|
|
|
|
default=None,
|
|
|
|
help="The plugin to run. Ex: aprsd.plugins.ping.PingPlugin",
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"-a",
|
|
|
|
"--all",
|
|
|
|
"load_all",
|
|
|
|
show_default=True,
|
|
|
|
is_flag=True,
|
|
|
|
default=False,
|
|
|
|
help="Load all the plugins in config?",
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"-n",
|
|
|
|
"--num",
|
|
|
|
"number",
|
|
|
|
show_default=True,
|
|
|
|
default=1,
|
|
|
|
help="Number of times to call the plugin",
|
|
|
|
)
|
|
|
|
@click.argument("message", nargs=-1, required=True)
|
|
|
|
@click.pass_context
|
2021-11-08 11:52:41 -05:00
|
|
|
@cli_helper.process_standard_options
|
2021-11-05 13:36:33 -04:00
|
|
|
def test_plugin(
|
|
|
|
ctx,
|
2021-11-05 14:05:24 -04:00
|
|
|
aprs_login,
|
2021-11-05 13:36:33 -04:00
|
|
|
plugin_path,
|
|
|
|
load_all,
|
|
|
|
number,
|
|
|
|
message,
|
|
|
|
):
|
2021-11-08 11:52:41 -05:00
|
|
|
"""Test an individual APRSD plugin given a python path."""
|
2021-12-12 16:35:26 -05:00
|
|
|
|
2022-12-27 14:30:03 -05:00
|
|
|
CONF.log_opt_values(LOG, logging.DEBUG)
|
2022-11-23 12:52:16 -05:00
|
|
|
|
2021-12-12 16:35:26 -05:00
|
|
|
if not aprs_login:
|
2022-12-27 14:30:03 -05:00
|
|
|
if CONF.aprs_network.login == conf.client.DEFAULT_LOGIN:
|
2021-12-12 16:35:26 -05:00
|
|
|
click.echo("Must set --aprs_login or APRS_LOGIN")
|
|
|
|
ctx.exit(-1)
|
|
|
|
return
|
|
|
|
else:
|
2022-12-27 14:30:03 -05:00
|
|
|
fromcall = CONF.aprs_network.login
|
2021-12-12 16:35:26 -05:00
|
|
|
else:
|
|
|
|
fromcall = aprs_login
|
2021-11-05 13:36:33 -04:00
|
|
|
|
|
|
|
if not plugin_path:
|
|
|
|
click.echo(ctx.get_help())
|
|
|
|
click.echo("")
|
2022-12-27 15:31:49 -05:00
|
|
|
click.echo("Failed to provide -p option to test a plugin")
|
|
|
|
ctx.exit(-1)
|
2021-12-12 16:35:26 -05:00
|
|
|
return
|
2021-11-05 13:36:33 -04:00
|
|
|
|
|
|
|
if type(message) is tuple:
|
|
|
|
message = " ".join(message)
|
2021-12-03 08:53:08 -05:00
|
|
|
|
2022-12-27 14:30:03 -05:00
|
|
|
if CONF.trace_enabled:
|
2021-12-03 08:53:08 -05:00
|
|
|
trace.setup_tracing(["method", "api"])
|
|
|
|
|
2022-12-27 14:30:03 -05:00
|
|
|
client.Client()
|
2021-11-05 13:36:33 -04:00
|
|
|
|
2022-12-27 14:30:03 -05:00
|
|
|
pm = plugin.PluginManager()
|
2021-11-05 13:36:33 -04:00
|
|
|
if load_all:
|
|
|
|
pm.setup_plugins()
|
2022-12-27 14:30:03 -05:00
|
|
|
obj = pm._create_class(plugin_path, plugin.APRSDPluginBase)
|
2021-11-05 14:05:24 -04:00
|
|
|
if not obj:
|
|
|
|
click.echo(ctx.get_help())
|
|
|
|
click.echo("")
|
|
|
|
ctx.fail(f"Failed to create object from plugin path '{plugin_path}'")
|
|
|
|
ctx.exit()
|
|
|
|
|
2021-11-05 13:36:33 -04:00
|
|
|
# Register the plugin they wanted tested.
|
|
|
|
LOG.info(
|
|
|
|
"Testing plugin {} Version {}".format(
|
|
|
|
obj.__class__, obj.version,
|
|
|
|
),
|
|
|
|
)
|
2023-04-17 10:51:17 -04:00
|
|
|
pm.register_msg(obj)
|
2022-12-27 14:30:03 -05:00
|
|
|
|
|
|
|
packet = packets.MessagePacket(
|
|
|
|
from_call=fromcall,
|
|
|
|
to_call=CONF.callsign,
|
|
|
|
msgNo=1,
|
|
|
|
message_text=message,
|
|
|
|
)
|
2021-11-05 13:36:33 -04:00
|
|
|
LOG.info(f"P'{plugin_path}' F'{fromcall}' C'{message}'")
|
|
|
|
|
|
|
|
for x in range(number):
|
|
|
|
reply = pm.run(packet)
|
|
|
|
# Plugin might have threads, so lets stop them so we can exit.
|
|
|
|
# obj.stop_threads()
|
|
|
|
LOG.info(f"Result{x} = '{reply}'")
|
|
|
|
pm.stop()
|