Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small refinements to matcher.py #868

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/paho/mqtt/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ def __getitem__(self, key):
node = self._root
for sym in key.split('/'):
node = node._children[sym]
except KeyError as err:
raise KeyError(key) from err
else:
if node._content is None:
raise KeyError(key)
return node._content
except KeyError as ke:
raise KeyError(key) from ke

def __delitem__(self, key):
"""Delete the value associated with some topic filter :key"""
Expand All @@ -44,11 +45,13 @@ def __delitem__(self, key):
for k in key.split('/'):
parent, node = node, node._children[k]
lst.append((parent, k, node))
# TODO
except KeyError as err:
raise KeyError(key) from err
else:
if node._content is None:
raise KeyError(key)
node._content = None
except KeyError as ke:
raise KeyError(key) from ke
else: # cleanup
# cleanup
for parent, k, node in reversed(lst):
if node._children or node._content is not None:
break
Expand All @@ -66,11 +69,9 @@ def rec(node, i=0):
else:
part = lst[i]
if part in node._children:
for content in rec(node._children[part], i + 1):
yield content
yield from rec(node._children[part], i + 1)
if '+' in node._children and (normal or i > 0):
for content in rec(node._children['+'], i + 1):
yield content
yield from rec(node._children['+'], i + 1)
if '#' in node._children and (normal or i > 0):
content = node._children['#']._content
if content is not None:
Expand Down
1 change: 1 addition & 0 deletions tests/test_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Test_client_function:
("#", "/foo/bar"),
("/#", "/foo/bar"),
("$SYS/bar", "$SYS/bar"),
("$SYS/#", "$SYS/foo/bar"),
])
def test_matching(self, sub, topic):
assert client.topic_matches_sub(sub, topic)
Expand Down
Loading