Skip to content

Commit

Permalink
The auto-register can now be turned on and off after the registry is …
Browse files Browse the repository at this point in the history
…initialized.
  • Loading branch information
canismarko committed Mar 25, 2024
1 parent 5b5eae5 commit f03da2d
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 65 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,27 @@ assert registry.find("my_device") is the_device
```

This greedy behavior can be suppressed with the *auto_register*
parameter. If *auto_register* is false, then a device class can be
parameter. It can also be turned off after initialization by setting
the *auto_register* attribute to ``False``::

```python

registry = Registry(auto_register=False)

# Make a bunch of devices
...

# Turn if off for this one
registry.auto_register = False
device = MyDevice(...)
registry.auto_register = True

# Register some other devices maybe
...

```

If *auto_register* is false, then a device class can be
decorated to allow the registry to find it:

```python
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies = ["ophyd"]

[project.optional-dependencies]

dev = ["black", "isort", "pytest", "build", "twine", "flake8", "caproto", "ruff"]
dev = ["black", "isort", "pytest", "build", "twine", "flake8", "ruff"]

[project.urls]
"Homepage" = "https://github.com/spc-group/ophyd-registry"
Expand Down
25 changes: 23 additions & 2 deletions src/ophydregistry/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class Registry:
"""

use_typhos: bool = False
_auto_register: bool

# components: Sequence
_objects_by_name: Mapping
Expand All @@ -119,11 +120,28 @@ def __init__(self, auto_register: bool = True, use_typhos: bool = False):
# Set up empty lists and things for registering components
self.clear()
self.use_typhos = use_typhos
# Add a callback to get notified of new objects
if auto_register:
self.auto_register = auto_register

@property
def auto_register(self):
return self._auto_register

@auto_register.setter
def auto_register(self, val):
"""Turn on or off the automatic registration of new devices."""
self._auto_register = val
if val:
# Add a callback to get notified of new objects
ophydobj.OphydObject.add_instantiation_callback(
self.register, fail_if_late=False
)
else:
try:
ophydobj.OphydObject._OphydObject__instantiation_callbacks.remove(
self.register
)
except ValueError:
pass

def __getitem__(self, key):
"""Retrieve the object from the dicionary.
Expand Down Expand Up @@ -454,11 +472,14 @@ def register(self, component: ophydobj.OphydObject) -> ophydobj.OphydObject:
# (Needed for some sub-components that are just readback
# values of the parent)
# Check that we're not adding a duplicate component name
if name == "I0_center":
print(self._objects_by_name.keys(), self)
if name in self._objects_by_name.keys():
old_obj = self._objects_by_name[name]
is_readback = component in [
getattr(old_obj, "readback", None),
getattr(old_obj, "user_readback", None),
getattr(old_obj, "val", None),
]
if is_readback:
msg = f"Ignoring readback with duplicate name: '{name}'"
Expand Down
Loading

0 comments on commit f03da2d

Please sign in to comment.