Skip to content

Commit f116335

Browse files
committed
cli: De-duplicate download code.
This is just downloading without starting it. For example, this got out of sync in 3c9a677, so it is better to just not duplicate everything.
1 parent c5ce71e commit f116335

File tree

2 files changed

+17
-89
lines changed

2 files changed

+17
-89
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
### Fixed
1313
- Fixed calling `PybricksHub.write()` methods.
1414

15+
### Changed
16+
- Downloading programs without starting them can now be done by
17+
adding `--no-start` to the run tool.
18+
1519
### Removed
1620
- Removed `REPLHub`. This was used for non-Pybricks MicroPython boards, but
1721
`mpremote` should be used for this.

pybricksdev/cli/__init__.py

Lines changed: 13 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -148,100 +148,21 @@ def add_parser(self, subparsers: argparse._SubParsersAction):
148148
)
149149

150150
parser.add_argument(
151-
"--wait",
152-
help="wait for the program to complete before disconnecting",
151+
"--start",
152+
help="Start the program immediately after downloading it.",
153153
action=argparse.BooleanOptionalAction,
154154
default=True,
155155
)
156156

157-
async def run(self, args: argparse.Namespace):
158-
159-
# Pick the right connection
160-
if args.conntype == "ble":
161-
from pybricksdev.ble import find_device as find_ble
162-
from pybricksdev.connections.pybricks import PybricksHubBLE
163-
164-
# It is a Pybricks Hub with BLE. Device name or address is given.
165-
print(f"Searching for {args.name or 'any hub with Pybricks service'}...")
166-
device_or_address = await find_ble(args.name)
167-
hub = PybricksHubBLE(device_or_address)
168-
elif args.conntype == "usb":
169-
from usb.core import find as find_usb
170-
171-
from pybricksdev.connections.pybricks import PybricksHubUSB
172-
from pybricksdev.usb import (
173-
EV3_USB_PID,
174-
LEGO_USB_VID,
175-
MINDSTORMS_INVENTOR_USB_PID,
176-
NXT_USB_PID,
177-
SPIKE_ESSENTIAL_USB_PID,
178-
SPIKE_PRIME_USB_PID,
179-
)
180-
181-
def is_pybricks_usb(dev):
182-
return (
183-
(dev.idVendor == LEGO_USB_VID)
184-
and (
185-
dev.idProduct
186-
in [
187-
NXT_USB_PID,
188-
EV3_USB_PID,
189-
SPIKE_PRIME_USB_PID,
190-
SPIKE_ESSENTIAL_USB_PID,
191-
MINDSTORMS_INVENTOR_USB_PID,
192-
]
193-
)
194-
and dev.product.endswith("Pybricks")
195-
)
196-
197-
device_or_address = find_usb(custom_match=is_pybricks_usb)
198-
199-
if device_or_address is None:
200-
print("Pybricks Hub not found.", file=sys.stderr)
201-
exit(1)
202-
203-
hub = PybricksHubUSB(device_or_address)
204-
else:
205-
raise ValueError(f"Unknown connection type: {args.conntype}")
206-
207-
# Connect to the address and run the script
208-
await hub.connect()
209-
try:
210-
with _get_script_path(args.file) as script_path:
211-
await hub.run(script_path, args.wait)
212-
finally:
213-
await hub.disconnect()
214-
215-
216-
class Download(Tool):
217-
def add_parser(self, subparsers: argparse._SubParsersAction):
218-
parser = subparsers.add_parser(
219-
"download",
220-
help="upload a Pybricks program without running it",
221-
)
222-
parser.tool = self
223157
parser.add_argument(
224-
"conntype",
225-
metavar="<connection type>",
226-
help="connection type: %(choices)s",
227-
choices=["ble", "usb"],
228-
)
229-
parser.add_argument(
230-
"file",
231-
metavar="<file>",
232-
help="path to a MicroPython script or `-` for stdin",
233-
type=argparse.FileType(),
234-
)
235-
parser.add_argument(
236-
"-n",
237-
"--name",
238-
metavar="<name>",
239-
required=False,
240-
help="Bluetooth device name or Bluetooth address for BLE connection; "
241-
"serial port name for USB connection",
158+
"--wait",
159+
help="Wait for the program to complete before disconnecting. Only applies when starting program right away.",
160+
action=argparse.BooleanOptionalAction,
161+
default=True,
242162
)
243163

244164
async def run(self, args: argparse.Namespace):
165+
245166
# Pick the right connection
246167
if args.conntype == "ble":
247168
from pybricksdev.ble import find_device as find_ble
@@ -290,11 +211,14 @@ def is_pybricks_usb(dev):
290211
else:
291212
raise ValueError(f"Unknown connection type: {args.conntype}")
292213

293-
# Connect to the address and upload the script without running it
214+
# Connect to the address and run the script
294215
await hub.connect()
295216
try:
296217
with _get_script_path(args.file) as script_path:
297-
await hub.download(script_path)
218+
if args.start:
219+
await hub.run(script_path, args.wait)
220+
else:
221+
await hub.download(script_path)
298222
finally:
299223
await hub.disconnect()
300224

@@ -522,7 +446,7 @@ def main():
522446
help="the tool to use",
523447
)
524448

525-
for tool in Compile(), Run(), Download(), Flash(), DFU(), OAD(), LWP3(), Udev():
449+
for tool in Compile(), Run(), Flash(), DFU(), OAD(), LWP3(), Udev():
526450
tool.add_parser(subparsers)
527451

528452
argcomplete.autocomplete(parser)

0 commit comments

Comments
 (0)