|
| 1 | +"""Wrap a plan with temporary modification to Periods Settings.""" |
| 2 | + |
| 3 | +from collections.abc import Generator |
| 4 | + |
| 5 | +import bluesky.plan_stubs as bps |
| 6 | +import bluesky.preprocessors as bpp |
| 7 | +from bluesky.utils import Msg |
| 8 | +from ophyd_async.plan_stubs import ensure_connected |
| 9 | + |
| 10 | +from ibex_bluesky_core.devices.dae import Dae |
| 11 | + |
| 12 | + |
| 13 | +def with_num_periods( |
| 14 | + plan: Generator[Msg, None, None], dae: Dae, number_of_periods: int |
| 15 | +) -> Generator[Msg, None, None]: |
| 16 | + """Wrap a plan with temporary modification to Periods Settings. |
| 17 | +
|
| 18 | + Args: |
| 19 | + plan: The plan to wrap. |
| 20 | + dae: The Dae instance. |
| 21 | + number_of_periods: The number of periods to set to temporarily. |
| 22 | +
|
| 23 | + Returns: |
| 24 | + A generator which runs the plan with the modified number of periods, restoring the original |
| 25 | + number of periods afterwards. |
| 26 | +
|
| 27 | + """ |
| 28 | + original_num_periods = None |
| 29 | + |
| 30 | + def _inner() -> Generator[Msg, None, None]: |
| 31 | + yield from ensure_connected(dae) |
| 32 | + nonlocal original_num_periods |
| 33 | + original_num_periods = yield from bps.rd(dae.number_of_periods) |
| 34 | + |
| 35 | + yield from bps.mv(dae.number_of_periods, number_of_periods) |
| 36 | + |
| 37 | + return (yield from plan) |
| 38 | + |
| 39 | + def _cleanup() -> Generator[Msg, None, None]: |
| 40 | + yield from bps.mv(dae.number_of_periods, original_num_periods) |
| 41 | + |
| 42 | + return (yield from bpp.finalize_wrapper(_inner(), _cleanup())) |
0 commit comments