1010import os
1111import sys
1212from abc import ABC , abstractmethod
13+ from enum import IntEnum
1314from os import PathLike , path
1415from tempfile import NamedTemporaryFile
1516from typing import ContextManager , TextIO
@@ -239,6 +240,12 @@ def is_pybricks_usb(dev):
239240 if not args .stay_connected :
240241 return
241242
243+ class ResponseOptions (IntEnum ):
244+ RECOMPILE_RUN = 0
245+ RECOMPILE_DOWNLOAD = 1
246+ CHANGE_TARGET_FILE = 2
247+ EXIT = 3
248+
242249 async def reconnect_hub ():
243250 if not await questionary .confirm (
244251 "\n The hub has been disconnected. Would you like to re-connect?"
@@ -264,6 +271,7 @@ async def reconnect_hub():
264271 response_options = [
265272 "Recompile and Run" ,
266273 "Recompile and Download" ,
274+ "Change Target File" ,
267275 "Exit" ,
268276 ]
269277 while True :
@@ -280,22 +288,50 @@ async def reconnect_hub():
280288 response = await hub .race_disconnect (
281289 hub .race_power_button_press (
282290 questionary .select (
283- "Would you like to re-compile your code ?" ,
291+ f "Would you like to re-compile { os . path . basename ( args . file . name ) } ?" ,
284292 response_options ,
285293 default = (
286- response_options [0 ]
294+ response_options [ResponseOptions . RECOMPILE_RUN ]
287295 if args .start
288- else response_options [1 ]
296+ else response_options [
297+ ResponseOptions .RECOMPILE_DOWNLOAD
298+ ]
289299 ),
290300 ).ask_async ()
291301 )
292302 )
293- with _get_script_path (args .file ) as script_path :
294- if response == response_options [0 ]:
295- await hub .run (script_path , wait = True )
296- elif response == response_options [1 ]:
297- await hub .download (script_path )
298- else :
303+
304+ match response_options .index (response ):
305+
306+ case ResponseOptions .RECOMPILE_RUN :
307+ with _get_script_path (args .file ) as script_path :
308+ await hub .run (script_path , wait = True )
309+
310+ case ResponseOptions .RECOMPILE_DOWNLOAD :
311+ with _get_script_path (args .file ) as script_path :
312+ await hub .download (script_path )
313+
314+ case ResponseOptions .CHANGE_TARGET_FILE :
315+ args .file .close ()
316+ while True :
317+ try :
318+ args .file = open (
319+ await hub .race_disconnect (
320+ hub .race_power_button_press (
321+ questionary .path (
322+ "What file would you like to use?"
323+ ).ask_async ()
324+ )
325+ )
326+ )
327+ break
328+ except FileNotFoundError :
329+ print ("The file was not found. Please try again." )
330+ # send the new target file to the hub
331+ with _get_script_path (args .file ) as script_path :
332+ await hub .download (script_path )
333+
334+ case _:
299335 return
300336
301337 except HubPowerButtonPressedError :
0 commit comments