Repository navigation
refactor: store device child tree in BlueapiDevice #1766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
35b0bc4
870f4a2
eb8c951
d1c70d0
8f616bd
5dfa9f0
d821a9a
5f56754
868c2c9
463347e
24e8257
00ab5fd
8ab5de9
2b3c5a7
7434dba
aef42e0
621f32c
2b15178
de6536e
f69b68a
d325aa6
3e891a2
4d70dda
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,5 +1,6 @@ | ||||||
| import logging | ||||||
| import sys | ||||||
| from collections import deque | ||||||
| from collections.abc import Callable | ||||||
| from dataclasses import InitVar, dataclass, field, fields, is_dataclass | ||||||
| from importlib import import_module, metadata | ||||||
|
|
@@ -193,7 +194,7 @@ def find_device(self, addr: str | list[str]) -> Device | None: | |||||
|
|
||||||
| def with_config(self, config: EnvironmentConfig) -> None: | ||||||
| if config.metadata is not None: | ||||||
| self.run_engine.md |= config.metadata.model_dump() | ||||||
| self.run_engine.md.update(config.metadata.model_dump()) | ||||||
|
|
||||||
| package_map = metadata.packages_distributions() | ||||||
| packages = {src.module.split(".")[0] for src in config.sources} | ||||||
|
|
@@ -221,6 +222,7 @@ def with_config(self, config: EnvironmentConfig) -> None: | |||||
| f"{name} in module {mod} is not a device manager" | ||||||
| ) | ||||||
| self.with_device_manager(manager, mock) | ||||||
|
|
||||||
| if not self.devices: | ||||||
| LOGGER.warning( | ||||||
| "Context had no devices after loading environment - are all modules " | ||||||
|
|
@@ -265,8 +267,15 @@ def with_device_manager(self, manager: DeviceManager, mock: bool = False): | |||||
| fixtures = {"path_provider": self.path_provider} if self.path_provider else {} | ||||||
| build_result = manager.build_and_connect(mock=mock, fixtures=fixtures) | ||||||
|
|
||||||
| for device in build_result.devices.values(): | ||||||
| self.register_device(device) | ||||||
| registered_devices = [] | ||||||
| for name, device in build_result.devices.items(): | ||||||
| name = getattr(device, "name", name) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not pass an empty device name to
Map an empty name to Suggested fix
Committable suggestion
|
||||||
| self.register_device( | ||||||
| device, name, include_children=False | ||||||
| ) # include_children=False so only register top-level devices not children | ||||||
| registered_devices.append(name) | ||||||
|
|
||||||
| self._register_children(*(self.devices[name] for name in registered_devices)) | ||||||
|
|
||||||
| if errs := build_result.build_errors: | ||||||
| LOGGER.warning( | ||||||
|
|
@@ -325,27 +334,31 @@ def my_plan(a: int, b: str): | |||||
| ) | ||||||
| return plan | ||||||
|
|
||||||
| def register_device(self, device: Device, name: str | None = None) -> None: | ||||||
| def register_device( | ||||||
| self, device: Device, name: str | None = None, include_children: bool = True | ||||||
| ) -> None: | ||||||
| """ | ||||||
| Register an device in the context. The device needs to be registered with a | ||||||
| name. If the device is Readable, Movable or Flyable it has a `name` | ||||||
| attribute which can be used. The attribute can be overridden with the | ||||||
| `name` parameter here. If the device conforms to a different protocol then | ||||||
| the parameter must be used to name it. | ||||||
|
|
||||||
| Children of ophyd-async devices, including signals, are also registered | ||||||
| under their own names, e.g. registering "stage" also registers "stage-x" | ||||||
| and "stage-x-velocity". If a name is already in use, the existing device | ||||||
| is kept and the new one and its children are not registered. | ||||||
| The include_children flag has been set to True so child devices are also | ||||||
| registered. This function will therefore register for example the top-level | ||||||
| device 'stage' and its children 'stage-x' 'stage-x-velocity'. If a name is | ||||||
| already in use, the existing device is kept and the new one and its children | ||||||
| are not registered. | ||||||
|
|
||||||
| Args: | ||||||
| device (Device): The device to register | ||||||
| name (Optional[str], optional): A name for the device. Defaults to None. | ||||||
| include_children: Flag to decide if child devices should be registered | ||||||
|
|
||||||
| Raises: | ||||||
| KeyError: If no name is found/supplied | ||||||
| ValueError: If the device specified has already been registered. | ||||||
| """ | ||||||
|
|
||||||
| if not is_bluesky_compatible_device(device): | ||||||
| raise TypeError(f"{device} is not a Bluesky compatible device") | ||||||
|
|
||||||
|
|
@@ -355,24 +368,31 @@ def register_device(self, device: Device, name: str | None = None) -> None: | |||||
| else: | ||||||
| raise KeyError(f"Must supply a name for this device: {device}") | ||||||
|
|
||||||
| if self._add_device(name, device): | ||||||
| self._register_children(device) | ||||||
|
|
||||||
| def _register_children(self, device: Device) -> None: | ||||||
| """Register the children of an ophyd-async device under their own names.""" | ||||||
|
|
||||||
| if not isinstance(device, AsyncDevice): | ||||||
| return | ||||||
|
|
||||||
| for _, child in device.children(): | ||||||
| if child.name and self._add_device(child.name, child): | ||||||
| self._register_children(child) | ||||||
|
|
||||||
| def _add_device(self, name: str, device: Device) -> bool: | ||||||
| if added_device := self._add_device(name, device): | ||||||
| if include_children: | ||||||
| self._register_children(added_device) | ||||||
| else: | ||||||
| raise ValueError(f"Device name already registered: {name}") | ||||||
|
|
||||||
| def _register_children(self, *devices: BlueapiDevice) -> None: | ||||||
| """Register the children of every top-level device, | ||||||
| once they are all registered.""" | ||||||
| buffer = deque(devices) | ||||||
| while buffer: | ||||||
| parent = buffer.popleft() | ||||||
| if not isinstance(parent.device, AsyncDevice): | ||||||
| continue | ||||||
| for attr, device in parent.device.children(): | ||||||
| if device.name and (child := self._add_device(device.name, device)): | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep colliding children in the stored tree.
Issue
|
||||||
| child.parent = parent | ||||||
| parent.children[attr] = child | ||||||
| buffer.append(child) | ||||||
|
|
||||||
| def _add_device(self, name: str, device: Device) -> BlueapiDevice | None: | ||||||
| """Add a device unless a different one is already registered as name.""" | ||||||
|
|
||||||
| existing = self.devices.setdefault(name, BlueapiDevice(device)) | ||||||
| if existing.device is not device: | ||||||
| wrapped = self.devices.setdefault(name, BlueapiDevice(name, device)) | ||||||
| if wrapped.device is not device: | ||||||
| # Expected for e.g. a motor's user_readback, which has the motor's name. | ||||||
| # The caller skips its children too, as their names start with this one | ||||||
| if not self._warned_name_clash: | ||||||
|
|
@@ -386,8 +406,8 @@ def _add_device(self, name: str, device: Device) -> bool: | |||||
| device, | ||||||
| name, | ||||||
| ) | ||||||
| return False | ||||||
| return True | ||||||
| return None | ||||||
| return wrapped | ||||||
|
|
||||||
| def unregister_all_devices(self): | ||||||
| """Unregister all devices from the context.""" | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.