11import datetime
2- import json
32import os
43from abc import ABC , abstractmethod
5- from json import JSONDecodeError , loads
4+ from json import JSONDecodeError
65from pathlib import Path
76from typing import Any , Union
87
@@ -127,34 +126,14 @@ def handle_static_command(self, command: str) -> None:
127126 else :
128127 raise ValueError (f"Unknown command { command } " )
129128
130- def _get_data_dir (self ) -> Path :
131- """Gets the TOOL_DATA_DIR that houses the input and output files of
132- tool execution.
133-
134- Returns:
135- Path: Path object of the TOOL_DATA_DIR that's configured.
136- """
137- data_dir = self .get_env_or_die (ToolEnv .DATA_DIR )
138- base_path = Path (data_dir )
139- if not base_path .exists ():
140- self .stream_error_and_exit (f"{ data_dir } does not exist" )
141- if not base_path .is_dir ():
142- self .stream_error_and_exit (f"{ data_dir } is not a directory" )
143- return base_path .absolute ()
144-
145129 def _get_file_from_data_dir (self , file_to_get : str , raise_err : bool = False ) -> str :
146- if self .workflow_filestorage :
147- base_path = self .execution_dir
148- file_path = base_path / file_to_get
149- if raise_err and not self .workflow_filestorage .exists (path = file_path ):
150- self .stream_error_and_exit (
151- f"{ file_to_get } is missing in EXECUTION_DATA_DIR"
152- )
153- else :
154- base_path : Path = self ._get_data_dir ()
155- file_path = base_path / file_to_get
156- if raise_err and not file_path .exists ():
157- self .stream_error_and_exit (f"{ file_to_get } is missing in TOOL_DATA_DIR" )
130+ base_path = self .execution_dir
131+ file_path = base_path / file_to_get
132+ if raise_err and not self .workflow_filestorage .exists (path = file_path ):
133+ self .stream_error_and_exit (
134+ f"{ file_to_get } is missing in EXECUTION_DATA_DIR"
135+ )
136+
158137 return str (file_path )
159138
160139 def get_source_file (self ) -> str :
@@ -183,10 +162,7 @@ def get_output_dir(self) -> str:
183162 Returns:
184163 str: Absolute path to the output directory.
185164 """
186- if self .workflow_filestorage :
187- base_path = self .execution_dir
188- else :
189- base_path : Path = self ._get_data_dir ()
165+ base_path = self .execution_dir
190166 return str (base_path / ToolExecKey .OUTPUT_DIR )
191167
192168 @property
@@ -205,20 +181,13 @@ def _get_exec_metadata(self) -> dict[str, Any]:
205181 Returns:
206182 dict[str, Any]: Contents of METADATA.json
207183 """
208- if self .workflow_filestorage :
209- base_path = self .execution_dir
210- else :
211- base_path : Path = self ._get_data_dir ()
184+ base_path = self .execution_dir
212185 metadata_path = base_path / ToolExecKey .METADATA_FILE
213186 metadata_json = {}
214187 try :
215- if self .workflow_filestorage :
216- metadata_json = ToolUtils .load_json (
217- file_to_load = metadata_path , fs = self .workflow_filestorage
218- )
219- else :
220- with open (metadata_path , encoding = "utf-8" ) as f :
221- metadata_json = loads (f .read ())
188+ metadata_json = ToolUtils .load_json (
189+ file_to_load = metadata_path , fs = self .workflow_filestorage
190+ )
222191 except JSONDecodeError as e :
223192 self .stream_error_and_exit (f"JSON decode error for { metadata_path } : { e } " )
224193 except FileNotFoundError :
@@ -233,19 +202,13 @@ def _write_exec_metadata(self, metadata: dict[str, Any]) -> None:
233202 Args:
234203 metadata (dict[str, Any]): Metadata to write
235204 """
236- if self .workflow_filestorage :
237- base_path = self .execution_dir
238- metadata_path = base_path / ToolExecKey .METADATA_FILE
239- ToolUtils .dump_json (
240- file_to_dump = metadata_path ,
241- json_to_dump = metadata ,
242- fs = self .workflow_filestorage ,
243- )
244- else :
245- base_path : Path = self ._get_data_dir ()
246- metadata_path = base_path / ToolExecKey .METADATA_FILE
247- with metadata_path .open ("w" , encoding = "utf-8" ) as f :
248- f .write (ToolUtils .json_to_str (metadata ))
205+ base_path = self .execution_dir
206+ metadata_path = base_path / ToolExecKey .METADATA_FILE
207+ ToolUtils .dump_json (
208+ file_to_dump = metadata_path ,
209+ json_to_dump = metadata ,
210+ fs = self .workflow_filestorage ,
211+ )
249212
250213 def _update_exec_metadata (self ) -> None :
251214 """Updates the execution metadata after a tool executes.
@@ -314,18 +277,13 @@ def write_tool_result(self, data: Union[str, dict[str, Any]]) -> None:
314277 self .stream_result (result )
315278
316279 self ._update_exec_metadata ()
317- json_data = json .dumps (data )
318280 # INFILE is overwritten for next tool to run
319281 input_file_path : Path = Path (self .get_input_file ())
320- if self .workflow_filestorage :
321- ToolUtils .dump_json (
322- file_to_dump = input_file_path ,
323- json_to_dump = data ,
324- fs = self .workflow_filestorage ,
325- )
326- else :
327- with input_file_path .open ("w" , encoding = "utf-8" ) as f :
328- f .write (json_data )
282+ ToolUtils .dump_json (
283+ file_to_dump = input_file_path ,
284+ json_to_dump = data ,
285+ fs = self .workflow_filestorage ,
286+ )
329287
330288 def validate (self , input_file : str , settings : dict [str , Any ]) -> None :
331289 """Override to implement custom validation for the tool.
0 commit comments