[GRASS-SVN] r49045 - grass/trunk/gui/wxpython/gui_modules

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Nov 2 08:42:25 EDT 2011


Author: martinl
Date: 2011-11-02 05:42:25 -0700 (Wed, 02 Nov 2011)
New Revision: 49045

Modified:
   grass/trunk/gui/wxpython/gui_modules/gmodeler.py
   grass/trunk/gui/wxpython/gui_modules/goutput.py
Log:
wxGUI/modeler: better variable substitution for input files
	       (merge r49044 from devbr6)


Modified: grass/trunk/gui/wxpython/gui_modules/gmodeler.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/gmodeler.py	2011-11-02 12:18:11 UTC (rev 49044)
+++ grass/trunk/gui/wxpython/gui_modules/gmodeler.py	2011-11-02 12:42:25 UTC (rev 49045)
@@ -432,11 +432,9 @@
         """
         errList = list()
         
-        if not hasattr(self, "fileInput"):
-            self.fileInput = dict()
-
+        self.fileInput = dict()
+        
         # collect ascii inputs
-        cmdFileInput = list()
         for p in item.GetParams()['params']:
             if p.get('element', '') == 'file' and \
                     p.get('prompt', '') == 'input' and \
@@ -444,9 +442,9 @@
                 filename = p.get('value', p.get('default', ''))
                 if filename and \
                         mimetypes.guess_type(filename)[0] == 'text/plain':
-                    cmdFileInput.append(filename)
+                    self.fileInput[filename] = None
         
-        for finput in cmdFileInput:
+        for finput in self.fileInput:
             # read lines
             fd = open(finput, "r")
             try:
@@ -492,16 +490,20 @@
                         fd.close()
                 else:
                     self.fileInput[finput] = None
-            
+        
         return errList
     
-    def RunAction(self, item, params, log, onDone, statusbar = None):
+    def OnPrepare(self, item, params):
+        self._substituteFile(item, params, checkOnly = False)
+
+    def RunAction(self, item, params, log, onDone, onPrepare = None, statusbar = None):
         """!Run given action
 
         @param item action item
         @param params parameters dict
         @param log logging window
         @param onDone on-done method
+        @param onPrepare on-prepare method
         @param statusbar wx.StatusBar instance or None
         """
         name = item.GetName()
@@ -511,31 +513,15 @@
         
         if statusbar:
             statusbar.SetStatusText(_('Running model...'), 0)
-        
-        self._substituteFile(item, params)
-        
+            
+        data = { 'item' : item,
+                 'params' : copy.deepcopy(params) }
         log.RunCmd(command = item.GetLog(string = False, substitute = params),
-                   onDone = onDone)
+                   onDone = onDone, onPrepare = self.OnPrepare, userData = data)
         
         if name in params:
             item.SetParams(paramsOrig)
 
-    def CleanUp(self):
-        """!Clean up model"""
-        if hasattr(self, "fileInput"):
-            # restore original files
-            for finput in self.fileInput:
-                data = self.fileInput[finput]
-                if not data:
-                    continue
-                
-                fd = open(finput, "w")
-                try:
-                    fd.write(data)
-                finally:
-                    fd.close()
-            del self.fileInput
-        
     def Run(self, log, onDone, parent = None):
         """!Run model
 
@@ -918,6 +904,11 @@
         except IndexError:
             pass
         
+    def OnCmdPrepare(self, event):
+        """!Prepare for running command"""
+        event.onPrepare(item = event.userData['item'],
+                        params = event.userData['params'])
+        
     def OnCmdDone(self, event):
         """!Command done (or aborted)"""
         try:
@@ -1180,7 +1171,19 @@
     def OnDone(self, cmd, returncode):
         """!Computation finished"""
         self.SetStatusText('', 0)
-        self.model.CleanUp()
+        # restore original files
+        if hasattr(self.model, "fileInput"):
+            for finput in self.model.fileInput:
+                data = self.model.fileInput[finput]
+                if not data:
+                    continue
+                
+                fd = open(finput, "w")
+                try:
+                    fd.write(data)
+                finally:
+                    fd.close()
+            del self.model.fileInput
         
     def OnValidateModel(self, event, showMsg = True):
         """!Validate entire model"""
@@ -1935,9 +1938,12 @@
                 if substitute and 'variables' in substitute:
                     for p in substitute['variables']['params']:
                         if variable == p.get('name', ''):
-                            value = p.get('value', '')
+                            if p.get('type', 'string') == 'string':
+                                value = p.get('value', '')
+                            else:
+                                value = str(p.get('value', ''))
                             break
-                
+                    
                 if not value:
                     value = variables[variable].get('value', '')
                 

Modified: grass/trunk/gui/wxpython/gui_modules/goutput.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/goutput.py	2011-11-02 12:18:11 UTC (rev 49044)
+++ grass/trunk/gui/wxpython/gui_modules/goutput.py	2011-11-02 12:42:25 UTC (rev 49045)
@@ -51,6 +51,7 @@
 wxCmdRun,      EVT_CMD_RUN      = NewEvent()
 wxCmdDone,     EVT_CMD_DONE     = NewEvent()
 wxCmdAbort,    EVT_CMD_ABORT    = NewEvent()
+wxCmdPrepare,  EVT_CMD_PREPARE  = NewEvent()
 
 def GrassCmd(cmd, stdout = None, stderr = None):
     """!Return GRASS command thread"""
@@ -89,7 +90,7 @@
         os.environ['GRASS_MESSAGE_FORMAT'] = 'gui'
         while True:
             requestId, args, kwds = self.requestQ.get()
-            for key in ('callable', 'onDone', 'userData'):
+            for key in ('callable', 'onDone', 'onPrepare', 'userData'):
                 if key in kwds:
                     vars()[key] = kwds[key]
                     del kwds[key]
@@ -100,6 +101,16 @@
                 vars()['callable'] = GrassCmd
             
             requestTime = time.time()
+            
+            # prepare
+            event = wxCmdPrepare(cmd = args[0],
+                                 time = requestTime,
+                                 pid = requestId,
+                                 onPrepare = vars()['onPrepare'],
+                                 userData = vars()['userData'])
+            wx.PostEvent(self.parent, event)
+            
+            # run command
             event = wxCmdRun(cmd = args[0],
                              pid = requestId)
             
@@ -209,8 +220,9 @@
         self.cmdOutputTimer = wx.Timer(self.cmdOutput, id = wx.ID_ANY)
         self.cmdOutput.Bind(EVT_CMD_OUTPUT, self.OnCmdOutput)
         self.cmdOutput.Bind(wx.EVT_TIMER, self.OnProcessPendingOutputWindowEvents)
-        self.Bind(EVT_CMD_RUN, self.OnCmdRun)
-        self.Bind(EVT_CMD_DONE, self.OnCmdDone)
+        self.Bind(EVT_CMD_RUN,     self.OnCmdRun)
+        self.Bind(EVT_CMD_DONE,    self.OnCmdDone)
+        self.Bind(EVT_CMD_PREPARE, self.OnCmdPrepare)
         
         # search & command prompt
         self.cmdPrompt = prompt.GPromptSTC(parent = self)
@@ -430,7 +442,7 @@
         self.WriteLog(line, style = self.cmdOutput.StyleError, switchPage = True)
 
     def RunCmd(self, command, compReg = True, switchPage = False,
-               onDone = None):
+               onDone = None, onPrepare = None, userData = None):
         """!Run command typed into console command prompt (GPrompt).
         
         @todo Display commands (*.d) are captured and processed
@@ -442,9 +454,8 @@
         @param compReg True use computation region
         @param switchPage switch to output page
         @param onDone function to be called when command is finished
-
-        @return 0 on success
-        @return 1 on failure
+        @param onPrepare function to be called before command is launched
+        @param userData data defined for the command
         """
         if len(command) == 0:
             Debug.msg(2, "GPrompt:RunCmd(): empty command")
@@ -558,10 +569,10 @@
                     tmpreg = os.getenv("GRASS_REGION")
                     if "GRASS_REGION" in os.environ:
                         del os.environ["GRASS_REGION"]
-                    
+                
                 # process GRASS command with argument
                 self.cmdThread.RunCmd(command, stdout = self.cmdStdOut, stderr = self.cmdStdErr,
-                                      onDone = onDone)
+                                      onDone = onDone, onPrepare = onPrepare, userData = userData)
                 self.cmdOutputTimer.Start(50)
                 
                 # deactivate computational region and return to display settings
@@ -583,7 +594,7 @@
                 menuform.GUI(parent = self).ParseCommand(command)
             else:
                 self.cmdThread.RunCmd(command, stdout = self.cmdStdOut, stderr = self.cmdStdErr,
-                                      onDone = onDone)
+                                      onDone = onDone, onPrepare = onPrepare, userData = userData)
             self.cmdOutputTimer.Start(50)
         
         return 0
@@ -741,6 +752,13 @@
         self.WriteCmdLog('(%s)\n%s' % (str(time.ctime()), ' '.join(event.cmd)))
         self.btnCmdAbort.Enable()
 
+    def OnCmdPrepare(self, event):
+        """!Prepare for running command"""
+        if self.parent.GetName() == 'Modeler':
+            self.parent.OnCmdPrepare(event)
+        
+        event.Skip()
+        
     def OnCmdDone(self, event):
         """!Command done (or aborted)"""
         if self.parent.GetName() == 'Modeler':



More information about the grass-commit mailing list