Skip to content

Commit

Permalink
some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hemna committed Jan 10, 2025
1 parent e4f82d6 commit e332d7c
Show file tree
Hide file tree
Showing 5 changed files with 300 additions and 296 deletions.
30 changes: 13 additions & 17 deletions aprsd/client/drivers/fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from aprsd.utils import trace

CONF = cfg.CONF
LOG = logging.getLogger("APRSD")
LOG = logging.getLogger('APRSD')


class APRSDFakeClient(metaclass=trace.TraceWrapperMetaclass):
Expand All @@ -24,12 +24,12 @@ class APRSDFakeClient(metaclass=trace.TraceWrapperMetaclass):
path = []

def __init__(self):
LOG.info("Starting APRSDFakeClient client.")
self.path = ["WIDE1-1", "WIDE2-1"]
LOG.info('Starting APRSDFakeClient client.')
self.path = ['WIDE1-1', 'WIDE2-1']

def stop(self):
self.thread_stop = True
LOG.info("Shutdown APRSDFakeClient client.")
LOG.info('Shutdown APRSDFakeClient client.')

def is_alive(self):
"""If the connection is alive or not."""
Expand All @@ -38,35 +38,31 @@ def is_alive(self):
@wrapt.synchronized(lock)
def send(self, packet: core.Packet):
"""Send an APRS Message object."""
LOG.info(f"Sending packet: {packet}")
LOG.info(f'Sending packet: {packet}')
payload = None
if isinstance(packet, core.Packet):
packet.prepare()
payload = packet.payload.encode("US-ASCII")
if packet.path:
packet.path
else:
self.path
payload = packet.payload.encode('US-ASCII')
else:
msg_payload = f"{packet.raw}{{{str(packet.msgNo)}"
msg_payload = f'{packet.raw}{{{str(packet.msgNo)}'
payload = (
":{:<9}:{}".format(
':{:<9}:{}'.format(
packet.to_call,
msg_payload,
)
).encode("US-ASCII")
).encode('US-ASCII')

LOG.debug(
f"FAKE::Send '{payload}' TO '{packet.to_call}' From "
f"'{packet.from_call}' with PATH \"{self.path}\"",
f'\'{packet.from_call}\' with PATH "{self.path}"',
)

def consumer(self, callback, blocking=False, immortal=False, raw=False):
LOG.debug("Start non blocking FAKE consumer")
LOG.debug('Start non blocking FAKE consumer')
# Generate packets here?
raw = "GTOWN>APDW16,WIDE1-1,WIDE2-1:}KM6LYW-9>APZ100,TCPIP,GTOWN*::KM6LYW :KM6LYW: 19 Miles SW"
raw = 'GTOWN>APDW16,WIDE1-1,WIDE2-1:}KM6LYW-9>APZ100,TCPIP,GTOWN*::KM6LYW :KM6LYW: 19 Miles SW'
pkt_raw = aprslib.parse(raw)
pkt = core.factory(pkt_raw)
callback(packet=pkt)
LOG.debug(f"END blocking FAKE consumer {self}")
LOG.debug(f'END blocking FAKE consumer {self}')
time.sleep(8)
75 changes: 35 additions & 40 deletions aprsd/cmds/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
from aprsd.utils import trace

CONF = cfg.CONF
LOG = logging.getLogger("APRSD")
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
LOG = logging.getLogger('APRSD')
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])


@cli.group(help="Development type subcommands", context_settings=CONTEXT_SETTINGS)
@cli.group(help='Development type subcommands', context_settings=CONTEXT_SETTINGS)
@click.pass_context
def dev(ctx):
pass
Expand All @@ -29,37 +29,37 @@ def dev(ctx):
@dev.command()
@cli_helper.add_options(cli_helper.common_options)
@click.option(
"--aprs-login",
envvar="APRS_LOGIN",
'--aprs-login',
envvar='APRS_LOGIN',
show_envvar=True,
help="What callsign to send the message from.",
help='What callsign to send the message from.',
)
@click.option(
"-p",
"--plugin",
"plugin_path",
'-p',
'--plugin',
'plugin_path',
show_default=True,
default=None,
help="The plugin to run. Ex: aprsd.plugins.ping.PingPlugin",
help='The plugin to run. Ex: aprsd.plugins.ping.PingPlugin',
)
@click.option(
"-a",
"--all",
"load_all",
'-a',
'--all',
'load_all',
show_default=True,
is_flag=True,
default=False,
help="Load all the plugins in config?",
help='Load all the plugins in config?',
)
@click.option(
"-n",
"--num",
"number",
'-n',
'--num',
'number',
show_default=True,
default=1,
help="Number of times to call the plugin",
help='Number of times to call the plugin',
)
@click.argument("message", nargs=-1, required=True)
@click.argument('message', nargs=-1, required=True)
@click.pass_context
@cli_helper.process_standard_options
def test_plugin(
Expand All @@ -76,7 +76,7 @@ def test_plugin(

if not aprs_login:
if CONF.aprs_network.login == conf.client.DEFAULT_LOGIN:
click.echo("Must set --aprs_login or APRS_LOGIN")
click.echo('Must set --aprs_login or APRS_LOGIN')
ctx.exit(-1)
return
else:
Expand All @@ -86,16 +86,16 @@ def test_plugin(

if not plugin_path:
click.echo(ctx.get_help())
click.echo("")
click.echo("Failed to provide -p option to test a plugin")
click.echo('')
click.echo('Failed to provide -p option to test a plugin')
ctx.exit(-1)
return

if type(message) is tuple:
message = " ".join(message)
message = ' '.join(message)

if CONF.trace_enabled:
trace.setup_tracing(["method", "api"])
trace.setup_tracing(['method', 'api'])

base.APRSClient()

Expand All @@ -105,13 +105,13 @@ def test_plugin(
obj = pm._create_class(plugin_path, plugin.APRSDPluginBase)
if not obj:
click.echo(ctx.get_help())
click.echo("")
click.echo('')
ctx.fail(f"Failed to create object from plugin path '{plugin_path}'")
ctx.exit()

# Register the plugin they wanted tested.
LOG.info(
"Testing plugin {} Version {}".format(
'Testing plugin {} Version {}'.format(
obj.__class__,
obj.version,
),
Expand All @@ -126,7 +126,7 @@ def test_plugin(
)
LOG.info(f"P'{plugin_path}' F'{fromcall}' C'{message}'")

for x in range(number):
for _ in range(number):
replies = pm.run(packet)
# Plugin might have threads, so lets stop them so we can exit.
# obj.stop_threads()
Expand All @@ -147,17 +147,12 @@ def test_plugin(
elif isinstance(reply, packets.Packet):
# We have a message based object.
LOG.info(reply)
else:
# A plugin can return a null message flag which signals
# us that they processed the message correctly, but have
# nothing to reply with, so we avoid replying with a
# usage string
if reply is not packets.NULL_MESSAGE:
LOG.info(
packets.MessagePacket(
from_call=CONF.callsign,
to_call=fromcall,
message_text=reply,
),
)
elif reply is not packets.NULL_MESSAGE:
LOG.info(
packets.MessagePacket(
from_call=CONF.callsign,
to_call=fromcall,
message_text=reply,
),
)
pm.stop()
Loading

0 comments on commit e332d7c

Please sign in to comment.