Skip to content

Commit 272daff

Browse files
authored
Merge branch 'main' into refactoring_scattering
2 parents 1cfed70 + 90f0caa commit 272daff

20 files changed

+130
-86
lines changed

pyaedt/application/Analysis.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2091,7 +2091,7 @@ def solve_in_batch(
20912091
if machine == "localhost":
20922092
while not os.path.exists(queue_file):
20932093
time.sleep(0.5)
2094-
with open(queue_file, "r") as f:
2094+
with open_file(queue_file, "r") as f:
20952095
lines = f.readlines()
20962096
for line in lines:
20972097
if "JobID" in line:

pyaedt/application/Design.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,7 @@ def project_properties(self):
512512
and settings.remote_rpc_session
513513
and settings.remote_rpc_session.filemanager.pathexists(self.project_file)
514514
):
515-
local_path = os.path.join(settings.remote_rpc_session_temp_folder, os.path.split(self.project_file)[-1])
516-
file_path = check_and_download_file(local_path, self.project_file)
515+
file_path = check_and_download_file(self.project_file)
517516
try:
518517
settings._project_properties[os.path.normpath(self.project_file)] = load_entire_aedt_file(file_path)
519518
except Exception:

pyaedt/common_rpc.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,11 @@
119119

120120

121121
def pyaedt_service_manager(port=17878, aedt_version=None, student_version=False):
122-
"""Starts an RPyC server on CPython and listens on a specified port.
122+
"""Starts the PyAEDT service manager using RPyC server on CPython.
123123
124-
This method must run on a server machine.
124+
This method, which must run on a server machine, is used as a service on the
125+
server machine to listen on a dedicated port for inbound requests to launch
126+
a new server connection and launch AEDT.
125127
126128
Parameters
127129
----------
@@ -252,7 +254,7 @@ def launch_server(port=18000, ansysem_path=None, non_graphical=False, threaded=T
252254

253255
def create_session(server_name, client_port=None, launch_aedt_on_server=False, aedt_port=None, non_graphical=True):
254256
"""
255-
Connect to an existing AEDT server session.
257+
Connect to an existing AEDT server session and create a new client session from it.
256258
257259
Parameters
258260
----------
@@ -266,9 +268,10 @@ def create_session(server_name, client_port=None, launch_aedt_on_server=False, a
266268
Aedt Grpc port on server.
267269
non_graphical : bool, optional
268270
Aedt Non Graphical Flag.
271+
269272
Returns
270273
-------
271-
RPyC object.
274+
RPyC client object.
272275
"""
273276
try:
274277
client = rpyc.connect(
@@ -314,7 +317,7 @@ def connect(server_name, aedt_client_port):
314317
315318
Returns
316319
-------
317-
RPyC object.
320+
RPyC client object.
318321
"""
319322
try:
320323
client = rpyc.connect(

pyaedt/desktop.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1647,7 +1647,7 @@ def change_registry_from_file(self, registry_file, make_active=True):
16471647
try:
16481648
self.odesktop.SetRegistryFromFile(registry_file)
16491649
if make_active:
1650-
with open(registry_file, "r") as f:
1650+
with open_file(registry_file, "r") as f:
16511651
for line in f:
16521652
stripped_line = line.strip()
16531653
if "ConfigName" in stripped_line:
@@ -1795,8 +1795,8 @@ def add_script_to_menu(
17951795
dst = os.path.join(tool_dir, file_name.replace("_", " ") + ".py")
17961796
if not os.path.isfile(src):
17971797
raise FileNotFoundError("File not found: {}".format(src))
1798-
with open(src, "r") as build_file:
1799-
with open(dst, "w") as out_file:
1798+
with open_file(src, "r") as build_file:
1799+
with open_file(dst, "w") as out_file:
18001800
self.logger.info("Building to " + dst)
18011801
build_file_data = build_file.read()
18021802
build_file_data = (
@@ -2099,10 +2099,10 @@ def get_ansyscloud_job_info(self, job_id=None, job_name=None): # pragma: no cov
20992099
elif job_id:
21002100
command = [command, "jobinfo", "-i", job_id]
21012101
cloud_info = os.path.join(tempfile.gettempdir(), generate_unique_name("job_info"))
2102-
with open(cloud_info, "w") as outfile:
2102+
with open_file(cloud_info, "w") as outfile:
21032103
subprocess.Popen(" ".join(command), stdout=outfile).wait()
21042104
out = {}
2105-
with open(cloud_info, "r") as infile:
2105+
with open_file(cloud_info, "r") as infile:
21062106
lines = infile.readlines()
21072107
for i in lines:
21082108
if ":" in i.strip():
@@ -2205,11 +2205,11 @@ def get_available_cloud_config(self, region="westeurope"): # pragma: no cover
22052205
ver = self.aedt_version_id.replace(".", "R")
22062206
command = [command, "getQueues", "-p", "AEDT", "-v", ver, "--details"]
22072207
cloud_info = os.path.join(tempfile.gettempdir(), generate_unique_name("cloud_info"))
2208-
with open(cloud_info, "w") as outfile:
2208+
with open_file(cloud_info, "w") as outfile:
22092209
subprocess.Popen(" ".join(command), stdout=outfile).wait()
22102210

22112211
dict_out = {}
2212-
with open(cloud_info, "r") as infile:
2212+
with open_file(cloud_info, "r") as infile:
22132213
lines = infile.readlines()
22142214
for i in range(len(lines)):
22152215
line = lines[i].strip()

pyaedt/generic/com_parameters.py

Whitespace-only changes.

pyaedt/generic/configurations.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from pyaedt.generic.LoadAEDTFile import load_keyword_in_aedt_file
1717
from pyaedt.generic.general_methods import GrpcApiError
1818
from pyaedt.generic.general_methods import generate_unique_name
19+
from pyaedt.generic.general_methods import open_file
1920
from pyaedt.generic.general_methods import pyaedt_function_handler
2021
from pyaedt.generic.general_methods import read_configuration_file
2122
from pyaedt.generic.general_methods import write_configuration_file
@@ -1232,7 +1233,7 @@ def _export_general(self, dict_out):
12321233
if list(self._app.output_variables):
12331234
oo_out = os.path.join(tempfile.gettempdir(), generate_unique_name("oo") + ".txt")
12341235
self._app.ooutput_variable.ExportOutputVariables(oo_out)
1235-
with open(oo_out, "r") as f:
1236+
with open_file(oo_out, "r") as f:
12361237
lines = f.readlines()
12371238
for line in lines:
12381239
line_split = line.split(" ")

pyaedt/generic/general_methods.py

+51-21
Original file line numberDiff line numberDiff line change
@@ -307,27 +307,32 @@ def check_numeric_equivalence(a, b, relative_tolerance=1e-7):
307307

308308

309309
@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):
311316
"""Check if a file is remote and either download it or return the path.
312317
313318
Parameters
314319
----------
315-
local_path : str
316-
Local path to save the file to.
317320
remote_path : str
318321
Path to the remote file.
319322
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.
321324
The default is ``True``.
322325
323326
Returns
324327
-------
325328
str
326329
"""
327330
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
331336
return remote_path
332337

333338

@@ -359,7 +364,7 @@ def check_and_download_folder(local_path, remote_path, overwrite=True):
359364
remote_path : str
360365
Path to the remote folder.
361366
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.
363368
The default is ``True``.
364369
365370
Returns
@@ -373,7 +378,7 @@ def check_and_download_folder(local_path, remote_path, overwrite=True):
373378
return remote_path
374379

375380

376-
def open_file(file_path, file_options="r"):
381+
def open_file(file_path, file_options="r", encoding=None, override_existing=True):
377382
"""Open a file and return the object.
378383
379384
Parameters
@@ -382,27 +387,41 @@ def open_file(file_path, file_options="r"):
382387
Full absolute path to the file (either local or remote).
383388
file_options : str, optional
384389
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``.
385397
386398
Returns
387399
-------
388400
object
389401
Opened file.
390402
"""
403+
file_path = str(file_path)
391404
file_path = file_path.replace("\\", "/") if file_path[0] != "\\" else file_path
405+
392406
dir_name = os.path.dirname(file_path)
393407
if "r" in file_options:
394408
if os.path.exists(file_path):
395-
return open(file_path, file_options)
409+
return open(file_path, file_options, encoding=encoding)
396410
elif settings.remote_rpc_session and settings.remote_rpc_session.filemanager.pathexists(
397411
file_path
398412
): # pragma: no cover
399413
local_file = os.path.join(tempfile.gettempdir(), os.path.split(file_path)[-1])
400414
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)
402416
elif os.path.exists(dir_name):
403-
return open(file_path, file_options)
417+
return open(file_path, file_options, encoding=encoding)
404418
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)
406425
else:
407426
settings.logger.error("The file or folder %s does not exist", dir_name)
408427

@@ -447,7 +466,7 @@ def read_json(fn):
447466
dict
448467
"""
449468
json_data = {}
450-
with open(fn) as json_file:
469+
with open_file(fn) as json_file:
451470
try:
452471
json_data = json.load(json_file)
453472
except json.JSONDecodeError as e: # pragma: no cover
@@ -865,6 +884,11 @@ def is_project_locked(project_path):
865884
bool
866885
``True`` when successful, ``False`` when failed.
867886
"""
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
868892
return check_if_path_exists(project_path + ".lock")
869893

870894

@@ -885,6 +909,9 @@ def remove_project_lock(project_path):
885909
bool
886910
``True`` when successful, ``False`` when failed.
887911
"""
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
888915
if os.path.exists(project_path + ".lock"):
889916
os.remove(project_path + ".lock")
890917
return True
@@ -906,6 +933,7 @@ def read_csv(filename, encoding="utf-8"):
906933
list
907934
908935
"""
936+
filename = check_and_download_file(filename)
909937

910938
lines = []
911939
with codecs.open(filename, "rb", encoding) as csvfile:
@@ -931,6 +959,7 @@ def read_csv_pandas(filename, encoding="utf-8"):
931959
:class:`pandas.DataFrame`
932960
933961
"""
962+
filename = check_and_download_file(filename)
934963
try:
935964
import pandas as pd
936965

@@ -973,6 +1002,7 @@ def read_xlsx(filename):
9731002
list
9741003
9751004
"""
1005+
filename = check_and_download_file(filename)
9761006
try:
9771007
import pandas as pd
9781008

@@ -1137,17 +1167,17 @@ def _create_json_file(json_dict, full_json_path):
11371167
if not os.path.exists(os.path.dirname(full_json_path)):
11381168
os.makedirs(os.path.dirname(full_json_path))
11391169
if not is_ironpython:
1140-
with open(full_json_path, "w") as fp:
1170+
with open_file(full_json_path, "w") as fp:
11411171
json.dump(json_dict, fp, indent=4)
11421172
else:
11431173
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:
11451175
json.dump(json_dict, fp, indent=4)
1146-
with open(temp_path, "r") as file:
1176+
with open_file(temp_path, "r") as file:
11471177
filedata = file.read()
11481178
filedata = filedata.replace("True", "true")
11491179
filedata = filedata.replace("False", "false")
1150-
with open(full_json_path, "w") as file:
1180+
with open_file(full_json_path, "w") as file:
11511181
file.write(filedata)
11521182
os.remove(temp_path)
11531183
return True
@@ -1635,7 +1665,7 @@ def tech_to_control_file(tech_path, unit="nm", control_path=None):
16351665
Out xml file.
16361666
"""
16371667
result = []
1638-
with open(tech_path) as f:
1668+
with open_file(tech_path) as f:
16391669
vals = list(CSS4_COLORS.values())
16401670
id_layer = 0
16411671
for line in f:
@@ -1656,7 +1686,7 @@ def tech_to_control_file(tech_path, unit="nm", control_path=None):
16561686
unit = line_split[1]
16571687
if not control_path:
16581688
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:
16601690
f.write('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n')
16611691
f.write(' <c:Control xmlns:c="http://www.ansys.com/control" schemaVersion="1.0">\n')
16621692
f.write("\n")
@@ -1963,7 +1993,7 @@ def _check_installed_version(install_path, long_version):
19631993
product_list_path = os.path.join(install_path, "config", "ProductList.txt")
19641994
if os.path.isfile(product_list_path):
19651995
try:
1966-
with open(product_list_path, "r") as f:
1996+
with open_file(product_list_path, "r") as f:
19671997
install_version = f.readline().strip()[-6:]
19681998
if install_version == long_version:
19691999
return True

pyaedt/generic/ibis_reader.py

+6-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pyaedt.aedt_logger import pyaedt_logger as logger
88
from pyaedt.generic.general_methods import check_and_download_file
99
from pyaedt.generic.general_methods import check_if_path_exists
10-
from pyaedt.generic.settings import settings
10+
from pyaedt.generic.general_methods import open_file
1111

1212

1313
class Component:
@@ -799,11 +799,8 @@ def parse_ibis_file(self):
799799

800800
ibis_name = pyaedt.generic.general_methods.get_filename_without_extension(self._filename)
801801
ibis = Ibis(ibis_name, self._circuit)
802-
if settings.remote_rpc_session_temp_folder:
803-
local_path = os.path.join(settings.remote_rpc_session_temp_folder, os.path.split(self._filename)[-1])
804-
file_to_open = check_and_download_file(local_path, self._filename)
805-
else:
806-
file_to_open = self._filename
802+
803+
check_and_download_file(self._filename)
807804

808805
# Read *.ibis file.
809806
ibis_info = ibis_parsing(self._filename)
@@ -1222,11 +1219,7 @@ def parse_ibis_file(self):
12221219

12231220
ami_name = pyaedt.generic.general_methods.get_filename_without_extension(self._filename)
12241221
ibis = AMI(ami_name, self._circuit)
1225-
if settings.remote_rpc_session_temp_folder:
1226-
local_path = os.path.join(settings.remote_rpc_session_temp_folder, os.path.split(self._filename)[-1])
1227-
file_to_open = check_and_download_file(local_path, self._filename)
1228-
else:
1229-
file_to_open = self._filename
1222+
check_and_download_file(self._filename)
12301223

12311224
# Read *.ibis file.
12321225
ibis_info = ibis_parsing(self._filename)
@@ -1342,10 +1335,10 @@ def ibis_parsing(file):
13421335
"""
13431336
ibis = {}
13441337
# OPEN AND READ IBIS FILE
1345-
with open(file, "r") as fp:
1338+
with open_file(file, "r") as fp:
13461339
ibis_data = list(enumerate(fp))
13471340

1348-
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "ibis_v7.json"), "r") as f:
1341+
with open_file(os.path.join(os.path.dirname(os.path.abspath(__file__)), "ibis_v7.json"), "r") as f:
13491342
ibis_ref = json.load(f)
13501343
ibis_ref = lowercase_json(ibis_ref)
13511344

0 commit comments

Comments
 (0)