From 1abb9fb21304d5afa9a6b5c5fff7dcc136beea23 Mon Sep 17 00:00:00 2001 From: Felix Moessbauer Date: Fri, 25 Oct 2024 14:30:53 +0200 Subject: [PATCH] refactor getPref to use uniform printing logic We add a local helper function to print a single setting. This is a preparation to correctly print non-trivial types. The existing code in getPref is ported over to use that function. By that, the output of "wholeField" is changed slightly to always print the full path for each setting (e.g. "security.serialEnabled" instead of "security:\nserialEnabled"). This improves support for grepping on the output. --- meshtastic/__main__.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index a60be1d..0d26789 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -87,6 +87,10 @@ def checkChannel(interface: MeshInterface, channelIndex: int) -> bool: def getPref(node, comp_name): """Get a channel or preferences value""" + def _printSetting(config_type, uni_name, pref_value): + """Pretty print the setting""" + print(f"{str(config_type.name)}.{uni_name}: {str(pref_value)}") + logging.debug(f"{str(config_type.name)}.{uni_name}: {str(pref_value)}") name = splitCompoundName(comp_name) wholeField = name[0] == name[1] # We want the whole field @@ -114,7 +118,7 @@ def getPref(node, comp_name): if not found: print( - f"{localConfig.__class__.__name__} and {moduleConfig.__class__.__name__} do not have an attribute {uni_name}." + f"{localConfig.__class__.__name__} and {moduleConfig.__class__.__name__} do not have attribute {uni_name}." ) print("Choices are...") printConfig(localConfig) @@ -127,11 +131,10 @@ def getPref(node, comp_name): config_values = getattr(config, config_type.name) if not wholeField: pref_value = getattr(config_values, pref.name) - print(f"{str(config_type.name)}.{uni_name}: {str(pref_value)}") - logging.debug(f"{str(config_type.name)}.{uni_name}: {str(pref_value)}") + _printSetting(config_type, uni_name, pref_value) else: - print(f"{str(config_type.name)}:\n{str(config_values)}") - logging.debug(f"{str(config_type.name)}: {str(config_values)}") + for field in config_values.ListFields(): + _printSetting(config_type, field[0].name, field[1]) else: # Always show whole field for remote node node.requestConfig(config_type)