Configtool: protect original config files.

Likely users don't care too much about the name of the saved file,
so they likely use the default ones. If they mess up, they also
likely want to return to the original, but, d'oh, it's overwritten.
Don't let this happen, enforce a non-original file name for user
saves.

In other words: don't let users shoot themselfs into their foot.
This commit is contained in:
jbernardis 2015-02-07 23:38:12 -05:00 committed by Markus Hitter
parent 1cd21251d2
commit 706ccf0637
5 changed files with 88 additions and 56 deletions

View File

@ -1,6 +1,9 @@
/** \file config.default.h /** \file config.default.h
In case you prefer to build with the makefile or with Arduino IDE instead In case you prefer to build with the makefile or with Arduino IDE instead
of the config tool, copy this file to config.h and adjust it to your needs. of the config tool, copy this file to config.h and adjust it to your needs.
\note to developers: when adding a file here, also add it to
configtool/protectedfiles.py.
*/ */
// Uncomment your controller board, comment out all others. // Uncomment your controller board, comment out all others.

View File

@ -230,19 +230,19 @@ class ConfigFrame(wx.Frame):
else: else:
self.buildMenu.Enable(ID_UPLOAD, False) self.buildMenu.Enable(ID_UPLOAD, False)
def enableSavePrinter(self, flag): def enableSavePrinter(self, saveFlag, saveAsFlag):
self.fileMenu.Enable(ID_SAVE_PRINTER, flag) self.fileMenu.Enable(ID_SAVE_PRINTER, saveFlag)
self.fileMenu.Enable(ID_SAVE_PRINTER_AS, flag) self.fileMenu.Enable(ID_SAVE_PRINTER_AS, saveAsFlag)
self.savePrtEna = flag self.savePrtEna = saveAsFlag
if self.savePrtEna and self.saveBrdEna: if self.savePrtEna and self.saveBrdEna:
self.enableSaveConfig(True) self.enableSaveConfig(True)
else: else:
self.enableSaveConfig(False) self.enableSaveConfig(False)
def enableSaveBoard(self, flag): def enableSaveBoard(self, saveFlag, saveAsFlag):
self.fileMenu.Enable(ID_SAVE_BOARD, flag) self.fileMenu.Enable(ID_SAVE_BOARD, saveFlag)
self.fileMenu.Enable(ID_SAVE_BOARD_AS, flag) self.fileMenu.Enable(ID_SAVE_BOARD_AS, saveAsFlag)
self.saveBrdEna = flag self.saveBrdEna = saveAsFlag
if self.savePrtEna and self.saveBrdEna: if self.savePrtEna and self.saveBrdEna:
self.enableSaveConfig(True) self.enableSaveConfig(True)
else: else:
@ -328,16 +328,14 @@ class ConfigFrame(wx.Frame):
return return
bfn = self.pgBoard.getFileName() bfn = self.pgBoard.getFileName()
if not self.pgBoard.saveConfigFile(bfn): if self.pgBoard.isModified() and self.pgBoard.isValid():
self.message("Unable to save board configuration %s." % if not self.pgBoard.saveConfigFile(bfn):
os.path.basename(bfn), "File error") return
return
pfn = self.pgPrinter.getFileName() pfn = self.pgPrinter.getFileName()
if not self.pgPrinter.saveConfigFile(pfn): if self.pgPrinter.isModified() and self.pgPrinter.isValid():
self.message("Unable to save printer configuration %s." % if not self.pgPrinter.saveConfigFile(pfn):
os.path.basename(pfn), "File error") return
return
prefix = cmd_folder + os.path.sep prefix = cmd_folder + os.path.sep
lpfx = len(prefix) lpfx = len(prefix)

View File

@ -17,6 +17,7 @@ from configtool.sensorpage import SensorsPage
from configtool.heaterspage import HeatersPage from configtool.heaterspage import HeatersPage
from configtool.communicationspage import CommunicationsPage from configtool.communicationspage import CommunicationsPage
from configtool.cpupage import CpuPage from configtool.cpupage import CpuPage
from configtool.protectedfiles import protectedFiles
class BoardPanel(wx.Panel): class BoardPanel(wx.Panel):
@ -24,6 +25,7 @@ class BoardPanel(wx.Panel):
wx.Panel.__init__(self, nb, wx.ID_ANY) wx.Panel.__init__(self, nb, wx.ID_ANY)
self.parent = parent self.parent = parent
self.settings = settings self.settings = settings
self.protFileLoaded = False
self.configFile = None self.configFile = None
@ -118,6 +120,9 @@ class BoardPanel(wx.Panel):
def isModified(self): def isModified(self):
return (True in self.pageModified) return (True in self.pageModified)
def isValid(self):
return not (False in self.pageValid)
def hasData(self): def hasData(self):
return (self.configFile != None) return (self.configFile != None)
@ -129,9 +134,9 @@ class BoardPanel(wx.Panel):
self.modifyTab(pg) self.modifyTab(pg)
if False in self.pageValid: if False in self.pageValid:
self.parent.enableSaveBoard(False) self.parent.enableSaveBoard(False, False)
else: else:
self.parent.enableSaveBoard(True) self.parent.enableSaveBoard(not self.protFileLoaded, True)
def modifyTab(self, pg): def modifyTab(self, pg):
if self.pageModified[pg] and not self.pageValid[pg]: if self.pageModified[pg] and not self.pageValid[pg]:
@ -342,7 +347,12 @@ class BoardPanel(wx.Panel):
self.heaters.append(s) self.heaters.append(s)
continue continue
self.parent.enableSaveBoard(True) if os.path.basename(fn) in protectedFiles:
self.parent.enableSaveBoard(False, True)
self.protFileLoaded = True
else:
self.protFileLoaded = False
self.parent.enableSaveBoard(True, True)
self.parent.setBoardTabFile(os.path.basename(fn)) self.parent.setBoardTabFile(os.path.basename(fn))
self.pgHeaters.setCandidatePins(self.candHeatPins) self.pgHeaters.setCandidatePins(self.candHeatPins)
self.pgSensors.setCandidatePins(self.candThermPins) self.pgSensors.setCandidatePins(self.candThermPins)
@ -381,15 +391,7 @@ class BoardPanel(wx.Panel):
def onSaveConfig(self, evt): def onSaveConfig(self, evt):
path = self.configFile path = self.configFile
if self.saveConfigFile(path): self.saveConfigFile(path)
dlg = wx.MessageDialog(self, "File %s successfully written." % path,
"Save successful", wx.OK + wx.ICON_INFORMATION)
else:
dlg = wx.MessageDialog(self, "Unable to write to file %s." % path,
"File error", wx.OK + wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy()
def onSaveConfigAs(self, evt): def onSaveConfigAs(self, evt):
wildcard = "Board configuration (board.*.h)|board.*.h" wildcard = "Board configuration (board.*.h)|board.*.h"
@ -408,17 +410,18 @@ class BoardPanel(wx.Panel):
dlg.Destroy() dlg.Destroy()
if self.saveConfigFile(path): if self.saveConfigFile(path):
dlg = wx.MessageDialog(self, "File %s successfully written." % path,
"Save successful", wx.OK + wx.ICON_INFORMATION)
self.parent.setBoardTabFile(os.path.basename(path)) self.parent.setBoardTabFile(os.path.basename(path))
self.protFileLoaded = False
else: self.parent.enableSaveBoard(True, True)
dlg = wx.MessageDialog(self, "Unable to write to file %s." % path,
"File error", wx.OK + wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy()
def saveConfigFile(self, path): def saveConfigFile(self, path):
if os.path.basename(path) in protectedFiles:
dlg = wx.MessageDialog(self, "Unable to overwrite %s." % path,
"Protected file error", wx.OK + wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy()
return False
ext = os.path.splitext(os.path.basename(path))[1] ext = os.path.splitext(os.path.basename(path))[1]
self.dir = os.path.dirname(path) self.dir = os.path.dirname(path)
@ -428,6 +431,10 @@ class BoardPanel(wx.Panel):
try: try:
fp = file(path, 'w') fp = file(path, 'w')
except: except:
dlg = wx.MessageDialog(self, "Unable to write to file %s." % path,
"File error", wx.OK + wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy()
return False return False
self.configFile = path self.configFile = path
@ -557,6 +564,11 @@ class BoardPanel(wx.Panel):
fp.close() fp.close()
dlg = wx.MessageDialog(self, "File %s successfully written." % path,
"Save successful", wx.OK + wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
return True return True
def addNewDefine(self, fp, key, val): def addNewDefine(self, fp, key, val):

View File

@ -10,6 +10,7 @@ from configtool.data import (defineValueFormat, defineBoolFormat, reCommDefBL,
from configtool.mechanicalpage import MechanicalPage from configtool.mechanicalpage import MechanicalPage
from configtool.accelerationpage import AccelerationPage from configtool.accelerationpage import AccelerationPage
from configtool.miscellaneouspage import MiscellaneousPage from configtool.miscellaneouspage import MiscellaneousPage
from configtool.protectedfiles import protectedFiles
class PrinterPanel(wx.Panel): class PrinterPanel(wx.Panel):
@ -18,6 +19,7 @@ class PrinterPanel(wx.Panel):
self.parent = parent self.parent = parent
self.configFile = None self.configFile = None
self.protFileLoaded = False
self.settings = settings self.settings = settings
@ -78,6 +80,9 @@ class PrinterPanel(wx.Panel):
def isModified(self): def isModified(self):
return (True in self.pageModified) return (True in self.pageModified)
def isValid(self):
return not (False in self.pageValid)
def hasData(self): def hasData(self):
return (self.configFile != None) return (self.configFile != None)
@ -86,9 +91,9 @@ class PrinterPanel(wx.Panel):
self.modifyTab(pg) self.modifyTab(pg)
if False in self.pageValid: if False in self.pageValid:
self.parent.enableSavePrinter(False) self.parent.enableSavePrinter(False, False)
else: else:
self.parent.enableSavePrinter(True) self.parent.enableSavePrinter(not self.protFileLoaded, True)
def modifyTab(self, pg): def modifyTab(self, pg):
if self.pageModified[pg] and not self.pageValid[pg]: if self.pageModified[pg] and not self.pageValid[pg]:
@ -243,7 +248,12 @@ class PrinterPanel(wx.Panel):
if len(t) == 1: if len(t) == 1:
self.cfgValues[t[0]] = True self.cfgValues[t[0]] = True
self.parent.enableSavePrinter(True) if os.path.basename(fn) in protectedFiles:
self.parent.enableSavePrinter(False, True)
self.protFileLoaded = True
else:
self.protFileLoaded = False
self.parent.enableSavePrinter(True, True)
self.parent.setPrinterTabFile(os.path.basename(fn)) self.parent.setPrinterTabFile(os.path.basename(fn))
for pg in self.pages: for pg in self.pages:
@ -260,15 +270,7 @@ class PrinterPanel(wx.Panel):
def onSaveConfig(self, evt): def onSaveConfig(self, evt):
path = self.configFile path = self.configFile
if self.saveConfigFile(path): self.saveConfigFile(path)
dlg = wx.MessageDialog(self, "File %s successfully written." % path,
"Save successful", wx.OK + wx.ICON_INFORMATION)
else:
dlg = wx.MessageDialog(self, "Unable to write to file %s." % path,
"File error", wx.OK + wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy()
def onSaveConfigAs(self, evt): def onSaveConfigAs(self, evt):
wildcard = "Printer configuration (printer.*.h)|printer.*.h" wildcard = "Printer configuration (printer.*.h)|printer.*.h"
@ -287,17 +289,18 @@ class PrinterPanel(wx.Panel):
dlg.Destroy() dlg.Destroy()
if self.saveConfigFile(path): if self.saveConfigFile(path):
dlg = wx.MessageDialog(self, "File %s successfully written." % path,
"Save successful", wx.OK + wx.ICON_INFORMATION)
self.parent.setPrinterTabFile(os.path.basename(path)) self.parent.setPrinterTabFile(os.path.basename(path))
self.protFileLoaded = False
else: self.parent.enableSavePrinter(True, True)
dlg = wx.MessageDialog(self, "Unable to write to file %s." % path,
"File error", wx.OK + wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy()
def saveConfigFile(self, path): def saveConfigFile(self, path):
if os.path.basename(path) in protectedFiles:
dlg = wx.MessageDialog(self, "Unable to overwrite %s." % path,
"Protected file error", wx.OK + wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy()
return False
ext = os.path.splitext(os.path.basename(path))[1] ext = os.path.splitext(os.path.basename(path))[1]
self.dir = os.path.dirname(path) self.dir = os.path.dirname(path)
@ -307,6 +310,10 @@ class PrinterPanel(wx.Panel):
try: try:
fp = file(path, 'w') fp = file(path, 'w')
except: except:
dlg = wx.MessageDialog(self, "Unable to write to file %s." % path,
"File error", wx.OK + wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy()
return False return False
self.configFile = path self.configFile = path
@ -398,6 +405,11 @@ class PrinterPanel(wx.Panel):
fp.close() fp.close()
dlg = wx.MessageDialog(self, "File %s successfully written." % path,
"Save successful", wx.OK + wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
return True return True
def addNewDefine(self, fp, key, val): def addNewDefine(self, fp, key, val):

View File

@ -0,0 +1,7 @@
protectedFiles = [
"board.gen7-v1.4.h",
"board.ramps-v1.3.h",
"printer.mendel.h",
"printer.wolfstrap.h"
]