Skip to content

Commit d842d40

Browse files
committed
Attribute parsing with Controller.attributes
1 parent 35b1870 commit d842d40

File tree

2 files changed

+86
-6
lines changed

2 files changed

+86
-6
lines changed

src/fastcs/controller.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from collections.abc import Iterator
44
from copy import copy
55
from dataclasses import dataclass
6+
from typing import get_type_hints
67

78
from .attributes import Attribute
89
from .cs_methods import Command, Put, Scan
@@ -19,7 +20,12 @@ class SingleMapping:
1920

2021

2122
class BaseController:
23+
#! Attributes passed from the device at runtime.
24+
attributes: dict[str, Attribute]
25+
2226
def __init__(self, path: list[str] | None = None) -> None:
27+
if not hasattr(self, "attributes"):
28+
self.attributes = {}
2329
self._path: list[str] = path or []
2430
self.__sub_controller_tree: dict[str, BaseController] = {}
2531

@@ -37,12 +43,26 @@ def set_path(self, path: list[str]):
3743
self._path = path
3844

3945
def _bind_attrs(self) -> None:
40-
for attr_name in dir(self):
41-
attr = getattr(self, attr_name)
46+
# Using a dictionary instead of a set to maintain order.
47+
class_dir = {key: None for key in dir(type(self))}
48+
class_type_hints = get_type_hints(type(self))
49+
50+
for attr_name in {**class_dir, **class_type_hints}:
51+
attr = getattr(self, attr_name, None)
4252
if isinstance(attr, Attribute):
53+
if (
54+
attr_name in self.attributes
55+
and self.attributes[attr_name] is not attr
56+
):
57+
raise ValueError(
58+
f"`{type(self).__name__}` has conflicting attribute "
59+
f"`{attr_name}` already present in the attributes dict."
60+
)
4361
new_attribute = copy(attr)
4462
setattr(self, attr_name, new_attribute)
4563

64+
self.attributes[attr_name] = new_attribute
65+
4666
def register_sub_controller(self, name: str, sub_controller: SubController):
4767
if name in self.__sub_controller_tree.keys():
4868
raise ValueError(
@@ -69,7 +89,6 @@ def _get_single_mapping(controller: BaseController) -> SingleMapping:
6989
scan_methods: dict[str, Scan] = {}
7090
put_methods: dict[str, Put] = {}
7191
command_methods: dict[str, Command] = {}
72-
attributes: dict[str, Attribute] = {}
7392
for attr_name in dir(controller):
7493
attr = getattr(controller, attr_name)
7594
match attr:
@@ -79,11 +98,14 @@ def _get_single_mapping(controller: BaseController) -> SingleMapping:
7998
scan_methods[attr_name] = scan_method
8099
case WrappedMethod(fastcs_method=Command(enabled=True) as command_method):
81100
command_methods[attr_name] = command_method
82-
case Attribute(enabled=True):
83-
attributes[attr_name] = attr
84101

102+
enabled_attributes = {
103+
name: attribute
104+
for name, attribute in controller.attributes.items()
105+
if attribute.enabled
106+
}
85107
return SingleMapping(
86-
controller, scan_methods, put_methods, command_methods, attributes
108+
controller, scan_methods, put_methods, command_methods, enabled_attributes
87109
)
88110

89111

tests/test_controller.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import pytest
22

3+
from fastcs.attributes import AttrR
34
from fastcs.controller import (
45
Controller,
56
SubController,
67
_get_single_mapping,
78
_walk_mappings,
89
)
10+
from fastcs.datatypes import Int
911

1012

1113
def test_controller_nesting():
@@ -33,3 +35,59 @@ def test_controller_nesting():
3335
ValueError, match=r"SubController is already registered under .*"
3436
):
3537
controller.register_sub_controller("c", sub_controller)
38+
39+
40+
class SomeSubController(SubController):
41+
def __init__(self):
42+
super().__init__()
43+
44+
sub_attribute = AttrR(Int())
45+
46+
root_attribute = AttrR(Int())
47+
48+
49+
class SomeController(Controller):
50+
annotated_attr: AttrR
51+
annotated_attr_not_defined_in_init: AttrR[int]
52+
equal_attr = AttrR(Int())
53+
annotated_and_equal_attr: AttrR[int] = AttrR(Int())
54+
55+
def __init__(self, sub_controller: SubController):
56+
self.attributes = {}
57+
58+
self.annotated_attr = AttrR(Int())
59+
self.attr_on_object = AttrR(Int())
60+
61+
self.attributes["_attributes_attr"] = AttrR(Int())
62+
self.attributes["_attributes_attr_equal"] = self.equal_attr
63+
64+
super().__init__()
65+
self.register_sub_controller("sub_controller", sub_controller)
66+
67+
68+
def test_attribute_parsing():
69+
sub_controller = SomeSubController()
70+
controller = SomeController(sub_controller)
71+
72+
mapping_walk = _walk_mappings(controller)
73+
74+
controller_mapping = next(mapping_walk)
75+
assert set(controller_mapping.attributes.keys()) == {
76+
"_attributes_attr",
77+
"annotated_attr",
78+
"_attributes_attr_equal",
79+
"annotated_and_equal_attr",
80+
"equal_attr",
81+
"sub_controller",
82+
}
83+
84+
assert SomeController.equal_attr is not controller.equal_attr
85+
assert (
86+
SomeController.annotated_and_equal_attr
87+
is not controller.annotated_and_equal_attr
88+
)
89+
90+
sub_controller_mapping = next(mapping_walk)
91+
assert sub_controller_mapping.attributes == {
92+
"sub_attribute": sub_controller.sub_attribute,
93+
}

0 commit comments

Comments
 (0)