Skip to content

Commit

Permalink
added indexing to handle tuple (#468)
Browse files Browse the repository at this point in the history
closes #444

With PR for dodal at
DiamondLightSource/dodal#550

---------

Co-authored-by: Callum Forrester <[email protected]>
  • Loading branch information
ZohebShaikh and callumforrester authored May 31, 2024
1 parent 49dfb08 commit 8bb9388
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
19 changes: 13 additions & 6 deletions src/blueapi/core/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
from typing import Any, Generic, TypeVar, Union, get_args, get_origin, get_type_hints

from bluesky.run_engine import RunEngine
from dodal.utils import make_all_devices
from ophyd_async.core import NotConnected
from pydantic import create_model
from pydantic.fields import FieldInfo, ModelField

from blueapi.config import EnvironmentConfig, SourceKind
from blueapi.utils import (
BlueapiPlanModelConfig,
load_module_all,
)
from blueapi.utils import BlueapiPlanModelConfig, load_module_all

from .bluesky_types import (
BLUESKY_PROTOCOLS,
Expand Down Expand Up @@ -104,11 +103,19 @@ def with_device_module(self, module: ModuleType) -> None:
self.with_dodal_module(module)

def with_dodal_module(self, module: ModuleType, **kwargs) -> None:
from dodal.utils import make_all_devices
devices, exceptions = make_all_devices(module, **kwargs)

for device in make_all_devices(module, **kwargs).values():
for device in devices.values():
self.device(device)

# If exceptions have occurred, we log them but we do not make blueapi
# fall over
if len(exceptions) > 0:
LOGGER.warning(
f"{len(exceptions)} exceptions occurred while instantiating devices"
)
LOGGER.exception(NotConnected(exceptions))

def plan(self, plan: PlanGenerator) -> PlanGenerator:
"""
Register the argument as a plan in the context. Can be used as a decorator e.g.
Expand Down
7 changes: 7 additions & 0 deletions src/blueapi/startup/example_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,10 @@ def current_det(
Imax=1,
labels={"detectors"},
)


def unplugged_motor(name="unplugged_motor") -> SynAxisWithMotionEvents:
raise TimeoutError(
"This device is supposed to fail, blueapi "
"will mark it as not present and start up regardless"
)
5 changes: 5 additions & 0 deletions tests/core/fake_device_module_failing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from ophyd import EpicsMotor


def failing_device() -> EpicsMotor:
raise TimeoutError("FooBar")
19 changes: 18 additions & 1 deletion tests/core/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from dls_bluesky_core.core import MsgGenerator, PlanGenerator, inject
from ophyd.sim import SynAxis, SynGauss
from pydantic import ValidationError, parse_obj_as
from pytest import LogCaptureFixture

from blueapi.config import EnvironmentConfig, Source, SourceKind
from blueapi.core import BlueskyContext, is_bluesky_compatible_device
Expand Down Expand Up @@ -174,6 +175,19 @@ def test_add_devices_from_module(empty_context: BlueskyContext) -> None:
} == empty_context.devices.keys()


def test_add_failing_deivces_from_module(
caplog: LogCaptureFixture, empty_context: BlueskyContext
) -> None:
import tests.core.fake_device_module_failing as device_module

caplog.set_level(10)
empty_context.with_device_module(device_module)
logs = caplog.get_records("call")

assert any("TimeoutError: FooBar" in log.message for log in logs)
assert len(empty_context.devices.keys()) == 0


def test_extra_kwargs_in_with_dodal_module_passed_to_make_all_devices(
empty_context: BlueskyContext,
) -> None:
Expand All @@ -182,7 +196,10 @@ def test_extra_kwargs_in_with_dodal_module_passed_to_make_all_devices(
"""
import tests.core.fake_device_module as device_module

with patch("dodal.utils.make_all_devices") as mock_make_all_devices:
with patch(
"blueapi.core.context.make_all_devices",
return_value=({}, {}),
) as mock_make_all_devices:
empty_context.with_dodal_module(
device_module, some_argument=1, another_argument="two"
)
Expand Down

0 comments on commit 8bb9388

Please sign in to comment.