1+ from typing import Protocol
2+
13from . import consts
24from . import utils
35
46import struct
57
68
7- class Frame (object ):
9+ class Handle (Protocol ):
10+ """
11+ Protocol for a handler.
12+ """
13+
14+ def read (self , size : int ) -> bytes :
15+ """"
16+ Read up to `size` bytes.
17+ """
18+
19+ def write (self , data : bytes ) -> int :
20+ """
21+ Write data and return the number of bytes written.
22+ """
23+
24+
25+ class Frame :
826 """
927 Represents a frame.
1028 """
1129
12- def __init__ (self , data = None , flags = consts .FLAG_NONE , damaged = False ):
30+ data : bytes
31+ flags : int
32+ damaged : int
33+
34+ def __init__ (self , data : bytes = None , flags : int = consts .FLAG_NONE ,
35+ damaged : bool = False ) -> None :
1336 if data is not None :
1437 if type (data ) is not bytes :
1538 raise ValueError ("Provided data must be encoded as bytes." )
@@ -20,12 +43,12 @@ def __init__(self, data=None, flags=consts.FLAG_NONE, damaged=False):
2043 self .flags = flags
2144 self .damaged = damaged
2245
23- def __repr__ (self ):
46+ def __repr__ (self ) -> str :
2447 return "%s(%s, flags=%d, damaged=%s)" % (
2548 self .__class__ .__name__ , repr (self .data ), self .flags , self .damaged )
2649
2750
28- class TinyLink ( object ) :
51+ class TinyLink :
2952 """
3053 TinyLink state machine for streaming communication with low-speed embedded
3154 applications that only use RX/TX. Every message is encapsulated in a frame.
@@ -39,8 +62,14 @@ class TinyLink(object):
3962 It does not provide error correction and the bytes are not aligned.
4063 """
4164
42- def __init__ (self , handle , endianness = consts .LITTLE_ENDIAN ,
43- max_length = 2 ** (consts .LEN_LENGTH * 8 ), ignore_damaged = False ):
65+ handle : Handle
66+ endianness : str
67+ max_length : int
68+ ignore_damaged : bool
69+
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 :
4473 """
4574 Construct a new TinyLink state machine. A state machine takes a handle,
4675 which provides a `read` and `write` method.
@@ -75,7 +104,7 @@ def __init__(self, handle, endianness=consts.LITTLE_ENDIAN,
75104 # Python 2 does not allow unpack from bytearray, but Python 3.
76105 self .buffer = self .stream
77106
78- def write_frame (self , frame ) :
107+ def write_frame (self , frame : Frame ) -> int :
79108 """
80109 Write a frame via the handle.
81110 """
@@ -105,14 +134,14 @@ def write_frame(self, frame):
105134 # Write to file.
106135 return self .handle .write (result )
107136
108- def write (self , data , flags = consts .FLAG_NONE ):
137+ def write (self , data : bytes , flags : int = consts .FLAG_NONE ) -> int :
109138 """
110139 Shorthand for `write_frame(Frame(data, flags=flags))`.
111140 """
112141
113142 return self .write_frame (Frame (data , flags = flags ))
114143
115- def read (self , limit = 1 ) :
144+ def read (self , limit : int = 1 ) -> list [ Frame ] :
116145 """
117146 Read up to `limit` bytes from the handle and process it. Returns a list
118147 of received frames, if any.
0 commit comments