Skip to content

Commit 25fb21a

Browse files
committed
Format code using black and isort
1 parent 4adad24 commit 25fb21a

File tree

5 files changed

+81
-51
lines changed

5 files changed

+81
-51
lines changed

tests/test_tinylink.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
23
import tinylink
34

45

@@ -17,7 +18,7 @@ def __init__(self) -> None:
1718
self.length = 0
1819

1920
def read(self, size: int) -> bytes:
20-
data = self.stream[self.index:min(self.length, self.index + size)]
21+
data = self.stream[self.index : min(self.length, self.index + size)]
2122
self.index += len(data)
2223

2324
# Return data.
@@ -48,8 +49,12 @@ def test_basic(self):
4849
size = link.write(message)
4950

5051
self.assertEqual(
51-
size, tinylink.LEN_PREAMBLE + tinylink.LEN_HEADER +
52-
tinylink.LEN_BODY + len(message))
52+
size,
53+
tinylink.LEN_PREAMBLE
54+
+ tinylink.LEN_HEADER
55+
+ tinylink.LEN_BODY
56+
+ len(message),
57+
)
5358

5459
# Read `size` bytes to receive the full frame, test it partially.
5560
link.read(1)
@@ -155,7 +160,7 @@ def test_damaged_a(self):
155160
message = b"Hello, this is a test"
156161

157162
size = link.write(message)
158-
handle.stream[-tinylink.LEN_CRC:] = [0x00] * tinylink.LEN_CRC
163+
handle.stream[-tinylink.LEN_CRC :] = [0x00] * tinylink.LEN_CRC
159164
frames = link.read(size)
160165

161166
self.assertEqual(len(frames), 1)
@@ -173,7 +178,7 @@ def test_damaged_b(self):
173178
message = b"Hello, this is a test"
174179

175180
size = link.write(message)
176-
handle.stream[tinylink.LEN_PREAMBLE+tinylink.LEN_HEADER-1] = 0x00
181+
handle.stream[tinylink.LEN_PREAMBLE + tinylink.LEN_HEADER - 1] = 0x00
177182
frames = link.read(size)
178183

179184
self.assertEqual(len(frames), 0)

tinylink/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from .consts import * # noqa
22
from .link import Frame, TinyLink # noqa
33

4-
__version__ = "2.0.0"
4+
__version__ = "2.0.0"

tinylink/cli.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
import argparse
12
import csv
2-
import sys
3-
import time
43
import select
54
import struct
5+
import sys
6+
import time
7+
from io import StringIO
68
from typing import Optional
7-
import tinylink
8-
import argparse
99

10-
from io import StringIO
10+
import tinylink
1111

1212
try:
1313
import serial
@@ -24,13 +24,17 @@ def parse_arguments(argv: list[str]) -> argparse.Namespace:
2424

2525
# Add options.
2626
parser.add_argument("port", type=str, help="serial port")
27+
parser.add_argument("baudrate", type=int, default=9600, help="serial baudrate")
2728
parser.add_argument(
28-
"baudrate", type=int, default=9600, help="serial baudrate")
29+
"--length", type=int, default=2**16, help="maximum length of frame"
30+
)
2931
parser.add_argument(
30-
"--length", type=int, default=2**16, help="maximum length of frame")
31-
parser.add_argument(
32-
"--endianness", type=str, default="little", choices=["big", "little"],
33-
help="maximum length of frame")
32+
"--endianness",
33+
type=str,
34+
default="little",
35+
choices=["big", "little"],
36+
help="maximum length of frame",
37+
)
3438

3539
# Parse command line.
3640
return parser.parse_args(argv[1:])
@@ -52,7 +56,7 @@ def dump(prefix: str, data: bytes) -> str:
5256
if i + j < length:
5357
b = data[i + j]
5458
hexstr += "%02x " % b
55-
bytestr += bytes((b, )) if 0x20 <= b < 0x7F else b"."
59+
bytestr += bytes((b,)) if 0x20 <= b < 0x7F else b"."
5660
else:
5761
hexstr += " "
5862

@@ -126,13 +130,13 @@ def process_stdin(link: tinylink.TinyLink) -> Optional[bool]:
126130
except:
127131
try:
128132
# Assume it is an int.
129-
value = struct.pack(
130-
link.endianness + pack, int(item, 0))
133+
value = struct.pack(link.endianness + pack, int(item, 0))
131134
except ValueError:
132135
# Assume it is a byte string.
133136
item_bytes = item.encode("ascii")
134137
value = struct.pack(
135-
link.endianness + str(len(item_bytes)) + "s", item_bytes)
138+
link.endianness + str(len(item_bytes)) + "s", item_bytes
139+
)
136140

137141
# Concat to frame.
138142
frame.data = (frame.data or bytes()) + value
@@ -171,7 +175,8 @@ def main(argv: list[str]) -> int:
171175
if serial is None:
172176
sys.stdout.write(
173177
"TinyLink CLI uses PySerial, but it is not installed. Please "
174-
"install this first.\n")
178+
"install this first.\n"
179+
)
175180
return 1
176181

177182
# Parse arguments.
@@ -184,8 +189,7 @@ def main(argv: list[str]) -> int:
184189

185190
# Open serial port and create link.
186191
handle = serial.Serial(arguments.port, baudrate=arguments.baudrate)
187-
link = tinylink.TinyLink(
188-
handle, max_length=arguments.length, endianness=endianness)
192+
link = tinylink.TinyLink(handle, max_length=arguments.length, endianness=endianness)
189193

190194
# Loop until finished.
191195
try:
@@ -214,6 +218,7 @@ def main(argv: list[str]) -> int:
214218
# Done.
215219
return 0
216220

221+
217222
# E.g. `python cli.py /dev/tty.usbmodem1337 --baudrate 9600`.
218223
if __name__ == "__main__":
219224
run()

tinylink/link.py

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1+
import struct
12
from typing import Protocol
23

3-
from . import consts
4-
from . import utils
5-
6-
import struct
4+
from . import consts, utils
75

86

97
class Handle(Protocol):
@@ -12,7 +10,7 @@ class Handle(Protocol):
1210
"""
1311

1412
def read(self, size: int) -> bytes:
15-
""""
13+
""" "
1614
Read up to `size` bytes.
1715
"""
1816

@@ -31,8 +29,9 @@ class Frame:
3129
flags: int
3230
damaged: int
3331

34-
def __init__(self, data: bytes = None, flags: int = consts.FLAG_NONE,
35-
damaged: bool = False) -> None:
32+
def __init__(
33+
self, data: bytes = None, flags: int = consts.FLAG_NONE, damaged: bool = False
34+
) -> None:
3635
if data is not None:
3736
if type(data) is not bytes:
3837
raise ValueError("Provided data must be encoded as bytes.")
@@ -45,7 +44,11 @@ def __init__(self, data: bytes = None, flags: int = consts.FLAG_NONE,
4544

4645
def __repr__(self) -> str:
4746
return "%s(%s, flags=%d, damaged=%s)" % (
48-
self.__class__.__name__, repr(self.data), self.flags, self.damaged)
47+
self.__class__.__name__,
48+
repr(self.data),
49+
self.flags,
50+
self.damaged,
51+
)
4952

5053

5154
class TinyLink:
@@ -67,9 +70,13 @@ class TinyLink:
6770
max_length: int
6871
ignore_damaged: bool
6972

70-
def __init__(self, handle: Handle, endianness: str = consts.LITTLE_ENDIAN,
71-
max_length: int = 2**(consts.LEN_LENGTH * 8),
72-
ignore_damaged: bool = False) -> None:
73+
def __init__(
74+
self,
75+
handle: Handle,
76+
endianness: str = consts.LITTLE_ENDIAN,
77+
max_length: int = 2 ** (consts.LEN_LENGTH * 8),
78+
ignore_damaged: bool = False,
79+
) -> None:
7380
"""
7481
Construct a new TinyLink state machine. A state machine takes a handle,
7582
which provides a `read` and `write` method.
@@ -115,21 +122,25 @@ def write_frame(self, frame: Frame) -> int:
115122
# Check length of message.
116123
if length > self.max_length:
117124
raise ValueError(
118-
"Message length %d exceeds max length %d" % (
119-
length, self.max_length))
125+
"Message length %d exceeds max length %d" % (length, self.max_length)
126+
)
120127

121128
# Pack header.
122129
checksum_header = utils.checksum_header(frame.flags, length)
123130
result += struct.pack(
124-
self.endianness + "IHHB", consts.PREAMBLE, frame.flags, length,
125-
checksum_header)
131+
self.endianness + "IHHB",
132+
consts.PREAMBLE,
133+
frame.flags,
134+
length,
135+
checksum_header,
136+
)
126137

127138
# Pack data.
128139
if frame.data is not None:
129140
checksum_frame = utils.checksum_frame(frame.data, checksum_header)
130141
result += struct.pack(
131-
self.endianness + str(length) + "sI", frame.data,
132-
checksum_frame)
142+
self.endianness + str(length) + "sI", frame.data, checksum_frame
143+
)
133144

134145
# Write to file.
135146
return self.handle.write(result)
@@ -164,14 +175,18 @@ def read(self, limit: int = 1) -> list[Frame]:
164175
# Decide what to do.
165176
if self.state == consts.WAITING_FOR_PREAMBLE:
166177
if self.index >= consts.LEN_PREAMBLE:
167-
start, = struct.unpack_from(
168-
self.endianness + "I", self.buffer, self.index - 4)
178+
(start,) = struct.unpack_from(
179+
self.endianness + "I", self.buffer, self.index - 4
180+
)
169181

170182
if start == consts.PREAMBLE:
171183
# Advance to next state.
172184
self.index = 0
173185
self.state = consts.WAITING_FOR_HEADER
174-
elif self.index == self.max_length + consts.LEN_HEADER + consts.LEN_BODY:
186+
elif (
187+
self.index
188+
== self.max_length + consts.LEN_HEADER + consts.LEN_BODY
189+
):
175190
# Preamble not found and stream is full. Copy last four
176191
# bytes, because the next byte may form the preamble
177192
# together with the last three bytes.
@@ -181,11 +196,14 @@ def read(self, limit: int = 1) -> list[Frame]:
181196
elif self.state == consts.WAITING_FOR_HEADER:
182197
if self.index == consts.LEN_HEADER:
183198
flags, length, checksum = struct.unpack_from(
184-
self.endianness + "HHB", self.buffer)
199+
self.endianness + "HHB", self.buffer
200+
)
185201

186202
# Verify checksum.
187-
if checksum == utils.checksum_header(flags, length) and \
188-
length <= self.max_length:
203+
if (
204+
checksum == utils.checksum_header(flags, length)
205+
and length <= self.max_length
206+
):
189207

190208
if length > 0:
191209
self.state = consts.WAITING_FOR_BODY
@@ -203,13 +221,16 @@ def read(self, limit: int = 1) -> list[Frame]:
203221
elif self.state == consts.WAITING_FOR_BODY:
204222
# Unpack header.
205223
flags, length, checksum_a = struct.unpack_from(
206-
self.endianness + "HHB", self.buffer)
224+
self.endianness + "HHB", self.buffer
225+
)
207226

208227
if self.index == consts.LEN_HEADER + length + consts.LEN_CRC:
209228
# Unpack body.
210229
result, checksum_b = struct.unpack_from(
211230
self.endianness + str(length) + "sI",
212-
self.buffer, consts.LEN_HEADER)
231+
self.buffer,
232+
consts.LEN_HEADER,
233+
)
213234

214235
# Verify checksum.
215236
if checksum_b == utils.checksum_frame(result, checksum_a):

tinylink/utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def crc32(buf):
1111

1212
def crc32_value(c):
1313
ulTemp1 = (result >> 8) & 0x00FFFFFF
14-
ulCRC = (result ^ c) & 0xff
14+
ulCRC = (result ^ c) & 0xFF
1515

1616
for i in range(8):
1717
if ulCRC & 0x01:
@@ -46,5 +46,4 @@ def checksum_frame(data, checksum_header):
4646
Calculate checksum of both the checksum header and the data.
4747
"""
4848

49-
return crc32(
50-
memoryview(data).tobytes() + bytearray([checksum_header])) & 0xFFFFFFFF
49+
return crc32(memoryview(data).tobytes() + bytearray([checksum_header])) & 0xFFFFFFFF

0 commit comments

Comments
 (0)