Skip to content

Commit c2c72fc

Browse files
committed
implement some suggested changes
1 parent ce2447a commit c2c72fc

File tree

1 file changed

+42
-25
lines changed

1 file changed

+42
-25
lines changed

pybricksdev/cli/__init__.py

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import os
1111
import sys
1212
from abc import ABC, abstractmethod
13+
from enum import IntEnum
1314
from os import PathLike, path
1415
from tempfile import NamedTemporaryFile
1516
from 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
"\nThe hub has been disconnected. Would you like to re-connect?"
@@ -262,9 +269,9 @@ async def reconnect_hub():
262269
return hub
263270

264271
response_options = [
265-
"Change Target File",
266272
"Recompile and Run",
267273
"Recompile and Download",
274+
"Change Target File",
268275
"Exit",
269276
]
270277
while True:
@@ -284,37 +291,50 @@ async def reconnect_hub():
284291
f"Would you like to re-compile {os.path.basename(args.file.name)}?",
285292
response_options,
286293
default=(
287-
response_options[1]
294+
response_options[ResponseOptions.RECOMPILE_RUN]
288295
if args.start
289-
else response_options[2]
296+
else response_options[
297+
ResponseOptions.RECOMPILE_DOWNLOAD
298+
]
290299
),
291300
).ask_async()
292301
)
293302
)
294303

295-
if response == response_options[0]:
304+
if response == response_options[ResponseOptions.RECOMPILE_RUN]:
305+
with _get_script_path(args.file) as script_path:
306+
await hub.run(script_path, wait=True)
307+
308+
elif (
309+
response == response_options[ResponseOptions.RECOMPILE_DOWNLOAD]
310+
):
311+
with _get_script_path(args.file) as script_path:
312+
await hub.download(script_path)
313+
314+
elif (
315+
response == response_options[ResponseOptions.CHANGE_TARGET_FILE]
316+
):
296317
args.file.close()
297-
args.file = open(
298-
await hub.race_disconnect(
299-
hub.race_power_button_press(
300-
questionary.path(
301-
"What file would you like to use?"
302-
).ask_async()
318+
while True:
319+
try:
320+
args.file = open(
321+
await hub.race_disconnect(
322+
hub.race_power_button_press(
323+
questionary.path(
324+
"What file would you like to use?"
325+
).ask_async()
326+
)
327+
)
303328
)
304-
)
305-
)
306-
307-
with _get_script_path(args.file) as script_path:
329+
break
330+
except FileNotFoundError:
331+
print("The file was not found. Please try again.")
308332
# send the new target file to the hub
309-
if (
310-
response == response_options[0]
311-
or response == response_options[2]
312-
):
333+
with _get_script_path(args.file) as script_path:
313334
await hub.download(script_path)
314-
elif response == response_options[1]:
315-
await hub.run(script_path, wait=True)
316-
else:
317-
return
335+
336+
else:
337+
return
318338

319339
except HubPowerButtonPressedError:
320340
# This means the user pressed the button on the hub to re-start the
@@ -332,9 +352,6 @@ async def reconnect_hub():
332352
await asyncio.sleep(0.3)
333353
hub = await reconnect_hub()
334354

335-
except FileNotFoundError:
336-
print("Your file is invalid.")
337-
338355
finally:
339356
await hub.disconnect()
340357

0 commit comments

Comments
 (0)