Parcourir la source

wxGUI: check if command is available

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@44096 15284696-431f-4ddb-bdfa-cd5b030d7da7
Martin Landa il y a 14 ans
Parent
commit
6f225374ef
2 fichiers modifiés avec 42 ajouts et 49 suppressions
  1. 36 44
      gui/wxpython/gui_modules/goutput.py
  2. 6 5
      gui/wxpython/gui_modules/prompt.py

+ 36 - 44
gui/wxpython/gui_modules/goutput.py

@@ -405,27 +405,21 @@ class GMConsole(wx.SplitterWindow):
 
 
     def RunCmd(self, command, compReg = True, switchPage = False,
     def RunCmd(self, command, compReg = True, switchPage = False,
                onDone = None):
                onDone = None):
-        """!Run in GUI GRASS (or other) commands typed into console
-        command text widget, and send stdout output to output text
-        widget.
-
-        Command is transformed into a list for processing.
-
+        """!Run command typed into console command prompt (GPrompt).
+        
         @todo Display commands (*.d) are captured and processed
         @todo Display commands (*.d) are captured and processed
         separately by mapdisp.py. Display commands are rendered in map
         separately by mapdisp.py. Display commands are rendered in map
         display widget that currently has the focus (as indicted by
         display widget that currently has the focus (as indicted by
         mdidx).
         mdidx).
-
-        @param command command (list)
-        @param compReg if true use computation region
+        
+        @param command command given as a list (produced e.g. by shlex.split())
+        @param compReg True use computation region
         @param switchPage switch to output page
         @param switchPage switch to output page
         @param onDone function to be called when command is finished
         @param onDone function to be called when command is finished
         """
         """
-        # command given as a string ?
-        try:
-            cmdlist = command.strip().split(' ')
-        except:
-            cmdlist = command
+        if len(command) == 0:
+            Debug.msg(2, "GPrompt:RunCmd(): empty command")
+            return
         
         
         # update history file
         # update history file
         env = grass.gisenv()
         env = grass.gisenv()
@@ -436,10 +430,9 @@ class GMConsole(wx.SplitterWindow):
             self.WriteError(str(e))
             self.WriteError(str(e))
             fileHistory = None
             fileHistory = None
         
         
-        cmdString = ' '.join(cmdlist)
         if fileHistory:
         if fileHistory:
             try:
             try:
-                fileHistory.write(cmdString + '\n')
+                fileHistory.write(' '.join(command) + os.linesep)
             finally:
             finally:
                 fileHistory.close()
                 fileHistory.close()
         
         
@@ -450,13 +443,12 @@ class GMConsole(wx.SplitterWindow):
             except AttributeError:
             except AttributeError:
                 pass
                 pass
         
         
-        if cmdlist[0] in globalvar.grassCmd['all']:
+        if command[0] in globalvar.grassCmd['all']:
             # send GRASS command without arguments to GUI command interface
             # send GRASS command without arguments to GUI command interface
             # except display commands (they are handled differently)
             # except display commands (they are handled differently)
-            if self.parent.GetName() == "LayerManager" and cmdlist[0][0:2] == "d.":
-                #
+            if self.parent.GetName() == "LayerManager" and \
+                    command[0][0:2] == "d.":
                 # display GRASS commands
                 # display GRASS commands
-                #
                 try:
                 try:
                     layertype = {'d.rast'         : 'raster',
                     layertype = {'d.rast'         : 'raster',
                                  'd.rast3d'       : '3d-raster',
                                  'd.rast3d'       : '3d-raster',
@@ -472,19 +464,19 @@ class GMConsole(wx.SplitterWindow):
                                  'd.grid'         : 'grid',
                                  'd.grid'         : 'grid',
                                  'd.geodesic'     : 'geodesic',
                                  'd.geodesic'     : 'geodesic',
                                  'd.rhumbline'    : 'rhumb',
                                  'd.rhumbline'    : 'rhumb',
-                                 'd.labels'       : 'labels'}[cmdlist[0]]
+                                 'd.labels'       : 'labels'}[command[0]]
                 except KeyError:
                 except KeyError:
                     gcmd.GMessage(parent = self.parent,
                     gcmd.GMessage(parent = self.parent,
                                   message = _("Command '%s' not yet implemented in the WxGUI. "
                                   message = _("Command '%s' not yet implemented in the WxGUI. "
-                                              "Try adding it as a command layer instead.") % cmdlist[0])
+                                              "Try adding it as a command layer instead.") % command[0])
                     return None
                     return None
                 
                 
                 # add layer into layer tree
                 # add layer into layer tree
-                if cmdlist[0] == 'd.rast':
-                    lname = utils.GetLayerNameFromCmd(cmdlist, fullyQualified = True,
+                if command[0] == 'd.rast':
+                    lname = utils.GetLayerNameFromCmd(command, fullyQualified = True,
                                                       layerType = 'raster')
                                                       layerType = 'raster')
-                elif cmdlist[0] == 'd.vect':
-                    lname = utils.GetLayerNameFromCmd(cmdlist, fullyQualified = True,
+                elif command[0] == 'd.vect':
+                    lname = utils.GetLayerNameFromCmd(command, fullyQualified = True,
                                                       layerType = 'vector')
                                                       layerType = 'vector')
                 else:
                 else:
                     lname = None
                     lname = None
@@ -492,19 +484,16 @@ class GMConsole(wx.SplitterWindow):
                 if self.parent.GetName() == "LayerManager":                
                 if self.parent.GetName() == "LayerManager":                
                     self.parent.curr_page.maptree.AddLayer(ltype=layertype,
                     self.parent.curr_page.maptree.AddLayer(ltype=layertype,
                                                            lname=lname,
                                                            lname=lname,
-                                                           lcmd=cmdlist)
+                                                           lcmd=command)
             
             
             else:
             else:
-                #
                 # other GRASS commands (r|v|g|...)
                 # other GRASS commands (r|v|g|...)
-                #
-                
-                # switch to 'Command output'
+                # switch to 'Command output' if required
                 if switchPage:
                 if switchPage:
                     if self._notebook.GetSelection() != self.parent.goutput.pageid:
                     if self._notebook.GetSelection() != self.parent.goutput.pageid:
                         self._notebook.SetSelection(self.parent.goutput.pageid)
                         self._notebook.SetSelection(self.parent.goutput.pageid)
                     
                     
-                    self.parent.SetFocus() # -> set focus
+                    self.parent.SetFocus()
                     self.parent.Raise()
                     self.parent.Raise()
                 
                 
                 # activate computational region (set with g.region)
                 # activate computational region (set with g.region)
@@ -513,23 +502,23 @@ class GMConsole(wx.SplitterWindow):
                     tmpreg = os.getenv("GRASS_REGION")
                     tmpreg = os.getenv("GRASS_REGION")
                     if os.environ.has_key("GRASS_REGION"):
                     if os.environ.has_key("GRASS_REGION"):
                         del os.environ["GRASS_REGION"]
                         del os.environ["GRASS_REGION"]
-
-                if len(cmdlist) == 1:
+                
+                if len(command) == 1:
                     import menuform
                     import menuform
-                    task = menuform.GUI().ParseInterface(cmdlist)
+                    task = menuform.GUI().ParseInterface(command)
                     if not task.has_required():
                     if not task.has_required():
                         task = None # run command
                         task = None # run command
                 else:
                 else:
                     task = None
                     task = None
                 
                 
-                if task and cmdlist[0] not in ('v.krige'):
+                if task and command[0] not in ('v.krige'):
                     # process GRASS command without argument
                     # process GRASS command without argument
-                    menuform.GUI().ParseCommand(cmdlist, parentframe = self)
+                    menuform.GUI().ParseCommand(command, parentframe = self)
                 else:
                 else:
                     # process GRASS command with argument
                     # process GRASS command with argument
                     self.cmdThread.RunCmd(GrassCmd,
                     self.cmdThread.RunCmd(GrassCmd,
                                           onDone,
                                           onDone,
-                                          cmdlist,
+                                          command,
                                           self.cmd_stdout, self.cmd_stderr)                                          
                                           self.cmd_stdout, self.cmd_stderr)                                          
                     self.cmd_output_timer.Start(50)
                     self.cmd_output_timer.Start(50)
                     
                     
@@ -541,12 +530,15 @@ class GMConsole(wx.SplitterWindow):
         else:
         else:
             # Send any other command to the shell. Send output to
             # Send any other command to the shell. Send output to
             # console output window
             # console output window
-            self.cmdThread.RunCmd(GrassCmd,
-                                  onDone,
-                                  cmdlist,
-                                  self.cmd_stdout, self.cmd_stderr)                                         
-            self.cmd_output_timer.Start(50)
-                
+            if grass.find_program(command[0]):
+                self.cmdThread.RunCmd(GrassCmd,
+                                      onDone,
+                                      command,
+                                      self.cmd_stdout, self.cmd_stderr)                                         
+                self.cmd_output_timer.Start(50)
+            else:
+                self.WriteError(_("Command '%s' not found") % command[0])
+        
         return None
         return None
 
 
     def ClearHistory(self, event):
     def ClearHistory(self, event):

+ 6 - 5
gui/wxpython/gui_modules/prompt.py

@@ -875,12 +875,12 @@ class GPromptSTC(GPrompt, wx.stc.StyledTextCtrl):
             
             
         elif event.GetKeyCode() == wx.WXK_RETURN and \
         elif event.GetKeyCode() == wx.WXK_RETURN and \
                 self.AutoCompActive() == False:
                 self.AutoCompActive() == False:
+            # run command on line when <return> is pressed
+            
             if self.parent.GetName() == "ModelerDialog":
             if self.parent.GetName() == "ModelerDialog":
                 self.parent.OnOk(None)
                 self.parent.OnOk(None)
                 return
                 return
             
             
-            # Run command on line when <return> is pressed    
-            
             # find the command to run
             # find the command to run
             line = self.GetCurLine()[0].strip()
             line = self.GetCurLine()[0].strip()
             if len(line) == 0:
             if len(line) == 0:
@@ -892,11 +892,11 @@ class GPromptSTC(GPrompt, wx.stc.StyledTextCtrl):
             except UnicodeError:
             except UnicodeError:
                 cmd = shlex.split(utils.EncodeString((line)))
                 cmd = shlex.split(utils.EncodeString((line)))
             
             
-            # send the command list to the processor 
+            #  send the command list to the processor 
             self.parent.RunCmd(cmd)
             self.parent.RunCmd(cmd)
             
             
             # add command to history    
             # add command to history    
-            self.cmdbuffer.append(line)
+            self.cmdbuffer.append(' '.join(cmd))
             
             
             # keep command history to a managable size
             # keep command history to a managable size
             if len(self.cmdbuffer) > 200:
             if len(self.cmdbuffer) > 200:
@@ -910,7 +910,8 @@ class GPromptSTC(GPrompt, wx.stc.StyledTextCtrl):
             items = self.GetTextLeft().split()
             items = self.GetTextLeft().split()
             if len(items) == 1:
             if len(items) == 1:
                 cmd = items[0].strip()
                 cmd = items[0].strip()
-                if not self.cmdDesc or cmd != self.cmdDesc.get_name():
+                if cmd in globalvar.grassCmd['all'] and \
+                        (not self.cmdDesc or cmd != self.cmdDesc.get_name()):
                     try:
                     try:
                         self.cmdDesc = menuform.GUI().ParseInterface(cmd = [cmd])
                         self.cmdDesc = menuform.GUI().ParseInterface(cmd = [cmd])
                     except IOError:
                     except IOError: