Skip to content

Commit abd83e9

Browse files
authored
Merge pull request #187 from AllenNeuralDynamics/issue-184
Remove launcher dependency from App
2 parents a3456cc + 8f0a967 commit abd83e9

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src/clabe/apps/_bonsai.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
import hashlib
12
import logging
23
import os
4+
import random
35
from os import PathLike
46
from pathlib import Path
57
from typing import Dict, Optional
68

9+
import pydantic
710
from aind_behavior_services import AindBehaviorRigModel, AindBehaviorSessionModel, AindBehaviorTaskLogicModel
811

9-
from clabe.launcher._base import Launcher
10-
1112
from ._base import Command, CommandResult, ExecutableApp, identity_parser
1213
from ._executors import _DefaultExecutorMixin
1314

@@ -177,7 +178,7 @@ def __init__(
177178
self,
178179
workflow: os.PathLike,
179180
*,
180-
launcher: Launcher,
181+
temp_directory: os.PathLike,
181182
rig: Optional[AindBehaviorRigModel] = None,
182183
session: Optional[AindBehaviorSessionModel] = None,
183184
task_logic: Optional[AindBehaviorTaskLogicModel] = None,
@@ -232,14 +233,35 @@ def __init__(
232233
"""
233234
additional_externalized_properties = kwargs.pop("additional_externalized_properties", {}) or {}
234235
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))
236237
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))
238239
if task_logic:
239240
additional_externalized_properties["TaskLogicPath"] = os.path.abspath(
240-
launcher.save_temp_model(model=task_logic)
241+
self._save_temp_model(model=task_logic)
241242
)
242243
super().__init__(
243244
workflow=workflow, additional_externalized_properties=additional_externalized_properties, **kwargs
244245
)
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

Comments
 (0)