Configtool: add --quit and --save commandline switches.

Teach configtool to save ini, board and printer files with the
--save commandline switch.  Add a feature to Printer and Board
to let us pass None for the "values" to save; this causes the
class to save the previously loaded settings instead of taking
new settings in the argument.

Also add --quit switch to tell commandline not to continue to run the
GUI.  There's not much point in running the gui after many of these
switches, but that will change in the future.  Add this --quit option
to quit early so we can begin to use this new mode for test validation.
This commit is contained in:
Phil Hord 2016-04-19 17:05:39 -04:00 committed by Markus Hitter
parent ba5447b409
commit 2a6f00454f
4 changed files with 59 additions and 19 deletions

View File

@ -78,6 +78,34 @@ def cmdLoad(arg):
print("Expected one of *.ini, board.*.h or printer.*.h.") print("Expected one of *.ini, board.*.h or printer.*.h.")
sys.exit(2) sys.exit(2)
def cmdSave(arg):
xx, ext = os.path.splitext(arg)
fn = os.path.basename(arg)
if ext == ".ini":
if not getSettings(arg).save(arg):
print("Failed to save settings file: %s." % arg)
sys.exit(2)
return
if ext == ".h":
if fn.startswith("board."):
global board
if not board.saveConfigFile(arg, None):
print("Failed trying to save board file: %s." % arg)
sys.exit(2)
return
elif fn.startswith("printer."):
global printer
if not printer.saveConfigFile(arg, None):
print("Failed trying to save printer file: %s." % arg)
sys.exit(2)
return
print("Unrecognized file: %s." % arg)
print("Expected one of *.ini, board.*.h or printer.*.h.")
sys.exit(2)
def cmdShowAll(): def cmdShowAll():
names = {"configtool": getSettings(), "board": board, "printer": printer} names = {"configtool": getSettings(), "board": board, "printer": printer}
for namespace in names: for namespace in names:
@ -105,7 +133,11 @@ Following options are available for command line automation:
loads, only. GUI will overwrite them with the loads, only. GUI will overwrite them with the
files found in config.h. files found in config.h.
-s <file>, --save=<file> Save the config, printer or board file.
-a, --show-all Show all loaded variables and values. -a, --show-all Show all loaded variables and values.
-q, --quit Quit processing without launching the GUI.
""" % sys.argv[0]) """ % sys.argv[0])
def CommandLine(argv): def CommandLine(argv):
@ -116,8 +148,8 @@ def CommandLine(argv):
global settings, verbose global settings, verbose
try: try:
opts, args = getopt.getopt(argv, "hvl:a", ["help", "verbose", "load=", opts, args = getopt.getopt(argv, "hvl:as:q", ["help", "verbose", "load=",
"show-all"]) "show-all", "save=", "quit"])
except getopt.GetoptError as err: except getopt.GetoptError as err:
print(err) print(err)
print("Use '%s --help' to get help with command line options." % print("Use '%s --help' to get help with command line options." %
@ -139,9 +171,15 @@ def CommandLine(argv):
elif opt in ("-l", "--load"): elif opt in ("-l", "--load"):
cmdLoad(arg) cmdLoad(arg)
elif opt in ("-s", "--save"):
cmdSave(arg)
elif opt in ("-a", "--show-all"): elif opt in ("-a", "--show-all"):
cmdShowAll() cmdShowAll()
elif opt in ("-q", "--quit"):
sys.exit()
if __name__ == '__main__': if __name__ == '__main__':
CommandLine(sys.argv[1:]) CommandLine(sys.argv[1:])
StartGui(getSettings()) StartGui(getSettings())

View File

@ -333,6 +333,9 @@ class Board:
return None return None
def saveConfigFile(self, path, values): def saveConfigFile(self, path, values):
if not values:
values = self.cfgValues
if self.settings.verbose >= 1: if self.settings.verbose >= 1:
print("Saving board: %s." % path) print("Saving board: %s." % path)
if self.settings.verbose >= 2: if self.settings.verbose >= 2:

View File

@ -183,6 +183,9 @@ class Printer:
return False return False
def saveConfigFile(self, path, values): def saveConfigFile(self, path, values):
if not values:
values = self.cfgValues
if self.settings.verbose >= 1: if self.settings.verbose >= 1:
print("Saving printer: %s." % path) print("Saving printer: %s." % path)
if self.settings.verbose >= 2: if self.settings.verbose >= 2:

View File

@ -116,35 +116,31 @@ class Settings:
"uploadspeed": str(self.uploadspeed) "uploadspeed": str(self.uploadspeed)
} }
def saveSettings(self): def saveSettings(self, inifile = None):
if not inifile:
inifile = self.inifile
self.section = "configtool" self.section = "configtool"
try: try:
self.cfg.add_section(self.section) self.cfg.add_section(self.section)
except ConfigParser.DuplicateSectionError: except ConfigParser.DuplicateSectionError:
pass pass
self.cfg.set(self.section, "arduinodir", str(self.arduinodir)) values = self.getValues()
self.cfg.set(self.section, "cflags", str(self.cflags)) for k in values.keys():
self.cfg.set(self.section, "ldflags", str(self.ldflags)) self.cfg.set(self.section, k, values[k])
self.cfg.set(self.section, "objcopyflags", str(self.objcopyflags))
self.cfg.set(self.section, "programmer", str(self.programmer))
self.cfg.set(self.section, "programflags", str(self.programflags))
self.cfg.set(self.section, "port", str(self.port))
self.cfg.set(self.section, "t0", str(self.t0))
self.cfg.set(self.section, "r1", str(self.r1))
self.cfg.set(self.section, "numtemps", str(self.numTemps))
self.cfg.set(self.section, "maxadc", str(self.maxAdc))
self.cfg.set(self.section, "minadc", str(self.minAdc))
self.cfg.set(self.section, "uploadspeed", str(self.uploadspeed))
try: try:
cfp = open(self.inifile, 'wb') cfp = open(inifile, 'wb')
except: except:
print "Unable to open settings file %s for writing." % self.inifile print("Unable to open settings file %s for writing." % inifile)
return return False
self.cfg.write(cfp) self.cfg.write(cfp)
cfp.close() cfp.close()
return True
class SettingsDlg(wx.Dialog): class SettingsDlg(wx.Dialog):
def __init__(self, parent, settings): def __init__(self, parent, settings):