[GRASS-SVN] r51416 - in grass/trunk/gui/wxpython: core gui_core

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Apr 13 12:54:19 EDT 2012


Author: martinl
Date: 2012-04-13 09:54:18 -0700 (Fri, 13 Apr 2012)
New Revision: 51416

Modified:
   grass/trunk/gui/wxpython/core/gcmd.py
   grass/trunk/gui/wxpython/gui_core/dialogs.py
   grass/trunk/gui/wxpython/gui_core/goutput.py
   grass/trunk/gui/wxpython/gui_core/gselect.py
Log:
wxGUI: RunCmd() use valid env variables
       various fixes related to GdalSelect


Modified: grass/trunk/gui/wxpython/core/gcmd.py
===================================================================
--- grass/trunk/gui/wxpython/core/gcmd.py	2012-04-13 13:27:42 UTC (rev 51415)
+++ grass/trunk/gui/wxpython/core/gcmd.py	2012-04-13 16:54:18 UTC (rev 51416)
@@ -474,10 +474,11 @@
 class CommandThread(Thread):
     """!Create separate thread for command. Used for commands launched
     on the background."""
-    def __init__ (self, cmd, stdin = None,
+    def __init__ (self, cmd, env = None, stdin = None,
                   stdout = sys.stdout, stderr = sys.stderr):
         """
         @param cmd command (given as list)
+        @param env environmental variables
         @param stdin standard input stream 
         @param stdout redirect standard output or None
         @param stderr redirect standard error output or None
@@ -488,6 +489,7 @@
         self.stdin  = stdin
         self.stdout = stdout
         self.stderr = stderr
+        self.env    = env
         
         self.module = None
         self.error  = ''
@@ -532,7 +534,8 @@
                                 stdin = subprocess.PIPE,
                                 stdout = subprocess.PIPE,
                                 stderr = subprocess.PIPE,
-                                shell = sys.platform == "win32")
+                                shell = sys.platform == "win32",
+                                env = self.env)
             
         except OSError, e:
             self.error = str(e)

Modified: grass/trunk/gui/wxpython/gui_core/dialogs.py
===================================================================
--- grass/trunk/gui/wxpython/gui_core/dialogs.py	2012-04-13 13:27:42 UTC (rev 51415)
+++ grass/trunk/gui/wxpython/gui_core/dialogs.py	2012-04-13 16:54:18 UTC (rev 51416)
@@ -1758,9 +1758,18 @@
         """!Import/Link data (each layes as separate vector map)"""
         self.commandId = -1
         data = self.list.GetLayers()
+        dsn  = self.dsnInput.GetDsn()
+        ext  = self.dsnInput.GetFormatExt()
         
-        dsn = self.dsnInput.GetDsn()
-        ext = self.dsnInput.GetFormatExt()
+        # determine data driver for PostGIS links
+        popOGR = False
+        if self.importType == 'ogr' and \
+                self.dsnInput.GetType() == 'db' and \
+                self.dsnInput.GetFormat() == 'PostgreSQL' and \
+                'GRASS_VECTOR_OGR' not in os.environ:
+            popOGR = True
+            os.environ['GRASS_VECTOR_OGR'] = '1'
+        
         for layer, output in data:
             if self.importType == 'ogr':
                 if ext and layer.rfind(ext) > -1:
@@ -1797,13 +1806,17 @@
                 if self.options[key].IsChecked():
                     cmd.append('-%s' % key)
             
-            if UserSettings.Get(group = 'cmd', key = 'overwrite', subkey = 'enabled'):
+            if UserSettings.Get(group = 'cmd', key = 'overwrite', subkey = 'enabled') and \
+                    '--overwrite' not in cmd:
                 cmd.append('--overwrite')
             
             # run in Layer Manager
             self.parent.goutput.RunCmd(cmd, switchPage = True,
                                        onDone = self.AddLayers)
         
+        if popOGR:
+            os.environ.pop('GRASS_VECTOR_OGR')
+        
     def _getCommand(self):
         """!Get command"""
         if self.link:

Modified: grass/trunk/gui/wxpython/gui_core/goutput.py
===================================================================
--- grass/trunk/gui/wxpython/gui_core/goutput.py	2012-04-13 13:27:42 UTC (rev 51415)
+++ grass/trunk/gui/wxpython/gui_core/goutput.py	2012-04-13 16:54:18 UTC (rev 51416)
@@ -52,9 +52,9 @@
 wxCmdAbort,    EVT_CMD_ABORT    = NewEvent()
 wxCmdPrepare,  EVT_CMD_PREPARE  = NewEvent()
 
-def GrassCmd(cmd, stdout = None, stderr = None):
+def GrassCmd(cmd, env = None, stdout = None, stderr = None):
     """!Return GRASS command thread"""
-    return CommandThread(cmd,
+    return CommandThread(cmd, env = env,
                          stdout = stdout, stderr = stderr)
 
 class CmdThread(threading.Thread):
@@ -70,7 +70,7 @@
         
         self.requestQ = requestQ
         self.resultQ = resultQ
-
+        
         self.start()
 
     def RunCmd(self, *args, **kwds):
@@ -578,7 +578,8 @@
                 
                 # process GRASS command with argument
                 self.cmdThread.RunCmd(command, stdout = self.cmdStdOut, stderr = self.cmdStdErr,
-                                      onDone = onDone, onPrepare = onPrepare, userData = userData)
+                                      onDone = onDone, onPrepare = onPrepare, userData = userData,
+                                      env = os.environ.copy())
                 self.cmdOutputTimer.Start(50)
                 
                 # deactivate computational region and return to display settings

Modified: grass/trunk/gui/wxpython/gui_core/gselect.py
===================================================================
--- grass/trunk/gui/wxpython/gui_core/gselect.py	2012-04-13 13:27:42 UTC (rev 51415)
+++ grass/trunk/gui/wxpython/gui_core/gselect.py	2012-04-13 16:54:18 UTC (rev 51416)
@@ -23,7 +23,7 @@
  - gselect::ElementSelect
  - gselect::OgrTypeSelect
 
-(C) 2007-2011 by the GRASS Development Team 
+(C) 2007-2012 by the GRASS Development Team 
 
 This program is free software under the GNU General Public License
 (>=v2). Read the file COPYING that comes with GRASS for details.
@@ -1151,13 +1151,16 @@
             self.settingsFile = os.path.join(GetSettingsPath(), 'wxOGR')
         else:
             self.settingsFile = os.path.join(GetSettingsPath(), 'wxGDAL')
-        
-        self._settings = self._loadSettings()
+
         self.settingsChoice = wx.Choice(parent = self, id = wx.ID_ANY)
         self.settingsChoice.Bind(wx.EVT_CHOICE, self.OnSettingsLoad)
-        self.settingsChoice.SetItems(self._settings.keys())
-        self.btnSettings = wx.Button(parent = self, id = wx.ID_SAVE)
-        self.btnSettings.Bind(wx.EVT_BUTTON, self.OnSettingsSave)
+        self._settings = self._loadSettings() # -> self.settingsChoice.SetItems()
+        self.btnSettingsSave = wx.Button(parent = self, id = wx.ID_SAVE)
+        self.btnSettingsSave.Bind(wx.EVT_BUTTON, self.OnSettingsSave)
+        self.btnSettingsSave.SetToolTipString(_("Save current settings"))
+        self.btnSettingsDel = wx.Button(parent = self, id = wx.ID_REMOVE)
+        self.btnSettingsDel.Bind(wx.EVT_BUTTON, self.OnSettingsDelete)
+        self.btnSettingsSave.SetToolTipString(_("Delete currently selected settings"))
         
         self.source = wx.RadioBox(parent = self, id = wx.ID_ANY,
                                   style = wx.RA_SPECIFY_COLS,
@@ -1305,9 +1308,12 @@
         settingsSizer.Add(item = self.settingsChoice,
                           proportion = 1,
                           flag = wx.EXPAND)
-        settingsSizer.Add(item = self.btnSettings,
-                          flag = wx.LEFT,
+        settingsSizer.Add(item = self.btnSettingsSave,
+                          flag = wx.LEFT | wx.RIGHT,
                           border = 5)
+        settingsSizer.Add(item = self.btnSettingsDel,
+                          flag = wx.RIGHT,
+                          border = 5)
         
         inputSizer = wx.StaticBoxSizer(self.inputBox, wx.HORIZONTAL)
         
@@ -1392,41 +1398,69 @@
         if dlg.ShowModal() != wx.ID_OK:
             return
         
+        # check required params
         if not dlg.GetValue():
             GMessage(parent = self,
                      message = _("Name not given, settings is not saved."))
             return
         
+        if not self.GetDsn():
+            GMessage(parent = self,
+                     message = _("No data source defined, settings is not saved."))
+            return
+        
         name = dlg.GetValue()
         
         # check if settings item already exists
         if name in self._settings:
             dlgOwt = wx.MessageDialog(self, message = _("Settings <%s> already exists. "
-                                                     "Do you want to overwrite the settings?") % name,
-                                   caption = _("Save settings"), style = wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
+                                                        "Do you want to overwrite the settings?") % name,
+                                      caption = _("Save settings"), style = wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
             if dlgOwt.ShowModal() != wx.ID_YES:
                 dlgOwt.Destroy()
                 return
-
+        
         self._settings[name] = (self.dsnType, self.GetDsn(),
                                 self.format.GetStringSelection(),
                                 self.creationOpt.GetValue())
+        
+        if self._saveSettings() == 0:
+            self._settings = self._loadSettings()
+            self.settingsChoice.SetStringSelection(name)
+        
+        dlg.Destroy()
+ 
+    def OnSettingsDelete(self, event):
+        """!Save settings"""
+        name = self.settingsChoice.GetStringSelection()
+        if not name:
+            GMessage(parent = self,
+                     message = _("No settings is defined. Operation canceled."))
+            return
+        
+        self._settings.pop(name)
+        if self._saveSettings() == 0:
+            self._settings = self._loadSettings()
+        
+    def _saveSettings(self):
+        """!Save settings into the file
+
+        @return 0 on success
+        @return -1 on failure
+        """
         try:
             fd = open(self.settingsFile, 'w')
-            for name, value in self._settings.iteritems():
-                fd.write('%s;%s;%s;%s\n' % (name, value[0], value[1], value[2]))
+            for key, value in self._settings.iteritems():
+                fd.write('%s;%s;%s;%s\n' % (key, value[0], value[1], value[2]))
         except IOError:
             GError(parent = self,
                    message = _("Unable to save settings"))
-            return
-        fd.close()
+            return -1
+        else:
+            fd.close()
         
-        self._settings = self._loadSettings()
-        self.settingsChoice.Append(name)
-        self.settingsChoice.SetStringSelection(name)
-        
-        dlg.Destroy()
-                
+        return 0
+
     def _loadSettings(self):
         """!Load settings from the file
 
@@ -1453,8 +1487,10 @@
                     pass
         except IOError:
             return data
+        else:
+            fd.close()
         
-        fd.close()
+        self.settingsChoice.SetItems(sorted(data.keys()))
         
         return data
 
@@ -1572,7 +1608,9 @@
                 for item in path.split(':', 1)[1].split(','):
                     key, value = item.split('=', 1)
                     if key == 'dbname':
-                        self.input[self.dsnType][1].SetStringSelection(value)
+                        if not self.input[self.dsnType][1].SetStringSelection(value):
+                            GMessage(_("Database <%s> not accessible.") % value,
+                                     parent = self)
                         break
             else:
                 self.input[self.dsnType][1].SetValue(path)



More information about the grass-commit mailing list