Skip to content

Commit bcff48b

Browse files
maxcapodi78maxcapodi78
maxcapodi78
authored and
maxcapodi78
committed
added encoding option to open_file method
1 parent 373940a commit bcff48b

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

pyaedt/generic/general_methods.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ def check_and_download_folder(local_path, remote_path, overwrite=True):
354354
return remote_path
355355

356356

357-
def open_file(file_path, file_options="r", encoding=None):
357+
def open_file(file_path, file_options="r", encoding=None, override_existing=True):
358358
"""Open a file and return the object.
359359
360360
Parameters
@@ -367,6 +367,8 @@ def open_file(file_path, file_options="r", encoding=None):
367367
Name of the encoding used to decode or encode the file.
368368
The default used is platform dependent, but any encoding supported by Python can be
369369
passed.
370+
override_existing : bool, optional
371+
Whether if override existing file in case of open file in write mode on remote machine.
370372
371373
Returns
372374
-------
@@ -377,17 +379,22 @@ def open_file(file_path, file_options="r", encoding=None):
377379
dir_name = os.path.dirname(file_path)
378380
if "r" in file_options:
379381
if os.path.exists(file_path):
380-
return open(file_path, file_options, encoding)
382+
return open(file_path, file_options, encoding=encoding)
381383
elif settings.remote_rpc_session and settings.remote_rpc_session.filemanager.pathexists(
382384
file_path
383385
): # pragma: no cover
384386
local_file = os.path.join(tempfile.gettempdir(), os.path.split(file_path)[-1])
385387
settings.remote_rpc_session.filemanager.download_file(file_path, local_file)
386-
return open(local_file, file_options, encoding)
388+
return open(local_file, file_options, encoding=encoding)
387389
elif os.path.exists(dir_name):
388-
return open(file_path, file_options, encoding)
390+
return open(file_path, file_options, encoding=encoding)
389391
elif settings.remote_rpc_session and settings.remote_rpc_session.filemanager.pathexists(dir_name):
390-
return settings.remote_rpc_session.open_file(file_path, file_options)
392+
if "w" in file_options:
393+
return settings.remote_rpc_session.create_file(
394+
file_path, file_options, encoding=encoding, override=override_existing
395+
)
396+
else:
397+
return settings.remote_rpc_session.open_file(file_path, file_options, encoding=encoding)
391398
else:
392399
settings.logger.error("The file or folder %s does not exist", dir_name)
393400

pyaedt/rpc/rpyc_services.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ def _download_dir(self, remotepath, localpath, overwrite=True):
147147
i += 1
148148
logger.info("Directory %s downloaded. %s files copied", localpath, i)
149149

150-
def open_file(self, remote_file, open_options="r"):
151-
return self.client.root.open(remote_file, open_options=open_options)
150+
def open_file(self, remote_file, open_options="r", encoding=None):
151+
return self.client.root.open(remote_file, open_options=open_options, encoding=encoding)
152152

153-
def create_file(self, remote_file, create_options="w"):
154-
return self.client.root.open(remote_file, open_options=create_options)
153+
def create_file(self, remote_file, create_options="w", encoding=None, override=True):
154+
return self.client.root.create(remote_file, open_options=create_options, encoding=encoding, override=override)
155155

156156
def makedirs(self, remotepath):
157157
if self.client.root.pathexists(remotepath):
@@ -1029,15 +1029,15 @@ def edb(edbpath=None,
10291029
use_ppe=use_ppe, )
10301030

10311031
@staticmethod
1032-
def exposed_open(filename, open_options="rb"):
1033-
f = open(filename, open_options)
1032+
def exposed_open(filename, open_options="rb", encoding=None):
1033+
f = open(filename, open_options, encoding=encoding)
10341034
return rpyc.restricted(f, ["read", "readlines", "close"], [])
10351035

10361036
@staticmethod
1037-
def exposed_create(filename,create_options="wb"):
1038-
if os.path.exists(filename):
1037+
def exposed_create(filename,create_options="wb", encoding=None, override=True):
1038+
if os.path.exists(filename) and not override:
10391039
return "File already exists"
1040-
f = open(filename, create_options)
1040+
f = open(filename, create_options, encoding=encoding)
10411041
return rpyc.restricted(f, ["read", "readlines", "write", "writelines", "close"], [])
10421042

10431043
@staticmethod

0 commit comments

Comments
 (0)