|
2 | 2 |
|
3 | 3 | import asyncio |
4 | 4 | import functools |
| 5 | +import inspect |
5 | 6 | import time |
6 | 7 | from collections.abc import AsyncGenerator, Awaitable, Callable |
7 | 8 | from typing import Any, Generic, cast |
8 | 9 |
|
9 | 10 | from bluesky.protocols import ( |
| 11 | + Configurable, |
10 | 12 | Locatable, |
11 | 13 | Location, |
12 | 14 | Movable, |
@@ -678,6 +680,36 @@ def walk_rw_signals(device: Device, path_prefix: str = "") -> dict[str, SignalRW |
678 | 680 | return signals |
679 | 681 |
|
680 | 682 |
|
| 683 | +async def walk_config_signals( |
| 684 | + device: Device, path_prefix: str = "" |
| 685 | +) -> dict[str, SignalRW[Any]]: |
| 686 | + """Retrieve all configuration signals from a device. |
| 687 | +
|
| 688 | + Stores retrieved signals with their dotted attribute paths in a dictionary. Used as |
| 689 | + part of saving and loading a device. |
| 690 | +
|
| 691 | + :param device: Device to retrieve configuration signals from. |
| 692 | + :param path_prefix: For internal use, leave blank when calling the method. |
| 693 | + :return: |
| 694 | + A dictionary matching the string attribute path of a SignalRW with the |
| 695 | + signal itself. |
| 696 | + """ |
| 697 | + signals: dict[str, SignalRW[Any]] = {} |
| 698 | + config_names: list[str] = [] |
| 699 | + if isinstance(device, Configurable): |
| 700 | + configuration = device.read_configuration() |
| 701 | + if inspect.isawaitable(configuration): |
| 702 | + configuration = await configuration |
| 703 | + config_names = list(configuration.keys()) |
| 704 | + for attr_name, attr in device.children(): |
| 705 | + dot_path = f"{path_prefix}{attr_name}" |
| 706 | + if isinstance(attr, SignalRW) and attr.name in config_names: |
| 707 | + signals[dot_path] = attr |
| 708 | + signals.update(await walk_config_signals(attr, path_prefix=dot_path + ".")) |
| 709 | + |
| 710 | + return signals |
| 711 | + |
| 712 | + |
681 | 713 | class Ignore: |
682 | 714 | """Annotation to ignore a signal when connecting a device.""" |
683 | 715 |
|
|
0 commit comments