Skip to content

Commit 0506645

Browse files
authored
Prepare pymodbus/framer for pylint docstyle. (#829)
1 parent 6c57403 commit 0506645

File tree

6 files changed

+45
-90
lines changed

6 files changed

+45
-90
lines changed

pymodbus/framer/__init__.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Framer start."""
1+
""" Framer start. """
22
import struct
33
from pymodbus.interfaces import IModbusFramer
44

@@ -13,13 +13,10 @@
1313
TLS_FRAME_HEADER = BYTE_ORDER + 'B'
1414

1515
class ModbusFramer(IModbusFramer):
16-
"""
17-
Base Framer class
18-
"""
16+
""" Base Framer class. """
1917

2018
def _validate_unit_id(self, units, single):
21-
"""
22-
Validates if the received data is valid for the client
19+
""" Validates if the received data is valid for the client
2320
:param units: list of unit id for which the transaction is valid
2421
:param single: Set to true to treat this as a single context
2522
:return: """
@@ -33,16 +30,14 @@ def _validate_unit_id(self, units, single):
3330
return self._header['uid'] in units # pylint: disable=no-member
3431

3532
def sendPacket(self, message): # pylint: disable=invalid-name
36-
"""
37-
Sends packets on the bus with 3.5char delay between frames
33+
""" Sends packets on the bus with 3.5char delay between frames
3834
:param message: Message to be sent over the bus
3935
:return:
4036
"""
4137
return self.client.send(message) # pylint: disable=no-member
4238

4339
def recvPacket(self, size): # pylint: disable=invalid-name
44-
"""
45-
Receives packet from the bus with specified len
40+
""" Receives packet from the bus with specified len
4641
:param size: Number of bytes to read
4742
:return:
4843
"""

pymodbus/framer/ascii_framer.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
# Modbus ASCII Message
2222
# --------------------------------------------------------------------------- #
2323
class ModbusAsciiFramer(ModbusFramer):
24-
"""
25-
Modbus ASCII Frame Controller::
24+
""" Modbus ASCII Frame Controller::
2625
2726
[ Start ][Address ][ Function ][ Data ][ LRC ][ End ]
2827
1c 2c 2c Nc 2c 2c
@@ -145,8 +144,7 @@ def populateResult(self, result):
145144
# Public Member Functions
146145
# ----------------------------------------------------------------------- #
147146
def processIncomingPacket(self, data, callback, unit, **kwargs): # pylint: disable=arguments-differ
148-
"""
149-
The new packet processing pattern
147+
""" The new packet processing pattern
150148
151149
This takes in a new request packet, adds it to the current
152150
packet stream, and performs framing on it. That is, checks

pymodbus/framer/binary_framer.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Binary framer."""
1+
""" Binary framer. """
22
import logging
33
import struct
44
from pymodbus.exceptions import ModbusIOException
@@ -18,8 +18,7 @@
1818

1919

2020
class ModbusBinaryFramer(ModbusFramer): # pylint: disable=too-many-instance-attributes
21-
"""
22-
Modbus Binary Frame Controller::
21+
""" Modbus Binary Frame Controller::
2322
2423
[ Start ][Address ][ Function ][ Data ][ CRC ][ End ]
2524
1b 1b 1b Nb 2b 1b
@@ -60,7 +59,7 @@ def __init__(self, decoder, client=None):
6059
# Private Helper Functions
6160
# ----------------------------------------------------------------------- #
6261
def decode_data(self, data):
63-
"""Decode data."""
62+
""" Decode data. """
6463
if len(data) > self._hsize:
6564
uid = struct.unpack('>B', data[1:2])[0]
6665
fcode = struct.unpack('>B', data[2:3])[0]
@@ -140,8 +139,7 @@ def populateResult(self, result):
140139
# Public Member Functions
141140
# ----------------------------------------------------------------------- #
142141
def processIncomingPacket(self, data, callback, unit, **kwargs): # pylint: disable=arguments-differ
143-
"""
144-
The new packet processing pattern
142+
""" The new packet processing pattern
145143
146144
This takes in a new request packet, adds it to the current
147145
packet stream, and performs framing on it. That is, checks
@@ -198,8 +196,7 @@ def buildPacket(self, message):
198196
return packet
199197

200198
def _preflight(self, data):
201-
"""
202-
Preflight buffer test
199+
""" Preflight buffer test
203200
204201
This basically scans the buffer for start and end
205202
tags and if found, escapes them.

pymodbus/framer/rtu_framer.py

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""RTU framer."""
1+
""" RTU framer. """
22
# pylint: disable=R0801
33
import logging
44
import struct
@@ -22,8 +22,7 @@
2222
# Modbus RTU Message
2323
# --------------------------------------------------------------------------- #
2424
class ModbusRtuFramer(ModbusFramer):
25-
"""
26-
Modbus RTU Frame controller::
25+
""" Modbus RTU Frame controller::
2726
2827
[ Start Wait ] [Address ][ Function Code] [ Data ][ CRC ][ End Wait ]
2928
3.5 chars 1b 1b Nb 2b 3.5 chars
@@ -72,16 +71,15 @@ def __init__(self, decoder, client=None):
7271
# Private Helper Functions
7372
# ----------------------------------------------------------------------- #
7473
def decode_data(self, data):
75-
"""Decode data."""
74+
""" Decode data. """
7675
if len(data) > self._hsize:
7776
uid = int(data[0])
7877
fcode = int(data[1])
7978
return dict(unit=uid, fcode=fcode)
8079
return {}
8180

8281
def checkFrame(self):
83-
"""
84-
Check if the next frame is available.
82+
""" Check if the next frame is available.
8583
Return True if we were successful.
8684
8785
1. Populate header
@@ -98,8 +96,7 @@ def checkFrame(self):
9896
return False
9997

10098
def advanceFrame(self):
101-
"""
102-
Skip over the current framed message
99+
""" Skip over the current framed message
103100
This allows us to skip over the current message after we have processed
104101
it or determined that it contains an error. It also has to reset the
105102
current frame header handle
@@ -110,8 +107,7 @@ def advanceFrame(self):
110107
self._header = {'uid': 0x00, 'len': 0, 'crc': b'\x00\x00'}
111108

112109
def resetFrame(self): # pylint: disable=invalid-name
113-
"""
114-
Reset the entire message frame.
110+
""" Reset the entire message frame.
115111
This allows us to skip over errors that may be in the stream.
116112
It is hard to know if we are simply out of sync or if there is
117113
an error in the stream as we have no way to check the start or
@@ -125,8 +121,7 @@ def resetFrame(self): # pylint: disable=invalid-name
125121
self._header = {'uid': 0x00, 'len': 0, 'crc': b'\x00\x00'}
126122

127123
def isFrameReady(self):
128-
"""
129-
Check if we should continue decode logic
124+
""" Check if we should continue decode logic
130125
This is meant to be used in a while loop in the decoding phase to let
131126
the decoder know that there is still data in the buffer.
132127
@@ -146,8 +141,7 @@ def isFrameReady(self):
146141
return True
147142

148143
def populateHeader(self, data=None): # pylint: disable=invalid-name
149-
"""
150-
Try to set the headers `uid`, `len` and `crc`.
144+
""" Try to set the headers `uid`, `len` and `crc`.
151145
152146
This method examines `self._buffer` and writes meta
153147
information into `self._header`.
@@ -168,17 +162,15 @@ def populateHeader(self, data=None): # pylint: disable=invalid-name
168162
self._header['crc'] = data[size - 2:size]
169163

170164
def addToFrame(self, message):
171-
"""
172-
This should be used before the decoding while loop to add the received
165+
""" This should be used before the decoding while loop to add the received
173166
data to the buffer handle.
174167
175168
:param message: The most recent packet
176169
"""
177170
self._buffer += message
178171

179172
def getFrame(self):
180-
"""
181-
Get the next frame from the buffer
173+
""" Get the next frame from the buffer
182174
183175
:returns: The frame data or ''
184176
"""
@@ -192,8 +184,7 @@ def getFrame(self):
192184
return b''
193185

194186
def populateResult(self, result):
195-
"""
196-
Populates the modbus result header
187+
""" Populates the modbus result header
197188
198189
The serial packets do not have any header information
199190
that is copied.
@@ -207,8 +198,7 @@ def populateResult(self, result):
207198
# Public Member Functions
208199
# ----------------------------------------------------------------------- #
209200
def processIncomingPacket(self, data, callback, unit, **kwargs): # pylint: disable=arguments-differ
210-
"""
211-
The new packet processing pattern
201+
""" The new packet processing pattern
212202
213203
This takes in a new request packet, adds it to the current
214204
packet stream, and performs framing on it. That is, checks
@@ -246,8 +236,7 @@ def processIncomingPacket(self, data, callback, unit, **kwargs): # pylint: disab
246236
_logger.debug(txt)
247237

248238
def buildPacket(self, message):
249-
"""
250-
Creates a ready to send modbus packet
239+
""" Creates a ready to send modbus packet
251240
252241
:param message: The populated request/response to send
253242
"""
@@ -261,8 +250,7 @@ def buildPacket(self, message):
261250
return packet
262251

263252
def sendPacket(self, message):
264-
"""
265-
Sends packets on the bus with 3.5char delay between frames
253+
""" Sends packets on the bus with 3.5char delay between frames
266254
:param message: Message to be sent over the bus
267255
:return:
268256
"""
@@ -303,8 +291,7 @@ def sendPacket(self, message):
303291
return size
304292

305293
def recvPacket(self, size):
306-
"""
307-
Receives packet from the bus with specified len
294+
""" Receives packet from the bus with specified len
308295
:param size: Number of bytes to read
309296
:return:
310297
"""
@@ -313,9 +300,7 @@ def recvPacket(self, size):
313300
return result
314301

315302
def _process(self, callback, error=False):
316-
"""
317-
Process incoming packets irrespective error condition
318-
"""
303+
""" Process incoming packets irrespective error condition. """
319304
data = self.getRawFrame() if error else self.getFrame()
320305
result = self.decoder.decode(data)
321306
if result is None:
@@ -327,9 +312,7 @@ def _process(self, callback, error=False):
327312
callback(result) # defer or push to a thread?
328313

329314
def getRawFrame(self): # pylint: disable=invalid-name
330-
"""
331-
Returns the complete buffer
332-
"""
315+
""" Returns the complete buffer. """
333316
txt = f"Getting Raw Frame - {hexlify_packets(self._buffer)}"
334317
_logger.debug(txt)
335318
return self._buffer

pymodbus/framer/socket_framer.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Socket framer."""
1+
""" Socket framer. """
22
# pylint: disable=R0801
33
import logging
44
import struct
@@ -51,9 +51,7 @@ def __init__(self, decoder, client=None):
5151
# Private Helper Functions
5252
# ----------------------------------------------------------------------- #
5353
def checkFrame(self):
54-
"""
55-
Check and decode the next frame Return true if we were successful
56-
"""
54+
""" Check and decode the next frame Return true if we were successful. """
5755
if self.isFrameReady():
5856
(self._header['tid'], self._header['pid'],
5957
self._header['len'], self._header['uid']) = struct.unpack(
@@ -103,8 +101,7 @@ def getFrame(self):
103101
return self._buffer[self._hsize:length]
104102

105103
def populateResult(self, result):
106-
"""
107-
Populates the modbus result with the transport specific header
104+
""" Populates the modbus result with the transport specific header
108105
information (pid, tid, uid, checksum, etc)
109106
110107
:param result: The response packet
@@ -117,16 +114,15 @@ def populateResult(self, result):
117114
# Public Member Functions
118115
# ----------------------------------------------------------------------- #
119116
def decode_data(self, data):
120-
"""Decode data."""
117+
""" Decode data. """
121118
if len(data) > self._hsize:
122119
tid, pid, length, uid, fcode = struct.unpack(SOCKET_FRAME_HEADER,
123120
data[0:self._hsize+1])
124121
return dict(tid=tid, pid=pid, length=length, unit=uid, fcode=fcode)
125122
return {}
126123

127124
def processIncomingPacket(self, data, callback, unit, **kwargs): #NOSONAR pylint: disable=arguments-differ
128-
"""
129-
The new packet processing pattern
125+
""" The new packet processing pattern
130126
131127
This takes in a new request packet, adds it to the current
132128
packet stream, and performs framing on it. That is, checks
@@ -170,9 +166,7 @@ def processIncomingPacket(self, data, callback, unit, **kwargs): #NOSONAR pylint
170166
break
171167

172168
def _process(self, callback, error=False):
173-
"""
174-
Process incoming packets irrespective error condition
175-
"""
169+
""" Process incoming packets irrespective error condition. """
176170
data = self.getRawFrame() if error else self.getFrame()
177171
result = self.decoder.decode(data)
178172
if result is None:
@@ -184,8 +178,7 @@ def _process(self, callback, error=False):
184178
callback(result) # defer or push to a thread?
185179

186180
def resetFrame(self): # pylint: disable=invalid-name
187-
"""
188-
Reset the entire message frame.
181+
""" Reset the entire message frame.
189182
This allows us to skip ovver errors that may be in the stream.
190183
It is hard to know if we are simply out of sync or if there is
191184
an error in the stream as we have no way to check the start or
@@ -196,9 +189,7 @@ def resetFrame(self): # pylint: disable=invalid-name
196189
self._header = {'tid': 0, 'pid': 0, 'len': 0, 'uid': 0}
197190

198191
def getRawFrame(self): # pylint: disable=invalid-name
199-
"""
200-
Returns the complete buffer
201-
"""
192+
""" Returns the complete buffer. """
202193
return self._buffer
203194

204195
def buildPacket(self, message):

0 commit comments

Comments
 (0)