Skip to content

Commit 6ceae7c

Browse files
committed
setPref: pass typed value instead of string
By passing a typed value we can conserve the type information to later use that to convert it back into the correct protobuf type. The type conversion is now done inside setPref.
1 parent 4c29d7d commit 6ceae7c

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

meshtastic/__main__.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,12 @@ def traverseConfig(config_root, config, interface_config):
165165
if isinstance(config[pref], dict):
166166
traverseConfig(pref_name, config[pref], interface_config)
167167
else:
168-
setPref(interface_config, pref_name, str(config[pref]))
168+
setPref(interface_config, pref_name, config[pref])
169169

170170
return True
171171

172172

173-
def setPref(config, comp_name, valStr) -> bool:
173+
def setPref(config, comp_name, raw_val) -> bool:
174174
"""Set a channel or preferences value"""
175175

176176
name = splitCompoundName(comp_name)
@@ -199,10 +199,13 @@ def setPref(config, comp_name, valStr) -> bool:
199199
if (not pref) or (not config_type):
200200
return False
201201

202-
val = meshtastic.util.fromStr(valStr)
203-
logging.debug(f"valStr:{valStr} val:{val}")
202+
if isinstance(raw_val, str):
203+
val = meshtastic.util.fromStr(raw_val)
204+
else:
205+
val = raw_val
206+
logging.debug(f"valStr:{raw_val} val:{val}")
204207

205-
if snake_name == "wifi_psk" and len(valStr) < 8:
208+
if snake_name == "wifi_psk" and len(str(raw_val)) < 8:
206209
print(f"Warning: network.wifi_psk must be 8 or more characters.")
207210
return False
208211

@@ -237,21 +240,21 @@ def setPref(config, comp_name, valStr) -> bool:
237240
except TypeError:
238241
# The setter didn't like our arg type guess try again as a string
239242
config_values = getattr(config_part, config_type.name)
240-
setattr(config_values, pref.name, valStr)
243+
setattr(config_values, pref.name, str(val))
241244
else:
242245
config_values = getattr(config, config_type.name)
243246
if val == 0:
244247
# clear values
245248
print(f"Clearing {pref.name} list")
246249
del getattr(config_values, pref.name)[:]
247250
else:
248-
print(f"Adding '{val}' to the {pref.name} list")
251+
print(f"Adding '{raw_val}' to the {pref.name} list")
249252
cur_vals = [x for x in getattr(config_values, pref.name) if x not in [0, "", b""]]
250253
cur_vals.append(val)
251254
getattr(config_values, pref.name)[:] = cur_vals
252255

253256
prefix = f"{'.'.join(name[0:-1])}." if config_type.message_type is not None else ""
254-
print(f"Set {prefix}{uni_name} to {valStr}")
257+
print(f"Set {prefix}{uni_name} to {raw_val}")
255258

256259
return True
257260

0 commit comments

Comments
 (0)