[GRASS-SVN] r45545 - in grass/branches/develbranch_6: gui/wxpython gui/wxpython/gui_modules lib/python

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Mar 3 18:11:25 EST 2011


Author: martinl
Date: 2011-03-03 15:11:24 -0800 (Thu, 03 Mar 2011)
New Revision: 45545

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


Modified: grass/branches/develbranch_6/gui/wxpython/gis_set.py
===================================================================
--- grass/branches/develbranch_6/gui/wxpython/gis_set.py	2011-03-03 23:09:10 UTC (rev 45544)
+++ grass/branches/develbranch_6/gui/wxpython/gis_set.py	2011-03-03 23:11:24 UTC (rev 45545)
@@ -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,34 +379,34 @@
 
         self.Layout()
 
-    def _read_grassrc(self):
+    def _readGisRC(self):
         """
         Read variables from $HOME/.grassrc6 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
@@ -585,10 +588,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
@@ -602,10 +605,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)
@@ -641,7 +644,7 @@
                                             self.listOfLocations[self.lblocations.GetSelection()]))
         else:
             self.listOfMapsets = []
-
+        
         disabled = []
         idx = 0
         try:

Modified: grass/branches/develbranch_6/gui/wxpython/gui_modules/gcmd.py
===================================================================
--- grass/branches/develbranch_6/gui/wxpython/gui_modules/gcmd.py	2011-03-03 23:09:10 UTC (rev 45544)
+++ grass/branches/develbranch_6/gui/wxpython/gui_modules/gcmd.py	2011-03-03 23:11:24 UTC (rev 45545)
@@ -599,8 +599,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/branches/develbranch_6/gui/wxpython/gui_modules/gselect.py
===================================================================
--- grass/branches/develbranch_6/gui/wxpython/gui_modules/gselect.py	2011-03-03 23:09:10 UTC (rev 45544)
+++ grass/branches/develbranch_6/gui/wxpython/gui_modules/gselect.py	2011-03-03 23:11:24 UTC (rev 45545)
@@ -324,9 +324,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
@@ -340,9 +342,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/branches/develbranch_6/gui/wxpython/gui_modules/utils.py
===================================================================
--- grass/branches/develbranch_6/gui/wxpython/gui_modules/utils.py	2011-03-03 23:09:10 UTC (rev 45544)
+++ grass/branches/develbranch_6/gui/wxpython/gui_modules/utils.py	2011-03-03 23:11:24 UTC (rev 45545)
@@ -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/branches/develbranch_6/lib/python/core.py
===================================================================
--- grass/branches/develbranch_6/lib/python/core.py	2011-03-03 23:09:10 UTC (rev 45544)
+++ grass/branches/develbranch_6/lib/python/core.py	2011-03-03 23:11:24 UTC (rev 45545)
@@ -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