Skip to content

Commit

Permalink
Merge pull request #3 from spc-group/remove_and_cleanup
Browse files Browse the repository at this point in the history
Remove and cleanup
  • Loading branch information
canismarko authored Mar 25, 2024
2 parents 2a70d12 + aaaa3b8 commit 278135b
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 67 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
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "ophyd-registry"
version = "1.0.0"
version = "1.1.0"
authors = [
{ name="Mark Wolfman", email="[email protected]" },
]
Expand All @@ -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
34 changes: 31 additions & 3 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 @@ -161,10 +179,17 @@ def pop(self, key, default=UNSET) -> ophydobj.OphydObject:
else:
raise
# Remove from the list by name
del self._objects_by_name[obj.name]
try:
del self._objects_by_name[obj.name]
except KeyError:
pass
# Remove from the list by label
for objects in self._objects_by_label.values():
objects.discard(obj)
# Remove children from the lists as well
sub_signals = getattr(obj, "_signals", {})
for cpt_name, cpt in sub_signals.items():
self.pop(cpt)
return obj

def clear(self, clear_typhos: bool = True):
Expand Down Expand Up @@ -447,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 278135b

Please sign in to comment.