Skip to content

Commit b401a3b

Browse files
committed
fields.py: Changed to using methods returning attributes
1 parent 3eb0bc4 commit b401a3b

File tree

2 files changed

+613
-684
lines changed

2 files changed

+613
-684
lines changed

src/fastcs_pandablocks/panda/controller.py

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncio
22

33
from fastcs.attributes import Attribute, AttrR, AttrRW, AttrW
4-
from fastcs.controller import Controller
4+
from fastcs.controller import Controller, SubController
55
from fastcs.datatypes import Bool, Float, Int, String, T
66
from fastcs.wrappers import scan
77

@@ -11,9 +11,42 @@
1111
RawFieldsType,
1212
RawInitialValuesType,
1313
)
14+
from fastcs_pandablocks.types._annotations import ResponseType
1415

1516
from .client_wrapper import RawPanda
16-
from .fields import FieldController
17+
from .fields import make_attributes
18+
19+
20+
class BlockController(SubController):
21+
def __init__(self, panda_name: PandaName, label: str | None = None):
22+
self.description = label
23+
self.panda_name = panda_name
24+
25+
self.attributes: dict[str, Attribute] = {}
26+
self.panda_name_to_attribute: dict[PandaName, Attribute] = {}
27+
super().__init__()
28+
29+
def make_attributes(
30+
self,
31+
field_info: dict[PandaName, ResponseType],
32+
initial_values: dict[PandaName, str],
33+
):
34+
if self.description is not None:
35+
self.attributes["LABEL"] = AttrR(
36+
String(),
37+
description="Label from metadata.",
38+
initial_value=self.description,
39+
)
40+
self.panda_name_to_attribute = make_attributes(
41+
self.panda_name, field_info, initial_values
42+
)
43+
attribute_name_to_attribute = {}
44+
for panda_name, attribute in self.panda_name_to_attribute.items():
45+
assert panda_name.field
46+
sub_field = f"_{panda_name.sub_field}" if panda_name.sub_field else ""
47+
attribute_name_to_attribute[f"{panda_name.field}{sub_field}"] = attribute
48+
49+
self.attributes.update(attribute_name_to_attribute)
1750

1851

1952
def _parse_introspected_data(
@@ -22,7 +55,7 @@ def _parse_introspected_data(
2255
raw_labels: RawInitialValuesType,
2356
raw_initial_values: RawInitialValuesType,
2457
):
25-
block_controllers: dict[PandaName, FieldController] = {}
58+
block_controllers: dict[PandaName, BlockController] = {}
2659
for (block_name, block_info), field_info in zip(
2760
raw_blocks.items(), raw_field_infos, strict=True
2861
):
@@ -41,11 +74,11 @@ def _parse_introspected_data(
4174
if key in numbered_block_name
4275
}
4376
label = raw_labels.get(numbered_block_name, None)
44-
block = FieldController(
77+
block = BlockController(
4578
numbered_block_name,
4679
label=block_info.description or label,
4780
)
48-
block.make_sub_fields(field_info, block_initial_values)
81+
block.make_attributes(field_info, block_initial_values)
4982
block_controllers[numbered_block_name] = block
5083

5184
return block_controllers
@@ -64,7 +97,7 @@ def __init__(self, hostname: str, poll_period: float) -> None:
6497

6598
self.attributes: dict[str, Attribute] = {}
6699
self._raw_panda = RawPanda(hostname)
67-
self._blocks: dict[PandaName, FieldController] = {}
100+
self._blocks: dict[PandaName, BlockController] = {}
68101

69102
self.connected = False
70103

@@ -83,27 +116,15 @@ async def connect(self) -> None:
83116
async def initialise(self) -> None:
84117
await self.connect()
85118
for block_name, block in self._blocks.items():
86-
if block.top_level_attribute is not None:
87-
self.attributes[block_name.attribute_name] = block.top_level_attribute
88-
if block.attributes or block.sub_fields:
89-
self.register_sub_controller(block_name.attribute_name.title(), block)
90-
await block.initialise()
119+
self.register_sub_controller(block_name.attribute_name.title(), block)
91120

92121
def get_attribute(self, panda_name: PandaName) -> Attribute:
93122
assert panda_name.block
94123
block_controller = self._blocks[panda_name.up_to_block()]
95124
if panda_name.field is None:
96-
assert block_controller.top_level_attribute is not None
97-
return block_controller.top_level_attribute
98-
99-
field_controller = block_controller.sub_fields[panda_name.up_to_field()]
100-
if panda_name.sub_field is None:
101-
assert field_controller.top_level_attribute is not None
102-
return field_controller.top_level_attribute
125+
raise RuntimeError
103126

104-
sub_field_controller = field_controller.sub_fields[panda_name]
105-
assert sub_field_controller.top_level_attribute is not None
106-
return sub_field_controller.top_level_attribute
127+
return block_controller.panda_name_to_attribute[panda_name]
107128

108129
async def update_field_value(self, panda_name: PandaName, value: str):
109130
attribute = self.get_attribute(panda_name)

0 commit comments

Comments
 (0)