diff --git a/configtool.py b/configtool.py index 800537a..558afd1 100755 --- a/configtool.py +++ b/configtool.py @@ -78,6 +78,34 @@ def cmdLoad(arg): print("Expected one of *.ini, board.*.h or printer.*.h.") 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(): names = {"configtool": getSettings(), "board": board, "printer": printer} for namespace in names: @@ -105,7 +133,11 @@ Following options are available for command line automation: loads, only. GUI will overwrite them with the files found in config.h. + -s , --save= Save the config, printer or board file. + -a, --show-all Show all loaded variables and values. + + -q, --quit Quit processing without launching the GUI. """ % sys.argv[0]) def CommandLine(argv): @@ -116,8 +148,8 @@ def CommandLine(argv): global settings, verbose try: - opts, args = getopt.getopt(argv, "hvl:a", ["help", "verbose", "load=", - "show-all"]) + opts, args = getopt.getopt(argv, "hvl:as:q", ["help", "verbose", "load=", + "show-all", "save=", "quit"]) except getopt.GetoptError as err: print(err) print("Use '%s --help' to get help with command line options." % @@ -139,9 +171,15 @@ def CommandLine(argv): elif opt in ("-l", "--load"): cmdLoad(arg) + elif opt in ("-s", "--save"): + cmdSave(arg) + elif opt in ("-a", "--show-all"): cmdShowAll() + elif opt in ("-q", "--quit"): + sys.exit() + if __name__ == '__main__': CommandLine(sys.argv[1:]) StartGui(getSettings()) diff --git a/configtool/board.py b/configtool/board.py index fab8b6f..1ff81b6 100644 --- a/configtool/board.py +++ b/configtool/board.py @@ -333,6 +333,9 @@ class Board: return None def saveConfigFile(self, path, values): + if not values: + values = self.cfgValues + if self.settings.verbose >= 1: print("Saving board: %s." % path) if self.settings.verbose >= 2: diff --git a/configtool/printer.py b/configtool/printer.py index 3b289e2..2fc9c7c 100644 --- a/configtool/printer.py +++ b/configtool/printer.py @@ -183,6 +183,9 @@ class Printer: return False def saveConfigFile(self, path, values): + if not values: + values = self.cfgValues + if self.settings.verbose >= 1: print("Saving printer: %s." % path) if self.settings.verbose >= 2: diff --git a/configtool/settings.py b/configtool/settings.py index 645770e..4cf9611 100644 --- a/configtool/settings.py +++ b/configtool/settings.py @@ -116,35 +116,31 @@ class Settings: "uploadspeed": str(self.uploadspeed) } - def saveSettings(self): + def saveSettings(self, inifile = None): + if not inifile: + inifile = self.inifile + self.section = "configtool" try: self.cfg.add_section(self.section) except ConfigParser.DuplicateSectionError: pass - self.cfg.set(self.section, "arduinodir", str(self.arduinodir)) - self.cfg.set(self.section, "cflags", str(self.cflags)) - self.cfg.set(self.section, "ldflags", str(self.ldflags)) - 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)) + values = self.getValues() + for k in values.keys(): + self.cfg.set(self.section, k, values[k]) try: - cfp = open(self.inifile, 'wb') + cfp = open(inifile, 'wb') except: - print "Unable to open settings file %s for writing." % self.inifile - return + print("Unable to open settings file %s for writing." % inifile) + return False + self.cfg.write(cfp) cfp.close() + return True + class SettingsDlg(wx.Dialog): def __init__(self, parent, settings):