1+ # Copyright (c) 2017 Yotch <https://github.com/yoch>
2+ #
3+ # This file is dual licensed under the Eclipse Public License 1.0 and the
4+ # Eclipse Distribution License 1.0 as described in the epl-v10 and edl-v10 files.
5+ #
6+ #
7+ """
8+ `matcher`
9+ ====================================================================================
10+
11+ MQTT topic filter matcher from the Eclipse Project's Paho.MQTT.Python
12+ https://github.com/eclipse/paho.mqtt.python/blob/master/src/paho/mqtt/matcher.py
13+ * Author(s): Yotch (https://github.com/yoch)
14+ """
15+
116class MQTTMatcher (object ):
217 """Intended to manage topic filters including wildcards.
318
@@ -7,7 +22,7 @@ class MQTTMatcher(object):
722 some topic name."""
823
924 class Node (object ):
10- __slots__ = " _children" , " _content"
25+ __slots__ = ' _children' , ' _content'
1126
1227 def __init__ (self ):
1328 self ._children = {}
@@ -20,15 +35,15 @@ def __setitem__(self, key, value):
2035 """Add a topic filter :key to the prefix tree
2136 and associate it to :value"""
2237 node = self ._root
23- for sym in key .split ("/" ):
38+ for sym in key .split ('/' ):
2439 node = node ._children .setdefault (sym , self .Node ())
2540 node ._content = value
2641
2742 def __getitem__ (self , key ):
2843 """Retrieve the value associated with some topic filter :key"""
2944 try :
3045 node = self ._root
31- for sym in key .split ("/" ):
46+ for sym in key .split ('/' ):
3247 node = node ._children [sym ]
3348 if node ._content is None :
3449 raise KeyError (key )
@@ -41,25 +56,24 @@ def __delitem__(self, key):
4156 lst = []
4257 try :
4358 parent , node = None , self ._root
44- for k in key .split ("/" ):
45- parent , node = node , node ._children [k ]
46- lst .append ((parent , k , node ))
59+ for k in key .split ('/' ):
60+ parent , node = node , node ._children [k ]
61+ lst .append ((parent , k , node ))
4762 # TODO
4863 node ._content = None
4964 except KeyError :
5065 raise KeyError (key )
5166 else : # cleanup
5267 for parent , k , node in reversed (lst ):
5368 if node ._children or node ._content is not None :
54- break
69+ break
5570 del parent ._children [k ]
5671
5772 def iter_match (self , topic ):
5873 """Return an iterator on all values associated with filters
5974 that match the :topic"""
60- lst = topic .split ("/" )
61- normal = not topic .startswith ("$" )
62-
75+ lst = topic .split ('/' )
76+ normal = not topic .startswith ('$' )
6377 def rec (node , i = 0 ):
6478 if i == len (lst ):
6579 if node ._content is not None :
@@ -69,12 +83,11 @@ def rec(node, i=0):
6983 if part in node ._children :
7084 for content in rec (node ._children [part ], i + 1 ):
7185 yield content
72- if "+" in node ._children and (normal or i > 0 ):
73- for content in rec (node ._children ["+" ], i + 1 ):
86+ if '+' in node ._children and (normal or i > 0 ):
87+ for content in rec (node ._children ['+' ], i + 1 ):
7488 yield content
75- if "#" in node ._children and (normal or i > 0 ):
76- content = node ._children ["#" ]._content
89+ if '#' in node ._children and (normal or i > 0 ):
90+ content = node ._children ['#' ]._content
7791 if content is not None :
7892 yield content
79-
80- return rec (self ._root )
93+ return rec (self ._root )
0 commit comments