2222from adafruit_ble .uuid import VendorUUID , StandardUUID
2323from adafruit_ble .services import Service
2424
25+ try :
26+ from typing import Optional , List
27+ from circuitpython_typing import WriteableBuffer , ReadableBuffer
28+ except ImportError :
29+ pass
30+
2531__version__ = "0.0.0-auto.0"
2632__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE_File_Transfer.git"
2733
@@ -33,7 +39,7 @@ class FileTransferUUID(VendorUUID):
3339
3440 # pylint: disable=too-few-public-methods
3541
36- def __init__ (self , uuid16 ) :
42+ def __init__ (self , uuid16 : int ) -> None :
3743 uuid128 = bytearray ("refsnarTeliF" .encode ("utf-8" ) + b"\x00 \x00 \xaf \xad " )
3844 uuid128 [- 3 ] = uuid16 >> 8
3945 uuid128 [- 4 ] = uuid16 & 0xFF
@@ -48,7 +54,7 @@ class _TransferCharacteristic(ComplexCharacteristic):
4854
4955 uuid = FileTransferUUID (0x0200 )
5056
51- def __init__ (self ):
57+ def __init__ (self ) -> None :
5258 super ().__init__ (
5359 properties = Characteristic .WRITE_NO_RESPONSE
5460 | Characteristic .READ
@@ -59,7 +65,7 @@ def __init__(self):
5965 fixed_length = False ,
6066 )
6167
62- def bind (self , service ) :
68+ def bind (self , service : Service ) -> _bleio . PacketBuffer :
6369 """Binds the characteristic to the given Service."""
6470 bound_characteristic = super ().bind (service )
6571 return _bleio .PacketBuffer (
@@ -117,13 +123,13 @@ class ProtocolError(BaseException):
117123class FileTransferClient :
118124 """Helper class to communicating with a File Transfer server"""
119125
120- def __init__ (self , service ) :
126+ def __init__ (self , service : Service ) -> None :
121127 self ._service = service
122128
123129 if service .version < 3 :
124130 raise RuntimeError ("Service on other device too old" )
125131
126- def _write (self , buffer ) :
132+ def _write (self , buffer : ReadableBuffer ) -> None :
127133 # print("write", binascii.hexlify(buffer))
128134 sent = 0
129135 while sent < len (buffer ):
@@ -132,7 +138,7 @@ def _write(self, buffer):
132138 self ._service .raw .write (buffer [sent : sent + next_send ])
133139 sent += next_send
134140
135- def _readinto (self , buffer ) :
141+ def _readinto (self , buffer : WriteableBuffer ) -> bytearray :
136142 read = 0
137143 long_buffer = bytearray (512 )
138144 # Read back how much we can write
@@ -144,7 +150,7 @@ def _readinto(self, buffer):
144150 buffer [:read ] = long_buffer [:read ]
145151 return read
146152
147- def read (self , path , * , offset = 0 ) :
153+ def read (self , path : str , * , offset : int = 0 ) -> bytearray :
148154 """Returns the contents of the file at the given path starting at the given offset"""
149155 # pylint: disable=too-many-locals
150156 path = path .encode ("utf-8" )
@@ -210,7 +216,14 @@ def read(self, path, *, offset=0):
210216 self ._write (encoded )
211217 return buf
212218
213- def write (self , path , contents , * , offset = 0 , modification_time = None ):
219+ def write (
220+ self ,
221+ path : str ,
222+ contents : bytearray ,
223+ * ,
224+ offset : int = 0 ,
225+ modification_time : Optional [int ] = None ,
226+ ) -> int :
214227 """Writes the given contents to the given path starting at the given offset.
215228 Returns the trunctated modification time.
216229
@@ -273,7 +286,7 @@ def write(self, path, contents, *, offset=0, modification_time=None):
273286 raise ProtocolError ()
274287 return truncated_time
275288
276- def mkdir (self , path , modification_time = None ):
289+ def mkdir (self , path : str , modification_time : Optional [ int ] = None ) -> int :
277290 """Makes the directory and any missing parents. Returns the truncated time"""
278291 path = path .encode ("utf-8" )
279292 if modification_time is None :
@@ -295,7 +308,7 @@ def mkdir(self, path, modification_time=None):
295308 raise ValueError ("Invalid path" )
296309 return truncated_time
297310
298- def listdir (self , path ) :
311+ def listdir (self , path : str ) -> List [ tuple ] :
299312 """Returns a list of tuples, one tuple for each file or directory in the given path"""
300313 # pylint: disable=too-many-locals
301314 paths = []
@@ -346,7 +359,7 @@ def listdir(self, path):
346359 offset += path_read
347360 return paths
348361
349- def delete (self , path ) :
362+ def delete (self , path : str ) -> None :
350363 """Deletes the file or directory at the given path."""
351364 path = path .encode ("utf-8" )
352365 encoded = struct .pack ("<BxH" , FileTransferService .DELETE , len (path )) + path
@@ -360,7 +373,7 @@ def delete(self, path):
360373 if status != FileTransferService .OK :
361374 raise ValueError ("Missing file" )
362375
363- def move (self , old_path , new_path ) :
376+ def move (self , old_path : str , new_path : str ) -> None :
364377 """Moves the file or directory from old_path to new_path."""
365378 if self ._service .version < 4 :
366379 raise RuntimeError ("Service on other device too old" )
0 commit comments