From 2c5a36b14e8f3a125902592f835e93080bd45011 Mon Sep 17 00:00:00 2001 From: Phil Hord Date: Thu, 21 Apr 2016 13:23:16 -0400 Subject: [PATCH] Configtool: make Settings the "global" container. Instead of passing myriad variables around in arguments to classes and functions, put the global settings like "verbose" and "cmdFolder" in the Settings object and pass that in to the top. --- configtool.py | 32 ++++++++++++++++++++++++++------ configtool/board.py | 36 ++++++++++++++++++++++-------------- configtool/boardpanel.py | 5 ++--- configtool/gui.py | 29 ++++++++++++----------------- configtool/printer.py | 22 +++++++++++++++------- configtool/printerpanel.py | 5 ++--- configtool/settings.py | 27 ++++++++++++++++++++------- 7 files changed, 99 insertions(+), 57 deletions(-) diff --git a/configtool.py b/configtool.py index ec483af..7002c0a 100755 --- a/configtool.py +++ b/configtool.py @@ -24,9 +24,23 @@ import getopt import os.path import inspect +from configtool.settings import Settings from configtool.gui import StartGui +cmdFolder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( + inspect.currentframe()))[0])) + +verbose = 0 +settings = None + +def getSettings(arg = None): + global settings + if arg or not settings: + settings = Settings(None, cmdFolder, arg) + settings.verbose = verbose + return settings + def cmdLoad(arg): print("Want to load %s, but don't know how.\n" % arg) @@ -38,6 +52,9 @@ Following options are available for command line automation: -h, --help Show this help text. + -v, --verbose Be more verbose. Can be applied multiple times + to get even more output. + -l , --load= Load a specific printer config, board config or .ini file. """ % sys.argv[0]) @@ -47,8 +64,10 @@ def CommandLine(argv): result in sys.exit() (i.e. they do not return from this function). Other options like --debug will return to allow the gui to launch. """ + global settings, verbose + try: - opts, args = getopt.getopt(argv, "hl:", ["help", "load="]) + opts, args = getopt.getopt(argv, "hvl:", ["help", "verbose", "load="]) except getopt.GetoptError as err: print(err) print("Use '%s --help' to get help with command line options." % @@ -63,12 +82,13 @@ def CommandLine(argv): # Now parse other options. for opt, arg in opts: - if opt in ("-l", "--load"): + if opt in ("-v", "--verbose"): + verbose += 1 + getSettings() + + elif opt in ("-l", "--load"): cmdLoad(arg) if __name__ == '__main__': CommandLine(sys.argv[1:]) - - cmdFolder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( - inspect.currentframe()))[0])) - StartGui(cmdFolder) + StartGui(getSettings()) diff --git a/configtool/board.py b/configtool/board.py index b01cf1e..a71fd15 100644 --- a/configtool/board.py +++ b/configtool/board.py @@ -15,8 +15,10 @@ from configtool.data import (defineValueFormat, reTempTable4, reTempTable7) class Board: - def __init__(self): + def __init__(self, settings): + self.settings = settings self.configFile = None + self.cfgDir = os.path.join(self.settings.folder, "configtool") self.cfgValues = {} self.heaters = [] @@ -41,8 +43,8 @@ class Board: def getFileName(self): return self.configFile - def loadConfigFile(self, cfgDir, fn): - cfgFn = os.path.join(cfgDir, "board.generic.h") + def loadConfigFile(self, fn): + cfgFn = os.path.join(self.cfgDir, "board.generic.h") try: self.cfgBuffer = list(open(cfgFn)) except: @@ -171,17 +173,18 @@ class Board: continue # Parsing done. All parsed stuff is now in these arrays and dicts. - # Uncomment for debugging. - #print self.sensors - #print self.heaters - #print self.candHeatPins - #print self.candThermPins - #print self.candProcessors - #print self.candClocks - #print self.tempTables - #print self.cfgValues # #defines with a value and booleans. - #print self.cfgNames # Names found in the generic file. - #print self.helpText + if self.settings.verbose >= 2: + print self.sensors + print self.heaters + print self.candHeatPins + print self.candThermPins + print self.candProcessors + print self.candClocks + print self.tempTables + print self.cfgValues # #defines with a value. + print self.cfgNames # Names found in the generic file. + if self.settings.verbose >= 3: + print self.helpText for k in range(len(self.sensors)): tn = self.sensors[k][0].upper() @@ -324,6 +327,11 @@ class Board: return None def saveConfigFile(self, path, values): + if self.settings.verbose >= 1: + print("Saving board: %s." % path) + if self.settings.verbose >= 2: + print values + fp = file(path, 'w') self.configFile = path diff --git a/configtool/boardpanel.py b/configtool/boardpanel.py index 27ffa8f..6c06cdc 100644 --- a/configtool/boardpanel.py +++ b/configtool/boardpanel.py @@ -36,10 +36,9 @@ class BoardPanel(wx.Panel): self.settings = settings - self.board = Board() + self.board = Board(self.settings) self.dir = os.path.join(self.settings.folder, "config") - self.cfgDir = os.path.join(self.settings.folder, "configtool") self.SetBackgroundColour(self.deco.getBackgroundColour()) self.Bind(wx.EVT_PAINT, self.deco.onPaintBackground) @@ -187,7 +186,7 @@ class BoardPanel(wx.Panel): return def loadConfigFile(self, fn): - ok, file = self.board.loadConfigFile(self.cfgDir, fn) + ok, file = self.board.loadConfigFile(fn) if not ok: return ok, file diff --git a/configtool/gui.py b/configtool/gui.py index 3420ae8..06128d3 100644 --- a/configtool/gui.py +++ b/configtool/gui.py @@ -36,10 +36,8 @@ ID_REPORT = 1051 ID_ABOUT = 1052 -cmdFolder = "placeholder" - class ConfigFrame(wx.Frame): - def __init__(self): + def __init__(self, settings): wx.Frame.__init__(self, None, -1, "Teacup Configtool", size = (880, 550)) self.Bind(wx.EVT_CLOSE, self.onClose) self.Bind(wx.EVT_SIZE, self.onResize) @@ -50,10 +48,10 @@ class ConfigFrame(wx.Frame): panel.SetBackgroundColour(self.deco.getBackgroundColour()) panel.Bind(wx.EVT_PAINT, self.deco.onPaintBackground) - self.settings = Settings(self, cmdFolder) + self.settings = settings + self.settings.app = self self.settings.font = wx.Font(8, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD) - self.settings.folder = cmdFolder self.heaters = [] self.savePrtEna = False @@ -257,7 +255,7 @@ class ConfigFrame(wx.Frame): return rc def checkEnableLoadConfig(self): - fn = os.path.join(cmdFolder, "config.h") + fn = os.path.join(self.settings.folder, "config.h") if os.path.isfile(fn): self.fileMenu.Enable(ID_LOAD_CONFIG, True) self.buildMenu.Enable(ID_BUILD, True) @@ -268,7 +266,7 @@ class ConfigFrame(wx.Frame): return False def checkEnableUpload(self): - fn = os.path.join(cmdFolder, "teacup.hex") + fn = os.path.join(self.settings.folder, "teacup.hex") if os.path.isfile(fn): self.buildMenu.Enable(ID_UPLOAD, True) else: @@ -334,7 +332,7 @@ class ConfigFrame(wx.Frame): def getConfigFileNames(self, fn): pfile = None bfile = None - path = os.path.join(cmdFolder, fn) + path = os.path.join(self.settings.folder, fn) try: cfgBuffer = list(open(path)) except: @@ -355,14 +353,14 @@ class ConfigFrame(wx.Frame): "Ignoring %s." % ln, "Config error", wx.OK + wx.ICON_WARNING) else: - pfile = os.path.join(cmdFolder, t[0]) + pfile = os.path.join(self.settings.folder, t[0]) elif "board." in t[0]: if bfile: self.message("Multiple board file include statements.\n" "Ignoring %s." % ln, "Config error", wx.OK + wx.ICON_WARNING) else: - bfile = os.path.join(cmdFolder, t[0]) + bfile = os.path.join(self.settings.folder, t[0]) else: self.message("Unable to parse include statement:\n%s" % ln, "Config error") @@ -370,7 +368,7 @@ class ConfigFrame(wx.Frame): return pfile, bfile def onSaveConfig(self, evt): - fn = os.path.join(cmdFolder, "config.h") + fn = os.path.join(self.settings.folder, "config.h") try: fp = open(fn, 'w') except: @@ -389,7 +387,7 @@ class ConfigFrame(wx.Frame): if not self.pgPrinter.saveConfigFile(pfn): return False - prefix = cmdFolder + os.path.sep + prefix = self.settings.folder + os.path.sep lpfx = len(prefix) if bfn.startswith(prefix): @@ -612,11 +610,8 @@ class ConfigFrame(wx.Frame): dlg.Destroy() -def StartGui(teacupFolder): - global cmdFolder - - cmdFolder = teacupFolder +def StartGui(settings): app = wx.App(False) - frame = ConfigFrame() + frame = ConfigFrame(settings) frame.Show(True) app.MainLoop() diff --git a/configtool/printer.py b/configtool/printer.py index 5cd332e..0af792a 100644 --- a/configtool/printer.py +++ b/configtool/printer.py @@ -9,10 +9,12 @@ from configtool.data import (defineValueFormat, defineBoolFormat, reDefQSm2, reDefBool, reDefBoolBL) class Printer: - def __init__(self): + def __init__(self, settings): self.configFile = None self.cfgValues = {} + self.settings = settings + self.cfgDir = os.path.join(self.settings.folder, "configtool") def hasData(self): return (self.configFile != None) @@ -20,8 +22,8 @@ class Printer: def getFileName(self): return self.configFile - def loadConfigFile(self, cfgDir, fn): - cfgFn = os.path.join(cfgDir, "printer.generic.h") + def loadConfigFile(self, fn): + cfgFn = os.path.join(self.cfgDir, "printer.generic.h") try: self.cfgBuffer = list(open(cfgFn)) except: @@ -115,10 +117,11 @@ class Printer: self.parseDefineValue(ln) # Parsing done. All parsed stuff is now in these array and dicts. - # Uncomment for debugging. - #print self.cfgValues # #defines with a value and booleans. - #print self.cfgNames # Names found in the generic file. - #print self.helpText + if self.settings.verbose >= 2: + print self.cfgValues # #defines with a value. + print self.cfgNames # Names found in the generic file. + if self.settings.verbose >= 3: + print self.helpText return True, None @@ -176,6 +179,11 @@ class Printer: return False def saveConfigFile(self, path, values): + if self.settings.verbose >= 1: + print("Saving printer: %s." % path) + if self.settings.verbose >= 2: + print values + fp = file(path, 'w') self.configFile = path diff --git a/configtool/printerpanel.py b/configtool/printerpanel.py index 26134af..b17a51b 100644 --- a/configtool/printerpanel.py +++ b/configtool/printerpanel.py @@ -26,10 +26,9 @@ class PrinterPanel(wx.Panel): self.settings = settings - self.printer = Printer() + self.printer = Printer(self.settings) self.dir = os.path.join(self.settings.folder, "config") - self.cfgDir = os.path.join(self.settings.folder, "configtool") self.SetBackgroundColour(self.deco.getBackgroundColour()) self.Bind(wx.EVT_PAINT, self.deco.onPaintBackground) @@ -170,7 +169,7 @@ class PrinterPanel(wx.Panel): return def loadConfigFile(self, fn): - ok, file = self.printer.loadConfigFile(self.cfgDir, fn) + ok, file = self.printer.loadConfigFile(fn) if not ok: return ok, file diff --git a/configtool/settings.py b/configtool/settings.py index d62caa6..1ce39a7 100644 --- a/configtool/settings.py +++ b/configtool/settings.py @@ -23,9 +23,9 @@ R1 = 12 class Settings: - def __init__(self, app, folder): + def __init__(self, app, folder, ini=None): self.app = app - self.cmdfolder = folder + self.folder = folder self.inifile = os.path.join(folder, INIFILE) self.section = "configtool" @@ -44,14 +44,24 @@ class Settings: self.maxAdc = 1023 self.minAdc = 1 + # Runtime settings + self.verbose = 0 + self.cfg = ConfigParser.ConfigParser() self.cfg.optionxform = str - if not self.cfg.read(self.inifile): - if not self.cfg.read(os.path.join(folder, DEFAULT_INIFILE)): - print ("Neither of settings files %s or %s exist. Using default values." - % (INIFILE, DEFAULT_INIFILE)) - return + self.loaded = self.readConfig(ini) + + def readConfig(self, ini): + if ini: + if not self.cfg.read(ini): + return False + else: + if not self.cfg.read(self.inifile): + if not self.cfg.read(os.path.join(self.folder, DEFAULT_INIFILE)): + print ("Neither of settings files %s or %s exist. Using default values." + % (INIFILE, DEFAULT_INIFILE)) + return False if self.cfg.has_section(self.section): for opt, value in self.cfg.items(self.section): @@ -86,6 +96,9 @@ class Settings: print "Unknown %s option: %s - ignoring." % (self.section, opt) else: print "Missing %s section - assuming defaults." % self.section + return False + + return True def saveSettings(self): self.section = "configtool"