28
28
"""
29
29
30
30
import os
31
+ from pathlib import Path
31
32
import warnings
32
33
import traceback
33
34
from typing import Union
@@ -142,7 +143,7 @@ def set_result_file_path(self, filepath, key=""):
142
143
['/tmp/file.rst']
143
144
144
145
"""
145
- extension = os . path . splitext (filepath )[ 1 ]
146
+ extension = Path (filepath ). suffix
146
147
# Handle .res files from CFX
147
148
if key == "" and extension == ".res" :
148
149
key = "cas"
@@ -162,7 +163,7 @@ def set_result_file_path(self, filepath, key=""):
162
163
def guess_result_key (filepath : str ) -> str :
163
164
"""Guess result key for files without a file extension."""
164
165
result_keys = ["d3plot" , "binout" ]
165
- base_name = os . path . basename (filepath )
166
+ base_name = Path (filepath ). name
166
167
# Handle files without extension
167
168
for result_key in result_keys :
168
169
if result_key in base_name :
@@ -172,14 +173,13 @@ def guess_result_key(filepath: str) -> str:
172
173
@staticmethod
173
174
def guess_second_key (filepath : str ) -> str :
174
175
"""For files with an h5 or cff extension, look for another extension."""
176
+
177
+ # These files usually end with .cas.h5 or .dat.h5
175
178
accepted = ["cas" , "dat" ]
176
- without_ext = os .path .splitext (filepath )[0 ]
177
- new_split = os .path .splitext (without_ext )
179
+ new_split = Path (filepath ).suffixes
178
180
new_key = ""
179
- if len (new_split ) > 1 :
180
- key = new_split [1 ][1 :]
181
- if key in accepted :
182
- new_key = key
181
+ if new_split [0 ].strip ("." ) in accepted :
182
+ new_key = new_split [0 ].strip ("." )
183
183
return new_key
184
184
185
185
def set_domain_result_file_path (
@@ -241,9 +241,12 @@ def add_file_path(self, filepath, key="", is_domain: bool = False, domain_id=0):
241
241
242
242
"""
243
243
# The filename needs to be a fully qualified file name
244
- if not os .path .dirname (filepath ):
244
+ # if not os.path.dirname(filepath)
245
+
246
+ filepath = Path (filepath )
247
+ if not filepath .parent .name :
245
248
# append local path
246
- filepath = os . path . join ( os . getcwd (), os . path . basename ( filepath ))
249
+ filepath = Path . cwd () / filepath . name
247
250
if is_domain :
248
251
if key == "" :
249
252
raise NotImplementedError ("A key must be given when using is_domain=True." )
@@ -280,9 +283,10 @@ def add_domain_file_path(self, filepath, key, domain_id):
280
283
281
284
"""
282
285
# The filename needs to be a fully qualified file name
283
- if not os .path .dirname (filepath ):
286
+ filepath = Path (filepath )
287
+ if not filepath .parent .name :
284
288
# append local path
285
- filepath = os . path . join ( os . getcwd (), os . path . basename ( filepath ))
289
+ filepath = Path . cwd () / filepath . name
286
290
self ._api .data_sources_add_domain_file_path_with_key_utf8 (
287
291
self , str (filepath ), key , domain_id
288
292
)
@@ -307,9 +311,10 @@ def add_file_path_for_specified_result(self, filepath, key="", result_key=""):
307
311
The default is ``""``, in which case the key is found directly.
308
312
"""
309
313
# The filename needs to be a fully qualified file name
310
- if not os .path .dirname (filepath ):
314
+ filepath = Path (filepath )
315
+ if not filepath .parent .name :
311
316
# append local path
312
- filepath = os . path . join ( os . getcwd (), os . path . basename ( filepath ))
317
+ filepath = Path . cwd () / filepath . name
313
318
314
319
self ._api .data_sources_add_file_path_for_specified_result_utf8 (
315
320
self , str (filepath ), key , result_key
0 commit comments