Skip to content

Commit afba5a7

Browse files
Copilotmomchil-flex
andcommitted
Refactor default path handling to use task-type configuration
Co-authored-by: momchil-flex <92756559+momchil-flex@users.noreply.github.com>
1 parent f42248e commit afba5a7

File tree

1 file changed

+62
-15
lines changed

1 file changed

+62
-15
lines changed

tidy3d/web/api/webapi.py

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,61 @@
6868
"VOLUME_MESH": "VolumeMesher",
6969
}
7070

71+
# map task_type to default data filename
72+
DEFAULT_DATA_FILENAME = {
73+
"FDTD": "simulation_data.hdf5",
74+
"MODE_SOLVER": "simulation_data.hdf5",
75+
"MODE": "simulation_data.hdf5",
76+
"EME": "simulation_data.hdf5",
77+
"HEAT": "simulation_data.hdf5",
78+
"HEAT_CHARGE": "simulation_data.hdf5",
79+
"VOLUME_MESH": "simulation_data.hdf5",
80+
"COMPONENT_MODELER": "cm_data.hdf5",
81+
"TERMINAL_COMPONENT_MODELER": "cm_data.hdf5",
82+
"RF": "cm_data.hdf5",
83+
}
84+
85+
86+
def _get_default_path(task_id: str, provided_path: PathLike) -> Path:
87+
"""Get the appropriate default path based on task type.
88+
89+
If the user provided a non-default path, returns it as-is.
90+
Otherwise, returns the task-type-specific default filename.
91+
92+
Parameters
93+
----------
94+
task_id : str
95+
Unique identifier of task on server.
96+
provided_path : PathLike
97+
Path provided by the user.
98+
99+
Returns
100+
-------
101+
Path
102+
The appropriate path to use for this task type.
103+
"""
104+
path = Path(provided_path)
105+
106+
# If user provided a custom path (not the generic default), respect it
107+
if path.name not in {"simulation_data.hdf5", "simulation_data.hdf5.gz"}:
108+
return path
109+
110+
# Determine task type
111+
if _is_modeler_batch(task_id):
112+
task_type = "RF"
113+
else:
114+
task_info = get_info(task_id)
115+
task_type = task_info.taskType
116+
117+
# Get the task-type-specific default filename
118+
default_filename = DEFAULT_DATA_FILENAME.get(task_type, "simulation_data.hdf5")
119+
120+
# Preserve .gz extension if present
121+
if path.name.endswith(".gz") and not default_filename.endswith(".gz"):
122+
default_filename += ".gz"
123+
124+
return path.with_name(default_filename)
125+
71126

72127
def _get_url(task_id: str) -> str:
73128
"""Get the URL for a task on our server."""
@@ -1018,15 +1073,10 @@ def download(
10181073
Optional callback function called when downloading file with ``bytes_in_chunk`` as argument.
10191074
10201075
"""
1021-
path = Path(path)
1076+
# Get the appropriate default path based on task type
1077+
path = _get_default_path(task_id, path)
10221078

10231079
if _is_modeler_batch(task_id):
1024-
# Use a more descriptive default filename for component modeler downloads.
1025-
# If the caller left the default as 'simulation_data.hdf5', prefer 'cm_data.hdf5'.
1026-
# TODO: seems like the default should then be maybe set to None and defined somewhere else
1027-
# per task type?
1028-
if path.name == "simulation_data.hdf5":
1029-
path = path.with_name("cm_data.hdf5")
10301080
BatchTask(task_id).get_data_hdf5(
10311081
remote_data_file_gz=CM_DATA_HDF5_GZ,
10321082
to_file=path,
@@ -1182,14 +1232,11 @@ def load(
11821232
Union[:class:`.SimulationData`, :class:`.HeatSimulationData`, :class:`.EMESimulationData`]
11831233
Object containing simulation data.
11841234
"""
1185-
path = Path(path)
1186-
# For component modeler batches, default to a clearer filename if the default was used.
1187-
if (
1188-
task_id
1189-
and _is_modeler_batch(task_id)
1190-
and path.name in {"simulation_data.hdf5", "simulation_data.hdf5.gz"}
1191-
):
1192-
path = path.with_name(path.name.replace("simulation", "cm"))
1235+
# Get the appropriate default path based on task type
1236+
if task_id is not None:
1237+
path = _get_default_path(task_id, path)
1238+
else:
1239+
path = Path(path)
11931240

11941241
if task_id is None:
11951242
if not path.exists():

0 commit comments

Comments
 (0)