Skip to content

Commit

Permalink
fix: file transfer file name refactor (#2856)
Browse files Browse the repository at this point in the history
* fix: file transfer file name refactor

* update service implemenatation

* resolve read_case_data issue

* design change

* design change

* resolve download error

* update test

* update test 1

* restructuring 1

* restructuring 1
  • Loading branch information
hpohekar authored May 23, 2024
1 parent 55631d0 commit 4baf082
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
47 changes: 34 additions & 13 deletions src/ansys/fluent/core/solver/flobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
PyFluentUserWarning = UserWarning

from .error_message import allowed_name_error_message, allowed_values_error
from .settings_external import expand_api_file_argument

settings_logger = logging.getLogger("pyfluent.settings_api")

Expand Down Expand Up @@ -375,18 +376,25 @@ def find_object(self, relative_path):
obj = getattr(obj, comp)
return obj

def before_execute(self, value):
def before_execute(self, command_name, value, kwargs):
"""Executes before command execution."""
if hasattr(self, "_do_before_execute"):
self._do_before_execute(value)
return True
base_file_name = self._do_before_execute(
command_name=command_name, value=value, kwargs=kwargs
)
return base_file_name
else:
return False
return value

def after_execute(self, value):
def after_execute(self, command_name, value, kwargs):
"""Executes after command execution."""
if hasattr(self, "_do_after_execute"):
self._do_after_execute(value)
base_file_name = self._do_after_execute(
command_name=command_name, value=value, kwargs=kwargs
)
return base_file_name
else:
return value

def _while_setting_state(self):
"""Avoid additional processing while setting the state."""
Expand Down Expand Up @@ -804,15 +812,25 @@ class FileName(Base):


class _InputFile(FileName):
def _do_before_execute(self, value):
def _do_before_execute(self, command_name, value, kwargs):
file_names = expand_api_file_argument(command_name, value, kwargs)
if self.file_transfer_service:
self.file_transfer_service.upload(file_name=value)
for file_name in file_names:
self.file_transfer_service.upload(file_name=file_name)
return os.path.basename(value)
else:
return value


class _OutputFile(FileName):
def _do_after_execute(self, value):
def _do_after_execute(self, command_name, value, kwargs):
file_names = expand_api_file_argument(command_name, value, kwargs)
if self.file_transfer_service:
self.file_transfer_service.download(file_name=value)
for file_name in file_names:
self.file_transfer_service.download(file_name=file_name)
return os.path.basename(value)
else:
return value


class _InOutFile(_InputFile, _OutputFile):
Expand Down Expand Up @@ -1551,12 +1569,15 @@ def execute_command(self, *args, **kwds):
"""Execute command."""
for arg, value in kwds.items():
argument = getattr(self, arg)
if argument.before_execute(value):
kwds[f"{arg}"] = os.path.basename(value)
kwds[arg] = argument.before_execute(
command_name=self.python_name, value=value, kwargs=kwds
)
ret = self._execute_command(*args, **kwds)
for arg, value in kwds.items():
argument = getattr(self, arg)
argument.after_execute(value)
kwds[arg] = argument.after_execute(
command_name=self.python_name, value=value, kwargs=kwds
)
return_t = getattr(self, "return_type", None)
if return_t:
base_t = _baseTypes.get(return_t)
Expand Down
13 changes: 13 additions & 0 deletions src/ansys/fluent/core/solver/settings_external.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Miscellaneous utility functions."""


def expand_api_file_argument(command_name, value, kwargs):
"""Expand API file argument."""
if kwargs.get("file_type") == "case-data" or command_name in [
"read_case_data",
"write_case_data",
]:
data_file = value.replace(".cas", ".dat")
return [value, data_file]
else:
return [value]
10 changes: 6 additions & 4 deletions tests/test_file_transfer_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,12 @@ def test_remote_grpc_fts_container(monkeypatch, new_solver_session, new_mesh_ses
assert file_downloaded_to_the_client("downloaded_meshing_mixing_elbow.msh.h5")


@pytest.mark.skip(
reason="Unable to copy data file to Fluent's current working directory."
)
@pytest.mark.standalone
def test_read_case_and_data():
import ansys.fluent.core as pyfluent

pyfluent.USE_FILE_TRANSFER_SERVICE = True

case_file_name = examples.download_file(
"mixing_elbow.cas.h5", "pyfluent/mixing_elbow"
)
Expand All @@ -75,8 +74,11 @@ def test_read_case_and_data():
assert data_file_name
solver = pyfluent.launch_fluent(file_transfer_service=LocalFileTransferStrategy())

# Unable to copy data file to Fluent's current working directory.
solver.file.read(file_type="case-data", file_name=case_file_name)
solver.file.write(file_type="case-data", file_name="write_data.cas.h5")

solver.file.read_case_data(file_name=case_file_name)
solver.file.write_case_data(file_name="write_case_data.cas.h5")


@pytest.mark.skip(reason="Skips upload even after adding ImportGeometry task object.")
Expand Down

0 comments on commit 4baf082

Please sign in to comment.