@@ -307,27 +307,32 @@ def check_numeric_equivalence(a, b, relative_tolerance=1e-7):
307
307
308
308
309
309
@pyaedt_function_handler ()
310
- def check_and_download_file (local_path , remote_path , overwrite = True ):
310
+ def _check_path (path_to_check ):
311
+ return path_to_check .replace ("\\ " , "/" ) if path_to_check [0 ] != "\\ " else path_to_check
312
+
313
+
314
+ @pyaedt_function_handler ()
315
+ def check_and_download_file (remote_path , overwrite = True ):
311
316
"""Check if a file is remote and either download it or return the path.
312
317
313
318
Parameters
314
319
----------
315
- local_path : str
316
- Local path to save the file to.
317
320
remote_path : str
318
321
Path to the remote file.
319
322
overwrite : bool, optional
320
- Whether to overwrite the file if it already exits locally.
323
+ Whether to overwrite the file if it already exists locally.
321
324
The default is ``True``.
322
325
323
326
Returns
324
327
-------
325
328
str
326
329
"""
327
330
if settings .remote_rpc_session :
328
- remote_path = remote_path .replace ("\\ " , "/" ) if remote_path [0 ] != "\\ " else remote_path
329
- settings .remote_rpc_session .filemanager .download_file (remote_path , local_path , overwrite = overwrite )
330
- return local_path
331
+ remote_path = _check_path (remote_path )
332
+ local_path = os .path .join (settings .remote_rpc_session_temp_folder , os .path .split (remote_path )[- 1 ])
333
+ if settings .remote_rpc_session .filemanager .pathexists (remote_path ):
334
+ settings .remote_rpc_session .filemanager .download_file (remote_path , local_path , overwrite = overwrite )
335
+ return local_path
331
336
return remote_path
332
337
333
338
@@ -359,7 +364,7 @@ def check_and_download_folder(local_path, remote_path, overwrite=True):
359
364
remote_path : str
360
365
Path to the remote folder.
361
366
overwrite : bool, optional
362
- Whether to overwrite the folder if it already exits locally.
367
+ Whether to overwrite the folder if it already exists locally.
363
368
The default is ``True``.
364
369
365
370
Returns
@@ -373,7 +378,7 @@ def check_and_download_folder(local_path, remote_path, overwrite=True):
373
378
return remote_path
374
379
375
380
376
- def open_file (file_path , file_options = "r" ):
381
+ def open_file (file_path , file_options = "r" , encoding = None , override_existing = True ):
377
382
"""Open a file and return the object.
378
383
379
384
Parameters
@@ -382,27 +387,41 @@ def open_file(file_path, file_options="r"):
382
387
Full absolute path to the file (either local or remote).
383
388
file_options : str, optional
384
389
Options for opening the file.
390
+ encoding : str, optional
391
+ Name of the encoding used to decode or encode the file.
392
+ The default is ``None``, which means a platform-dependent encoding is used. You can
393
+ specify any encoding supported by Python.
394
+ override_existing : bool, optional
395
+ Whether to override an existing file if opening a file in write mode on a remote
396
+ machine. The default is ``True``.
385
397
386
398
Returns
387
399
-------
388
400
object
389
401
Opened file.
390
402
"""
403
+ file_path = str (file_path )
391
404
file_path = file_path .replace ("\\ " , "/" ) if file_path [0 ] != "\\ " else file_path
405
+
392
406
dir_name = os .path .dirname (file_path )
393
407
if "r" in file_options :
394
408
if os .path .exists (file_path ):
395
- return open (file_path , file_options )
409
+ return open (file_path , file_options , encoding = encoding )
396
410
elif settings .remote_rpc_session and settings .remote_rpc_session .filemanager .pathexists (
397
411
file_path
398
412
): # pragma: no cover
399
413
local_file = os .path .join (tempfile .gettempdir (), os .path .split (file_path )[- 1 ])
400
414
settings .remote_rpc_session .filemanager .download_file (file_path , local_file )
401
- return open (local_file , file_options )
415
+ return open (local_file , file_options , encoding = encoding )
402
416
elif os .path .exists (dir_name ):
403
- return open (file_path , file_options )
417
+ return open (file_path , file_options , encoding = encoding )
404
418
elif settings .remote_rpc_session and settings .remote_rpc_session .filemanager .pathexists (dir_name ):
405
- return settings .remote_rpc_session .open_file (file_path , file_options )
419
+ if "w" in file_options :
420
+ return settings .remote_rpc_session .create_file (
421
+ file_path , file_options , encoding = encoding , override = override_existing
422
+ )
423
+ else :
424
+ return settings .remote_rpc_session .open_file (file_path , file_options , encoding = encoding )
406
425
else :
407
426
settings .logger .error ("The file or folder %s does not exist" , dir_name )
408
427
@@ -447,7 +466,7 @@ def read_json(fn):
447
466
dict
448
467
"""
449
468
json_data = {}
450
- with open (fn ) as json_file :
469
+ with open_file (fn ) as json_file :
451
470
try :
452
471
json_data = json .load (json_file )
453
472
except json .JSONDecodeError as e : # pragma: no cover
@@ -865,6 +884,11 @@ def is_project_locked(project_path):
865
884
bool
866
885
``True`` when successful, ``False`` when failed.
867
886
"""
887
+ if settings .remote_rpc_session :
888
+ if settings .remote_rpc_session .filemanager .pathexists (project_path + ".lock" ):
889
+ return True
890
+ else :
891
+ return False
868
892
return check_if_path_exists (project_path + ".lock" )
869
893
870
894
@@ -885,6 +909,9 @@ def remove_project_lock(project_path):
885
909
bool
886
910
``True`` when successful, ``False`` when failed.
887
911
"""
912
+ if settings .remote_rpc_session and settings .remote_rpc_session .filemanager .pathexists (project_path + ".lock" ):
913
+ settings .remote_rpc_session .filemanager .unlink (project_path + ".lock" )
914
+ return True
888
915
if os .path .exists (project_path + ".lock" ):
889
916
os .remove (project_path + ".lock" )
890
917
return True
@@ -906,6 +933,7 @@ def read_csv(filename, encoding="utf-8"):
906
933
list
907
934
908
935
"""
936
+ filename = check_and_download_file (filename )
909
937
910
938
lines = []
911
939
with codecs .open (filename , "rb" , encoding ) as csvfile :
@@ -931,6 +959,7 @@ def read_csv_pandas(filename, encoding="utf-8"):
931
959
:class:`pandas.DataFrame`
932
960
933
961
"""
962
+ filename = check_and_download_file (filename )
934
963
try :
935
964
import pandas as pd
936
965
@@ -973,6 +1002,7 @@ def read_xlsx(filename):
973
1002
list
974
1003
975
1004
"""
1005
+ filename = check_and_download_file (filename )
976
1006
try :
977
1007
import pandas as pd
978
1008
@@ -1137,17 +1167,17 @@ def _create_json_file(json_dict, full_json_path):
1137
1167
if not os .path .exists (os .path .dirname (full_json_path )):
1138
1168
os .makedirs (os .path .dirname (full_json_path ))
1139
1169
if not is_ironpython :
1140
- with open (full_json_path , "w" ) as fp :
1170
+ with open_file (full_json_path , "w" ) as fp :
1141
1171
json .dump (json_dict , fp , indent = 4 )
1142
1172
else :
1143
1173
temp_path = full_json_path .replace (".json" , "_temp.json" )
1144
- with open (temp_path , "w" ) as fp :
1174
+ with open_file (temp_path , "w" ) as fp :
1145
1175
json .dump (json_dict , fp , indent = 4 )
1146
- with open (temp_path , "r" ) as file :
1176
+ with open_file (temp_path , "r" ) as file :
1147
1177
filedata = file .read ()
1148
1178
filedata = filedata .replace ("True" , "true" )
1149
1179
filedata = filedata .replace ("False" , "false" )
1150
- with open (full_json_path , "w" ) as file :
1180
+ with open_file (full_json_path , "w" ) as file :
1151
1181
file .write (filedata )
1152
1182
os .remove (temp_path )
1153
1183
return True
@@ -1635,7 +1665,7 @@ def tech_to_control_file(tech_path, unit="nm", control_path=None):
1635
1665
Out xml file.
1636
1666
"""
1637
1667
result = []
1638
- with open (tech_path ) as f :
1668
+ with open_file (tech_path ) as f :
1639
1669
vals = list (CSS4_COLORS .values ())
1640
1670
id_layer = 0
1641
1671
for line in f :
@@ -1656,7 +1686,7 @@ def tech_to_control_file(tech_path, unit="nm", control_path=None):
1656
1686
unit = line_split [1 ]
1657
1687
if not control_path :
1658
1688
control_path = os .path .splitext (tech_path )[0 ] + ".xml"
1659
- with open (control_path , "w" ) as f :
1689
+ with open_file (control_path , "w" ) as f :
1660
1690
f .write ('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n ' )
1661
1691
f .write (' <c:Control xmlns:c="http://www.ansys.com/control" schemaVersion="1.0">\n ' )
1662
1692
f .write ("\n " )
@@ -1963,7 +1993,7 @@ def _check_installed_version(install_path, long_version):
1963
1993
product_list_path = os .path .join (install_path , "config" , "ProductList.txt" )
1964
1994
if os .path .isfile (product_list_path ):
1965
1995
try :
1966
- with open (product_list_path , "r" ) as f :
1996
+ with open_file (product_list_path , "r" ) as f :
1967
1997
install_version = f .readline ().strip ()[- 6 :]
1968
1998
if install_version == long_version :
1969
1999
return True
0 commit comments