DiamondLightSource / DiamondLightSource/dodal
Potentially refactor the StartDocumentPathProvider
- Dominant language
- Python
- Stars
- 5
- Forks
- 13
- Avg merge
- 3d 2h
- Merged PRs (30d)
- 10
Description
From discussions with MX we could refactor the StartDocumentPathProvider to use `CallbackBase` as the cache for the start document. As below:
```
DEFAULT_TEMPLATE = "{device_name}-{instrument}-{scan_id}"
from bluesky.callbacks import CallbackBase
class StartDocumentCache(CallbackBase):
def __init__(self) -> None:
self._cached_run_start: RunStart | None = None
super().__init__(emit=None)
def latest_start_document(self) -> RunStart | None:
return self._cached_run_start
def start(self, doc: RunStart) -> RunStart | None:
self._cached_run_start = doc
return super().start(doc)
class StartDocumentPathProvider(PathProvider):
"""A PathProvider that sources from metadata in a RunStart document.
This uses metadata a RunStart document to determine file names and data session
directories. The file naming defaults to "{device_name}-{instrument}-{scan_id}", so
the file name is incremented by scan number. A template can be included in the
StartDocument to allow for custom naming conventions.
"""
def __init__(self) -> None:
self._cache = StartDocumentCache()
@property
def cache(self) -> StartDocumentCache:
return self._cache
def __call__(self, device_name: str | None = None) -> PathInfo:
"""Returns the directory path and filename for a given data_session.
The default template for file naming is: "{device_name}-{instrument}-{scan_id}"
however, this can be changed by providing a template in the start document. For
example: "template": "custom-{device_name}--{scan_id}".
If you do not provide a data_session_directory it will default to "/tmp".
"""
doc = self._cache.latest_start_document() or {}
template = doc.get("template", DEFAULT_TEMPLATE)
sub_path = template.format_map(doc | {"device_name": device_name})
data_session_directory = Path(doc.get("data_session_directory", "/tmp"))
return PathInfo(directory_path=data_session_directory, filename=sub_path)
```
Contributor guide
Assessment
This issue has not been assessed yet.