Skip to content

Commit

Permalink
Added root_attribute
Browse files Browse the repository at this point in the history
`root_attribute` is parsed on `register_sub_controller`
  • Loading branch information
evalott100 committed Dec 4, 2024
1 parent d842d40 commit f45ac5c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/fastcs/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ class SingleMapping:


class BaseController:
#! Attributes passed from the device at runtime.
#: Attributes passed from the device at runtime.
attributes: dict[str, Attribute]

def __init__(self, path: list[str] | None = None) -> None:
if not hasattr(self, "attributes"):
self.attributes = {}
self._path: list[str] = path or []
self.__sub_controller_tree: dict[str, BaseController] = {}
self.__sub_controller_tree: dict[str, SubController] = {}

self._bind_attrs()

Expand All @@ -48,6 +48,9 @@ def _bind_attrs(self) -> None:
class_type_hints = get_type_hints(type(self))

for attr_name in {**class_dir, **class_type_hints}:
if attr_name == "root_attribute":
continue

attr = getattr(self, attr_name, None)
if isinstance(attr, Attribute):
if (
Expand All @@ -60,7 +63,6 @@ def _bind_attrs(self) -> None:
)
new_attribute = copy(attr)
setattr(self, attr_name, new_attribute)

self.attributes[attr_name] = new_attribute

def register_sub_controller(self, name: str, sub_controller: SubController):
Expand All @@ -72,7 +74,16 @@ def register_sub_controller(self, name: str, sub_controller: SubController):
self.__sub_controller_tree[name] = sub_controller
sub_controller.set_path(self.path + [name])

def get_sub_controllers(self) -> dict[str, BaseController]:
if isinstance(sub_controller.root_attribute, Attribute):
if name in self.attributes:
raise TypeError(
f"Cannot set SubController `{name}` root attribute "
f"on the parent controller `{type(self).__name__}` "
f"as it already has an attribute of that name."
)
self.attributes[name] = sub_controller.root_attribute

def get_sub_controllers(self) -> dict[str, SubController]:
return self.__sub_controller_tree

def get_controller_mappings(self) -> list[SingleMapping]:
Expand Down Expand Up @@ -104,6 +115,7 @@ def _get_single_mapping(controller: BaseController) -> SingleMapping:
for name, attribute in controller.attributes.items()
if attribute.enabled
}

return SingleMapping(
controller, scan_methods, put_methods, command_methods, enabled_attributes
)
Expand Down Expand Up @@ -135,5 +147,7 @@ class SubController(BaseController):
it as part of a larger device.
"""

root_attribute: Attribute | None = None

def __init__(self) -> None:
super().__init__()
33 changes: 33 additions & 0 deletions tests/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,36 @@ def test_attribute_parsing():
assert sub_controller_mapping.attributes == {
"sub_attribute": sub_controller.sub_attribute,
}


def test_attribute_in_both_class_and_get_attributes():
class FailingController(Controller):
duplicate_attribute = AttrR(Int())

def __init__(self):
self.attributes = {"duplicate_attribute": AttrR(Int())}
super().__init__()

with pytest.raises(
ValueError,
match=(
"`FailingController` has conflicting attribute `duplicate_attribute` "
"already present in the attributes dict."
),
):
FailingController()


def test_root_attribute():
class FailingController(SomeController):
sub_controller = AttrR(Int())

with pytest.raises(
TypeError,
match=(
"Cannot set SubController `sub_controller` root attribute "
"on the parent controller `FailingController` as it already "
"has an attribute of that name."
),
):
next(_walk_mappings(FailingController(SomeSubController())))

0 comments on commit f45ac5c

Please sign in to comment.