Skip to content

Commit

Permalink
Detect binout files automatically (#1277)
Browse files Browse the repository at this point in the history
* Automatic key handling of binout files

* Add test

* Improve key automatic discovery

* Restrict key discovery to files without extension as the extension is used by default if no key is given

* Add tests

* Remove redundant test

* Refactor DataSources.guess_result_key
  • Loading branch information
PProfizi authored Nov 21, 2023
1 parent a301bbe commit 6944e20
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/ansys/dpf/core/data_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,25 @@ def set_result_file_path(self, filepath, key=""):
['/tmp/file.rst']
"""
# Handle the case of files without extension, such as the LS-DYNA d3plot file
if os.path.splitext(filepath)[1] == "":
if "d3plot" in os.path.basename(filepath):
key = "d3plot"
# Handle no key given and no file extension
if key == "" and os.path.splitext(filepath)[1] == "":
key = self.guess_result_key(str(filepath))
if key == "":
self._api.data_sources_set_result_file_path_utf8(self, str(filepath))
else:
self._api.data_sources_set_result_file_path_with_key_utf8(self, str(filepath), key)

@staticmethod
def guess_result_key(filepath: str) -> str:
"""Guess result key for files without a file extension."""
result_keys = ["d3plot", "binout"]
base_name = os.path.basename(filepath)
# Handle files without extension
for result_key in result_keys:
if result_key in base_name:
return result_key
return ""

def set_domain_result_file_path(self, path, domain_id):
"""Add a result file path by domain.
Expand Down
6 changes: 5 additions & 1 deletion tests/test_datasources.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ def test_addfilepathspecifiedresult_data_sources(allkindofcomplexity, server_typ
data_sources.add_file_path_for_specified_result(allkindofcomplexity, "d3plot")


def test_setresultpath_data_sources_no_extension(d3plot_beam, server_type):
def test_setresultpath_data_sources_no_extension(d3plot_beam, binout_glstat, server_type):
data_sources = dpf.core.DataSources(server=server_type)
data_sources.set_result_file_path(d3plot_beam)
assert data_sources.result_key == "d3plot"
data_sources = dpf.core.DataSources(server=server_type)
data_sources.set_result_file_path(binout_glstat)
assert data_sources.result_key == "binout"


def test_addupstream_data_sources(allkindofcomplexity, server_type):
Expand Down

0 comments on commit 6944e20

Please sign in to comment.