Skip to content

Commit c049d34

Browse files
authored
Merge pull request #205 from mkinney/remove_nested_keys
remove nested keys from nodes so we do not display garbage
2 parents a1668e8 + 4715358 commit c049d34

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
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', 'decoded', '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):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# This call to setup() does all the work
1313
setup(
1414
name="meshtastic",
15-
version="1.2.50",
15+
version="1.2.51",
1616
description="Python API & client shell for talking to Meshtastic devices",
1717
long_description=long_description,
1818
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)