Skip to content

Commit c49e1ab

Browse files
refactor to comply python3.8
1 parent 07c4c70 commit c49e1ab

38 files changed

Lines changed: 710 additions & 927 deletions

docs/references/broker.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ The following example shows how to start a broker using the default configuratio
1616
from hbmqtt.broker import Broker
1717
1818
19-
@asyncio.coroutine
20-
def broker_coro():
19+
async def broker_coro():
2120
broker = Broker()
2221
yield from broker.start()
2322

docs/references/mqttclient.rst

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ The example below shows how to write a simple MQTT client which subscribes a top
2222
2323
logger = logging.getLogger(__name__)
2424
25-
@asyncio.coroutine
26-
def uptime_coro():
25+
async def uptime_coro():
2726
C = MQTTClient()
2827
yield from C.connect('mqtt://test.mosquitto.org/')
2928
# Subscribe to '$SYS/broker/uptime' with QOS=1
@@ -71,8 +70,7 @@ This example also shows to method for publishing message asynchronously.
7170
7271
logger = logging.getLogger(__name__)
7372
74-
@asyncio.coroutine
75-
def test_coro():
73+
async def test_coro():
7674
C = MQTTClient()
7775
yield from C.connect('mqtt://test.mosquitto.org/')
7876
tasks = [
@@ -85,8 +83,7 @@ This example also shows to method for publishing message asynchronously.
8583
yield from C.disconnect()
8684
8785
88-
@asyncio.coroutine
89-
def test_coro2():
86+
async def test_coro2():
9087
try:
9188
C = MQTTClient()
9289
ret = yield from C.connect('mqtt://test.mosquitto.org:1883/')

hbmqtt/adapters.py

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ class ReaderAdapter:
1616
Reader adapters are used to adapt read operations on the network depending on the protocol used
1717
"""
1818

19-
@asyncio.coroutine
20-
def read(self, n=-1) -> bytes:
19+
async def read(self, n=-1) -> bytes:
2120
"""
2221
Read up to n bytes. If n is not provided, or set to -1, read until EOF and return all read bytes.
2322
If the EOF was received and the internal buffer is empty, return an empty bytes object.
@@ -42,8 +41,7 @@ def write(self, data):
4241
write some data to the protocol layer
4342
"""
4443

45-
@asyncio.coroutine
46-
def drain(self):
44+
async def drain(self):
4745
"""
4846
Let the write buffer of the underlying transport a chance to be flushed.
4947
"""
@@ -53,8 +51,7 @@ def get_peer_info(self):
5351
Return peer socket info (remote address and remote port as tuple
5452
"""
5553

56-
@asyncio.coroutine
57-
def close(self):
54+
async def close(self):
5855
"""
5956
Close the protocol connection
6057
"""
@@ -69,22 +66,20 @@ def __init__(self, protocol: WebSocketCommonProtocol):
6966
self._protocol = protocol
7067
self._stream = io.BytesIO(b'')
7168

72-
@asyncio.coroutine
73-
def read(self, n=-1) -> bytes:
74-
yield from self._feed_buffer(n)
69+
async def read(self, n=-1) -> bytes:
70+
await self._feed_buffer(n)
7571
data = self._stream.read(n)
7672
return data
7773

78-
@asyncio.coroutine
79-
def _feed_buffer(self, n=1):
74+
async def _feed_buffer(self, n=1):
8075
"""
8176
Feed the data buffer by reading a Websocket message.
8277
:param n: if given, feed buffer until it contains at least n bytes
8378
"""
8479
buffer = bytearray(self._stream.read())
8580
while len(buffer) < n:
8681
try:
87-
message = yield from self._protocol.recv()
82+
message = await self._protocol.recv()
8883
except ConnectionClosed:
8984
message = None
9085
if message is None:
@@ -110,22 +105,20 @@ def write(self, data):
110105
"""
111106
self._stream.write(data)
112107

113-
@asyncio.coroutine
114-
def drain(self):
108+
async def drain(self):
115109
"""
116110
Let the write buffer of the underlying transport a chance to be flushed.
117111
"""
118112
data = self._stream.getvalue()
119113
if len(data):
120-
yield from self._protocol.send(data)
114+
await self._protocol.send(data)
121115
self._stream = io.BytesIO(b'')
122116

123117
def get_peer_info(self):
124118
return self._protocol.remote_address
125119

126-
@asyncio.coroutine
127-
def close(self):
128-
yield from self._protocol.close()
120+
async def close(self):
121+
await self._protocol.close()
129122

130123

131124
class StreamReaderAdapter(ReaderAdapter):
@@ -137,12 +130,11 @@ class StreamReaderAdapter(ReaderAdapter):
137130
def __init__(self, reader: StreamReader):
138131
self._reader = reader
139132

140-
@asyncio.coroutine
141-
def read(self, n=-1) -> bytes:
133+
async def read(self, n=-1) -> bytes:
142134
if n == -1:
143-
data = yield from self._reader.read(n)
135+
data = await self._reader.read(n)
144136
else:
145-
data = yield from self._reader.readexactly(n)
137+
data = await self._reader.readexactly(n)
146138
return data
147139

148140
def feed_eof(self):
@@ -164,24 +156,22 @@ def write(self, data):
164156
if not self.is_closed:
165157
self._writer.write(data)
166158

167-
@asyncio.coroutine
168-
def drain(self):
159+
async def drain(self):
169160
if not self.is_closed:
170-
yield from self._writer.drain()
161+
await self._writer.drain()
171162

172163
def get_peer_info(self):
173164
extra_info = self._writer.get_extra_info('peername')
174165
return extra_info[0], extra_info[1]
175166

176-
@asyncio.coroutine
177-
def close(self):
167+
async def close(self):
178168
if not self.is_closed:
179169
self.is_closed = True # we first mark this closed so yields below don't cause races with waiting writes
180-
yield from self._writer.drain()
170+
await self._writer.drain()
181171
if self._writer.can_write_eof():
182172
self._writer.write_eof()
183173
self._writer.close()
184-
try: yield from self._writer.wait_closed() # py37+
174+
try: await self._writer.wait_closed() # py37+
185175
except AttributeError: pass
186176

187177

@@ -193,8 +183,7 @@ class BufferReader(ReaderAdapter):
193183
def __init__(self, buffer: bytes):
194184
self._stream = io.BytesIO(buffer)
195185

196-
@asyncio.coroutine
197-
def read(self, n=-1) -> bytes:
186+
async def read(self, n=-1) -> bytes:
198187
return self._stream.read(n)
199188

200189

@@ -212,8 +201,7 @@ def write(self, data):
212201
"""
213202
self._stream.write(data)
214203

215-
@asyncio.coroutine
216-
def drain(self):
204+
async def drain(self):
217205
pass
218206

219207
def get_buffer(self):
@@ -222,6 +210,5 @@ def get_buffer(self):
222210
def get_peer_info(self):
223211
return "BufferWriter", 0
224212

225-
@asyncio.coroutine
226-
def close(self):
213+
async def close(self):
227214
self._stream.close()

0 commit comments

Comments
 (0)