Skip to content

Commit 6d53b68

Browse files
committed
PEP8 compatibility.
1 parent cf36443 commit 6d53b68

File tree

4 files changed

+62
-44
lines changed

4 files changed

+62
-44
lines changed

setup.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
author="Bas Stottelaar",
99
author_email="basstottelaar@gmail.com",
1010
packages=["tinylink"],
11-
license = "MIT",
12-
keywords = "python embedded arm arduino tinylink streaming serial",
11+
license="MIT",
12+
keywords="python embedded arm arduino tinylink streaming serial",
1313
test_suite="tests",
1414
entry_points={
1515
"console_scripts": [
1616
"tinylink = tinylink.cli:run",
1717
]
1818
},
19-
classifiers = [
19+
classifiers=[
2020
"Development Status :: 5 - Production/Stable",
2121
"Intended Audience :: Developers",
2222
"License :: OSI Approved :: MIT License",
@@ -25,4 +25,4 @@
2525
"Programming Language :: Python :: 2",
2626
"Programming Language :: Python :: 2.7",
2727
],
28-
)
28+
)

tinylink/__init__.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
LEN_HEADER = LEN_FLAGS + LEN_LENGTH + LEN_XOR
3232
LEN_BODY = LEN_CRC
3333

34+
3435
class BaseFrame(object):
3536
"""
3637
Base frame.
@@ -41,21 +42,24 @@ def __init__(self, data=None, flags=FLAG_NONE):
4142
self.flags = flags
4243

4344
def __repr__(self):
44-
return "%s(%s, flags=%d)" % (self.__class__.__name__,
45-
repr(self.data), self.flags)
45+
return "%s(%s, flags=%d)" % (
46+
self.__class__.__name__, repr(self.data), self.flags)
47+
4648

4749
class Frame(BaseFrame):
4850
"""
4951
Represent normal frame.
5052
"""
5153
pass
5254

55+
5356
class DamagedFrame(BaseFrame):
5457
"""
5558
Represent damaged frame.
5659
"""
5760
pass
5861

62+
5963
class ResetFrame(BaseFrame):
6064
"""
6165
Represent reset frame.
@@ -67,12 +71,14 @@ def __init__(self):
6771
def __repr__(self):
6872
return "ResetFrame()"
6973

74+
7075
class TinyLink(object):
7176
"""
7277
TinyLink state machine for streaming communication with low-speed embedded
7378
applications that only use RX/TX. Every message is encapsulated in a frame.
74-
A frame has a header checksum and a frame checksum, to detect errors as fast
75-
as possible (this can happen when you jump right into a stream of packets).
79+
A frame has a header checksum and a frame checksum, to detect errors as
80+
fast as possible (this can happen when you jump right into a stream of
81+
packets).
7682
7783
A typical frame has 13 bytes overhead, and can have a data payload up to
7884
65536 bytes.
@@ -81,7 +87,7 @@ class TinyLink(object):
8187
"""
8288

8389
def __init__(self, handle, endianness=LITTLE_ENDIAN,
84-
max_length=2**(LEN_LENGTH * 8), ignore_damaged=False):
90+
max_length=2**(LEN_LENGTH * 8), ignore_damaged=False):
8591
"""
8692
Construct a new TinyLink state machine. A state machine takes a handle,
8793
which provides a `read' and `write' method.
@@ -122,19 +128,22 @@ def write_frame(self, frame):
122128

123129
# Check length of message
124130
if length > self.max_length:
125-
raise ValueError("Message length %d exceeds max length %d" %
126-
(length, self.max_length))
131+
raise ValueError(
132+
"Message length %d exceeds max length %d" % (
133+
length, self.max_length))
127134

128135
# Pack header
129136
checksum_header = utils.checksum_header(frame.flags, length)
130-
result += struct.pack(self.endianness + "IHHB", PREAMBLE, frame.flags,
131-
length, checksum_header)
137+
result += struct.pack(
138+
self.endianness + "IHHB", PREAMBLE, frame.flags, length,
139+
checksum_header)
132140

133141
# Pack data
134142
if frame.data is not None:
135143
checksum_frame = utils.checksum_frame(frame.data, checksum_header)
136-
result += struct.pack(self.endianness + str(length) + "sI",
137-
frame.data, checksum_frame)
144+
result += struct.pack(
145+
self.endianness + str(length) + "sI", frame.data,
146+
checksum_frame)
138147

139148
# Write to file
140149
return self.handle.write(result)
@@ -177,8 +186,8 @@ def read(self, limit=1):
177186
# Decide what to do
178187
if self.state == WAITING_FOR_PREAMBLE:
179188
if self.index >= LEN_PREAMBLE:
180-
start, = struct.unpack_from(self.endianness + "I",
181-
self.buffer, self.index - 4)
189+
start, = struct.unpack_from(
190+
self.endianness + "I", self.buffer, self.index - 4)
182191

183192
if start == PREAMBLE:
184193
# Advance to next state
@@ -198,7 +207,7 @@ def read(self, limit=1):
198207

199208
# Verify checksum
200209
if checksum == utils.checksum_header(flags, length) and \
201-
length <= self.max_length:
210+
length <= self.max_length:
202211

203212
if length > 0:
204213
self.state = WAITING_FOR_BODY
@@ -238,4 +247,4 @@ def read(self, limit=1):
238247
limit -= 1
239248

240249
# Done
241-
return frames
250+
return frames

tinylink/cli.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import sys
22
import csv
3-
import signal
43
import select
54
import struct
65
import tinylink
@@ -13,13 +12,15 @@
1312
except ImportError:
1413
serial = None
1514

15+
1616
def run():
1717
"""
1818
Entry point for console script.
1919
"""
2020

2121
sys.exit(main())
2222

23+
2324
def parse_arguments():
2425
"""
2526
Create and parse command line arguments.
@@ -29,16 +30,18 @@ def parse_arguments():
2930

3031
# Add option
3132
parser.add_argument("port", type=str, help="serial port")
32-
parser.add_argument("baudrate", type=int, default=9600,
33-
help="serial baudrate")
34-
parser.add_argument("--length", type=int, default=2**16,
33+
parser.add_argument(
34+
"baudrate", type=int, default=9600, help="serial baudrate")
35+
parser.add_argument(
36+
"--length", type=int, default=2**16, help="maximum length of frame")
37+
parser.add_argument(
38+
"--endianness", type=str, default="little", choices=["big", "little"],
3539
help="maximum length of frame")
36-
parser.add_argument("--endianness", type=str, default="little",
37-
choices=["big", "little"], help="maximum length of frame")
3840

3941
# Parse command line
4042
return parser.parse_args(), parser
4143

44+
4245
def dump(prefix, data):
4346
"""
4447
Dump data as two hex columns
@@ -53,10 +56,10 @@ def dump(prefix, data):
5356
for j in xrange(0, 16):
5457
if i + j < length:
5558
b = ord(data[i + j])
56-
hexstr += "%02x " % b
59+
hexstr += "%02x " % b
5760
bytestr += data[i + j] if 0x20 <= b < 0x7F else "."
5861
else:
59-
hexstr += " "
62+
hexstr += " "
6063

6164
if (j % 4) == 3:
6265
hexstr += " "
@@ -66,6 +69,7 @@ def dump(prefix, data):
6669
# Return concatenated string
6770
return "\n".join(result)
6871

72+
6973
def process_link(link):
7074
"""
7175
Process incoming link data.
@@ -84,6 +88,7 @@ def process_link(link):
8488
else:
8589
sys.stdout.write("\n")
8690

91+
8792
def process_stdin(link):
8893
"""
8994
Process stdin commands.
@@ -96,7 +101,8 @@ def process_stdin(link):
96101
return False
97102

98103
# Very simple command parser
99-
items = list(csv.reader(cStringIO.StringIO(command.strip()), delimiter=" "))
104+
items = list(
105+
csv.reader(cStringIO.StringIO(command.strip()), delimiter=" "))
100106

101107
if not items:
102108
return
@@ -120,23 +126,22 @@ def process_stdin(link):
120126
elif k == "repeat":
121127
repeat = int(v)
122128
else:
123-
raise ValueError("Unkown option: %s" % key)
129+
raise ValueError("Unkown option: %s" % k)
124130
else:
125131
try:
126-
# Assume it's a float
132+
# Assume it is a float
127133
value = struct.pack(link.endianness + pack, float(item))
128134
except:
129135
try:
130-
# Assume it's an int
131-
value = struct.pack(link.endianness + pack,
132-
int(item, 0))
136+
# Assume it is an int
137+
value = struct.pack(
138+
link.endianness + pack, int(item, 0))
133139
except ValueError:
134-
# Assume as string
140+
# Assume it is a string
135141
value = ""
136142

137143
for b in item:
138-
value += struct.pack(link.endianness + "B",
139-
ord(b))
144+
value += struct.pack(link.endianness + "B", ord(b))
140145

141146
# Concat to frame
142147
frame.data = (frame.data or "") + value
@@ -159,14 +164,16 @@ def process_stdin(link):
159164
sys.stdout.write("Could not send frame: %s\n" % e)
160165
return
161166

167+
162168
def main():
163169
"""
164170
Main entry point.
165171
"""
166172

167173
if serial is None:
168-
sys.stdout.write("TinyLink CLI uses PySerial, but it is not " \
169-
"installed. Please install this first.\n")
174+
sys.stdout.write(
175+
"TinyLink CLI uses PySerial, but it is not installed. Please "
176+
"install this first.\n")
170177
return 1
171178

172179
# Parse arguments
@@ -179,8 +186,8 @@ def main():
179186

180187
# Open serial port and create link
181188
handle = serial.Serial(arguments.port, baudrate=arguments.baudrate)
182-
link = tinylink.TinyLink(handle, max_length=arguments.length,
183-
endianness=endianness)
189+
link = tinylink.TinyLink(
190+
handle, max_length=arguments.length, endianness=endianness)
184191

185192
# Loop until finished
186193
try:
@@ -211,4 +218,4 @@ def main():
211218

212219
# E.g. `python tinylink_cli.py /dev/tty.usbmodem1337 --baudrate 9600'
213220
if __name__ == "__main__":
214-
run()
221+
run()

tinylink/utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
CRC32_POLYNOMIAL = 0xEDB88320L
22
CRC32_INITIAL = 0x00000000L
33

4+
45
def crc32(buf):
56
"""
67
Calculate CRC32 of given input.
@@ -20,13 +21,13 @@ def crc32_value(c):
2021

2122
return ulTemp1 ^ ulCRC
2223

23-
# Execute above function for each byte
24+
# Execute above function for each byte.
2425
for b in buf:
2526
result = crc32_value(b)
2627

27-
# Done
2828
return result
2929

30+
3031
def checksum_header(flags, length):
3132
"""
3233
Calculate checksum over the header.
@@ -39,9 +40,10 @@ def checksum_header(flags, length):
3940

4041
return a ^ b ^ c ^ d
4142

43+
4244
def checksum_frame(data, checksum_header):
4345
"""
4446
Calculate checksum of both the checksum header and the data.
4547
"""
4648

47-
return crc32(data + bytearray([checksum_header])) & 0xffffffff
49+
return crc32(data + bytearray([checksum_header])) & 0xffffffff

0 commit comments

Comments
 (0)