Skip to content

Commit 7fdda11

Browse files
committed
Add path to attribute so __repr__ is fully unique
1 parent f3a92b8 commit 7fdda11

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/fastcs/attributes.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ def __init__(
4444
# changing the units on an int.
4545
self._update_datatype_callbacks: list[Callable[[DataType[T]], None]] = []
4646

47-
# Name to be filled in by Controller when the Attribute is bound
48-
self._name = None
47+
# Path and name to be filled in by Controller it is bound to
48+
self._name = ""
49+
self._path = []
4950

5051
@property
5152
def io_ref(self) -> AttributeIORefT:
@@ -68,6 +69,14 @@ def dtype(self) -> type[T]:
6869
def group(self) -> str | None:
6970
return self._group
7071

72+
@property
73+
def name(self) -> str:
74+
return self._name
75+
76+
@property
77+
def path(self) -> list[str]:
78+
return self._path
79+
7180
def add_update_datatype_callback(
7281
self, callback: Callable[[DataType[T]], None]
7382
) -> None:
@@ -82,16 +91,28 @@ def update_datatype(self, datatype: DataType[T]) -> None:
8291
for callback in self._update_datatype_callbacks:
8392
callback(datatype)
8493

85-
def set_name(self, name: list[str]):
94+
def set_name(self, name: str):
8695
if self._name:
87-
raise ValueError(
96+
raise RuntimeError(
8897
f"Attribute is already registered with a controller as {self._name}"
8998
)
9099

91100
self._name = name
92101

102+
def set_path(self, path: list[str]):
103+
if self._path:
104+
raise RuntimeError(
105+
f"Attribute is already registered with a controller at {self._path}"
106+
)
107+
108+
self._path = path
109+
93110
def __repr__(self):
94-
return f"{self.__class__.__name__}({self._name}, {self._datatype})"
111+
name = self.__class__.__name__
112+
path = ".".join(self._path + [self._name]) or None
113+
datatype = self._datatype.__class__.__name__
114+
115+
return f"{name}(path={path}, datatype={datatype}, io_ref={self._io_ref})"
95116

96117

97118
AttrIOUpdateCallback = Callable[["AttrR[T, Any]"], Awaitable[None]]

src/fastcs/controller.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ def set_path(self, path: list[str]):
8080
raise ValueError(f"sub controller is already registered under {self.path}")
8181

8282
self._path = path
83+
for attribute in self.attributes.values():
84+
attribute.set_path(path)
8385

8486
def _bind_attrs(self) -> None:
8587
"""Search for `Attributes` and `Methods` to bind them to this instance.
@@ -136,6 +138,7 @@ def add_attribute(self, name, attribute: Attribute):
136138
)
137139

138140
attribute.set_name(name)
141+
attribute.set_path(self.path)
139142
self.attributes[name] = attribute
140143
super().__setattr__(name, attribute)
141144

0 commit comments

Comments
 (0)