Skip to content

Commit a915b05

Browse files
committed
remove nested keys from nodes so we do not display garbage
1 parent e7664cb commit a915b05

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

meshtastic/mesh_interface.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ def showInfo(self, file=sys.stdout):
8383
nodes = ""
8484
if self.nodes:
8585
for n in self.nodes.values():
86-
# when the TBeam is first booted, it sometimes shows the 'raw' data
86+
# when the TBeam is first booted, it sometimes shows the raw data
8787
# so, we will just remove any raw keys
88-
n2 = remove_keys_from_dict('raw', n)
89-
n2 = remove_keys_from_dict('decode', n2)
88+
keys_to_remove = ('raw', 'decode', 'payload')
89+
n2 = remove_keys_from_dict(keys_to_remove, n)
9090

9191
# if we have 'macaddr', re-format it
9292
if 'macaddr' in n2['user']:

meshtastic/tests/test_util.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,23 @@ def test_remove_keys_from_dict():
184184
assert remove_keys_from_dict(('b'), {'a':1, 'b':2}) == {'a':1}
185185

186186

187+
@pytest.mark.unit
188+
def test_remove_keys_from_dict_multiple_keys():
189+
"""Test remove_keys_from_dict()"""
190+
keys = ('a', 'b')
191+
adict = {'a': 1, 'b': 2, 'c': 3}
192+
assert remove_keys_from_dict(keys, adict) == {'c':3}
193+
194+
195+
@pytest.mark.unit
196+
def test_remove_keys_from_dict_nested():
197+
"""Test remove_keys_from_dict()"""
198+
keys = ('b')
199+
adict = {'a': {'b': 1}, 'b': 2, 'c': 3}
200+
exp = {'a': {}, 'c': 3}
201+
assert remove_keys_from_dict(keys, adict) == exp
202+
203+
187204
@pytest.mark.unitslow
188205
def test_Timeout_not_found():
189206
"""Test Timeout()"""

meshtastic/util.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,18 @@ def support_info():
204204

205205

206206
def remove_keys_from_dict(keys, adict):
207-
"""Return a dictionary without some keys in it."""
208-
newdict = adict
207+
"""Return a dictionary without some keys in it.
208+
Will removed nested keys.
209+
"""
209210
for key in keys:
210-
if key in adict:
211-
del newdict[key]
212-
return newdict
211+
try:
212+
del adict[key]
213+
except:
214+
pass
215+
for val in adict.values():
216+
if isinstance(val, dict):
217+
remove_keys_from_dict(keys, val)
218+
return adict
213219

214220

215221
def hexstr(barray):

0 commit comments

Comments
 (0)