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 2, 2024
1 parent 4d9e130 commit 4d09743
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/fastcs/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ 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 Down Expand Up @@ -60,7 +60,8 @@ def _bind_attrs(self) -> None:
new_attribute = copy(attr)
setattr(self, attr_name, new_attribute)

self.attributes[attr_name] = new_attribute
if attr_name != "root_attribute":
self.attributes[attr_name] = new_attribute

def register_sub_controller(self, name: str, sub_controller: SubController):
if name in self.__sub_controller_tree.keys():
Expand All @@ -71,7 +72,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 @@ -103,6 +113,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 @@ -134,5 +145,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 4d09743

Please sign in to comment.