Skip to content

Commit 2e2e46d

Browse files
committed
apply PEP8; port to python3
1 parent 3e00b04 commit 2e2e46d

File tree

9 files changed

+445
-394
lines changed

9 files changed

+445
-394
lines changed

modem/base.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from modem.const import CRC16_MAP, CRC32_MAP
21
from modem.tools import crc16, crc32
32

43

@@ -29,14 +28,14 @@ def calc_crc16(self, data, crc=0):
2928
Calculate the 16 bit Cyclic Redundancy Check for a given block of data,
3029
can also be used to update a CRC.
3130
32-
>>> crc = modem.calc_crc16('hello')
33-
>>> crc = modem.calc_crc16('world', crc)
31+
>>> crc = modem.calc_crc16(b'hello')
32+
>>> crc = modem.calc_crc16(b'world', crc)
3433
>>> hex(crc)
3534
'0xd5e3'
3635
3736
'''
38-
for char in data:
39-
crc = crc16(char, crc)
37+
for byte in data:
38+
crc = crc16(byte, crc)
4039
return crc
4140

4241
def calc_crc32(self, data, crc=0):
@@ -50,8 +49,8 @@ def calc_crc32(self, data, crc=0):
5049
'0x20ad'
5150
5251
'''
53-
for char in data:
54-
crc = crc32(char, crc)
52+
for byte in data:
53+
crc = crc32(byte, crc)
5554
return crc
5655

5756
def _check_crc(self, data, crc_mode):
@@ -68,13 +67,13 @@ def _check_crc(self, data, crc_mode):
6867
or returns False in case of invalid checksum/CRC
6968
'''
7069
if crc_mode:
71-
csum = (ord(data[-2]) << 8) + ord(data[-1])
70+
csum = (data[-2] << 8) + data[-1]
7271
data = data[:-2]
7372
mine = self.calc_crc16(data)
7473
if csum == mine:
7574
return data
7675
else:
77-
csum = ord(data[-3])
76+
csum = data[-3]
7877
data = data[:-1]
7978
mine = self.calc_checksum(data)
8079
if csum == mine:

modem/const.py

Lines changed: 79 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -279,110 +279,110 @@
279279
280280
'''
281281

282-
SOH = chr(0x01)
283-
STX = chr(0x02)
284-
EOT = chr(0x04)
285-
ACK = chr(0x06)
286-
XON = chr(0x11)
287-
XOFF = chr(0x13)
288-
NAK = chr(0x15)
289-
CAN = chr(0x18)
290-
CRC = chr(0x43)
291-
292-
TIMEOUT = None
293-
ZPAD = 0x2a
294-
ZDLE = 0x18
295-
ZDLEE = 0x58
296-
ZBIN = 0x41
297-
ZHEX = 0x42
298-
ZBIN32 = 0x43
299-
ZBINR32 = 0x44
300-
ZVBIN = 0x61
301-
ZVHEX = 0x62
302-
ZVBIN32 = 0x63
303-
ZVBINR32 = 0x64
304-
ZRESC = 0x7e
282+
SOH = b'\x01'
283+
STX = b'\x02'
284+
EOT = b'\x04'
285+
ACK = b'\x06'
286+
XON = b'\x11'
287+
XOFF = b'\x13'
288+
NAK = b'\x15'
289+
CAN = b'\x18'
290+
CRC = b'\x43'
291+
292+
TIMEOUT = None
293+
ZPAD = 0x2a
294+
ZDLE = 0x18
295+
ZDLEE = 0x58
296+
ZBIN = 0x41
297+
ZHEX = 0x42
298+
ZBIN32 = 0x43
299+
ZBINR32 = 0x44
300+
ZVBIN = 0x61
301+
ZVHEX = 0x62
302+
ZVBIN32 = 0x63
303+
ZVBINR32 = 0x64
304+
ZRESC = 0x7e
305305

306306
# ZMODEM Frame types
307-
ZRQINIT = 0x00
308-
ZRINIT = 0x01
309-
ZSINIT = 0x02
310-
ZACK = 0x03
311-
ZFILE = 0x04
312-
ZSKIP = 0x05
313-
ZNAK = 0x06
314-
ZABORT = 0x07
315-
ZFIN = 0x08
316-
ZRPOS = 0x09
317-
ZDATA = 0x0a
318-
ZEOF = 0x0b
319-
ZFERR = 0x0c
320-
ZCRC = 0x0d
321-
ZCHALLENGE = 0x0e
322-
ZCOMPL = 0x0f
323-
ZCAN = 0x10
324-
ZFREECNT = 0x11
325-
ZCOMMAND = 0x12
326-
ZSTDERR = 0x13
307+
ZRQINIT = 0x00
308+
ZRINIT = 0x01
309+
ZSINIT = 0x02
310+
ZACK = 0x03
311+
ZFILE = 0x04
312+
ZSKIP = 0x05
313+
ZNAK = 0x06
314+
ZABORT = 0x07
315+
ZFIN = 0x08
316+
ZRPOS = 0x09
317+
ZDATA = 0x0a
318+
ZEOF = 0x0b
319+
ZFERR = 0x0c
320+
ZCRC = 0x0d
321+
ZCHALLENGE = 0x0e
322+
ZCOMPL = 0x0f
323+
ZCAN = 0x10
324+
ZFREECNT = 0x11
325+
ZCOMMAND = 0x12
326+
ZSTDERR = 0x13
327327

328328
# ZMODEM ZDLE sequences
329-
ZCRCE = 0x68
330-
ZCRCG = 0x69
331-
ZCRCQ = 0x6a
332-
ZCRCW = 0x6b
333-
ZRUB0 = 0x6c
334-
ZRUB1 = 0x6d
329+
ZCRCE = 0x68
330+
ZCRCG = 0x69
331+
ZCRCQ = 0x6a
332+
ZCRCW = 0x6b
333+
ZRUB0 = 0x6c
334+
ZRUB1 = 0x6d
335335

336336
# ZMODEM Receiver capability flags
337-
CANFDX = 0x01
338-
CANOVIO = 0x02
339-
CANBRK = 0x04
340-
CANCRY = 0x08
341-
CANLZW = 0x10
342-
CANFC32 = 0x20
343-
ESCCTL = 0x40
344-
ESC8 = 0x80
337+
CANFDX = 0x01
338+
CANOVIO = 0x02
339+
CANBRK = 0x04
340+
CANCRY = 0x08
341+
CANLZW = 0x10
342+
CANFC32 = 0x20
343+
ESCCTL = 0x40
344+
ESC8 = 0x80
345345

346346
# ZMODEM ZRINIT frame
347-
ZF0_CANFDX = 0x01
347+
ZF0_CANFDX = 0x01
348348
ZF0_CANOVIO = 0x02
349-
ZF0_CANBRK = 0x04
350-
ZF0_CANCRY = 0x08
351-
ZF0_CANLZW = 0x10
349+
ZF0_CANBRK = 0x04
350+
ZF0_CANCRY = 0x08
351+
ZF0_CANLZW = 0x10
352352
ZF0_CANFC32 = 0x20
353-
ZF0_ESCCTL = 0x40
354-
ZF0_ESC8 = 0x80
353+
ZF0_ESCCTL = 0x40
354+
ZF0_ESC8 = 0x80
355355
ZF1_CANVHDR = 0x01
356356

357357
# ZMODEM ZSINIT frame
358358
ZF0_TESCCTL = 0x40
359-
ZF0_TESC8 = 0x80
359+
ZF0_TESC8 = 0x80
360360

361361
# ZMODEM Byte positions within header array
362362
ZF0, ZF1, ZF2, ZF3 = range(4, 0, -1)
363363
ZP0, ZP1, ZP2, ZP3 = range(1, 5)
364364

365365
# ZMODEM Frame contents
366-
ENDOFFRAME = 2
367-
FRAMEOK = 1
368-
TIMEOUT = -1 # Rx routine did not receive a character within timeout
369-
INVHDR = -2 # Invalid header received; but within timeout
370-
INVDATA = -3 # Invalid data subpacket received
371-
ZDLEESC = 0x8000 # One of ZCRCE/ZCRCG/ZCRCQ/ZCRCW was ZDLE escaped
366+
ENDOFFRAME = 2
367+
FRAMEOK = 1
368+
TIMEOUT = -1 # Rx routine did not receive a character within timeout
369+
INVHDR = -2 # Invalid header received; but within timeout
370+
INVDATA = -3 # Invalid data subpacket received
371+
ZDLEESC = 0x8000 # One of ZCRCE/ZCRCG/ZCRCQ/ZCRCW was ZDLE escaped
372372

373373
# MODEM Protocol types
374-
PROTOCOL_XMODEM = 0x00
375-
PROTOCOL_XMODEMCRC = 0x01
376-
PROTOCOL_XMODEM1K = 0x02
377-
PROTOCOL_YMODEM = 0x03
378-
PROTOCOL_ZMODEM = 0x04
374+
PROTOCOL_XMODEM = 0x00
375+
PROTOCOL_XMODEMCRC = 0x01
376+
PROTOCOL_XMODEM1K = 0x02
377+
PROTOCOL_YMODEM = 0x03
378+
PROTOCOL_ZMODEM = 0x04
379379

380380
PACKET_SIZE = {
381-
PROTOCOL_XMODEM: 128,
381+
PROTOCOL_XMODEM: 128,
382382
PROTOCOL_XMODEMCRC: 128,
383-
PROTOCOL_XMODEM1K: 1024,
384-
PROTOCOL_YMODEM: 1024,
385-
PROTOCOL_ZMODEM: 1024,
383+
PROTOCOL_XMODEM1K: 1024,
384+
PROTOCOL_YMODEM: 1024,
385+
PROTOCOL_ZMODEM: 1024,
386386
}
387387

388388
# CRC tab calculated by Mark G. Mendel, Network Systems Corporation

modem/error.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,40 @@
11
from gettext import gettext as _
22

3-
ABORT = _('Aborting transfer')
4-
ABORT_WHY = _('Aborting transfer; %s')
5-
ERROR = _('Error')
6-
ERROR_WHY = _('Error; %s')
7-
WARNS = _('Warning')
8-
WARNS_WHY = _('Warnings; %s')
3+
ABORT = _('Aborting transfer')
4+
ABORT_WHY = _('Aborting transfer; %s')
5+
ERROR = _('Error')
6+
ERROR_WHY = _('Error; %s')
7+
WARNS = _('Warning')
8+
WARNS_WHY = _('Warnings; %s')
99

10-
ABORT_ERROR_LIMIT = ABORT_WHY % _('error limit reached')
10+
ABORT_ERROR_LIMIT = ABORT_WHY % _('error limit reached')
1111
ABORT_EXPECT_NAK_CRC = ABORT_WHY % _('expected <NAK>/<CRC>, got "%02x"')
1212
ABORT_EXPECT_SOH_EOT = ABORT_WHY % _('expected <SOH>/<EOT>, got "%02x"')
13-
ABORT_INIT_NEXT = ABORT_WHY % _('initialisation of next failed')
14-
ABORT_OPEN_FILE = ABORT_WHY % _('error opening file')
15-
ABORT_PACKET_SIZE = ABORT_WHY % _('incompatible packet size')
16-
ABORT_PROTOCOL = ABORT_WHY % _('protocol error')
17-
ABORT_RECV_CAN_CAN = ABORT_WHY % _('second <CAN> received')
18-
ABORT_RECV_PACKET = ABORT_WHY % _('packet recv failed')
19-
ABORT_RECV_STREAM = ABORT_WHY % _('stream recv failed')
20-
ABORT_SEND_PACKET = ABORT_WHY % _('packet send failed')
21-
ABORT_SEND_STREAM = ABORT_WHY % _('stream send failed')
13+
ABORT_INIT_NEXT = ABORT_WHY % _('initialisation of next failed')
14+
ABORT_OPEN_FILE = ABORT_WHY % _('error opening file')
15+
ABORT_PACKET_SIZE = ABORT_WHY % _('incompatible packet size')
16+
ABORT_PROTOCOL = ABORT_WHY % _('protocol error')
17+
ABORT_RECV_CAN_CAN = ABORT_WHY % _('second <CAN> received')
18+
ABORT_RECV_PACKET = ABORT_WHY % _('packet recv failed')
19+
ABORT_RECV_STREAM = ABORT_WHY % _('stream recv failed')
20+
ABORT_SEND_PACKET = ABORT_WHY % _('packet send failed')
21+
ABORT_SEND_STREAM = ABORT_WHY % _('stream send failed')
2222

23-
DEBUG_RECV_CAN = _('First <CAN> received')
24-
DEBUG_SEND_CAN = _('First <CAN> sent')
23+
DEBUG_RECV_CAN = _('First <CAN> received')
24+
DEBUG_SEND_CAN = _('First <CAN> sent')
2525
DEBUG_START_FILENAME = _('Start sending "%s"')
26-
DEBUG_TRY_CRC = _('Try CRC mode')
27-
DEBUG_TRY_CHECKSUM = _('Try check sum mode')
26+
DEBUG_TRY_CRC = _('Try CRC mode')
27+
DEBUG_TRY_CHECKSUM = _('Try check sum mode')
28+
DEBUG_SEND_EOT = _('Send <EOT>')
29+
DEBUG_FILENAME_SENT = _('File name {} sent')
30+
DEBUG_FILE_SENT = _('File {} sent')
31+
DEBUG_SEND_PROGRESS = _('Progress: |{}>{:3.0f}%{}|')
2832

2933
ERROR_EXPECT_NAK_CRC = ERROR_WHY % _('expected <NAK>/<CRC>, got "%02x"')
3034
ERROR_EXPECT_SOH_EOT = ERROR_WHY % _('expected <SOH>/<EOT>, got "%02x"')
31-
ERROR_PROTOCOL = ERROR_WHY % _('protocol error')
32-
ERROR_SEND_EOT = ERROR_WHY % _('failed sending <EOT>')
33-
ERROR_SEND_PACKET = ERROR_WHY % _('failed to send packet')
35+
ERROR_PROTOCOL = ERROR_WHY % _('protocol error')
36+
ERROR_SEND_EOT = ERROR_WHY % _('failed sending <EOT>')
37+
ERROR_SEND_PACKET = ERROR_WHY % _('failed to send packet')
3438

35-
WARNS_SEQUENCE = WARNS_WHY % \
39+
WARNS_SEQUENCE = WARNS_WHY % \
3640
_('invalid sequence; expected %02x got %02x/%02x')

0 commit comments

Comments
 (0)