|
| 1 | +import hashlib |
1 | 2 | import logging |
2 | 3 | import os |
| 4 | +import random |
3 | 5 | from os import PathLike |
4 | 6 | from pathlib import Path |
5 | 7 | from typing import Dict, Optional |
6 | 8 |
|
| 9 | +import pydantic |
7 | 10 | from aind_behavior_services import AindBehaviorRigModel, AindBehaviorSessionModel, AindBehaviorTaskLogicModel |
8 | 11 |
|
9 | | -from clabe.launcher._base import Launcher |
10 | | - |
11 | 12 | from ._base import Command, CommandResult, ExecutableApp, identity_parser |
12 | 13 | from ._executors import _DefaultExecutorMixin |
13 | 14 |
|
@@ -177,7 +178,7 @@ def __init__( |
177 | 178 | self, |
178 | 179 | workflow: os.PathLike, |
179 | 180 | *, |
180 | | - launcher: Launcher, |
| 181 | + temp_directory: os.PathLike, |
181 | 182 | rig: Optional[AindBehaviorRigModel] = None, |
182 | 183 | session: Optional[AindBehaviorSessionModel] = None, |
183 | 184 | task_logic: Optional[AindBehaviorTaskLogicModel] = None, |
@@ -232,14 +233,35 @@ def __init__( |
232 | 233 | """ |
233 | 234 | additional_externalized_properties = kwargs.pop("additional_externalized_properties", {}) or {} |
234 | 235 | if rig: |
235 | | - additional_externalized_properties["RigPath"] = os.path.abspath(launcher.save_temp_model(model=rig)) |
| 236 | + additional_externalized_properties["RigPath"] = os.path.abspath(self._save_temp_model(model=rig)) |
236 | 237 | if session: |
237 | | - additional_externalized_properties["SessionPath"] = os.path.abspath(launcher.save_temp_model(model=session)) |
| 238 | + additional_externalized_properties["SessionPath"] = os.path.abspath(self._save_temp_model(model=session)) |
238 | 239 | if task_logic: |
239 | 240 | additional_externalized_properties["TaskLogicPath"] = os.path.abspath( |
240 | | - launcher.save_temp_model(model=task_logic) |
| 241 | + self._save_temp_model(model=task_logic) |
241 | 242 | ) |
242 | 243 | super().__init__( |
243 | 244 | workflow=workflow, additional_externalized_properties=additional_externalized_properties, **kwargs |
244 | 245 | ) |
245 | | - self._launcher = launcher |
| 246 | + self._temp_directory = Path(temp_directory) |
| 247 | + |
| 248 | + def _save_temp_model(self, model: pydantic.BaseModel) -> Path: |
| 249 | + """ |
| 250 | + Saves a temporary JSON representation of a pydantic model. |
| 251 | +
|
| 252 | + Args: |
| 253 | + model: The pydantic model to save |
| 254 | + directory: The directory to save the file in. |
| 255 | +
|
| 256 | + Returns: |
| 257 | + Path: The path to the saved file |
| 258 | + """ |
| 259 | + self._temp_directory.mkdir(parents=True, exist_ok=True) |
| 260 | + |
| 261 | + random_data = str(random.random()).encode("utf-8") |
| 262 | + sha_hash = hashlib.sha256(random_data).hexdigest()[:8] |
| 263 | + |
| 264 | + fpath = self._temp_directory / f"{model.__class__.__name__}_{sha_hash}.json" |
| 265 | + with open(fpath, "w+", encoding="utf-8") as f: |
| 266 | + f.write(model.model_dump_json(indent=2)) |
| 267 | + return Path(fpath) |
0 commit comments