Skip to content

Commit

Permalink
File Save Permission Detection [AARD-1724] (#1092)
Browse files Browse the repository at this point in the history
  • Loading branch information
HunterBarclay authored Aug 21, 2024
2 parents 5bd353f + 6ffc408 commit 9510b56
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion exporter/SynthesisFusionAddin/src/UI/ConfigCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def notify(self, args):
if generalConfigTab.exportLocation == ExportLocation.DOWNLOAD:
savepath = FileDialogConfig.saveFileDialog(defaultPath=exporterOptions.fileLocation)

if savepath == False:
if not savepath:
# save was canceled
return

Expand Down
32 changes: 27 additions & 5 deletions exporter/SynthesisFusionAddin/src/UI/FileDialogConfig.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import os
import tempfile
from pathlib import Path

import adsk.core
import adsk.fusion

from src import gm
from src.Types import OString


def saveFileDialog(defaultPath: str | None = None, defaultName: str | None = None) -> str | bool:
def saveFileDialog(defaultPath: str | None = None, defaultName: str | None = None) -> str | os.PathLike[str] | None:
"""Function to generate the Save File Dialog for the Hellion Data files
Args:
defaultPath (str): default path for the saving location
defaultName (str): default name for the saving file
Returns:
bool: False if canceled
None: if canceled
str: full file path
"""

Expand All @@ -36,11 +40,29 @@ def saveFileDialog(defaultPath: str | None = None, defaultName: str | None = Non
fileDialog.filterIndex = 0
dialogResult = fileDialog.showSave()

if dialogResult == adsk.core.DialogResults.DialogOK:
return fileDialog.filename
else:
if dialogResult != adsk.core.DialogResults.DialogOK:
return None

canWrite = isWriteableDirectory(Path(fileDialog.filename).parent)
if not canWrite:
gm.ui.messageBox("Synthesis does not have the required permissions to write to this directory.")
return saveFileDialog(defaultPath, defaultName)

return fileDialog.filename


def isWriteableDirectory(path: str | os.PathLike[str]) -> bool:
if not os.access(path, os.W_OK):
return False

try:
with tempfile.NamedTemporaryFile(dir=path, delete=True) as f:
f.write(b"test")
except OSError:
return False

return True


def generateFilePath() -> str:
"""Generates a temporary file path that can be used to save the file for exporting
Expand Down

0 comments on commit 9510b56

Please sign in to comment.