Skip to content

Commit

Permalink
Disallow Changing Devices' Tags after Registration (#391)
Browse files Browse the repository at this point in the history
* Added `lock_tag()`. (#390)

* Comments updated. (#390)

* Using `lock_tag()`. (#390)

* Comments updated. (#390)
  • Loading branch information
ATATC authored Sep 8, 2024
1 parent 8ccb9d5 commit 10b9cf7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions leads/dt/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def __init__(self) -> None:
def _attach_device(self, tag: str, device: Device) -> None:
self._devices[tag] = device
device.tag(tag)
device.lock_tag()

def devices(self) -> list[Device]:
"""
Expand Down
11 changes: 10 additions & 1 deletion leads/dt/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def __init__(self, *args, **kwargs) -> None: # real signature unknown

def __init__(self, *pins: int | str) -> None:
self._tag: str = ""
self._tag_locked: bool = False
self._parent_tags: tuple[str, ...] = ()
self._pins: tuple[int | str, ...] = pins

Expand All @@ -29,12 +30,20 @@ def level(self) -> int:
def tag(self, tag: str | None = None) -> str | None:
"""
Set or get the tag of the device.
The tag will not be set if it is locked.
:param tag: the tag or None if getter mode
:return: the tag or None if setter mode
"""
if tag is None:
return self._tag
self._tag = tag
if not self._tag_locked:
self._tag = tag

def lock_tag(self) -> None:
"""
Lock the tag of the device.
"""
self._tag_locked = True

def parent_tags(self) -> tuple[str, ...]:
"""
Expand Down
1 change: 1 addition & 0 deletions leads/dt/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def register_controller(tag: str, c: Controller, parent: str | None = None) -> N
_controllers[parent].device(tag, c)
else:
c.tag(tag)
c.lock_tag()
_controllers[tag] = c


Expand Down

0 comments on commit 10b9cf7

Please sign in to comment.