diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 22ec5d0..8a9dce5 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -335,6 +335,44 @@ def onConnected(interface): print("Writing modified preferences to device") getNode().writeConfig() + if args.configure_dump: + # dump out the configuration (the opposite of '--configure') + closeNow = True + owner = interface.getLongName() + channel_url = interface.localNode.getURL() + myinfo = interface.getMyNodeInfo() + pos = myinfo.get('position') + lat = None + lon = None + alt = None + if pos: + lat = pos.get('latitude') + lon = pos.get('longitude') + alt = pos.get('altitude') + + config = "# start of Meshtastic configure yaml\n" + if owner: + config += f"owner: {owner}\n\n" + if channel_url: + config += f"channel_url: {channel_url}\n\n" + if lat or lon or alt: + config += "location:\n" + if lat: + config += f" lat: {lat}\n" + if lon: + config += f" lon: {lon}\n" + if alt: + config += f" alt: {alt}\n" + config += "\n" + preferences = f'{interface.localNode.radioConfig.preferences}' + prefs = preferences.splitlines() + if prefs: + config += "user_prefs:\n" + for pref in prefs: + config += f" {meshtastic.util.quoteBooleans(pref)}\n" + + print(config) + if args.seturl: closeNow = True getNode().setURL(args.seturl) @@ -605,6 +643,11 @@ def initParser(): help="Specify a path to a yaml(.yml) file containing the desired settings for the connected device.", action='append') + parser.add_argument( + "--configure-dump", + help="Dump the configuration in yaml(.yml) format.", + action='store_true') + parser.add_argument( "--port", help="The port the Meshtastic device is connected to, i.e. /dev/ttyUSB0. If unspecified, we'll try to find it.", diff --git a/meshtastic/tests/test_util.py b/meshtastic/tests/test_util.py index b03501d..b3c6d39 100644 --- a/meshtastic/tests/test_util.py +++ b/meshtastic/tests/test_util.py @@ -4,7 +4,7 @@ import re import pytest -from meshtastic.util import fixme, stripnl, pskToString, our_exit, support_info, genPSK256, fromStr, fromPSK +from meshtastic.util import fixme, stripnl, pskToString, our_exit, support_info, genPSK256, fromStr, fromPSK, quoteBooleans @pytest.mark.unit @@ -35,6 +35,16 @@ def test_fromStr(): assert fromStr('abc') == 'abc' +@pytest.mark.unit +def test_quoteBooleans(): + """Test quoteBooleans""" + assert quoteBooleans('') == '' + assert quoteBooleans('foo') == 'foo' + assert quoteBooleans('true') == 'true' + assert quoteBooleans('false') == 'false' + assert quoteBooleans(': true') == ": 'true'" + assert quoteBooleans(': false') == ": 'false'" + @pytest.mark.unit def test_fromPSK(): """Test fromPSK""" diff --git a/meshtastic/util.py b/meshtastic/util.py index f9f95d7..b4376f9 100644 --- a/meshtastic/util.py +++ b/meshtastic/util.py @@ -16,6 +16,14 @@ import pkg_resources blacklistVids = dict.fromkeys([0x1366]) +def quoteBooleans(a_string): + """Quote booleans + given a string that contains ": true", replace with ": 'true'" (or false) + """ + tmp = a_string.replace(": true", ": 'true'") + tmp = tmp.replace(": false", ": 'false'") + return tmp + def genPSK256(): """Generate a random preshared key""" return os.urandom(32)