[GRASS-SVN] r53497 - grass/branches/develbranch_6/gui/wxpython/core

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Oct 19 05:41:02 PDT 2012


Author: martinl
Date: 2012-10-19 05:41:01 -0700 (Fri, 19 Oct 2012)
New Revision: 53497

Modified:
   grass/branches/develbranch_6/gui/wxpython/core/utils.py
Log:
wxGUI: StoreEnvVariable() - variable mismatch
       write also skipped lines


Modified: grass/branches/develbranch_6/gui/wxpython/core/utils.py
===================================================================
--- grass/branches/develbranch_6/gui/wxpython/core/utils.py	2012-10-19 12:37:57 UTC (rev 53496)
+++ grass/branches/develbranch_6/gui/wxpython/core/utils.py	2012-10-19 12:41:01 UTC (rev 53497)
@@ -781,9 +781,12 @@
     
     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
 
+    If value is not given (is None) then environmental variable is
+    unset.
+    
     @param key env key
     @param value env value
     @param envFile path to the environmental file (None for default location)
@@ -798,32 +801,39 @@
     
     # read env file
     environ = dict()
+    lineSkipped = list()
     if os.path.exists(envFile):
         try:
             fd = open(envFile)
         except IOError, e:
-            sys.stderr.write(_("Unable to open file '%s'") % envFile)
+            sys.stderr.write(_("Unable to open file '%s'\n") % envFile)
             return
         for line in fd.readlines():
+            line = line.rstrip(os.linesep)
             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
-            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()
     
     # 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
     try:
         fd = open(envFile, 'w')
     except IOError, e:
-        sys.stderr.write(_("Unable to create file '%s'") % envFile)
+        sys.stderr.write(_("Unable to create file '%s'\n") % envFile)
         return
     if windows:
         expCmd = 'set'
@@ -832,7 +842,11 @@
     
     for key, value in environ.iteritems():
         fd.write('%s %s=%s\n' % (expCmd, key, value))
-        
+    
+    # write also skipped lines
+    for line in lineSkipped:
+        fd.write(line + os.linesep)
+    
     fd.close()
 
 def SetAddOnPath(addonPath = None):



More information about the grass-commit mailing list