From 3d822cf2695f41fb10175e849b1ea2051c7b049d Mon Sep 17 00:00:00 2001 From: sliptonic Date: Mon, 30 Nov 2020 15:26:29 -0600 Subject: [PATCH 1/2] [path] make toolbit reject invalid filenames help user create toolbit working location --- .../Path/Gui/Resources/preferences/PathJob.ui | 11 ++-- src/Mod/Path/PathScripts/PathToolBitGui.py | 19 ++++++- .../Path/PathScripts/PathToolBitLibraryGui.py | 53 +++++++++++++++++++ 3 files changed, 77 insertions(+), 6 deletions(-) diff --git a/src/Mod/Path/Gui/Resources/preferences/PathJob.ui b/src/Mod/Path/Gui/Resources/preferences/PathJob.ui index 9e1b6f7fcc..bd29f158c0 100644 --- a/src/Mod/Path/Gui/Resources/preferences/PathJob.ui +++ b/src/Mod/Path/Gui/Resources/preferences/PathJob.ui @@ -142,8 +142,8 @@ 0 0 - 424 - 537 + 366 + 330 @@ -348,8 +348,8 @@ 0 0 - 407 - 570 + 319 + 528 @@ -655,6 +655,9 @@ + + <html><head/><body><p>Causes the toollibrary manager to remember last directory. This ONLY affects legacy tools. </p><p>This control will be deprecated and removed in future version</p></body></html> + Remember last library diff --git a/src/Mod/Path/PathScripts/PathToolBitGui.py b/src/Mod/Path/PathScripts/PathToolBitGui.py index 21a755f2ce..6daac0e5e0 100644 --- a/src/Mod/Path/PathScripts/PathToolBitGui.py +++ b/src/Mod/Path/PathScripts/PathToolBitGui.py @@ -181,16 +181,31 @@ class ToolBitGuiFactory(PathToolBit.ToolBitFactory): FreeCAD.ActiveDocument.commitTransaction() return tool +def isValidFile(filename): + print(filename) + try: + with open(filename, "w") as tempfile: + return True + except: + return False + def GetNewToolFile(parent=None): if parent is None: parent = QtGui.QApplication.activeWindow() + foo = QtGui.QFileDialog.getSaveFileName(parent, 'Tool', PathPreferences.lastPathToolBit(), '*.fctb') if foo and foo[0]: - PathPreferences.setLastPathToolBit(os.path.dirname(foo[0])) - return foo[0] + if not isValidFile(foo[0]): + msgBox = QtGui.QMessageBox() + msg = translate("Path", "Invalid Filename", None) + msgBox.setText(msg) + msgBox.exec_() + else: + PathPreferences.setLastPathToolBit(os.path.dirname(foo[0])) + return foo[0] return None diff --git a/src/Mod/Path/PathScripts/PathToolBitLibraryGui.py b/src/Mod/Path/PathScripts/PathToolBitLibraryGui.py index a0a52f0de3..83417baec7 100644 --- a/src/Mod/Path/PathScripts/PathToolBitLibraryGui.py +++ b/src/Mod/Path/PathScripts/PathToolBitLibraryGui.py @@ -38,6 +38,7 @@ import os import glob import uuid as UUID from functools import partial +import shutil # PathLog.setLevel(PathLog.Level.DEBUG, PathLog.thisModule()) # PathLog.trackModule(PathLog.thisModule()) @@ -360,6 +361,9 @@ class ToolBitLibrary(object): def __init__(self): PathLog.track() + if not self.checkWorkingDir(): + return + self.factory = ModelFactory() self.temptool = None self.toolModel = PySide.QtGui.QStandardItemModel(0, len(self.columnNames())) @@ -371,6 +375,55 @@ class ToolBitLibrary(object): self.setupUI() self.title = self.form.windowTitle() + def checkWorkingDir(self): + # users shouldn't use the example toolbits and libraries. + # working directory should be writable + PathLog.track() + + workingdir = os.path.dirname(PathPreferences.lastPathToolLibrary()) + defaultdir = os.path.dirname(PathPreferences.pathDefaultToolsPath()) + + dirOK = lambda : workingdir != defaultdir and (os.access(workingdir, os.W_OK)) + + if dirOK(): + return True + + qm = PySide.QtGui.QMessageBox + ret = qm.question(None,'', "Please set up Toolbit Working Directory", qm.Ok | qm.Cancel) + + if ret == qm.Cancel: + return False + + msg = translate("Path", "Choose a writable location for your toolbits", None) + while not dirOK(): + workingdir = PySide.QtGui.QFileDialog.getExistingDirectory(None, msg, + PathPreferences.filePath()) + + PathPreferences.setLastPathToolLibrary("{}/Library".format(workingdir)) + + subdirlist = ['Bit', 'Library', 'Shape'] + mode = 0o777 + for dir in subdirlist: + subdir = "{}/{}".format(workingdir, dir) + if not os.path.exists(subdir): + qm = PySide.QtGui.QMessageBox + ret = qm.question(None,'', "Toolbit Working directory {} should contain a '{}' subdirectory. Create it?".format(workingdir, dir), qm.Yes | qm.No) + + if ret == qm.Yes: + os.mkdir(subdir, mode) + qm = PySide.QtGui.QMessageBox + ret = qm.question(None,'', "Copy example files to new {} directory?".format(dir), qm.Yes | qm.No) + if ret == qm.Yes: + src="{}/{}".format(defaultdir, dir) + src_files = os.listdir(src) + for file_name in src_files: + full_file_name = os.path.join(src, file_name) + if os.path.isfile(full_file_name): + shutil.copy(full_file_name, subdir) + + return True + + def toolBitNew(self): PathLog.track() From af153643f23d44df60f851cdcfe588c786ce0710 Mon Sep 17 00:00:00 2001 From: sliptonic Date: Tue, 1 Dec 2020 09:43:45 -0600 Subject: [PATCH 2/2] PR review suggestions and fixes --- src/Mod/Path/Gui/Resources/preferences/PathJob.ui | 4 ++-- src/Mod/Path/PathScripts/PathToolBitGui.py | 4 ++-- src/Mod/Path/PathScripts/PathToolBitLibraryGui.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Mod/Path/Gui/Resources/preferences/PathJob.ui b/src/Mod/Path/Gui/Resources/preferences/PathJob.ui index bd29f158c0..15c9cfcb30 100644 --- a/src/Mod/Path/Gui/Resources/preferences/PathJob.ui +++ b/src/Mod/Path/Gui/Resources/preferences/PathJob.ui @@ -656,10 +656,10 @@ - <html><head/><body><p>Causes the toollibrary manager to remember last directory. This ONLY affects legacy tools. </p><p>This control will be deprecated and removed in future version</p></body></html> + <html><head/><body><p>Causes the toollibrary manager to remember last directory. This ONLY affects legacy tools. </p><p>This control is deprecated and will be removed in a future version</p></body></html> - Remember last library + Remember last library (legacy) diff --git a/src/Mod/Path/PathScripts/PathToolBitGui.py b/src/Mod/Path/PathScripts/PathToolBitGui.py index 6daac0e5e0..661531967e 100644 --- a/src/Mod/Path/PathScripts/PathToolBitGui.py +++ b/src/Mod/Path/PathScripts/PathToolBitGui.py @@ -181,7 +181,7 @@ class ToolBitGuiFactory(PathToolBit.ToolBitFactory): FreeCAD.ActiveDocument.commitTransaction() return tool -def isValidFile(filename): +def isValidFileName(filename): print(filename) try: with open(filename, "w") as tempfile: @@ -198,7 +198,7 @@ def GetNewToolFile(parent=None): PathPreferences.lastPathToolBit(), '*.fctb') if foo and foo[0]: - if not isValidFile(foo[0]): + if not isValidFileName(foo[0]): msgBox = QtGui.QMessageBox() msg = translate("Path", "Invalid Filename", None) msgBox.setText(msg) diff --git a/src/Mod/Path/PathScripts/PathToolBitLibraryGui.py b/src/Mod/Path/PathScripts/PathToolBitLibraryGui.py index 83417baec7..f28186fc7b 100644 --- a/src/Mod/Path/PathScripts/PathToolBitLibraryGui.py +++ b/src/Mod/Path/PathScripts/PathToolBitLibraryGui.py @@ -389,9 +389,9 @@ class ToolBitLibrary(object): return True qm = PySide.QtGui.QMessageBox - ret = qm.question(None,'', "Please set up Toolbit Working Directory", qm.Ok | qm.Cancel) + ret = qm.question(None,'', "Toolbit working directory not set up. Do that now?", qm.Yes | qm.No) - if ret == qm.Cancel: + if ret == qm.No: return False msg = translate("Path", "Choose a writable location for your toolbits", None)