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

Add sample metadata validation #9

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
31 changes: 30 additions & 1 deletion src/dls_bluesky_core/core/types.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
from typing import Any, Callable, Generator
from functools import wraps
from typing import Any, Callable, Generator, Optional, TypeVar, TypedDict

from bluesky import Msg
from pydantic import BaseModel

# 'A true "plan", usually the output of a generator function'
MsgGenerator = Generator[Msg, Any, None]
# 'A function that generates a plan'
PlanGenerator = Callable[..., MsgGenerator]


class SampleMetadata(BaseModel):
"""Definition of schema relating to sample metadata."""

name: Optional[str]
chemical_formula: Optional[str]


class MetadataWithSampleInfo(TypedDict, total=False):
sample: SampleMetadata


T = TypeVar("T")


def with_sample_information(
callable: Callable[..., T]
) -> Callable[[..., Optional[SampleMetadata]], T]:
@wraps(callable, updated=["__dict__"])
def wrapped_callable(*args, **kwargs) -> T:
sample = kwargs.get("metadata", {}).get("sample", {})
sa = SampleMetadata(**sample)
kwargs.setdefault("metadata", {})["sample"] = sa
return callable(*args, **kwargs)

Check warning on line 35 in src/dls_bluesky_core/core/types.py

View check run for this annotation

Codecov / codecov/patch

src/dls_bluesky_core/core/types.py#L30-L35

Added lines #L30 - L35 were not covered by tests

return wrapped_callable

Check warning on line 37 in src/dls_bluesky_core/core/types.py

View check run for this annotation

Codecov / codecov/patch

src/dls_bluesky_core/core/types.py#L37

Added line #L37 was not covered by tests
Loading