From 706ccf06373e833590fb36e06a6bf79bad96f455 Mon Sep 17 00:00:00 2001 From: jbernardis Date: Sat, 7 Feb 2015 23:38:12 -0500 Subject: [PATCH] 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. --- config.default.h | 3 +++ configtool.py | 30 ++++++++++----------- configtool/boardpanel.py | 52 ++++++++++++++++++++++-------------- configtool/printerpanel.py | 52 ++++++++++++++++++++++-------------- configtool/protectedfiles.py | 7 +++++ 5 files changed, 88 insertions(+), 56 deletions(-) create mode 100644 configtool/protectedfiles.py diff --git a/config.default.h b/config.default.h index 66ac4b0..9d21a2c 100644 --- a/config.default.h +++ b/config.default.h @@ -1,6 +1,9 @@ /** \file config.default.h 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. + + \note to developers: when adding a file here, also add it to + configtool/protectedfiles.py. */ // Uncomment your controller board, comment out all others. diff --git a/configtool.py b/configtool.py index fb73e8b..e9a1dc7 100755 --- a/configtool.py +++ b/configtool.py @@ -230,19 +230,19 @@ class ConfigFrame(wx.Frame): else: self.buildMenu.Enable(ID_UPLOAD, False) - def enableSavePrinter(self, flag): - self.fileMenu.Enable(ID_SAVE_PRINTER, flag) - self.fileMenu.Enable(ID_SAVE_PRINTER_AS, flag) - self.savePrtEna = flag + def enableSavePrinter(self, saveFlag, saveAsFlag): + self.fileMenu.Enable(ID_SAVE_PRINTER, saveFlag) + self.fileMenu.Enable(ID_SAVE_PRINTER_AS, saveAsFlag) + self.savePrtEna = saveAsFlag if self.savePrtEna and self.saveBrdEna: self.enableSaveConfig(True) else: self.enableSaveConfig(False) - def enableSaveBoard(self, flag): - self.fileMenu.Enable(ID_SAVE_BOARD, flag) - self.fileMenu.Enable(ID_SAVE_BOARD_AS, flag) - self.saveBrdEna = flag + def enableSaveBoard(self, saveFlag, saveAsFlag): + self.fileMenu.Enable(ID_SAVE_BOARD, saveFlag) + self.fileMenu.Enable(ID_SAVE_BOARD_AS, saveAsFlag) + self.saveBrdEna = saveAsFlag if self.savePrtEna and self.saveBrdEna: self.enableSaveConfig(True) else: @@ -328,16 +328,14 @@ class ConfigFrame(wx.Frame): return bfn = self.pgBoard.getFileName() - if not self.pgBoard.saveConfigFile(bfn): - self.message("Unable to save board configuration %s." % - os.path.basename(bfn), "File error") - return + if self.pgBoard.isModified() and self.pgBoard.isValid(): + if not self.pgBoard.saveConfigFile(bfn): + return pfn = self.pgPrinter.getFileName() - if not self.pgPrinter.saveConfigFile(pfn): - self.message("Unable to save printer configuration %s." % - os.path.basename(pfn), "File error") - return + if self.pgPrinter.isModified() and self.pgPrinter.isValid(): + if not self.pgPrinter.saveConfigFile(pfn): + return prefix = cmd_folder + os.path.sep lpfx = len(prefix) diff --git a/configtool/boardpanel.py b/configtool/boardpanel.py index 139c0af..26a1148 100644 --- a/configtool/boardpanel.py +++ b/configtool/boardpanel.py @@ -17,6 +17,7 @@ from configtool.sensorpage import SensorsPage from configtool.heaterspage import HeatersPage from configtool.communicationspage import CommunicationsPage from configtool.cpupage import CpuPage +from configtool.protectedfiles import protectedFiles class BoardPanel(wx.Panel): @@ -24,6 +25,7 @@ class BoardPanel(wx.Panel): wx.Panel.__init__(self, nb, wx.ID_ANY) self.parent = parent self.settings = settings + self.protFileLoaded = False self.configFile = None @@ -118,6 +120,9 @@ class BoardPanel(wx.Panel): def isModified(self): return (True in self.pageModified) + def isValid(self): + return not (False in self.pageValid) + def hasData(self): return (self.configFile != None) @@ -129,9 +134,9 @@ class BoardPanel(wx.Panel): self.modifyTab(pg) if False in self.pageValid: - self.parent.enableSaveBoard(False) + self.parent.enableSaveBoard(False, False) else: - self.parent.enableSaveBoard(True) + self.parent.enableSaveBoard(not self.protFileLoaded, True) def modifyTab(self, pg): if self.pageModified[pg] and not self.pageValid[pg]: @@ -342,7 +347,12 @@ class BoardPanel(wx.Panel): self.heaters.append(s) 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.pgHeaters.setCandidatePins(self.candHeatPins) self.pgSensors.setCandidatePins(self.candThermPins) @@ -381,15 +391,7 @@ class BoardPanel(wx.Panel): def onSaveConfig(self, evt): path = self.configFile - if 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() - + self.saveConfigFile(path) def onSaveConfigAs(self, evt): wildcard = "Board configuration (board.*.h)|board.*.h" @@ -408,17 +410,18 @@ class BoardPanel(wx.Panel): dlg.Destroy() 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)) - - else: - dlg = wx.MessageDialog(self, "Unable to write to file %s." % path, - "File error", wx.OK + wx.ICON_ERROR) - dlg.ShowModal() - dlg.Destroy() + self.protFileLoaded = False + self.parent.enableSaveBoard(True, True) 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] self.dir = os.path.dirname(path) @@ -428,6 +431,10 @@ class BoardPanel(wx.Panel): try: fp = file(path, 'w') 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 self.configFile = path @@ -557,6 +564,11 @@ class BoardPanel(wx.Panel): fp.close() + dlg = wx.MessageDialog(self, "File %s successfully written." % path, + "Save successful", wx.OK + wx.ICON_INFORMATION) + dlg.ShowModal() + dlg.Destroy() + return True def addNewDefine(self, fp, key, val): diff --git a/configtool/printerpanel.py b/configtool/printerpanel.py index 1919732..423308c 100644 --- a/configtool/printerpanel.py +++ b/configtool/printerpanel.py @@ -10,6 +10,7 @@ from configtool.data import (defineValueFormat, defineBoolFormat, reCommDefBL, from configtool.mechanicalpage import MechanicalPage from configtool.accelerationpage import AccelerationPage from configtool.miscellaneouspage import MiscellaneousPage +from configtool.protectedfiles import protectedFiles class PrinterPanel(wx.Panel): @@ -18,6 +19,7 @@ class PrinterPanel(wx.Panel): self.parent = parent self.configFile = None + self.protFileLoaded = False self.settings = settings @@ -78,6 +80,9 @@ class PrinterPanel(wx.Panel): def isModified(self): return (True in self.pageModified) + def isValid(self): + return not (False in self.pageValid) + def hasData(self): return (self.configFile != None) @@ -86,9 +91,9 @@ class PrinterPanel(wx.Panel): self.modifyTab(pg) if False in self.pageValid: - self.parent.enableSavePrinter(False) + self.parent.enableSavePrinter(False, False) else: - self.parent.enableSavePrinter(True) + self.parent.enableSavePrinter(not self.protFileLoaded, True) def modifyTab(self, pg): if self.pageModified[pg] and not self.pageValid[pg]: @@ -243,7 +248,12 @@ class PrinterPanel(wx.Panel): if len(t) == 1: 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)) for pg in self.pages: @@ -260,15 +270,7 @@ class PrinterPanel(wx.Panel): def onSaveConfig(self, evt): path = self.configFile - if 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() + self.saveConfigFile(path) def onSaveConfigAs(self, evt): wildcard = "Printer configuration (printer.*.h)|printer.*.h" @@ -287,17 +289,18 @@ class PrinterPanel(wx.Panel): dlg.Destroy() 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)) - - else: - dlg = wx.MessageDialog(self, "Unable to write to file %s." % path, - "File error", wx.OK + wx.ICON_ERROR) - dlg.ShowModal() - dlg.Destroy() + self.protFileLoaded = False + self.parent.enableSavePrinter(True, True) 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] self.dir = os.path.dirname(path) @@ -307,6 +310,10 @@ class PrinterPanel(wx.Panel): try: fp = file(path, 'w') 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 self.configFile = path @@ -398,6 +405,11 @@ class PrinterPanel(wx.Panel): fp.close() + dlg = wx.MessageDialog(self, "File %s successfully written." % path, + "Save successful", wx.OK + wx.ICON_INFORMATION) + dlg.ShowModal() + dlg.Destroy() + return True def addNewDefine(self, fp, key, val): diff --git a/configtool/protectedfiles.py b/configtool/protectedfiles.py new file mode 100644 index 0000000..073a3c6 --- /dev/null +++ b/configtool/protectedfiles.py @@ -0,0 +1,7 @@ + +protectedFiles = [ + "board.gen7-v1.4.h", + "board.ramps-v1.3.h", + "printer.mendel.h", + "printer.wolfstrap.h" +]