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

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Mar 6 18:40:58 EST 2011


Author: martinl
Date: 2011-03-06 15:40:58 -0800 (Sun, 06 Mar 2011)
New Revision: 45591

Modified:
   grass/trunk/gui/wxpython/gui_modules/gcmd.py
   grass/trunk/gui/wxpython/gui_modules/gdialogs.py
   grass/trunk/gui/wxpython/gui_modules/goutput.py
   grass/trunk/gui/wxpython/gui_modules/mapdisp.py
Log:
wxGUI: clean up CommandThread class


Modified: grass/trunk/gui/wxpython/gui_modules/gcmd.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/gcmd.py	2011-03-06 18:38:31 UTC (rev 45590)
+++ grass/trunk/gui/wxpython/gui_modules/gcmd.py	2011-03-06 23:40:58 UTC (rev 45591)
@@ -123,13 +123,13 @@
         
         subprocess.Popen.__init__(self, *args, **kwargs)
         
-    def recv(self, maxsize=None):
+    def recv(self, maxsize = None):
         return self._recv('stdout', maxsize)
     
-    def recv_err(self, maxsize=None):
+    def recv_err(self, maxsize = None):
         return self._recv('stderr', maxsize)
 
-    def send_recv(self, input='', maxsize=None):
+    def send_recv(self, input = '', maxsize = None):
         return self.send(input), self.recv(maxsize), self.recv_err(maxsize)
 
     def get_conn_maxsize(self, which, maxsize):
@@ -239,7 +239,7 @@
 
 message = "Other end disconnected!"
 
-def recv_some(p, t=.1, e=1, tr=5, stderr=0):
+def recv_some(p, t = .1, e = 1, tr = 5, stderr = 0):
     if tr < 1:
         tr = 1
     x = time.time()+t
@@ -286,9 +286,9 @@
             print 'FAILURE (%d)' % cmd.returncode
     @endcode
     """
-    def __init__ (self, cmd, stdin=None,
-                  verbose=None, wait=True, rerr=False,
-                  stdout=None, stderr=None):
+    def __init__ (self, cmd, stdin = None,
+                  verbose = None, wait = True, rerr = False,
+                  stdout = None, stderr = None):
         """
         @param cmd     command given as list
         @param stdin   standard input stream
@@ -442,8 +442,8 @@
 class CommandThread(Thread):
     """!Create separate thread for command. Used for commands launched
     on the background."""
-    def __init__ (self, cmd, stdin=None,
-                  stdout=sys.stdout, stderr=sys.stderr):
+    def __init__ (self, cmd, stdin = None,
+                  stdout = sys.stdout, stderr = sys.stderr):
         """
         @param cmd command (given as list)
         @param stdin standard input stream 
@@ -451,7 +451,7 @@
         @param stderr redirect standard error output or None
         """
         Thread.__init__(self)
-
+        
         self.cmd    = cmd
         self.stdin  = stdin
         self.stdout = stdout
@@ -480,13 +480,15 @@
         if len(self.cmd) == 0:
             return
 
+        Debug.msg(1, "gcmd.CommandThread(): %s" % ' '.join(self.cmd))
+
         self.startTime = time.time()
         try:
             self.module = Popen(self.cmd,
-                                stdin=subprocess.PIPE,
-                                stdout=subprocess.PIPE,
-                                stderr=subprocess.PIPE,
-                                shell=sys.platform=="win32")
+                                stdin = subprocess.PIPE,
+                                stdout = subprocess.PIPE,
+                                stderr = subprocess.PIPE,
+                                shell = sys.platform == "win32")
         except OSError, e:
             self.error = str(e)
             return 1
@@ -496,10 +498,9 @@
             self.module.stdin.close()
             
         # redirect standard outputs...
-        if self.stdout or self.stderr:
-            self.__redirect_stream()
-
-    def __redirect_stream(self):
+        self._redirect_stream()
+        
+    def _redirect_stream(self):
         """!Redirect stream"""
         if self.stdout:
             # make module stdout/stderr non-blocking
@@ -522,20 +523,20 @@
                 self.aborted = True
                 return 
             if self.stdout:
-                line = recv_some(self.module, e=0, stderr=0)
+                line = recv_some(self.module, e = 0, stderr = 0)
                 self.stdout.write(line)
             if self.stderr:
-                line = recv_some(self.module, e=0, stderr=1)
+                line = recv_some(self.module, e = 0, stderr = 1)
                 self.stderr.write(line)
                 if len(line) > 0:
                     self.error = line
 
         # get the last output
         if self.stdout:
-            line = recv_some(self.module, e=0, stderr=0)
+            line = recv_some(self.module, e = 0, stderr = 0)
             self.stdout.write(line)
         if self.stderr:
-            line = recv_some(self.module, e=0, stderr=1)
+            line = recv_some(self.module, e = 0, stderr = 1)
             self.stderr.write(line)
             if len(line) > 0:
                 self.error = line

Modified: grass/trunk/gui/wxpython/gui_modules/gdialogs.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/gdialogs.py	2011-03-06 18:38:31 UTC (rev 45590)
+++ grass/trunk/gui/wxpython/gui_modules/gdialogs.py	2011-03-06 23:40:58 UTC (rev 45591)
@@ -1221,7 +1221,7 @@
                 cmd.append('--overwrite')
             
             # run in Layer Manager
-            self.parent.goutput.RunCmd(cmd, switchPage=True,
+            self.parent.goutput.RunCmd(cmd, switchPage = True,
                                        onDone = self.AddLayers)
         
         self.OnCancel()

Modified: grass/trunk/gui/wxpython/gui_modules/goutput.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/goutput.py	2011-03-06 18:38:31 UTC (rev 45590)
+++ grass/trunk/gui/wxpython/gui_modules/goutput.py	2011-03-06 23:40:58 UTC (rev 45591)
@@ -49,10 +49,10 @@
 wxCmdDone,     EVT_CMD_DONE     = NewEvent()
 wxCmdAbort,    EVT_CMD_ABORT    = NewEvent()
 
-def GrassCmd(cmd, stdout, stderr):
+def GrassCmd(cmd, stdout = None, stderr = None):
     """!Return GRASS command thread"""
     return gcmd.CommandThread(cmd,
-                              stdout=stdout, stderr=stderr)
+                              stdout = stdout, stderr = stderr)
 
 class CmdThread(threading.Thread):
     """!Thread for GRASS commands"""
@@ -70,11 +70,11 @@
 
         self.start()
 
-    def RunCmd(self, callable, onDone, *args, **kwds):
+    def RunCmd(self, *args, **kwds):
         CmdThread.requestId += 1
         
         self.requestCmd = None
-        self.requestQ.put((CmdThread.requestId, callable, onDone, args, kwds))
+        self.requestQ.put((CmdThread.requestId, args, kwds))
         
         return CmdThread.requestId
 
@@ -85,24 +85,32 @@
     def run(self):
         os.environ['GRASS_MESSAGE_FORMAT'] = 'gui'
         while True:
-            requestId, callable, onDone, args, kwds = self.requestQ.get()
+            requestId, args, kwds = self.requestQ.get()
+            for key in ('callable', 'onDone', 'userData'):
+                if kwds.has_key(key):
+                    vars()[key] = kwds[key]
+                    del kwds[key]
+                else:
+                    vars()[key] = None
             
+            if not vars()['callable']:
+                vars()['callable'] = GrassCmd
+            
             requestTime = time.time()
-            event = wxCmdRun(cmd=args[0],
-                             pid=requestId)
+            event = wxCmdRun(cmd = args[0],
+                             pid = requestId)
             
             wx.PostEvent(self.parent, event)
             
             time.sleep(.1)
-            
-            self.requestCmd = callable(*args, **kwds)
+            self.requestCmd = vars()['callable'](*args, **kwds)
             if self._want_abort_all:
                 self.requestCmd.abort()
                 if self.requestQ.empty():
                     self._want_abort_all = False
             
             self.resultQ.put((requestId, self.requestCmd.run()))
-
+            
             try:
                 returncode = self.requestCmd.module.returncode
             except AttributeError:
@@ -136,7 +144,7 @@
                     argsColor[0] = [ 'r.colors',
                                      'map=%s' % mapName,
                                      'color=%s' % colorTable ]
-                    self.requestCmdColor = callable(*argsColor, **kwds)
+                    self.requestCmdColor = vars()['callable'](*argsColor, **kwds)
                     self.resultQ.put((requestId, self.requestCmdColor.run()))
             
             event = wxCmdDone(cmd = args[0],
@@ -144,7 +152,8 @@
                               returncode = returncode,
                               time = requestTime,
                               pid = requestId,
-                              onDone = onDone)
+                              onDone = vars()['onDone'],
+                              userData = vars()['userData'])
             
             # send event
             wx.PostEvent(self.parent, event)
@@ -530,10 +539,8 @@
                     menuform.GUI(parent = self).ParseCommand(command)
                 else:
                     # process GRASS command with argument
-                    self.cmdThread.RunCmd(GrassCmd,
-                                          onDone,
-                                          command,
-                                          self.cmd_stdout, self.cmd_stderr)                                          
+                    self.cmdThread.RunCmd(command, stdout = self.cmd_stdout, stderr = self.cmd_stderr,
+                                          onDone = onDone)
                     self.cmd_output_timer.Start(50)
                     
                     return None
@@ -557,10 +564,8 @@
                 # process GRASS command without argument
                 menuform.GUI(parent = self).ParseCommand(command)
             else:
-                self.cmdThread.RunCmd(GrassCmd,
-                                      onDone,
-                                      command,
-                                      self.cmd_stdout, self.cmd_stderr)                                         
+                self.cmdThread.RunCmd(command, stdout = self.cmd_stdout, stderr = self.cmd_stderr,
+                                      onDone = onDone)
             self.cmd_output_timer.Start(50)
         
         return None

Modified: grass/trunk/gui/wxpython/gui_modules/mapdisp.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/mapdisp.py	2011-03-06 18:38:31 UTC (rev 45590)
+++ grass/trunk/gui/wxpython/gui_modules/mapdisp.py	2011-03-06 23:40:58 UTC (rev 45591)
@@ -307,7 +307,7 @@
             if dlg.ShowModal() == wx.ID_YES:
                 mapName = self.tree.GetPyData(self.tree.layer_selected)[0]['maplayer'].GetName()
                 self._layerManager.goutput.RunCmd(['v.digit', 'map=%s' % mapName],
-                                                      switchPage=False)
+                                                  switchPage = False)
             dlg.Destroy()
             
             self.toolbars['map'].combo.SetValue(_("2D view"))



More information about the grass-commit mailing list