Skip to content

Commit 2424144

Browse files
committed
fields are now SubControllers
Also made field generator a match statement.
1 parent b7e0c8d commit 2424144

File tree

7 files changed

+382
-398
lines changed

7 files changed

+382
-398
lines changed

src/fastcs_pandablocks/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import logging
55
from pathlib import Path
66

7-
from fastcs.backends.epics.ioc import PvNamingConvention
7+
from fastcs.backends.epics.util import PvNamingConvention
88

99
from fastcs_pandablocks import DEFAULT_POLL_PERIOD, ioc
1010
from fastcs_pandablocks.types import EpicsName

src/fastcs_pandablocks/handlers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from typing import Any
22

3-
from fastcs.attributes import AttrW, Handler, Sender
3+
from fastcs.attributes import Attribute, AttrW, Sender
44

5-
from fastcs_pandablocks.types import AttrType, PandaName
5+
from fastcs_pandablocks.types import PandaName
66

77

88
class DefaultFieldSender(Sender):
@@ -14,7 +14,7 @@ async def put(self, controller: Any, attr: AttrW, value: str) -> None:
1414

1515

1616
class UpdateEguSender(Sender):
17-
def __init__(self, attr_to_update: AttrType):
17+
def __init__(self, attr_to_update: Attribute):
1818
"""Update the attr"""
1919
self.attr_to_update = attr_to_update
2020

src/fastcs_pandablocks/panda/blocks.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
from collections.abc import Generator
22

3-
from fastcs.attributes import AttrR, AttrRW, AttrW
3+
from fastcs.attributes import Attribute, AttrR, AttrRW, AttrW
44
from fastcs.controller import SubController
55
from pandablocks.responses import BlockInfo
66

7-
from fastcs_pandablocks.types import AttrType, EpicsName, PandaName, ResponseType
7+
from fastcs_pandablocks.types import EpicsName, PandaName, ResponseType
88

9-
from .fields import FIELD_TYPE_TO_FASTCS_TYPE, FieldType
9+
from .fields import FieldControllerType, get_field_controller_from_field_info
1010

1111

1212
class BlockController(SubController):
13-
fields: dict[str, FieldType]
13+
fields: dict[str, FieldControllerType]
1414

1515
def __init__(
1616
self,
@@ -19,30 +19,31 @@ def __init__(
1919
description: str | None | None,
2020
raw_fields: dict[str, ResponseType],
2121
):
22-
super().__init__()
22+
self._additional_attributes: dict[str, Attribute] = {}
2323
self.panda_name = panda_name
2424
self.number = number
2525
self.description = description
2626
self.fields = {}
2727

2828
for field_raw_name, field_info in raw_fields.items():
29-
field_panda_name = self.panda_name + PandaName(field=field_raw_name)
29+
field_panda_name = PandaName(field=field_raw_name)
30+
field = get_field_controller_from_field_info(field_info)
31+
self.fields[field_panda_name.attribute_name] = field
3032

31-
field = FIELD_TYPE_TO_FASTCS_TYPE[field_info.type][field_info.subtype](
32-
# TODO make type safe after match statment
33-
field_panda_name,
34-
field_info, # type: ignore
35-
)
36-
self.fields[field_raw_name] = field
33+
super().__init__()
3734

3835
def initialise(self):
3936
for field_name, field in self.fields.items():
40-
if field.named_attribute:
41-
setattr(self, *field.named_attribute)
42-
if field.sub_field_controller:
43-
self.register_sub_controller(
44-
field_name, sub_controller=field.sub_field_controller
45-
)
37+
if field.additional_attributes:
38+
self.register_sub_controller(field_name, sub_controller=field)
39+
if field.top_level_attribute:
40+
self._additional_attributes[field_name] = field.top_level_attribute
41+
42+
field.initialise()
43+
44+
@property
45+
def additional_attributes(self) -> dict[str, Attribute]:
46+
return self._additional_attributes
4647

4748

4849
class Blocks:
@@ -97,8 +98,8 @@ def flattened_attribute_tree(
9798
yield (block.panda_name.attribute_name, block)
9899

99100
def __getitem__(
100-
self, name: EpicsName | PandaName
101-
) -> dict[int | None, BlockController] | BlockController | AttrType:
101+
self, name: PandaName
102+
) -> dict[int | None, BlockController] | BlockController | Attribute:
102103
if name.block is None:
103104
raise ValueError(f"Cannot find block for name {name}.")
104105
blocks = self._blocks[name.block]
@@ -109,8 +110,7 @@ def __getitem__(
109110
return block
110111
field = block.fields[name.field]
111112
if not name.sub_field:
112-
assert field.named_attribute
113-
return field.named_attribute.attribute
113+
assert field.top_level_attribute
114+
return field.top_level_attribute
114115

115-
sub_field = getattr(field.sub_field_controller, name.sub_field)
116-
return sub_field
116+
return field.additional_attributes[name.sub_field]

0 commit comments

Comments
 (0)