From e06d8bbc0680fe78ad701fde901f2d7c692f11aa Mon Sep 17 00:00:00 2001 From: Douile <25043847+Douile@users.noreply.github.com> Date: Thu, 28 Jul 2022 15:11:35 +0100 Subject: [PATCH 1/2] Use pyyaml to generate valid configuration output The custom yaml generator made an invalid yaml file that needed to be fixed, by using the already included pyyaml library we can make sure the output is valid. --- meshtastic/__main__.py | 48 +++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 6cbd7ea..c873497 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -20,6 +20,8 @@ from meshtastic import portnums_pb2, channel_pb2, config_pb2 from meshtastic.globals import Globals from meshtastic.__init__ import BROADCAST_ADDR +from google.protobuf.json_format import MessageToDict + def onReceive(packet, interface): """Callback invoked when a packet arrives""" our_globals = Globals.getInstance() @@ -598,6 +600,8 @@ def subscribe(): def export_config(interface): """used in--export-config""" + configObj = {} + owner = interface.getLongName() owner_short = interface.getShortName() channel_url = interface.localNode.getURL() @@ -611,38 +615,34 @@ def export_config(interface): lon = pos.get('longitude') alt = pos.get('altitude') - config = "# start of Meshtastic configure yaml\n" if owner: - config += f"owner: {owner}\n\n" + configObj["owner"] = owner if owner_short: - config += f"owner_short: {owner_short}\n\n" + configObj["owner_short"] = owner_short if channel_url: if Globals.getInstance().get_camel_case(): - config += f"channelUrl: {channel_url}\n\n" + configObj["channelUrl"] = channel_url else: - config += f"channel_url: {channel_url}\n\n" + configObj["channel_url"] = channel_url 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.localConfig}' - prefs = preferences.splitlines() - if prefs: - if Globals.getInstance().get_camel_case(): - config += "userPrefs:\n" - else: - config += "user_prefs:\n" - for pref in prefs: + configObj["location"] = { "lat": lat, "lon": lon, "alt": alt } + preferences = MessageToDict(interface.localNode.localConfig) + if preferences: + # Convert inner keys to correct snake/camelCase + prefs = {} + for pref in preferences: if Globals.getInstance().get_camel_case(): - # Note: This may not work if the value has '_' - config += f" {meshtastic.util.snake_to_camel(meshtastic.util.quoteBooleans(pref))}\n" + prefs[meshtastic.util.snake_to_camel(pref)] = preferences[pref] else: - config += f" {meshtastic.util.quoteBooleans(pref)}\n" + # TODO: Possibly convert camel to snake? + prefs[pref] = preferences[pref] + if Globals.getInstance().get_camel_case(): + configObj["userPrefs"] = preferences + else: + configObj["user_prefs"] = preferences + + config = "# start of Meshtastic configure yaml\n" + config += yaml.dump(configObj) print(config) return config From e4078e84d74085431b1a76db8bffeb64d2d7c55a Mon Sep 17 00:00:00 2001 From: Douile <25043847+Douile@users.noreply.github.com> Date: Fri, 5 Aug 2022 13:34:25 +0100 Subject: [PATCH 2/2] Fix linting errors caused by this PR --- meshtastic/__main__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index c873497..813e422 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -12,6 +12,7 @@ import yaml from pubsub import pub import pyqrcode import pkg_resources +from google.protobuf.json_format import MessageToDict import meshtastic.util import meshtastic.test from meshtastic import remote_hardware @@ -20,8 +21,6 @@ from meshtastic import portnums_pb2, channel_pb2, config_pb2 from meshtastic.globals import Globals from meshtastic.__init__ import BROADCAST_ADDR -from google.protobuf.json_format import MessageToDict - def onReceive(packet, interface): """Callback invoked when a packet arrives""" our_globals = Globals.getInstance()