Skip to content

Commit

Permalink
move dae to separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
rerpha committed Aug 29, 2024
1 parent 0aadac9 commit 6136486
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 58 deletions.
9 changes: 4 additions & 5 deletions src/ibex_bluesky_core/demo_plan.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Demonstration plan showing basic bluesky functionality."""

import random
from typing import Generator

import bluesky.plan_stubs as bps
Expand All @@ -11,14 +10,14 @@

from ibex_bluesky_core.devices import get_pv_prefix
from ibex_bluesky_core.devices.block import BlockRwRbv, block_rw_rbv
from ibex_bluesky_core.devices.dae import Dae
from ibex_bluesky_core.devices.dae_settings import DaeSettingsData
from ibex_bluesky_core.devices.dae_tcb_settings import DaeTCBSettingsData
from ibex_bluesky_core.devices.dae.dae import Dae
from ibex_bluesky_core.devices.dae.dae_settings import DaeSettingsData
from ibex_bluesky_core.devices.dae.dae_tcb_settings import DaeTCBSettingsData
from ibex_bluesky_core.run_engine import get_run_engine

__all__ = ["run_demo_plan", "demo_plan"]

from src.ibex_bluesky_core.devices.dae_period_settings import DaePeriodSettingsData
from ibex_bluesky_core.devices.dae.dae_period_settings import DaePeriodSettingsData


def run_demo_plan() -> None:
Expand Down
40 changes: 1 addition & 39 deletions src/ibex_bluesky_core/devices/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import binascii
import os
import zlib
from enum import Enum
from typing import Any, Dict, List, Tuple, Type
from xml.etree import ElementTree as ET
from typing import Type

from ophyd_async.core import SignalRW, T
from ophyd_async.epics.signal import epics_signal_rw
Expand Down Expand Up @@ -48,45 +46,9 @@ def compress_and_hex(value: str) -> bytes:
return binascii.hexlify(compr)


def convert_xml_to_names_and_values(xml) -> Dict[str, str]:
names_and_values = dict()
elements = get_all_elements_in_xml_with_child_called_name(xml)
for element in elements:
name, value = _get_names_and_values(element)
names_and_values[name] = value
return names_and_values


def get_all_elements_in_xml_with_child_called_name(xml):
# This finds all elements with a "name" element, but ignores the first one as it's the root
elements = xml.findall(".//Name/..")[1:]
return elements


def _get_names_and_values(element) -> Tuple[str, str]:
name = element.find("Name")
if name is not None and hasattr(name, "text"):
name = name.text
value = element.find("Val")
if value is not None and hasattr(value, "text"):
value = value.text
# TODO hmmmm, should we get choices here and store them somewhere? not sure.
return name, value


def isis_epics_signal_rw(datatype: Type[T], read_pv: str, name: str = "") -> SignalRW[T]:
"""Utility function for making a RW signal using the ISIS PV naming standard ie. read_pv being TITLE,
write_pv being TITLE:SP
"""
write_pv = f"{read_pv}:SP"
return epics_signal_rw(datatype, read_pv, write_pv, name)


def set_value_in_dae_xml(elements: List[ET.ElementTree], name: str, value: Any):
"""TODO add some docs here pls"""
if value is not None:
if isinstance(value, Enum):
value = value.value
for i in elements:
if i.find("Name").text == name:
i.find("Val").text = str(value)
41 changes: 41 additions & 0 deletions src/ibex_bluesky_core/devices/dae/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from __future__ import annotations

from enum import Enum
from typing import Any, Dict, List, Tuple
from xml.etree import ElementTree as ET


def convert_xml_to_names_and_values(xml) -> Dict[str, str]:
names_and_values = dict()
elements = get_all_elements_in_xml_with_child_called_name(xml)
for element in elements:
name, value = _get_names_and_values(element)
names_and_values[name] = value
return names_and_values


def get_all_elements_in_xml_with_child_called_name(xml):
# This finds all elements with a "name" element, but ignores the first one as it's the root
elements = xml.findall(".//Name/..")[1:]
return elements


def _get_names_and_values(element) -> Tuple[str, str]:
name = element.find("Name")
if name is not None and hasattr(name, "text"):
name = name.text
value = element.find("Val")
if value is not None and hasattr(value, "text"):
value = value.text
# TODO hmmmm, should we get choices here and store them somewhere? not sure.
return name, value


def set_value_in_dae_xml(elements: List[ET.ElementTree], name: str, value: Any):
"""TODO add some docs here pls"""
if value is not None:
if isinstance(value, Enum):
value = value.value
for i in elements:
if i.find("Name").text == name:
i.find("Val").text = str(value)
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
from ophyd_async.epics.signal import epics_signal_r, epics_signal_rw

from ibex_bluesky_core.devices import isis_epics_signal_rw
from ibex_bluesky_core.devices.dae_controls import DaeControls
from ibex_bluesky_core.devices.dae_event_mode import DaeEventMode
from ibex_bluesky_core.devices.dae_monitor import DaeMonitor
from ibex_bluesky_core.devices.dae_period import DaePeriod
from ibex_bluesky_core.devices.dae_period_settings import DaePeriodSettings
from ibex_bluesky_core.devices.dae_settings import DaeSettings
from ibex_bluesky_core.devices.dae_tcb_settings import DaeTCBSettings
from ibex_bluesky_core.devices.dae.dae_controls import DaeControls
from ibex_bluesky_core.devices.dae.dae_event_mode import DaeEventMode
from ibex_bluesky_core.devices.dae.dae_monitor import DaeMonitor
from ibex_bluesky_core.devices.dae.dae_period import DaePeriod
from ibex_bluesky_core.devices.dae.dae_period_settings import DaePeriodSettings
from ibex_bluesky_core.devices.dae.dae_settings import DaeSettings
from ibex_bluesky_core.devices.dae.dae_tcb_settings import DaeTCBSettings


class RunstateEnum(str, Enum):
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
from ophyd_async.core import AsyncStatus, Device, SignalRW

from ibex_bluesky_core.devices import (
isis_epics_signal_rw,
)
from ibex_bluesky_core.devices.dae import (
convert_xml_to_names_and_values,
get_all_elements_in_xml_with_child_called_name,
isis_epics_signal_rw,
set_value_in_dae_xml,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
from ophyd_async.core import AsyncStatus, Device, SignalRW

from ibex_bluesky_core.devices import (
isis_epics_signal_rw,
)
from ibex_bluesky_core.devices.dae import (
convert_xml_to_names_and_values,
get_all_elements_in_xml_with_child_called_name,
isis_epics_signal_rw,
set_value_in_dae_xml,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

from ibex_bluesky_core.devices import (
compress_and_hex,
convert_xml_to_names_and_values,
dehex_and_decompress,
isis_epics_signal_rw,
)
from ibex_bluesky_core.devices.dae import (
convert_xml_to_names_and_values,
get_all_elements_in_xml_with_child_called_name,
set_value_in_dae_xml,
)
from src.ibex_bluesky_core.devices import get_all_elements_in_xml_with_child_called_name

TIME_UNIT = "Time Unit"
CALCULATION_METHOD = "Calculation Method"
Expand Down
2 changes: 1 addition & 1 deletion tests/devices/test_dae.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# pyright: reportMissingParameterType=false

import pytest
from ibex_bluesky_core.devices.dae import Dae, RunstateEnum
from ibex_bluesky_core.devices.dae.dae import Dae, RunstateEnum
from ophyd_async.core import get_mock_put


Expand Down
3 changes: 1 addition & 2 deletions tests/devices/test_init.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from unittest.mock import patch

import pytest
from ibex_bluesky_core.devices import get_pv_prefix
from ibex_bluesky_core.devices import dehex_and_decompress, compress_and_hex
from ibex_bluesky_core.devices import compress_and_hex, dehex_and_decompress, get_pv_prefix


def test_can_dehex_and_decompress():
Expand Down

0 comments on commit 6136486

Please sign in to comment.