22
22
from adafruit_ble .uuid import VendorUUID , StandardUUID
23
23
from adafruit_ble .services import Service
24
24
25
+ try :
26
+ from typing import Optional , List
27
+ from circuitpython_typing import WriteableBuffer , ReadableBuffer
28
+ except ImportError :
29
+ pass
30
+
25
31
__version__ = "0.0.0-auto.0"
26
32
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE_File_Transfer.git"
27
33
@@ -33,7 +39,7 @@ class FileTransferUUID(VendorUUID):
33
39
34
40
# pylint: disable=too-few-public-methods
35
41
36
- def __init__ (self , uuid16 ) :
42
+ def __init__ (self , uuid16 : int ) -> None :
37
43
uuid128 = bytearray ("refsnarTeliF" .encode ("utf-8" ) + b"\x00 \x00 \xaf \xad " )
38
44
uuid128 [- 3 ] = uuid16 >> 8
39
45
uuid128 [- 4 ] = uuid16 & 0xFF
@@ -48,7 +54,7 @@ class _TransferCharacteristic(ComplexCharacteristic):
48
54
49
55
uuid = FileTransferUUID (0x0200 )
50
56
51
- def __init__ (self ):
57
+ def __init__ (self ) -> None :
52
58
super ().__init__ (
53
59
properties = Characteristic .WRITE_NO_RESPONSE
54
60
| Characteristic .READ
@@ -59,7 +65,7 @@ def __init__(self):
59
65
fixed_length = False ,
60
66
)
61
67
62
- def bind (self , service ) :
68
+ def bind (self , service : Service ) -> _bleio . PacketBuffer :
63
69
"""Binds the characteristic to the given Service."""
64
70
bound_characteristic = super ().bind (service )
65
71
return _bleio .PacketBuffer (
@@ -117,13 +123,13 @@ class ProtocolError(BaseException):
117
123
class FileTransferClient :
118
124
"""Helper class to communicating with a File Transfer server"""
119
125
120
- def __init__ (self , service ) :
126
+ def __init__ (self , service : Service ) -> None :
121
127
self ._service = service
122
128
123
129
if service .version < 3 :
124
130
raise RuntimeError ("Service on other device too old" )
125
131
126
- def _write (self , buffer ) :
132
+ def _write (self , buffer : ReadableBuffer ) -> None :
127
133
# print("write", binascii.hexlify(buffer))
128
134
sent = 0
129
135
while sent < len (buffer ):
@@ -132,7 +138,7 @@ def _write(self, buffer):
132
138
self ._service .raw .write (buffer [sent : sent + next_send ])
133
139
sent += next_send
134
140
135
- def _readinto (self , buffer ) :
141
+ def _readinto (self , buffer : WriteableBuffer ) -> bytearray :
136
142
read = 0
137
143
long_buffer = bytearray (512 )
138
144
# Read back how much we can write
@@ -144,7 +150,7 @@ def _readinto(self, buffer):
144
150
buffer [:read ] = long_buffer [:read ]
145
151
return read
146
152
147
- def read (self , path , * , offset = 0 ) :
153
+ def read (self , path : str , * , offset : int = 0 ) -> bytearray :
148
154
"""Returns the contents of the file at the given path starting at the given offset"""
149
155
# pylint: disable=too-many-locals
150
156
path = path .encode ("utf-8" )
@@ -210,7 +216,14 @@ def read(self, path, *, offset=0):
210
216
self ._write (encoded )
211
217
return buf
212
218
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 :
214
227
"""Writes the given contents to the given path starting at the given offset.
215
228
Returns the trunctated modification time.
216
229
@@ -273,7 +286,7 @@ def write(self, path, contents, *, offset=0, modification_time=None):
273
286
raise ProtocolError ()
274
287
return truncated_time
275
288
276
- def mkdir (self , path , modification_time = None ):
289
+ def mkdir (self , path : str , modification_time : Optional [ int ] = None ) -> int :
277
290
"""Makes the directory and any missing parents. Returns the truncated time"""
278
291
path = path .encode ("utf-8" )
279
292
if modification_time is None :
@@ -295,7 +308,7 @@ def mkdir(self, path, modification_time=None):
295
308
raise ValueError ("Invalid path" )
296
309
return truncated_time
297
310
298
- def listdir (self , path ) :
311
+ def listdir (self , path : str ) -> List [ tuple ] :
299
312
"""Returns a list of tuples, one tuple for each file or directory in the given path"""
300
313
# pylint: disable=too-many-locals
301
314
paths = []
@@ -346,7 +359,7 @@ def listdir(self, path):
346
359
offset += path_read
347
360
return paths
348
361
349
- def delete (self , path ) :
362
+ def delete (self , path : str ) -> None :
350
363
"""Deletes the file or directory at the given path."""
351
364
path = path .encode ("utf-8" )
352
365
encoded = struct .pack ("<BxH" , FileTransferService .DELETE , len (path )) + path
@@ -360,7 +373,7 @@ def delete(self, path):
360
373
if status != FileTransferService .OK :
361
374
raise ValueError ("Missing file" )
362
375
363
- def move (self , old_path , new_path ) :
376
+ def move (self , old_path : str , new_path : str ) -> None :
364
377
"""Moves the file or directory from old_path to new_path."""
365
378
if self ._service .version < 4 :
366
379
raise RuntimeError ("Service on other device too old" )
0 commit comments