Merge pull request #4096 from sliptonic/bug/toolbit

[path]  make toolbit reject invalid filenames and prompt user for valid toolbit working location
This commit is contained in:
sliptonic
2020-12-02 08:51:49 -06:00
committed by GitHub
3 changed files with 78 additions and 7 deletions

View File

@@ -142,8 +142,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>424</width>
<height>537</height>
<width>366</width>
<height>330</height>
</rect>
</property>
<attribute name="label">
@@ -348,8 +348,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>407</width>
<height>570</height>
<width>319</width>
<height>528</height>
</rect>
</property>
<attribute name="label">
@@ -655,8 +655,11 @@
</item>
<item>
<widget class="QCheckBox" name="toolsOpenLastLibrary">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Causes the toollibrary manager to remember last directory. This ONLY affects legacy tools. &lt;/p&gt;&lt;p&gt;This control is deprecated and will be removed in a future version&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Remember last library</string>
<string>Remember last library (legacy)</string>
</property>
</widget>
</item>

View File

@@ -181,16 +181,31 @@ class ToolBitGuiFactory(PathToolBit.ToolBitFactory):
FreeCAD.ActiveDocument.commitTransaction()
return tool
def isValidFileName(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 isValidFileName(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

View File

@@ -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,'', "Toolbit working directory not set up. Do that now?", qm.Yes | qm.No)
if ret == qm.No:
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()