Skip to content

Commit 2592b6b

Browse files
authored
Make machine configuration extendable (#392)
Allow extra configuration options to be extracted from the Murfey machine configuration and validated against a pydantic model specified as an entry point. This allows external packages to add their configuration to the Murfey config for convenience
1 parent e2fdd75 commit 2592b6b

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ murfey = "murfey.client:run"
9595
"murfey.transfer" = "murfey.cli.transfer:run"
9696
[project.entry-points."murfey.auth.token_validation"]
9797
"password" = "murfey.server.api.auth:password_token_validation"
98+
[project.entry-points."murfey.config.extraction"]
99+
"murfey_machine" = "murfey.util.config:get_extended_machine_config"
98100
[project.entry-points."murfey.workflows"]
99101
"lif_to_stack" = "murfey.workflows.lif_to_stack:zocalo_cluster_request"
100102
"tiff_to_stack" = "murfey.workflows.tiff_to_stack:zocalo_cluster_request"

src/murfey/util/config.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
from typing import Dict, List, Literal, Optional, Union
88

99
import yaml
10-
from pydantic import BaseModel, BaseSettings
10+
from backports.entry_points_selectable import entry_points
11+
from pydantic import BaseModel, BaseSettings, Extra
1112

1213

13-
class MachineConfig(BaseModel):
14+
class MachineConfig(BaseModel, extra=Extra.allow): # type: ignore
1415
acquisition_software: List[str]
1516
calibrations: Dict[str, Dict[str, Union[dict, float]]]
1617
data_directories: Dict[Path, str]
@@ -157,3 +158,16 @@ def get_machine_config(instrument_name: str = "") -> Dict[str, MachineConfig]:
157158
Path(settings.murfey_machine_configuration), microscope
158159
)
159160
return machine_config
161+
162+
163+
def get_extended_machine_config(
164+
extension_name: str, instrument_name: str = ""
165+
) -> Optional[BaseModel]:
166+
machine_config = get_machine_config(instrument_name=instrument_name).get(
167+
instrument_name or get_microscope()
168+
)
169+
if not machine_config:
170+
return None
171+
model = entry_points.select(group="murfey.config", name=extension_name)[0].load()
172+
data = getattr(machine_config, extension_name, {})
173+
return model(data)

0 commit comments

Comments
 (0)