Skip to content

Commit 84bec5a

Browse files
committed
fix up misc. lint/type/test issues, streamline, align argument names and groupings
1 parent 579383c commit 84bec5a

3 files changed

Lines changed: 33 additions & 32 deletions

File tree

meshtastic/__main__.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -674,9 +674,9 @@ def onConnected(interface):
674674
closeNow = True
675675
export_config(interface)
676676

677-
if args.seturl:
677+
if args.ch_set_url:
678678
closeNow = True
679-
interface.getNode(args.dest, **getNode_kwargs).setURL(args.seturl)
679+
interface.getNode(args.dest, **getNode_kwargs).setURL(args.ch_set_url, addOnly=False)
680680

681681
# handle changing channels
682682

@@ -1447,7 +1447,20 @@ def addConfigArgs(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
14471447
"--set-ham", help="Set licensed Ham ID and turn off encryption", action="store"
14481448
)
14491449

1450-
group.add_argument("--seturl", help="Set all channels with a URL", action="store")
1450+
group.add_argument(
1451+
"--ch-set-url", "--seturl",
1452+
help="Set all channels and set LoRa config from a supplied URL",
1453+
metavar="URL",
1454+
action="store"
1455+
)
1456+
1457+
group.add_argument(
1458+
"--ch-add-url",
1459+
help="Add secondary channels and set LoRa config from a supplied URL",
1460+
metavar="URL",
1461+
default=None,
1462+
)
1463+
14511464

14521465
return parser
14531466

@@ -1465,13 +1478,6 @@ def addChannelConfigArgs(parser: argparse.ArgumentParser) -> argparse.ArgumentPa
14651478
default=None,
14661479
)
14671480

1468-
group.add_argument(
1469-
"--ch-add-url",
1470-
help="Add secondary channels from a supplied URL",
1471-
metavar="URL",
1472-
default=None,
1473-
)
1474-
14751481
group.add_argument(
14761482
"--ch-del", help="Delete the ch-index channel", action="store_true"
14771483
)

meshtastic/node.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
Timeout,
1313
camel_to_snake,
1414
fromPSK,
15-
genPSK256,
1615
our_exit,
1716
pskToString,
1817
stripnl,
@@ -338,10 +337,10 @@ def getURL(self, includeAll: bool = True):
338337
s = s.replace("=", "").replace("+", "-").replace("/", "_")
339338
return f"https://meshtastic.org/e/#{s}"
340339

341-
def setURL(self, url, addOnly: bool = False):
340+
def setURL(self, url: str, addOnly: bool = False):
342341
"""Set mesh network URL"""
343-
if self.localConfig is None:
344-
our_exit("Warning: No Config has been read")
342+
if self.localConfig is None or self.channels is None:
343+
our_exit("Warning: config or channels not loaded")
345344

346345
# URLs are of the form https://meshtastic.org/d/#{base64_channel_set}
347346
# Split on '/#' to find the base64 encoded channel settings
@@ -370,22 +369,18 @@ def setURL(self, url, addOnly: bool = False):
370369
if addOnly:
371370
# Add new channels with names not already present
372371
# Don't change existing channels
373-
for ch in channelSet.settings:
374-
channelExists = self.getChannelByName(ch.name)
375-
if channelExists or ch.name == "":
376-
print("Ignoring existing channel from add URL")
377-
next
378-
else:
379-
newChannel = self.getDisabledChannel()
380-
if not newChannel:
381-
our_exit("Warning: No free channels were found")
382-
chs = channel_pb2.ChannelSettings()
383-
chs.name = ch.name
384-
chs.psk = ch.psk
385-
newChannel.settings.CopyFrom(chs)
386-
newChannel.role = channel_pb2.Channel.Role.SECONDARY
387-
print(f"Adding new channel '{ch.name}' to device")
388-
self.writeChannel(newChannel.index)
372+
for chs in channelSet.settings:
373+
channelExists = self.getChannelByName(chs.name)
374+
if channelExists or chs.name == "":
375+
print(f"Ignoring existing or empty channel \"{chs.name}\" from add URL")
376+
continue
377+
ch = self.getDisabledChannel()
378+
if not ch:
379+
our_exit("Warning: No free channels were found")
380+
ch.settings.CopyFrom(chs)
381+
ch.role = channel_pb2.Channel.Role.SECONDARY
382+
print(f"Adding new channel '{chs.name}' to device")
383+
self.writeChannel(ch.index)
389384
else:
390385
i = 0
391386
for chs in channelSet.settings:

meshtastic/tests/test_node.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def test_setURL_empty_url(capsys):
270270
assert pytest_wrapped_e.type == SystemExit
271271
assert pytest_wrapped_e.value.code == 1
272272
out, err = capsys.readouterr()
273-
assert re.search(r"Warning: There were no settings.", out, re.MULTILINE)
273+
assert re.search(r"Warning: config or channels not loaded", out, re.MULTILINE)
274274
assert err == ""
275275

276276

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

310310

0 commit comments

Comments
 (0)