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

feat: Add overwrite option for App.save_as() #951

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
1 change: 1 addition & 0 deletions doc/changelog.d/951.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add overwrite option for `App.save_as()`
34 changes: 31 additions & 3 deletions src/ansys/mechanical/core/embedding/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import atexit
import os
import shutil
import tempfile
import typing
import warnings

Expand Down Expand Up @@ -194,9 +196,35 @@ def save(self, path=None):
else:
self.DataModel.Project.Save()

def save_as(self, path):
"""Save the project as."""
self.DataModel.Project.SaveAs(path)
def save_as(self, path: str, overwrite: bool = False):
"""Save the project as a new file.

If the `overwrite` flag is enabled, the current saved file is temporarily moved
to a backup location. The new file is then saved in its place. If the process fails,
the backup file is restored to its original location.

Parameters
----------
path: int, optional
The path where file needs to be saved.
overwrite: bool, optional
Whether the file should be overwritten if it already exists (default is False).
"""
if overwrite:
dipinknair marked this conversation as resolved.
Show resolved Hide resolved
if os.path.exists(path):
dipinknair marked this conversation as resolved.
Show resolved Hide resolved
temp_dir = tempfile.mkdtemp()
temp_file_path = os.path.join(temp_dir, os.path.basename(path))
try:
shutil.move(path, temp_file_path) # Move current file to temp location
self.DataModel.Project.SaveAs(path) # Save as new file
os.remove(temp_file_path) # Remove file from temp location
except Exception as e:
shutil.move(temp_file_path, path) # Restore original file from temp location
raise e
finally:
shutil.rmtree(temp_dir) # Cleanup temp directory
else:
self.DataModel.Project.SaveAs(path)

def launch_gui(self, delete_tmp_on_close: bool = True, dry_run: bool = False):
"""Launch the GUI."""
Expand Down
1 change: 1 addition & 0 deletions tests/embedding/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def test_app_save_open(embedded_app, tmp_path: pytest.TempPathFactory):
embedded_app.DataModel.Project.Name = "PROJECT 1"
project_file = os.path.join(tmp_path, f"{NamedTemporaryFile().name}.mechdat")
embedded_app.save_as(project_file)
embedded_app.save_as(project_file, overwrite=True)
embedded_app.new()
embedded_app.open(project_file)
assert embedded_app.DataModel.Project.Name == "PROJECT 1"
Expand Down
Loading