fix up misc. lint/type/test issues, streamline, align argument names and groupings

This commit is contained in:
Ian McEwen
2025-02-19 10:15:27 -07:00
parent 579383cd5a
commit 84bec5a7c4
3 changed files with 33 additions and 32 deletions

View File

@@ -674,9 +674,9 @@ def onConnected(interface):
closeNow = True
export_config(interface)
if args.seturl:
if args.ch_set_url:
closeNow = True
interface.getNode(args.dest, **getNode_kwargs).setURL(args.seturl)
interface.getNode(args.dest, **getNode_kwargs).setURL(args.ch_set_url, addOnly=False)
# handle changing channels
@@ -1447,7 +1447,20 @@ def addConfigArgs(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
"--set-ham", help="Set licensed Ham ID and turn off encryption", action="store"
)
group.add_argument("--seturl", help="Set all channels with a URL", action="store")
group.add_argument(
"--ch-set-url", "--seturl",
help="Set all channels and set LoRa config from a supplied URL",
metavar="URL",
action="store"
)
group.add_argument(
"--ch-add-url",
help="Add secondary channels and set LoRa config from a supplied URL",
metavar="URL",
default=None,
)
return parser
@@ -1465,13 +1478,6 @@ def addChannelConfigArgs(parser: argparse.ArgumentParser) -> argparse.ArgumentPa
default=None,
)
group.add_argument(
"--ch-add-url",
help="Add secondary channels from a supplied URL",
metavar="URL",
default=None,
)
group.add_argument(
"--ch-del", help="Delete the ch-index channel", action="store_true"
)

View File

@@ -12,7 +12,6 @@ from meshtastic.util import (
Timeout,
camel_to_snake,
fromPSK,
genPSK256,
our_exit,
pskToString,
stripnl,
@@ -338,10 +337,10 @@ class Node:
s = s.replace("=", "").replace("+", "-").replace("/", "_")
return f"https://meshtastic.org/e/#{s}"
def setURL(self, url, addOnly: bool = False):
def setURL(self, url: str, addOnly: bool = False):
"""Set mesh network URL"""
if self.localConfig is None:
our_exit("Warning: No Config has been read")
if self.localConfig is None or self.channels is None:
our_exit("Warning: config or channels not loaded")
# URLs are of the form https://meshtastic.org/d/#{base64_channel_set}
# Split on '/#' to find the base64 encoded channel settings
@@ -370,22 +369,18 @@ class Node:
if addOnly:
# Add new channels with names not already present
# Don't change existing channels
for ch in channelSet.settings:
channelExists = self.getChannelByName(ch.name)
if channelExists or ch.name == "":
print("Ignoring existing channel from add URL")
next
else:
newChannel = self.getDisabledChannel()
if not newChannel:
our_exit("Warning: No free channels were found")
chs = channel_pb2.ChannelSettings()
chs.name = ch.name
chs.psk = ch.psk
newChannel.settings.CopyFrom(chs)
newChannel.role = channel_pb2.Channel.Role.SECONDARY
print(f"Adding new channel '{ch.name}' to device")
self.writeChannel(newChannel.index)
for chs in channelSet.settings:
channelExists = self.getChannelByName(chs.name)
if channelExists or chs.name == "":
print(f"Ignoring existing or empty channel \"{chs.name}\" from add URL")
continue
ch = self.getDisabledChannel()
if not ch:
our_exit("Warning: No free channels were found")
ch.settings.CopyFrom(chs)
ch.role = channel_pb2.Channel.Role.SECONDARY
print(f"Adding new channel '{chs.name}' to device")
self.writeChannel(ch.index)
else:
i = 0
for chs in channelSet.settings:

View File

@@ -270,7 +270,7 @@ def test_setURL_empty_url(capsys):
assert pytest_wrapped_e.type == SystemExit
assert pytest_wrapped_e.value.code == 1
out, err = capsys.readouterr()
assert re.search(r"Warning: There were no settings.", out, re.MULTILINE)
assert re.search(r"Warning: config or channels not loaded", out, re.MULTILINE)
assert err == ""
@@ -304,7 +304,7 @@ def test_setURL_valid_URL_but_no_settings(capsys):
assert pytest_wrapped_e.type == SystemExit
assert pytest_wrapped_e.value.code == 1
out, err = capsys.readouterr()
assert re.search(r"Warning: There were no settings", out, re.MULTILINE)
assert re.search(r"Warning: config or channels not loaded", out, re.MULTILINE)
assert err == ""