Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enh/control child name mangling #1222

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ophyd/_pyepics_shim.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

from ._dispatch import EventDispatcher, _CallbackThread, wrap_callback

# suspect attempt to monkey-patch printf is causing segfaults
if hasattr(ca, "WITH_CA_MESSAGES"):
ca.WITH_CA_MESSAGES = True

_min_pyepics = "3.4.2"

if parse(epics.__version__) < parse(_min_pyepics):
Expand Down
9 changes: 6 additions & 3 deletions ophyd/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def maybe_add_prefix(self, instance, kw, suffix):
suffix from.

kw : str
The key of associated with the suffix. If this key is
The key of associated with the suffix. If this key is in
self.add_prefix than prepend the prefix to the suffix and
return, else just return the suffix.

Expand All @@ -253,10 +253,12 @@ def create_component(self, instance):
"Instantiate the object described by this Component for a Device"
kwargs = self.kwargs.copy()
kwargs.update(
name=f"{instance.name}_{self.attr}",
name=f"{instance.name}{instance._child_name_separator}{self.attr}",
kind=instance._component_kinds[self.attr],
attr_name=self.attr,
)
if issubclass(self.cls, Device):
kwargs.setdefault("child_name_separator", instance._child_name_separator)

for kw, val in list(kwargs.items()):
kwargs[kw] = self.maybe_add_prefix(instance, kw, val)
Expand Down Expand Up @@ -839,10 +841,11 @@ def __init__(
read_attrs=None,
configuration_attrs=None,
parent=None,
child_name_separator="_",
**kwargs,
):
self._destroyed = False

self._child_name_separator = child_name_separator
# Store EpicsSignal objects (only created once they are accessed)
self._signals = {}

Expand Down
28 changes: 28 additions & 0 deletions ophyd/tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,3 +969,31 @@ class FakeTriggerableDevice(Device):

d.trigger()
assert d.strigger.get() == after


def test_child_separator():
class Test(Device):
a = Component(Signal)
b = Component(Signal)

t = Test(name="bob")
assert t.a.name == "bob_a"

t = Test(name="bob", child_name_separator="-")
assert t.a.name == "bob-a"

class Test2(Device):
c = Component(Signal)
d = Component(Signal)
t = Component(Test)
s = Component(Test, child_name_separator="?")

t2 = Test2(name="bob", child_name_separator="!")

assert t2.c.name == "bob!c"
assert t2.d.name == "bob!d"

assert t2.t.a.name == "bob!t!a"
assert t2.t.b.name == "bob!t!b"
assert t2.s.a.name == "bob!s?a"
assert t2.s.b.name == "bob!s?b"
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ dev =
numpydoc
pre-commit
pydata-sphinx-theme
pyepics>=3.4.2
pyepics>=3.4.2,<3.5.7
pytest
pytest-asyncio
pytest-cov
Expand Down
Loading