Skip to content

Commit 7a3d161

Browse files
authored
Merge pull request eclipse-paho#913 from pswsm/refactor/reason-codes
fix: move names definition to class-level
2 parents c4af656 + 7d615d4 commit 7a3d161

1 file changed

Lines changed: 72 additions & 71 deletions

File tree

src/paho/mqtt/reasoncodes.py

Lines changed: 72 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import functools
1818
import warnings
19+
from types import MappingProxyType
1920
from typing import Any
2021

2122
from .packettypes import PacketTypes
@@ -30,96 +31,96 @@ class ReasonCode:
3031
3132
"""
3233

33-
def __init__(self, packetType: int, aName: str ="Success", identifier: int =-1):
34-
"""
35-
packetType: the type of the packet, such as PacketTypes.CONNECT that
36-
this reason code will be used with. Some reason codes have different
37-
names for the same identifier when used a different packet type.
38-
39-
aName: the String name of the reason code to be created. Ignored
40-
if the identifier is set.
41-
42-
identifier: an integer value of the reason code to be created.
43-
44-
"""
45-
46-
self.packetType = packetType
47-
self.names = {
48-
0: {"Success": [PacketTypes.CONNACK, PacketTypes.PUBACK,
49-
PacketTypes.PUBREC, PacketTypes.PUBREL, PacketTypes.PUBCOMP,
50-
PacketTypes.UNSUBACK, PacketTypes.AUTH],
34+
names = MappingProxyType(
35+
{
36+
0: {
37+
"Success": [
38+
PacketTypes.CONNACK,
39+
PacketTypes.PUBACK,
40+
PacketTypes.PUBREC,
41+
PacketTypes.PUBREL,
42+
PacketTypes.PUBCOMP,
43+
PacketTypes.UNSUBACK,
44+
PacketTypes.AUTH,
45+
],
5146
"Normal disconnection": [PacketTypes.DISCONNECT],
52-
"Granted QoS 0": [PacketTypes.SUBACK]},
47+
"Granted QoS 0": [PacketTypes.SUBACK],
48+
},
5349
1: {"Granted QoS 1": [PacketTypes.SUBACK]},
5450
2: {"Granted QoS 2": [PacketTypes.SUBACK]},
5551
4: {"Disconnect with will message": [PacketTypes.DISCONNECT]},
56-
16: {"No matching subscribers":
57-
[PacketTypes.PUBACK, PacketTypes.PUBREC]},
52+
16: {"No matching subscribers": [PacketTypes.PUBACK, PacketTypes.PUBREC]},
5853
17: {"No subscription found": [PacketTypes.UNSUBACK]},
5954
24: {"Continue authentication": [PacketTypes.AUTH]},
6055
25: {"Re-authenticate": [PacketTypes.AUTH]},
61-
128: {"Unspecified error": [PacketTypes.CONNACK, PacketTypes.PUBACK,
62-
PacketTypes.PUBREC, PacketTypes.SUBACK, PacketTypes.UNSUBACK,
63-
PacketTypes.DISCONNECT], },
64-
129: {"Malformed packet":
65-
[PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
66-
130: {"Protocol error":
67-
[PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
68-
131: {"Implementation specific error": [PacketTypes.CONNACK,
69-
PacketTypes.PUBACK, PacketTypes.PUBREC, PacketTypes.SUBACK,
70-
PacketTypes.UNSUBACK, PacketTypes.DISCONNECT], },
56+
128: {
57+
"Unspecified error": [PacketTypes.CONNACK, PacketTypes.PUBACK, PacketTypes.PUBREC, PacketTypes.SUBACK, PacketTypes.UNSUBACK, PacketTypes.DISCONNECT],
58+
},
59+
129: {"Malformed packet": [PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
60+
130: {"Protocol error": [PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
61+
131: {
62+
"Implementation specific error": [
63+
PacketTypes.CONNACK,
64+
PacketTypes.PUBACK,
65+
PacketTypes.PUBREC,
66+
PacketTypes.SUBACK,
67+
PacketTypes.UNSUBACK,
68+
PacketTypes.DISCONNECT,
69+
],
70+
},
7171
132: {"Unsupported protocol version": [PacketTypes.CONNACK]},
7272
133: {"Client identifier not valid": [PacketTypes.CONNACK]},
7373
134: {"Bad user name or password": [PacketTypes.CONNACK]},
74-
135: {"Not authorized": [PacketTypes.CONNACK, PacketTypes.PUBACK,
75-
PacketTypes.PUBREC, PacketTypes.SUBACK, PacketTypes.UNSUBACK,
76-
PacketTypes.DISCONNECT], },
74+
135: {
75+
"Not authorized": [PacketTypes.CONNACK, PacketTypes.PUBACK, PacketTypes.PUBREC, PacketTypes.SUBACK, PacketTypes.UNSUBACK, PacketTypes.DISCONNECT],
76+
},
7777
136: {"Server unavailable": [PacketTypes.CONNACK]},
7878
137: {"Server busy": [PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
7979
138: {"Banned": [PacketTypes.CONNACK]},
8080
139: {"Server shutting down": [PacketTypes.DISCONNECT]},
81-
140: {"Bad authentication method":
82-
[PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
81+
140: {"Bad authentication method": [PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
8382
141: {"Keep alive timeout": [PacketTypes.DISCONNECT]},
8483
142: {"Session taken over": [PacketTypes.DISCONNECT]},
85-
143: {"Topic filter invalid":
86-
[PacketTypes.SUBACK, PacketTypes.UNSUBACK, PacketTypes.DISCONNECT]},
87-
144: {"Topic name invalid":
88-
[PacketTypes.CONNACK, PacketTypes.PUBACK,
89-
PacketTypes.PUBREC, PacketTypes.DISCONNECT]},
90-
145: {"Packet identifier in use":
91-
[PacketTypes.PUBACK, PacketTypes.PUBREC,
92-
PacketTypes.SUBACK, PacketTypes.UNSUBACK]},
93-
146: {"Packet identifier not found":
94-
[PacketTypes.PUBREL, PacketTypes.PUBCOMP]},
84+
143: {"Topic filter invalid": [PacketTypes.SUBACK, PacketTypes.UNSUBACK, PacketTypes.DISCONNECT]},
85+
144: {"Topic name invalid": [PacketTypes.CONNACK, PacketTypes.PUBACK, PacketTypes.PUBREC, PacketTypes.DISCONNECT]},
86+
145: {"Packet identifier in use": [PacketTypes.PUBACK, PacketTypes.PUBREC, PacketTypes.SUBACK, PacketTypes.UNSUBACK]},
87+
146: {"Packet identifier not found": [PacketTypes.PUBREL, PacketTypes.PUBCOMP]},
9588
147: {"Receive maximum exceeded": [PacketTypes.DISCONNECT]},
9689
148: {"Topic alias invalid": [PacketTypes.DISCONNECT]},
9790
149: {"Packet too large": [PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
9891
150: {"Message rate too high": [PacketTypes.DISCONNECT]},
99-
151: {"Quota exceeded": [PacketTypes.CONNACK, PacketTypes.PUBACK,
100-
PacketTypes.PUBREC, PacketTypes.SUBACK, PacketTypes.DISCONNECT], },
92+
151: {
93+
"Quota exceeded": [PacketTypes.CONNACK, PacketTypes.PUBACK, PacketTypes.PUBREC, PacketTypes.SUBACK, PacketTypes.DISCONNECT],
94+
},
10195
152: {"Administrative action": [PacketTypes.DISCONNECT]},
102-
153: {"Payload format invalid":
103-
[PacketTypes.PUBACK, PacketTypes.PUBREC, PacketTypes.DISCONNECT]},
104-
154: {"Retain not supported":
105-
[PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
106-
155: {"QoS not supported":
107-
[PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
108-
156: {"Use another server":
109-
[PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
110-
157: {"Server moved":
111-
[PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
112-
158: {"Shared subscription not supported":
113-
[PacketTypes.SUBACK, PacketTypes.DISCONNECT]},
114-
159: {"Connection rate exceeded":
115-
[PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
116-
160: {"Maximum connect time":
117-
[PacketTypes.DISCONNECT]},
118-
161: {"Subscription identifiers not supported":
119-
[PacketTypes.SUBACK, PacketTypes.DISCONNECT]},
120-
162: {"Wildcard subscription not supported":
121-
[PacketTypes.SUBACK, PacketTypes.DISCONNECT]},
96+
153: {"Payload format invalid": [PacketTypes.PUBACK, PacketTypes.PUBREC, PacketTypes.DISCONNECT]},
97+
154: {"Retain not supported": [PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
98+
155: {"QoS not supported": [PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
99+
156: {"Use another server": [PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
100+
157: {"Server moved": [PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
101+
158: {"Shared subscription not supported": [PacketTypes.SUBACK, PacketTypes.DISCONNECT]},
102+
159: {"Connection rate exceeded": [PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
103+
160: {"Maximum connect time": [PacketTypes.DISCONNECT]},
104+
161: {"Subscription identifiers not supported": [PacketTypes.SUBACK, PacketTypes.DISCONNECT]},
105+
162: {"Wildcard subscription not supported": [PacketTypes.SUBACK, PacketTypes.DISCONNECT]},
122106
}
107+
)
108+
109+
def __init__(self, packetType: int, aName: str = "Success", identifier: int = -1):
110+
"""
111+
packetType: the type of the packet, such as PacketTypes.CONNECT that
112+
this reason code will be used with. Some reason codes have different
113+
names for the same identifier when used a different packet type.
114+
115+
aName: the String name of the reason code to be created. Ignored
116+
if the identifier is set.
117+
118+
identifier: an integer value of the reason code to be created.
119+
120+
"""
121+
122+
self.names = dict(type(self).names)
123+
self.packetType = packetType
123124
if identifier == -1:
124125
if packetType == PacketTypes.DISCONNECT and aName == "Success":
125126
aName = "Normal disconnection"
@@ -167,8 +168,7 @@ def unpack(self, buffer):
167168
return 1
168169

169170
def getName(self):
170-
"""Returns the reason code name corresponding to the numeric value which is set.
171-
"""
171+
"""Returns the reason code name corresponding to the numeric value which is set."""
172172
return self.__getName__(self.packetType, self.value)
173173

174174
def __eq__(self, other):
@@ -216,7 +216,8 @@ def __instancecheck__(self, other: Any) -> bool:
216216

217217
class ReasonCodes(ReasonCode, metaclass=_CompatibilityIsInstance):
218218
def __init__(self, *args, **kwargs):
219-
warnings.warn("ReasonCodes is deprecated, use ReasonCode (singular) instead",
219+
warnings.warn(
220+
"ReasonCodes is deprecated, use ReasonCode (singular) instead",
220221
category=DeprecationWarning,
221222
stacklevel=2,
222223
)

0 commit comments

Comments
 (0)