diff --git a/meshtastic/mesh_interface.py b/meshtastic/mesh_interface.py index 275eea2d..2234f08a 100644 --- a/meshtastic/mesh_interface.py +++ b/meshtastic/mesh_interface.py @@ -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']: diff --git a/meshtastic/tests/test_util.py b/meshtastic/tests/test_util.py index d05d8e0d..bff028b1 100644 --- a/meshtastic/tests/test_util.py +++ b/meshtastic/tests/test_util.py @@ -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()""" diff --git a/meshtastic/util.py b/meshtastic/util.py index e87f7257..289603ce 100644 --- a/meshtastic/util.py +++ b/meshtastic/util.py @@ -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): diff --git a/setup.py b/setup.py index 22585db1..c19f9c47 100644 --- a/setup.py +++ b/setup.py @@ -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",