Skip to content

Commit

Permalink
Merge pull request #4096 from sliptonic/bug/toolbit
Browse files Browse the repository at this point in the history
[path]  make toolbit reject invalid filenames and prompt user for valid toolbit working location
  • Loading branch information
sliptonic committed Dec 2, 2020
2 parents af15d6c + af15364 commit 60e1d7a
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 7 deletions.
13 changes: 8 additions & 5 deletions src/Mod/Path/Gui/Resources/preferences/PathJob.ui
Expand Up @@ -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">
Expand Down Expand Up @@ -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">
Expand Down Expand Up @@ -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>
Expand Down
19 changes: 17 additions & 2 deletions src/Mod/Path/PathScripts/PathToolBitGui.py
Expand Up @@ -181,16 +181,31 @@ def Create(self, name='ToolBit', shapeFile=None):
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


Expand Down
53 changes: 53 additions & 0 deletions src/Mod/Path/PathScripts/PathToolBitLibraryGui.py
Expand Up @@ -38,6 +38,7 @@
import glob
import uuid as UUID
from functools import partial
import shutil

# PathLog.setLevel(PathLog.Level.DEBUG, PathLog.thisModule())
# PathLog.trackModule(PathLog.thisModule())
Expand Down Expand Up @@ -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()))
Expand All @@ -371,6 +375,55 @@ def __init__(self):
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()

Expand Down

0 comments on commit 60e1d7a

Please sign in to comment.