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 4 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 @@ -1999,7 +1999,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 @@ -534,8 +534,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:
Expand Down
14 changes: 7 additions & 7 deletions pyaedt/desktop.py
Original file line number Diff line number Diff line change
Expand Up @@ -1632,7 +1632,7 @@ def change_registry_from_file(self, registry_file, make_active=True):
try:
self._main.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 @@ -1780,8 +1780,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 @@ -2084,10 +2084,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 @@ -2190,11 +2190,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
5 changes: 3 additions & 2 deletions pyaedt/generic/com_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from pyaedt import pyaedt_function_handler
from pyaedt import settings
from pyaedt.generic.general_methods import open_file

logger = settings.logger

Expand Down Expand Up @@ -50,7 +51,7 @@ def load(self, file_path):
bool
"""
self._standard = "custom"
with open(file_path, "r") as fp:
with open_file(file_path, "r") as fp:
lines = fp.readlines()
for line in lines:
if not line.startswith("#") and "=" in line:
Expand All @@ -72,7 +73,7 @@ def export(self, file_path):
-------
bool
"""
with open(file_path, "w") as fp:
with open_file(file_path, "w") as fp:
fp.write("################################################################################\n")
fp.write("# MODULE: COM\n")
fp.write("# GENERATED ON\n")
Expand Down
3 changes: 2 additions & 1 deletion pyaedt/generic/configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from pyaedt.generic.DataHandlers import _arg2dict
from pyaedt.generic.LoadAEDTFile import load_keyword_in_aedt_file
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 @@ -1213,7 +1214,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
56 changes: 38 additions & 18 deletions pyaedt/generic/general_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,16 @@ 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
Expand All @@ -301,9 +304,11 @@ def check_and_download_file(local_path, remote_path, overwrite=True):
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 @@ -349,7 +354,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):
"""Open a file and return the object.

Parameters
Expand All @@ -358,6 +363,10 @@ 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 used is platform dependent, but any encoding supported by Python can be
passed.

Returns
-------
Expand All @@ -368,15 +377,15 @@ def open_file(file_path, file_options="r"):
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)
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)
elif os.path.exists(dir_name):
return open(file_path, file_options)
return open(file_path, file_options, 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)
else:
Expand Down Expand Up @@ -423,7 +432,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 @@ -841,6 +850,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 @@ -861,6 +875,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 @@ -882,6 +899,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 @@ -907,6 +925,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 @@ -949,6 +968,7 @@ def read_xlsx(filename):
list

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

Expand Down Expand Up @@ -1113,17 +1133,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 @@ -1611,7 +1631,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 @@ -1632,7 +1652,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 @@ -1939,7 +1959,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 @@ -620,11 +620,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 @@ -983,11 +980,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 @@ -1100,10 +1093,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
3 changes: 2 additions & 1 deletion pyaedt/generic/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from pyaedt import __version__
from pyaedt.generic.constants import unit_converter
from pyaedt.generic.general_methods import open_file


@dataclass
Expand Down Expand Up @@ -77,7 +78,7 @@ def read_template(self, template_file):
if template_file:
self.report_specs.template_name = template_file
if os.path.exists(self.report_specs.template_name):
with open(self.report_specs.template_name, "r") as f:
with open_file(self.report_specs.template_name, "r") as f:
tdata = json.load(f)
self.report_specs = ReportSpec(**tdata)

Expand Down
Loading
Loading