浏览代码

wxGUI: StoreEnvVariable() - variable mismatch
write also skipped lines


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@53496 15284696-431f-4ddb-bdfa-cd5b030d7da7

Martin Landa 12 年之前
父节点
当前提交
538ba40599
共有 2 个文件被更改,包括 29 次插入15 次删除
  1. 26 12
      gui/wxpython/core/utils.py
  2. 3 3
      gui/wxpython/gui_core/preferences.py

+ 26 - 12
gui/wxpython/core/utils.py

@@ -762,9 +762,12 @@ def GetSettingsPath():
     
     
     return os.path.join(os.getenv('HOME'), '.grass%d' % version)
     return os.path.join(os.getenv('HOME'), '.grass%d' % version)
 
 
-def StoreEnvVariable(key, value, envFile = None):
+def StoreEnvVariable(key, value = None, envFile = None):
     """!Store environmental variable
     """!Store environmental variable
 
 
+    If value is not given (is None) then environmental variable is
+    unset.
+    
     @param key env key
     @param key env key
     @param value env value
     @param value env value
     @param envFile path to the environmental file (None for default location)
     @param envFile path to the environmental file (None for default location)
@@ -779,32 +782,39 @@ def StoreEnvVariable(key, value, envFile = None):
     
     
     # read env file
     # read env file
     environ = dict()
     environ = dict()
+    lineSkipped = list()
     if os.path.exists(envFile):
     if os.path.exists(envFile):
         try:
         try:
             fd = open(envFile)
             fd = open(envFile)
         except IOError, e:
         except IOError, e:
-            sys.stderr.write(_("Unable to open file '%s'") % envFile)
+            sys.stderr.write(_("Unable to open file '%s'\n") % envFile)
             return
             return
         for line in fd.readlines():
         for line in fd.readlines():
+            line = line.rstrip(os.linesep)
             try:
             try:
-                key, value = line.split(' ', 1).strip().split('=', 1)
-            except:
-                sys.stderr.write(_("%s: unable to parse '%s'") % (envFile, line))
+                k, v = map(lambda x: x.strip(), line.split(' ', 1)[1].split('=', 1))
+            except StandardError, e:
+                sys.stderr.write(_("%s: line skipped - unable to parse '%s'\n"
+                                   "Reason: %s\n") % (envFile, line, e))
+                lineSkipped.append(line)
                 continue
                 continue
-            if key in environ:
-                sys.stderr.write(_("Duplicated key: %s") % key)
-            environ[key] = value
+            if k in environ:
+                sys.stderr.write(_("Duplicated key: %s\n") % k)
+            environ[k] = v
         
         
         fd.close()
         fd.close()
     
     
     # update environmental variables
     # update environmental variables
-    environ[key] = value
+    if value is None and key in environ:
+        del environ[key]
+    else:
+        environ[key] = value
     
     
     # write update env file
     # write update env file
     try:
     try:
         fd = open(envFile, 'w')
         fd = open(envFile, 'w')
     except IOError, e:
     except IOError, e:
-        sys.stderr.write(_("Unable to create file '%s'") % envFile)
+        sys.stderr.write(_("Unable to create file '%s'\n") % envFile)
         return
         return
     if windows:
     if windows:
         expCmd = 'set'
         expCmd = 'set'
@@ -813,7 +823,11 @@ def StoreEnvVariable(key, value, envFile = None):
     
     
     for key, value in environ.iteritems():
     for key, value in environ.iteritems():
         fd.write('%s %s=%s\n' % (expCmd, key, value))
         fd.write('%s %s=%s\n' % (expCmd, key, value))
-        
+    
+    # write also skipped lines
+    for line in lineSkipped:
+        fd.write(line + os.linesep)
+    
     fd.close()
     fd.close()
 
 
 def SetAddOnPath(addonPath = None, key = 'PATH'):
 def SetAddOnPath(addonPath = None, key = 'PATH'):
@@ -834,7 +848,7 @@ def SetAddOnPath(addonPath = None, key = 'PATH'):
                                                   'GRASS%s' % gVersion,
                                                   'GRASS%s' % gVersion,
                                                   'addons'))
                                                   'addons'))
     
     
-    StoreEnvVariable('GRASS_ADDON_' + key, addonPath)
+    StoreEnvVariable(key = 'GRASS_ADDON_' + key, value = addonPath)
     os.environ['GRASS_ADDON_' + key] = addonPath
     os.environ['GRASS_ADDON_' + key] = addonPath
 
 
 # From lib/gis/col_str.c, except purple which is mentioned
 # From lib/gis/col_str.c, except purple which is mentioned

+ 3 - 3
gui/wxpython/gui_core/preferences.py

@@ -47,7 +47,7 @@ from grass.script import core as grass
 
 
 from core          import globalvar
 from core          import globalvar
 from core.gcmd     import RunCommand, GError
 from core.gcmd     import RunCommand, GError
-from core.utils    import ListOfMapsets, GetColorTables, ReadEpsgCodes, GetSettingsPath
+from core.utils    import ListOfMapsets, GetColorTables, ReadEpsgCodes, GetSettingsPath, StoreEnvVariable
 from core.settings import UserSettings
 from core.settings import UserSettings
 from gui_core.dialogs import SymbolDialog
 from gui_core.dialogs import SymbolDialog
 
 
@@ -177,9 +177,9 @@ class PreferencesBaseDialog(wx.Dialog):
             self.settings.SaveToFile()
             self.settings.SaveToFile()
             self.parent.goutput.WriteLog(_('Settings saved to file \'%s\'.') % self.settings.filePath)
             self.parent.goutput.WriteLog(_('Settings saved to file \'%s\'.') % self.settings.filePath)
             if lang:
             if lang:
-                RunCommand('g.gisenv', set = 'LANG=%s' % lang)
+                StoreEnvVariable(key = 'LANG', value = lang)
             else:
             else:
-                RunCommand('g.gisenv', unset = 'LANG')
+                StoreEnvVariable(key = 'LANG')
             event = wxSettingsChanged()
             event = wxSettingsChanged()
             wx.PostEvent(self, event)
             wx.PostEvent(self, event)
             self.Close()
             self.Close()