Repository navigation
make device factory decorator #597
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
Closed
stan-dot
wants to merge
85
commits into
main
from
483-make-device_factory-decorator-to-ease-making-and-connecting-ophyd-async-devices
Closed
Changes from all commits
Commits
Show all changes
85 commits
Select commit
Hold shift + click to select a range
87e9e36
example beamline with implementation
stan-dot dee1d0e
small cleanup
stan-dot 45828c0
added controller demonstration
stan-dot 2a7efa0
fix wrong extension
stan-dot 8531403
Update src/dodal/beamlines/example_beamline.py
stan-dot 0490806
Update src/dodal/beamlines/example_beamline.py
stan-dot 39e372b
Update src/dodal/beamlines/example_beamline.py
stan-dot 11b1671
Update src/dodal/beamlines/example_beamline.py
stan-dot 28bcad1
fix example beamline into a dataclass
stan-dot 698ec5b
new-i22 as an example
stan-dot a8a925f
respond to feedback
stan-dot e2a517f
respond to comments
stan-dot cb5211e
initial test
stan-dot 1f139f7
comment out some tests to check coverage
stan-dot 42e4c2c
lint
stan-dot 035e34a
use factory name for active devices dict key
stan-dot 3caa89c
this cannot be active devices, globals harder to mock
stan-dot 011f009
amend the test
stan-dot 543ee47
added a red test for caching by name
stan-dot 01ac2ab
fix the tests and add the support for a name flag
stan-dot 248ea93
add the API for blueapi
stan-dot dabc376
remove todos
stan-dot ce95b44
unused utils deleted for coverage
stan-dot 97c4c45
Just one i22 now
stan-dot c277821
respond to comments
stan-dot f7578f9
rename to device factory
stan-dot f298379
is this enough to replace skip_device()
stan-dot 3cd8763
ruff
stan-dot 63a2211
remove the connect default timeout =10
stan-dot 152ff73
add support for the terminal use case and some tests
stan-dot c5b7d2e
fix mock status and add the new decorator@
stan-dot dcb6102
fix the tests
stan-dot ffb41ee
delete bad comments, add good comments
stan-dot 826e96f
ruff
stan-dot 9e0be91
feedback response
stan-dot 2a4c432
add return type annotation for testing coverage purposes
stan-dot cb43eb9
respond to feedback
stan-dot 34d6c4e
adapt tetramm
stan-dot 2e874c5
respond to feedback
stan-dot 4660721
add repr that includes the config
stan-dot 1d95a9a
lint
stan-dot 7c90c0d
remove old panda ref
stan-dot ddd583b
fix import
stan-dot 01b5b7b
fix the tests
stan-dot 29fd6fc
pin pyright version due to is_device check failing
stan-dot df1082b
cancel pyright pinning
stan-dot 17a8f5f
renaming
stan-dot 006e97a
partial update to fix the test coverage for the factory
stan-dot e02d9d4
maybe fixed
stan-dot 8572b49
imports fix
stan-dot ef604dd
fix imports
stan-dot 3e1643a
forgot one
stan-dot 12980b5
added test to increase coverage, still broken a bit
stan-dot f5f6949
towards a stable problem definitio a stable problme definition
stan-dot 048c7c7
fix the test instance
stan-dot 8f6c8ca
finally fixed tests
stan-dot 3888726
lint
stan-dot 305f1d9
fix tests
stan-dot 3bb1815
exit some tests early
stan-dot 8d1873f
remove pytest skip
stan-dot 48ad3d1
hardcode the i22 slit refs
stan-dot a47b2d7
i22 remove device_instantiation
stan-dot 4a96f79
respond to feedback
stan-dot d3020c7
more tests to raise coverage
stan-dot 31beeef
respond to comment on the connect logic in controller call
stan-dot f704cb2
deprecation warning added
stan-dot e2887aa
remove the v1 ophyd relevant piece of logic - fake_with_ophyd_sim
stan-dot 1af9cd7
halfway through
stan-dot c75e693
identified the issue
stan-dot adef60b
name param issues
stan-dot 396cfbe
add new appropriate mocks
stan-dot a986877
merged the two dictionaries to preserve the connected functionality
stan-dot 2aca80f
quickfix
stan-dot b58cb7a
try reverting the factory name change
stan-dot f4aa047
change the expected beamline in test_cli
stan-dot 2955088
increase coverage
stan-dot 580b7c9
fix lint newlien
stan-dot 06954f1
add one more line
stan-dot 057080a
linting
0433521
remove unused imports
stan-dot 259e68f
restore the movable readable interface
stan-dot 9321f4c
respond to feedback
stan-dot 7553a97
apply suggestions to the i22.py
stan-dot c28c7c4
respond to feedback
stan-dot c2e8553
add hasattr to the module parsing
stan-dot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # 3. Add device factory decorator with lazy connect support | ||
|
|
||
| Date: 2024-04-26 | ||
|
|
||
| ## Status | ||
|
|
||
| Accepted | ||
|
|
||
| ## Context | ||
|
|
||
| We should add a decorator to support verified device connection. | ||
|
|
||
| ## Decision | ||
|
|
||
| DAQ members led us to this proposal: | ||
|
|
||
| - ophyd-async: make Device.connect(mock, timeout, force=False) be idempotent | ||
| - ophyd-async: make ensure_connected(\*devices) plan stub | ||
| - dodal: make device_factory(eager=False) decorator that makes, names, caches and optionally connects a device | ||
| - dodal: make get_device_factories() that returns all device factories and whether they should be connected at startup | ||
| - blueapi: call get_device_factories(), make all the Devices, connect the ones that should be connected at startup in parallel and log those that fail | ||
| - blueapi: when plan is called, run ensure_connected on all plan args and defaults that are Devices | ||
|
|
||
| We can then iterate on this if the parallel connect causes a broadcast storm. We could also in future add a monitor to a heartbeat PV per device in Device.connect so that it would reconnect next time it was called. | ||
|
|
||
| ## Consequences | ||
|
|
||
| The changes above. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| from collections.abc import Callable | ||
| from typing import TypeAlias | ||
|
|
||
| from ophyd.device import Device as OphydV1Device | ||
| from ophyd_async.core import Device as OphydV2Device | ||
|
|
||
| AnyDevice: TypeAlias = OphydV1Device | OphydV2Device | ||
| V1DeviceFactory: TypeAlias = Callable[..., OphydV1Device] | ||
| V2DeviceFactory: TypeAlias = Callable[..., OphydV2Device] | ||
| AnyDeviceFactory: TypeAlias = V1DeviceFactory | V2DeviceFactory |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,30 +20,49 @@ | |
| } | ||
|
|
||
|
|
||
| def all_beamline_modules() -> Iterable[str]: | ||
| """ | ||
| Get the names of all importable modules in beamlines | ||
| class ModuleDiscoveryError(Exception): | ||
| """Custom exception for module discovery errors.""" | ||
|
|
||
| pass | ||
|
|
||
|
|
||
| def get_all_beamline_modules() -> Iterable[str]: | ||
| """ | ||
| Get the names of all importable modules in the beamlines package by inspecting file paths. | ||
| Returns: | ||
| Iterable[str]: Generator of beamline module names | ||
| Iterable[str]: Generator of beamline module names (excluding __init__.py and __pycache__). | ||
| Raises: | ||
| ModuleDiscoveryError: If the beamlines module could not be found. | ||
| """ | ||
|
|
||
| # This is done by inspecting file names rather than modules to avoid | ||
| # premature importing | ||
| spec = importlib.util.find_spec(__name__) | ||
| if spec is not None: | ||
| assert spec.submodule_search_locations | ||
| search_paths = [Path(path) for path in spec.submodule_search_locations] | ||
| for path in search_paths: | ||
| for subpath in path.glob("**/*"): | ||
| if ( | ||
| subpath.name.endswith(".py") | ||
| and subpath.name != "__init__.py" | ||
| and ("__pycache__" not in str(subpath)) | ||
| ): | ||
| yield subpath.with_suffix("").name | ||
| else: | ||
| raise KeyError(f"Unable to find {__name__} module") | ||
| try: | ||
| # Find the package spec for the current module | ||
| spec = importlib.util.find_spec(__name__) | ||
| except Exception as e: | ||
| raise ModuleDiscoveryError( | ||
| f"Error while finding module spec for {__name__}: {e}" | ||
| ) from e | ||
|
|
||
| if not spec or not spec.submodule_search_locations: | ||
| raise ModuleDiscoveryError( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should: A unit test covering this would be good
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok that is one of the lines not covered in the test, I'll try to come up with something |
||
| f"Unable to find module search locations for {__name__}" | ||
| ) | ||
|
|
||
| search_paths = [Path(path) for path in spec.submodule_search_locations] | ||
|
|
||
| # Yield valid module names by filtering Python files | ||
| for path in search_paths: | ||
| for subpath in path.glob("**/*.py"): | ||
| if _is_valid_module(subpath): | ||
| yield subpath.stem # `.stem` gives the filename without the suffix | ||
|
|
||
|
|
||
| def _is_valid_module(subpath): | ||
| return ( | ||
| subpath.name.endswith(".py") | ||
| and subpath.name != "__init__.py" | ||
| and ("__pycache__" not in str(subpath)) | ||
| ) | ||
|
|
||
|
|
||
| def all_beamline_names() -> Iterable[str]: | ||
|
|
@@ -54,7 +73,7 @@ def all_beamline_names() -> Iterable[str]: | |
| Iterable[str]: Generator of beamline names that dodal supports | ||
| """ | ||
| inverse_mapping = _module_name_overrides() | ||
| for module_name in all_beamline_modules(): | ||
| for module_name in get_all_beamline_modules(): | ||
| yield from inverse_mapping.get(module_name, set()).union({module_name}) | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did this file need quite so much refactoring?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the answer is - I had to work with the logic it provided to make the decorator and it's hard to do that dealing with the complexity so I refactored it