1+ import struct
12from typing import Protocol
23
3- from . import consts
4- from . import utils
5-
6- import struct
4+ from . import consts , utils
75
86
97class 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
5154class 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 ):
0 commit comments