Skip to content

Commit

Permalink
Merge pull request #205 from mkinney/remove_nested_keys
Browse files Browse the repository at this point in the history
remove nested keys from nodes so we do not display garbage
  • Loading branch information
mkinney authored Jan 2, 2022
2 parents a1668e8 + 4715358 commit c049d34
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
6 changes: 3 additions & 3 deletions meshtastic/mesh_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ def showInfo(self, file=sys.stdout):
nodes = ""
if self.nodes:
for n in self.nodes.values():
# when the TBeam is first booted, it sometimes shows the 'raw' data
# when the TBeam is first booted, it sometimes shows the raw data
# so, we will just remove any raw keys
n2 = remove_keys_from_dict('raw', n)
n2 = remove_keys_from_dict('decode', n2)
keys_to_remove = ('raw', 'decoded', 'payload')
n2 = remove_keys_from_dict(keys_to_remove, n)

# if we have 'macaddr', re-format it
if 'macaddr' in n2['user']:
Expand Down
17 changes: 17 additions & 0 deletions meshtastic/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,23 @@ def test_remove_keys_from_dict():
assert remove_keys_from_dict(('b'), {'a':1, 'b':2}) == {'a':1}


@pytest.mark.unit
def test_remove_keys_from_dict_multiple_keys():
"""Test remove_keys_from_dict()"""
keys = ('a', 'b')
adict = {'a': 1, 'b': 2, 'c': 3}
assert remove_keys_from_dict(keys, adict) == {'c':3}


@pytest.mark.unit
def test_remove_keys_from_dict_nested():
"""Test remove_keys_from_dict()"""
keys = ('b')
adict = {'a': {'b': 1}, 'b': 2, 'c': 3}
exp = {'a': {}, 'c': 3}
assert remove_keys_from_dict(keys, adict) == exp


@pytest.mark.unitslow
def test_Timeout_not_found():
"""Test Timeout()"""
Expand Down
16 changes: 11 additions & 5 deletions meshtastic/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,18 @@ def support_info():


def remove_keys_from_dict(keys, adict):
"""Return a dictionary without some keys in it."""
newdict = adict
"""Return a dictionary without some keys in it.
Will removed nested keys.
"""
for key in keys:
if key in adict:
del newdict[key]
return newdict
try:
del adict[key]
except:
pass
for val in adict.values():
if isinstance(val, dict):
remove_keys_from_dict(keys, val)
return adict


def hexstr(barray):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# This call to setup() does all the work
setup(
name="meshtastic",
version="1.2.50",
version="1.2.51",
description="Python API & client shell for talking to Meshtastic devices",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit c049d34

Please sign in to comment.