Skip to content

Commit 15142a8

Browse files
committed
Make 'devices' default name for device manager in CLI
1 parent e5e6f3c commit 15142a8

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

src/dodal/cli.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ def main(ctx: click.Context) -> None:
4545
"attempt any I/O. Useful as a a dry-run.",
4646
default=False,
4747
)
48-
@click.option("-n", "--name")
49-
def connect(beamline: str, all: bool, sim_backend: bool, name: str | None) -> None:
48+
@click.option("-n", "--name", "device_manager", default="devices")
49+
def connect(beamline: str, all: bool, sim_backend: bool, device_manager: str) -> None:
5050
"""Initialises a beamline module, connects to all devices, reports
5151
any connection issues."""
5252

@@ -70,18 +70,14 @@ def connect(beamline: str, all: bool, sim_backend: bool, name: str | None) -> No
7070
# because the alternatives is handling the fact that only some devices may
7171
# be lazy.
7272

73-
if name:
74-
if (manager := getattr(mod, name, None)) and isinstance(manager, DeviceManager):
75-
devices, instance_exceptions, connect_exceptions = (
76-
manager.build_and_connect(
77-
mock=sim_backend,
78-
)
79-
)
80-
else:
81-
raise ValueError(
82-
f"Name '{name}' could not be found or is not a DeviceManager"
83-
)
73+
if (manager := getattr(mod, device_manager, None)) and isinstance(
74+
manager, DeviceManager
75+
):
76+
devices, instance_exceptions, connect_exceptions = manager.build_and_connect(
77+
mock=sim_backend,
78+
)
8479
else:
80+
print(f"No device manager named '{device_manager}' found in {mod}")
8581
_spoof_path_provider()
8682
devices, instance_exceptions = make_all_devices(
8783
full_module_path,

tests/test_cli.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -298,18 +298,17 @@ def test_cli_connect_when_devices_error(
298298
)
299299

300300

301+
@patch("dodal.cli.importlib")
302+
@patch("dodal.cli.make_all_devices")
303+
@patch("dodal.cli._connect_devices")
301304
@patch.dict(os.environ, clear=True)
302-
def test_missing_device_manager(runner: CliRunner):
303-
with patch("dodal.cli.importlib"):
304-
with pytest.raises(
305-
ValueError,
306-
match="Name 'devices' could not be found or is not a DeviceManager",
307-
):
308-
print("Invoking connect")
309-
runner.invoke(
310-
main, ["connect", "-n", "devices", "i22"], catch_exceptions=False
311-
)
312-
print("Ran connect")
305+
def test_missing_device_manager(connect, make, imp, runner: CliRunner):
306+
# If the device manager cannot be found, it should fall back to the
307+
# make_all_devices + _connect_devices approach.
308+
make.return_value = ({}, {})
309+
runner.invoke(main, ["connect", "-n", "devices", "i22"])
310+
make.assert_called_once()
311+
connect.assert_called_once()
313312

314313

315314
@patch.dict(os.environ, clear=True)

0 commit comments

Comments
 (0)