11
11
* Author(s): Yoch (https://github.com/yoch)
12
12
"""
13
13
14
+ try :
15
+ from typing import Dict
16
+ except ImportError :
17
+ pass
18
+
14
19
15
20
class MQTTMatcher :
16
21
"""Intended to manage topic filters including wildcards.
@@ -27,22 +32,22 @@ class Node:
27
32
28
33
__slots__ = "children" , "content"
29
34
30
- def __init__ (self ):
31
- self .children = {}
35
+ def __init__ (self ) -> None :
36
+ self .children : Dict [ str , MQTTMatcher . Node ] = {}
32
37
self .content = None
33
38
34
- def __init__ (self ):
39
+ def __init__ (self ) -> None :
35
40
self ._root = self .Node ()
36
41
37
- def __setitem__ (self , key , value ):
42
+ def __setitem__ (self , key : str , value ) -> None :
38
43
"""Add a topic filter :key to the prefix tree
39
44
and associate it to :value"""
40
45
node = self ._root
41
46
for sym in key .split ("/" ):
42
47
node = node .children .setdefault (sym , self .Node ())
43
48
node .content = value
44
49
45
- def __getitem__ (self , key ):
50
+ def __getitem__ (self , key : str ):
46
51
"""Retrieve the value associated with some topic filter :key"""
47
52
try :
48
53
node = self ._root
@@ -54,7 +59,7 @@ def __getitem__(self, key):
54
59
except KeyError :
55
60
raise KeyError (key ) from None
56
61
57
- def __delitem__ (self , key ) :
62
+ def __delitem__ (self , key : str ) -> None :
58
63
"""Delete the value associated with some topic filter :key"""
59
64
lst = []
60
65
try :
@@ -71,13 +76,13 @@ def __delitem__(self, key):
71
76
break
72
77
del parent .children [k ]
73
78
74
- def iter_match (self , topic ):
79
+ def iter_match (self , topic : str ):
75
80
"""Return an iterator on all values associated with filters
76
81
that match the :topic"""
77
82
lst = topic .split ("/" )
78
83
normal = not topic .startswith ("$" )
79
84
80
- def rec (node , i = 0 ):
85
+ def rec (node : MQTTMatcher . Node , i : int = 0 ):
81
86
if i == len (lst ):
82
87
if node .content is not None :
83
88
yield node .content
0 commit comments