Skip to content

Commit

Permalink
renamed pre_initialized blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
evalott100 committed Apr 17, 2024
1 parent f655b2a commit c4d8e04
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/ophyd_async/epics/pvi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .pvi import PVIEntry, fill_pvi_entries, pre_initialize_blocks
from .pvi import PVIEntry, create_children_from_annotations, fill_pvi_entries

__all__ = ["PVIEntry", "fill_pvi_entries", "pre_initialize_blocks"]
__all__ = ["PVIEntry", "fill_pvi_entries", "create_children_from_annotations"]
24 changes: 8 additions & 16 deletions src/ophyd_async/epics/pvi/pvi.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,10 @@ def _verify_common_blocks(entry: PVIEntry, common_device: Type[Device]):
continue
assert entry.sub_entries
device_t, is_optional = _strip_union(sub_device)
if sub_name not in entry.sub_entries:
if is_optional:
continue
else:
raise RuntimeError(
f"sub device `{sub_name}:{type(sub_device)}` "
"was not provided by pvi"
)
if sub_name not in entry.sub_entries and not is_optional:
raise RuntimeError(
f"sub device `{sub_name}:{type(sub_device)}` " "was not provided by pvi"
)
if isinstance(entry.sub_entries[sub_name], dict):
for sub_sub_entry in entry.sub_entries[sub_name].values(): # type: ignore
_verify_common_blocks(sub_sub_entry, sub_device) # type: ignore
Expand Down Expand Up @@ -304,19 +300,15 @@ async def fill_pvi_entries(
device.set_name(device.name)


def pre_initialize_blocks(
device: Device, included_optional_fields: Optional[Tuple[str, ...]] = None
def create_children_from_annotations(
device: Device, included_optional_fields: Tuple[str, ...] = ()
):
"""For intializing blocks at __init__ of ``device``."""
for name, device_type in get_type_hints(type(device)).items():
if name in ("_name", "parent"):
continue
device_type, is_optional = _strip_union(device_type)
if (
is_optional
and included_optional_fields
and name not in included_optional_fields
):
if is_optional and name not in included_optional_fields:
continue
is_device_vector, device_type = _strip_device_vector(device_type)
if (
Expand All @@ -328,4 +320,4 @@ def pre_initialize_blocks(

sub_device = device_type()
setattr(device, name, sub_device)
pre_initialize_blocks(sub_device)
create_children_from_annotations(sub_device)
5 changes: 2 additions & 3 deletions src/ophyd_async/panda/_hdf_panda.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
SignalR,
StandardDetector,
)
from ophyd_async.epics.pvi import fill_pvi_entries, pre_initialize_blocks

from ophyd_async.epics.pvi import create_children_from_annotations, fill_pvi_entries

from ._common_panda import CommonPandaBlocks
from ._panda_controller import PandaPcapController
Expand All @@ -26,7 +25,7 @@ def __init__(
):
self._prefix = prefix

pre_initialize_blocks(self)
create_children_from_annotations(self)
controller = PandaPcapController(pcap=self.pcap)
writer = PandaHDFWriter(
prefix=prefix,
Expand Down
14 changes: 8 additions & 6 deletions tests/epics/test_pvi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
SignalRW,
SignalX,
)
from ophyd_async.epics.pvi import fill_pvi_entries, pre_initialize_blocks
from ophyd_async.epics.pvi import create_children_from_annotations, fill_pvi_entries


class Block1(Device):
Expand Down Expand Up @@ -97,14 +97,14 @@ async def test_fill_pvi_entries_sim_mode(pvi_test_device_t):


@pytest.fixture
def pvi_test_device_pre_initialize_blocks_t():
def pvi_test_device_create_children_from_annotations_t():
"""A fixture since pytest discourages init in test case classes"""

class TestDevice(Block3, Device):
def __init__(self, prefix: str, name: str = ""):
self._prefix = prefix
super().__init__(name)
pre_initialize_blocks(self)
create_children_from_annotations(self)

async def connect(
self, sim: bool = False, timeout: float = DEFAULT_TIMEOUT
Expand All @@ -116,14 +116,16 @@ async def connect(
yield TestDevice


async def test_device_pre_initialize_blocks(pvi_test_device_pre_initialize_blocks_t):
device = pvi_test_device_pre_initialize_blocks_t("PREFIX:")
async def test_device_create_children_from_annotations(
pvi_test_device_create_children_from_annotations_t,
):
device = pvi_test_device_create_children_from_annotations_t("PREFIX:")

block_2_device = device.device
block_1_device = device.device.device
top_block_1_device = device.signal_device

# The pre_initialize_blocks has only made blocks,
# The create_children_from_annotations has only made blocks,
# not signals or device vectors
assert isinstance(block_2_device, Block2)
assert isinstance(block_1_device, Block1)
Expand Down
4 changes: 2 additions & 2 deletions tests/panda/test_panda_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ophyd_async.core import DEFAULT_TIMEOUT, Device, DeviceCollector, DeviceVector
from ophyd_async.core.utils import NotConnected
from ophyd_async.epics.pvi import PVIEntry, fill_pvi_entries
from ophyd_async.epics.pvi.pvi import pre_initialize_blocks
from ophyd_async.epics.pvi.pvi import create_children_from_annotations
from ophyd_async.panda import PcapBlock, PulseBlock, SeqBlock, SeqTable, SeqTrigger


Expand Down Expand Up @@ -47,7 +47,7 @@ class CommonPandaBlocksNoData(Device):
class Panda(CommonPandaBlocksNoData):
def __init__(self, prefix: str, name: str = ""):
self._prefix = prefix
pre_initialize_blocks(self)
create_children_from_annotations(self)
super().__init__(name)

async def connect(self, sim: bool = False, timeout: float = DEFAULT_TIMEOUT):
Expand Down
8 changes: 4 additions & 4 deletions tests/panda/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
StaticDirectoryProvider,
set_sim_value,
)
from ophyd_async.epics.pvi import fill_pvi_entries, pre_initialize_blocks
from ophyd_async.panda._common_panda import CommonPandaBlocks
from ophyd_async.panda.writers import PandaHDFWriter
from ophyd_async.epics.pvi import create_children_from_annotations, fill_pvi_entries
from ophyd_async.panda import CommonPandaBlocks
from ophyd_async.panda.writers._hdf_writer import (
Capture,
CaptureSignalWrapper,
PandaHDFWriter,
get_capture_signals,
get_signals_marked_for_capture,
)
Expand All @@ -35,7 +35,7 @@ class Panda(CommonPandaBlocks):

def __init__(self, prefix: str, name: str = ""):
self._prefix = prefix
pre_initialize_blocks(self)
create_children_from_annotations(self)
super().__init__(name)

async def connect(self, sim: bool = False, timeout: float = DEFAULT_TIMEOUT):
Expand Down

0 comments on commit c4d8e04

Please sign in to comment.