Skip to content

Commit 839bbbc

Browse files
committed
config: correctly print byte and array types on get
When getting config values of type bytes or list (technically a protobuf repeated container type), these were directly printed on the output. However, the retrieved values could not be set by --set again, as the format was different (e.g. python string representation of bytes vs. base64 prefixed and encoded as expected by --set). We fix this by adding a toStr utility function (similar to the fromStr) function to convert byte types correctly to the base64 representation. Further, we check if the type is repeated and apply this operation to all values.
1 parent 1abb9fb commit 839bbbc

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

meshtastic/__main__.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,12 @@ def checkChannel(interface: MeshInterface, channelIndex: int) -> bool:
8787

8888
def getPref(node, comp_name):
8989
"""Get a channel or preferences value"""
90-
def _printSetting(config_type, uni_name, pref_value):
90+
def _printSetting(config_type, uni_name, pref_value, repeated):
9191
"""Pretty print the setting"""
92+
if repeated:
93+
pref_value = [meshtastic.util.toStr(v) for v in pref_value]
94+
else:
95+
pref_value = meshtastic.util.toStr(pref_value)
9296
print(f"{str(config_type.name)}.{uni_name}: {str(pref_value)}")
9397
logging.debug(f"{str(config_type.name)}.{uni_name}: {str(pref_value)}")
9498

@@ -131,10 +135,12 @@ def _printSetting(config_type, uni_name, pref_value):
131135
config_values = getattr(config, config_type.name)
132136
if not wholeField:
133137
pref_value = getattr(config_values, pref.name)
134-
_printSetting(config_type, uni_name, pref_value)
138+
repeated = pref.label == pref.LABEL_REPEATED
139+
_printSetting(config_type, uni_name, pref_value, repeated)
135140
else:
136141
for field in config_values.ListFields():
137-
_printSetting(config_type, field[0].name, field[1])
142+
repeated = field[0].label == field[0].LABEL_REPEATED
143+
_printSetting(config_type, field[0].name, field[1], repeated)
138144
else:
139145
# Always show whole field for remote node
140146
node.requestConfig(config_type)

meshtastic/util.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ def fromStr(valstr):
100100
return val
101101

102102

103+
def toStr(raw_value):
104+
"""Convert a value to a string that can be used in a config file"""
105+
if isinstance(raw_value, bytes):
106+
return "base64:" + base64.b64encode(raw_value).decode("utf-8")
107+
return str(raw_value)
108+
109+
103110
def pskToString(psk: bytes):
104111
"""Given an array of PSK bytes, decode them into a human readable (but privacy protecting) string"""
105112
if len(psk) == 0:

0 commit comments

Comments
 (0)