Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace open_file to all the methods which requires open #4274

Merged
merged 28 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
1527306
added open_file to all the methods which requires open
Feb 23, 2024
2970d86
Merge branch 'main' into improve_remote_file_access
Samuelopez-ansys Feb 23, 2024
1c66915
Fix Codacy
Samuelopez-ansys Feb 23, 2024
373940a
Merge branch 'main' into improve_remote_file_access
Samuelopez-ansys Feb 23, 2024
bcff48b
added encoding option to open_file method
Feb 23, 2024
86a6095
Merge branch 'main' into improve_remote_file_access
Feb 23, 2024
fa98a8a
added encoding option to open_file method
Feb 23, 2024
b9af30a
Update pyaedt/common_rpc.py
maxcapodi78 Mar 28, 2024
3337dd4
Update pyaedt/generic/general_methods.py
maxcapodi78 Mar 28, 2024
7a5245e
Update pyaedt/generic/general_methods.py
maxcapodi78 Mar 28, 2024
3e23f38
Merge branch 'main' into improve_remote_file_access
Mar 28, 2024
be7e87c
Merge remote-tracking branch 'origin/improve_remote_file_access' into…
Mar 28, 2024
b296a1c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 28, 2024
31261b3
moved IcepakPostProcessor
Mar 28, 2024
9d1d04c
Merge remote-tracking branch 'origin/improve_remote_file_access' into…
Mar 28, 2024
6c1ae94
Merge branch 'main' into improve_remote_file_access
Apr 4, 2024
2419637
fixed test
Apr 4, 2024
4bab53c
Update pyaedt/common_rpc.py
maxcapodi78 Apr 4, 2024
76cadc7
fixed test
Apr 4, 2024
4d0bc95
Merge remote-tracking branch 'origin/improve_remote_file_access' into…
Apr 4, 2024
442499f
fixed test
Apr 4, 2024
a225c03
Merge branch 'main' into improve_remote_file_access
Apr 4, 2024
486eacd
fixed typo
Apr 4, 2024
73e2323
fixed typo
Apr 4, 2024
1edd089
Update pyaedt/common_rpc.py
maxcapodi78 Apr 5, 2024
cb9e4a2
Merge branch 'main' into improve_remote_file_access
Apr 5, 2024
59619ec
Merge branch 'improve_remote_file_access' of https://github.com/pyans…
Apr 5, 2024
65af38c
Merge branch 'main' into improve_remote_file_access
maxcapodi78 Apr 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyaedt/application/Analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2050,7 +2050,7 @@ def solve_in_batch(
if machine == "localhost":
while not os.path.exists(queue_file):
time.sleep(0.5)
with open(queue_file, "r") as f:
with open_file(queue_file, "r") as f:
lines = f.readlines()
for line in lines:
if "JobID" in line:
Expand Down
3 changes: 1 addition & 2 deletions pyaedt/application/Design.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,7 @@ def project_properties(self):
and settings.remote_rpc_session
and settings.remote_rpc_session.filemanager.pathexists(self.project_file)
):
local_path = os.path.join(settings.remote_rpc_session_temp_folder, os.path.split(self.project_file)[-1])
file_path = check_and_download_file(local_path, self.project_file)
file_path = check_and_download_file(self.project_file)
try:
settings._project_properties[os.path.normpath(self.project_file)] = load_entire_aedt_file(file_path)
except Exception:
Expand Down
13 changes: 8 additions & 5 deletions pyaedt/common_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@


def pyaedt_service_manager(port=17878, aedt_version=None, student_version=False):
"""Starts an RPyC server on CPython and listens on a specified port.
"""Starts the PyAEDT service manager using RPyC server on CPython.

This method must run on a server machine.
This method, which must run on a server machine, is used as a service on the
server machine to listen on a dedicated port for inbound requests to launch
a new server connection and launch AEDT.

Parameters
----------
Expand Down Expand Up @@ -252,7 +254,7 @@ def launch_server(port=18000, ansysem_path=None, non_graphical=False, threaded=T

def create_session(server_name, client_port=None, launch_aedt_on_server=False, aedt_port=None, non_graphical=True):
"""
Connect to an existing AEDT server session.
Connect to an existing AEDT server session and create a new client session from it.

Parameters
----------
Expand All @@ -266,9 +268,10 @@ def create_session(server_name, client_port=None, launch_aedt_on_server=False, a
Aedt Grpc port on server.
non_graphical : bool, optional
Aedt Non Graphical Flag.

Returns
-------
RPyC object.
RPyC client object.
"""
try:
client = rpyc.connect(
Expand Down Expand Up @@ -314,7 +317,7 @@ def connect(server_name, aedt_client_port):

Returns
-------
RPyC object.
RPyC client object.
"""
try:
client = rpyc.connect(
Expand Down
14 changes: 7 additions & 7 deletions pyaedt/desktop.py
Original file line number Diff line number Diff line change
Expand Up @@ -1647,7 +1647,7 @@ def change_registry_from_file(self, registry_file, make_active=True):
try:
self.odesktop.SetRegistryFromFile(registry_file)
if make_active:
with open(registry_file, "r") as f:
with open_file(registry_file, "r") as f:
for line in f:
stripped_line = line.strip()
if "ConfigName" in stripped_line:
Expand Down Expand Up @@ -1795,8 +1795,8 @@ def add_script_to_menu(
dst = os.path.join(tool_dir, file_name.replace("_", " ") + ".py")
if not os.path.isfile(src):
raise FileNotFoundError("File not found: {}".format(src))
with open(src, "r") as build_file:
with open(dst, "w") as out_file:
with open_file(src, "r") as build_file:
with open_file(dst, "w") as out_file:
self.logger.info("Building to " + dst)
build_file_data = build_file.read()
build_file_data = (
Expand Down Expand Up @@ -2099,10 +2099,10 @@ def get_ansyscloud_job_info(self, job_id=None, job_name=None): # pragma: no cov
elif job_id:
command = [command, "jobinfo", "-i", job_id]
cloud_info = os.path.join(tempfile.gettempdir(), generate_unique_name("job_info"))
with open(cloud_info, "w") as outfile:
with open_file(cloud_info, "w") as outfile:
subprocess.Popen(" ".join(command), stdout=outfile).wait()
out = {}
with open(cloud_info, "r") as infile:
with open_file(cloud_info, "r") as infile:
lines = infile.readlines()
for i in lines:
if ":" in i.strip():
Expand Down Expand Up @@ -2205,11 +2205,11 @@ def get_available_cloud_config(self, region="westeurope"): # pragma: no cover
ver = self.aedt_version_id.replace(".", "R")
command = [command, "getQueues", "-p", "AEDT", "-v", ver, "--details"]
cloud_info = os.path.join(tempfile.gettempdir(), generate_unique_name("cloud_info"))
with open(cloud_info, "w") as outfile:
with open_file(cloud_info, "w") as outfile:
subprocess.Popen(" ".join(command), stdout=outfile).wait()

dict_out = {}
with open(cloud_info, "r") as infile:
with open_file(cloud_info, "r") as infile:
lines = infile.readlines()
for i in range(len(lines)):
line = lines[i].strip()
Expand Down
Empty file.
3 changes: 2 additions & 1 deletion pyaedt/generic/configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from pyaedt.generic.LoadAEDTFile import load_keyword_in_aedt_file
from pyaedt.generic.general_methods import GrpcApiError
from pyaedt.generic.general_methods import generate_unique_name
from pyaedt.generic.general_methods import open_file
from pyaedt.generic.general_methods import pyaedt_function_handler
from pyaedt.generic.general_methods import read_configuration_file
from pyaedt.generic.general_methods import write_configuration_file
Expand Down Expand Up @@ -1232,7 +1233,7 @@ def _export_general(self, dict_out):
if list(self._app.output_variables):
oo_out = os.path.join(tempfile.gettempdir(), generate_unique_name("oo") + ".txt")
self._app.ooutput_variable.ExportOutputVariables(oo_out)
with open(oo_out, "r") as f:
with open_file(oo_out, "r") as f:
lines = f.readlines()
for line in lines:
line_split = line.split(" ")
Expand Down
72 changes: 51 additions & 21 deletions pyaedt/generic/general_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,27 +307,32 @@ def check_numeric_equivalence(a, b, relative_tolerance=1e-7):


@pyaedt_function_handler()
def check_and_download_file(local_path, remote_path, overwrite=True):
def _check_path(path_to_check):
return path_to_check.replace("\\", "/") if path_to_check[0] != "\\" else path_to_check


@pyaedt_function_handler()
def check_and_download_file(remote_path, overwrite=True):
"""Check if a file is remote and either download it or return the path.

Parameters
----------
local_path : str
Local path to save the file to.
remote_path : str
Path to the remote file.
overwrite : bool, optional
Whether to overwrite the file if it already exits locally.
Whether to overwrite the file if it already exists locally.
The default is ``True``.

Returns
-------
str
"""
if settings.remote_rpc_session:
remote_path = remote_path.replace("\\", "/") if remote_path[0] != "\\" else remote_path
settings.remote_rpc_session.filemanager.download_file(remote_path, local_path, overwrite=overwrite)
return local_path
remote_path = _check_path(remote_path)
local_path = os.path.join(settings.remote_rpc_session_temp_folder, os.path.split(remote_path)[-1])
if settings.remote_rpc_session.filemanager.pathexists(remote_path):
settings.remote_rpc_session.filemanager.download_file(remote_path, local_path, overwrite=overwrite)
return local_path
return remote_path


Expand Down Expand Up @@ -359,7 +364,7 @@ def check_and_download_folder(local_path, remote_path, overwrite=True):
remote_path : str
Path to the remote folder.
overwrite : bool, optional
Whether to overwrite the folder if it already exits locally.
Whether to overwrite the folder if it already exists locally.
The default is ``True``.

Returns
Expand All @@ -373,7 +378,7 @@ def check_and_download_folder(local_path, remote_path, overwrite=True):
return remote_path


def open_file(file_path, file_options="r"):
def open_file(file_path, file_options="r", encoding=None, override_existing=True):
"""Open a file and return the object.

Parameters
Expand All @@ -382,27 +387,41 @@ def open_file(file_path, file_options="r"):
Full absolute path to the file (either local or remote).
file_options : str, optional
Options for opening the file.
encoding : str, optional
Name of the encoding used to decode or encode the file.
The default is ``None``, which means a platform-dependent encoding is used. You can
specify any encoding supported by Python.
override_existing : bool, optional
Whether to override an existing file if opening a file in write mode on a remote
machine. The default is ``True``.

Returns
-------
object
Opened file.
"""
file_path = str(file_path)
file_path = file_path.replace("\\", "/") if file_path[0] != "\\" else file_path

dir_name = os.path.dirname(file_path)
if "r" in file_options:
if os.path.exists(file_path):
return open(file_path, file_options)
return open(file_path, file_options, encoding=encoding)
elif settings.remote_rpc_session and settings.remote_rpc_session.filemanager.pathexists(
file_path
): # pragma: no cover
local_file = os.path.join(tempfile.gettempdir(), os.path.split(file_path)[-1])
settings.remote_rpc_session.filemanager.download_file(file_path, local_file)
return open(local_file, file_options)
return open(local_file, file_options, encoding=encoding)
elif os.path.exists(dir_name):
return open(file_path, file_options)
return open(file_path, file_options, encoding=encoding)
elif settings.remote_rpc_session and settings.remote_rpc_session.filemanager.pathexists(dir_name):
return settings.remote_rpc_session.open_file(file_path, file_options)
if "w" in file_options:
return settings.remote_rpc_session.create_file(
file_path, file_options, encoding=encoding, override=override_existing
)
else:
return settings.remote_rpc_session.open_file(file_path, file_options, encoding=encoding)
else:
settings.logger.error("The file or folder %s does not exist", dir_name)

Expand Down Expand Up @@ -447,7 +466,7 @@ def read_json(fn):
dict
"""
json_data = {}
with open(fn) as json_file:
with open_file(fn) as json_file:
try:
json_data = json.load(json_file)
except json.JSONDecodeError as e: # pragma: no cover
Expand Down Expand Up @@ -865,6 +884,11 @@ def is_project_locked(project_path):
bool
``True`` when successful, ``False`` when failed.
"""
if settings.remote_rpc_session:
if settings.remote_rpc_session.filemanager.pathexists(project_path + ".lock"):
return True
else:
return False
return check_if_path_exists(project_path + ".lock")


Expand All @@ -885,6 +909,9 @@ def remove_project_lock(project_path):
bool
``True`` when successful, ``False`` when failed.
"""
if settings.remote_rpc_session and settings.remote_rpc_session.filemanager.pathexists(project_path + ".lock"):
settings.remote_rpc_session.filemanager.unlink(project_path + ".lock")
return True
if os.path.exists(project_path + ".lock"):
os.remove(project_path + ".lock")
return True
Expand All @@ -906,6 +933,7 @@ def read_csv(filename, encoding="utf-8"):
list

"""
filename = check_and_download_file(filename)

lines = []
with codecs.open(filename, "rb", encoding) as csvfile:
Expand All @@ -931,6 +959,7 @@ def read_csv_pandas(filename, encoding="utf-8"):
:class:`pandas.DataFrame`

"""
filename = check_and_download_file(filename)
try:
import pandas as pd

Expand Down Expand Up @@ -973,6 +1002,7 @@ def read_xlsx(filename):
list

"""
filename = check_and_download_file(filename)
try:
import pandas as pd

Expand Down Expand Up @@ -1137,17 +1167,17 @@ def _create_json_file(json_dict, full_json_path):
if not os.path.exists(os.path.dirname(full_json_path)):
os.makedirs(os.path.dirname(full_json_path))
if not is_ironpython:
with open(full_json_path, "w") as fp:
with open_file(full_json_path, "w") as fp:
json.dump(json_dict, fp, indent=4)
else:
temp_path = full_json_path.replace(".json", "_temp.json")
with open(temp_path, "w") as fp:
with open_file(temp_path, "w") as fp:
json.dump(json_dict, fp, indent=4)
with open(temp_path, "r") as file:
with open_file(temp_path, "r") as file:
filedata = file.read()
filedata = filedata.replace("True", "true")
filedata = filedata.replace("False", "false")
with open(full_json_path, "w") as file:
with open_file(full_json_path, "w") as file:
file.write(filedata)
os.remove(temp_path)
return True
Expand Down Expand Up @@ -1635,7 +1665,7 @@ def tech_to_control_file(tech_path, unit="nm", control_path=None):
Out xml file.
"""
result = []
with open(tech_path) as f:
with open_file(tech_path) as f:
vals = list(CSS4_COLORS.values())
id_layer = 0
for line in f:
Expand All @@ -1656,7 +1686,7 @@ def tech_to_control_file(tech_path, unit="nm", control_path=None):
unit = line_split[1]
if not control_path:
control_path = os.path.splitext(tech_path)[0] + ".xml"
with open(control_path, "w") as f:
with open_file(control_path, "w") as f:
f.write('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n')
f.write(' <c:Control xmlns:c="http://www.ansys.com/control" schemaVersion="1.0">\n')
f.write("\n")
Expand Down Expand Up @@ -1963,7 +1993,7 @@ def _check_installed_version(install_path, long_version):
product_list_path = os.path.join(install_path, "config", "ProductList.txt")
if os.path.isfile(product_list_path):
try:
with open(product_list_path, "r") as f:
with open_file(product_list_path, "r") as f:
install_version = f.readline().strip()[-6:]
if install_version == long_version:
return True
Expand Down
19 changes: 6 additions & 13 deletions pyaedt/generic/ibis_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pyaedt.aedt_logger import pyaedt_logger as logger
from pyaedt.generic.general_methods import check_and_download_file
from pyaedt.generic.general_methods import check_if_path_exists
from pyaedt.generic.settings import settings
from pyaedt.generic.general_methods import open_file


class Component:
Expand Down Expand Up @@ -799,11 +799,8 @@ def parse_ibis_file(self):

ibis_name = pyaedt.generic.general_methods.get_filename_without_extension(self._filename)
ibis = Ibis(ibis_name, self._circuit)
if settings.remote_rpc_session_temp_folder:
local_path = os.path.join(settings.remote_rpc_session_temp_folder, os.path.split(self._filename)[-1])
file_to_open = check_and_download_file(local_path, self._filename)
else:
file_to_open = self._filename

check_and_download_file(self._filename)

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

ami_name = pyaedt.generic.general_methods.get_filename_without_extension(self._filename)
ibis = AMI(ami_name, self._circuit)
if settings.remote_rpc_session_temp_folder:
local_path = os.path.join(settings.remote_rpc_session_temp_folder, os.path.split(self._filename)[-1])
file_to_open = check_and_download_file(local_path, self._filename)
else:
file_to_open = self._filename
check_and_download_file(self._filename)

# Read *.ibis file.
ibis_info = ibis_parsing(self._filename)
Expand Down Expand Up @@ -1342,10 +1335,10 @@ def ibis_parsing(file):
"""
ibis = {}
# OPEN AND READ IBIS FILE
with open(file, "r") as fp:
with open_file(file, "r") as fp:
ibis_data = list(enumerate(fp))

with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "ibis_v7.json"), "r") as f:
with open_file(os.path.join(os.path.dirname(os.path.abspath(__file__)), "ibis_v7.json"), "r") as f:
ibis_ref = json.load(f)
ibis_ref = lowercase_json(ibis_ref)

Expand Down
Loading
Loading