[GRASS-SVN] r45544 - in grass/trunk: gui/wxpython gui/wxpython/gui_modules lib/python

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Mar 3 18:09:10 EST 2011


Author: martinl
Date: 2011-03-03 15:09:10 -0800 (Thu, 03 Mar 2011)
New Revision: 45544

Modified:
   grass/trunk/gui/wxpython/gis_set.py
   grass/trunk/gui/wxpython/gui_modules/gcmd.py
   grass/trunk/gui/wxpython/gui_modules/gselect.py
   grass/trunk/gui/wxpython/gui_modules/utils.py
   grass/trunk/lib/python/core.py
Log:
attempt to fix #1293 (Creating mapset with non-latin letter gives python ascii error)


Modified: grass/trunk/gui/wxpython/gis_set.py
===================================================================
--- grass/trunk/gui/wxpython/gis_set.py	2011-03-03 19:49:36 UTC (rev 45543)
+++ grass/trunk/gui/wxpython/gis_set.py	2011-03-03 23:09:10 UTC (rev 45544)
@@ -26,6 +26,7 @@
 import shutil
 import copy
 import platform
+import codecs
 
 ### i18N
 import gettext
@@ -46,6 +47,8 @@
 import wx.lib.mixins.listctrl as listmix
 import wx.lib.scrolledpanel as scrolled
 
+sys.stderr = codecs.getwriter('utf8')(sys.stderr)
+
 class GRASSStartup(wx.Frame):
     """!GRASS start-up screen"""
     def __init__(self, parent = None, id = wx.ID_ANY, style = wx.DEFAULT_FRAME_STYLE):
@@ -54,7 +57,7 @@
         # GRASS variables
         #
         self.gisbase  = os.getenv("GISBASE")
-        self.grassrc  = self._read_grassrc()
+        self.grassrc  = self._readGisRC()
         self.gisdbase = self.GetRCValue("GISDBASE")
 
         #
@@ -376,32 +379,32 @@
 
         self.Layout()
 
-    def _read_grassrc(self):
+    def _readGisRC(self):
         """!Read variables from $HOME/.grass7/rc file
         """
         grassrc = {}
-
+        
         gisrc = os.getenv("GISRC")
-
+        
         if gisrc and os.path.isfile(gisrc):
             try:
                 rc = open(gisrc, "r")
                 for line in rc.readlines():
                     key, val = line.split(":", 1)
-                    grassrc[key.strip()] = val.strip()
+                    grassrc[key.strip()] = utils.DecodeString(val.strip())
             finally:
                 rc.close()
 
         return grassrc
 
     def GetRCValue(self, value):
-        "Return GRASS variable (read from GISRC)"""
-
+        """!Return GRASS variable (read from GISRC)
+        """
         if self.grassrc.has_key(value):
             return self.grassrc[value]
         else:
             return None
-
+        
     def OnWizard(self, event):
         """!Location wizard started"""
         from gui_modules import location_wizard
@@ -583,10 +586,10 @@
     def UpdateMapsets(self, location):
         """!Update list of mapsets"""
         self.FormerMapsetSelection = wx.NOT_FOUND # for non-selectable item
-
+        
         self.listOfMapsetsSelectable = list()
         self.listOfMapsets = utils.GetListOfMapsets(self.gisdbase, location)
-         
+        
         self.lbmapsets.Clear()
 
         # disable mapset with denied permission
@@ -600,10 +603,10 @@
                                   gisdbase = self.gisdbase)
             
             if not ret:
-                raise gcmd.CmdError("")
+                raise gcmd.GError(_("No mapsets available in location <%s>") % locationName)
             
             for line in ret.splitlines():
-                self.listOfMapsetsSelectable +=  line.split(' ')
+                self.listOfMapsetsSelectable += line.split(' ')
         except:
             gcmd.RunCommand("g.gisenv",
                             set =  "GISDBASE = %s" % self.gisdbase)
@@ -639,7 +642,7 @@
                                             self.listOfLocations[self.lblocations.GetSelection()]))
         else:
             self.listOfMapsets = []
-
+        
         disabled = []
         idx = 0
         try:

Modified: grass/trunk/gui/wxpython/gui_modules/gcmd.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/gcmd.py	2011-03-03 19:49:36 UTC (rev 45543)
+++ grass/trunk/gui/wxpython/gui_modules/gcmd.py	2011-03-03 23:09:10 UTC (rev 45544)
@@ -600,8 +600,8 @@
         ps.stdin.close()
         ps.stdin = None
     
-    stdout, stderr = ps.communicate()
-    
+    stdout, stderr = map(lambda x: utils.DecodeString(x) if x and not 'None' else x, ps.communicate())
+        
     ret = ps.returncode
         
     if ret != 0 and parent: 

Modified: grass/trunk/gui/wxpython/gui_modules/gselect.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/gselect.py	2011-03-03 19:49:36 UTC (rev 45543)
+++ grass/trunk/gui/wxpython/gui_modules/gselect.py	2011-03-03 23:09:10 UTC (rev 45544)
@@ -325,9 +325,11 @@
                 first_dir = dir_node
             
             self.seltree.SetItemTextColour(dir_node, wx.Colour(50, 50, 200))
+            if not filesdict.has_key(dir):
+                continue
             try:
                 elem_list = filesdict[dir]
-                elem_list.sort(key=str.lower)
+                elem_list.sort(key = unicode.lower)
                 for elem in elem_list:
                     if elem != '':
                         fullqElem = elem + '@' + dir
@@ -341,9 +343,10 @@
                                 self.AddItem(elem, parent=dir_node)
                         else:
                             self.AddItem(elem, parent=dir_node)
-            except:
+            except StandardError, e:
+                sys.stderr.write(_("GSelect: invalid item: %s") % e)
                 continue
-
+            
             if self.seltree.ItemHasChildren(dir_node):
                 sel = UserSettings.Get(group='general', key='elementListExpand',
                                        subkey='selection')

Modified: grass/trunk/gui/wxpython/gui_modules/utils.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/utils.py	2011-03-03 19:49:36 UTC (rev 45543)
+++ grass/trunk/gui/wxpython/gui_modules/utils.py	2011-03-03 23:09:10 UTC (rev 45544)
@@ -619,16 +619,16 @@
         
         if not ret:
             return listOfMapsets
-            
+        
         for line in ret.rstrip().splitlines():
             listOfMapsets += line.split(' ')
     else:
         for mapset in glob.glob(os.path.join(dbase, location, "*")):
             if os.path.isdir(mapset) and \
                     os.path.isfile(os.path.join(dbase, location, mapset, "WIND")):
-                listOfMapsets.append(EncodeString(os.path.basename(mapset)))
+                listOfMapsets.append(os.path.basename(mapset))
     
-    ListSortLower(listOfMapsets)    
+    ListSortLower(listOfMapsets)
     return listOfMapsets
 
 def GetColorTables():
@@ -641,11 +641,24 @@
     
     return ret.splitlines()
 
+def DecodeString(string):
+    """!Return decoded string using system locales
+    
+    @param string string to be decoded
+    
+    @return decoded string
+    """
+    enc = locale.getdefaultlocale()[1]
+    if enc:
+        return string.decode(enc)
+    
+    return string
+
 def EncodeString(string):
-    """!Return encoded string
-
+    """!Return encoded string using system locales
+    
     @param string string to be encoded
-
+    
     @return encoded string
     """
     enc = locale.getdefaultlocale()[1]

Modified: grass/trunk/lib/python/core.py
===================================================================
--- grass/trunk/lib/python/core.py	2011-03-03 19:49:36 UTC (rev 45543)
+++ grass/trunk/lib/python/core.py	2011-03-03 23:09:10 UTC (rev 45544)
@@ -30,6 +30,7 @@
 import atexit
 import subprocess
 import shutil
+import locale
 
 # i18N
 import gettext
@@ -75,6 +76,13 @@
 	       "preexec_fn", "close_fds", "cwd", "env",
 	       "universal_newlines", "startupinfo", "creationflags"]
 
+def _decode(string):
+    enc = locale.getdefaultlocale()[1]
+    if enc:
+        return string.decode(enc)
+    
+    return string
+
 def _make_val(val):
     if isinstance(val, types.StringType) or \
             isinstance(val, types.UnicodeType):
@@ -222,7 +230,7 @@
     @return stdout
     """
     ps = pipe_command(*args, **kwargs)
-    return ps.communicate()[0]
+    return _decode(ps.communicate()[0])
 
 def parse_command(*args, **kwargs):
     """!Passes all arguments to read_command, then parses the output by



More information about the grass-commit mailing list