From 7fe98bc266f5969d33c648b0e198327ee5b2389f Mon Sep 17 00:00:00 2001 From: geeksville Date: Fri, 26 Apr 2024 15:15:57 -0700 Subject: [PATCH] Pretty indent --info JSON output (see below for details) Changes to make --info much more human readable (while still keeping machine readabilty for anyone foolish enough to be parsing the existing output as text) * change message_to_json to optionally not strip the multiline JSON * use multiline=True for the two places we are printing to the console * make the node list JSON indented --- meshtastic/mesh_interface.py | 2 +- meshtastic/node.py | 4 ++-- meshtastic/util.py | 8 +++++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/meshtastic/mesh_interface.py b/meshtastic/mesh_interface.py index 5ce0e67..a31cb3f 100644 --- a/meshtastic/mesh_interface.py +++ b/meshtastic/mesh_interface.py @@ -133,7 +133,7 @@ class MeshInterface: # use id as dictionary key for correct json format in list of nodes nodeid = n2["user"]["id"] nodes[nodeid] = n2 - infos = owner + myinfo + metadata + mesh + json.dumps(nodes) + infos = owner + myinfo + metadata + mesh + json.dumps(nodes, indent=2) print(infos) return infos diff --git a/meshtastic/node.py b/meshtastic/node.py index 156fbba..37b250d 100644 --- a/meshtastic/node.py +++ b/meshtastic/node.py @@ -64,11 +64,11 @@ class Node: """Show human readable description of our node""" prefs = "" if self.localConfig: - prefs = message_to_json(self.localConfig) + prefs = message_to_json(self.localConfig, multiline=True) print(f"Preferences: {prefs}\n") prefs = "" if self.moduleConfig: - prefs = message_to_json(self.moduleConfig) + prefs = message_to_json(self.moduleConfig, multiline=True) print(f"Module preferences: {prefs}\n") self.showChannels() diff --git a/meshtastic/util.py b/meshtastic/util.py index 76b4b9e..f9e1b1e 100644 --- a/meshtastic/util.py +++ b/meshtastic/util.py @@ -627,6 +627,8 @@ def check_if_newer_version(): return pypi_version -def message_to_json(message): - "Return protobuf message as JSON. Always print all fields, even when not present in data." - return stripnl(MessageToJson(message, always_print_fields_with_no_presence=True)) + +def message_to_json(message, multiline=False): + """Return protobuf message as JSON. Always print all fields, even when not present in data.""" + json = MessageToJson(message, always_print_fields_with_no_presence=True) + return stripnl(json) if not multiline else json