Переглянути джерело

attempt to fix https://trac.osgeo.org/grass/ticket/1293 (Creating mapset with non-latin letter gives python ascii error)

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@45544 15284696-431f-4ddb-bdfa-cd5b030d7da7
Martin Landa 14 роки тому
батько
коміт
598842b8f9

+ 16 - 13
gui/wxpython/gis_set.py

@@ -26,6 +26,7 @@ import glob
 import shutil
 import copy
 import platform
+import codecs
 
 ### i18N
 import gettext
@@ -46,6 +47,8 @@ import wx.lib.filebrowsebutton as filebrowse
 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 @@ class GRASSStartup(wx.Frame):
         # 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 @@ class GRASSStartup(wx.Frame):
 
         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 @@ class GRASSStartup(wx.Frame):
     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 @@ class GRASSStartup(wx.Frame):
                                   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 @@ class GRASSStartup(wx.Frame):
                                             self.listOfLocations[self.lblocations.GetSelection()]))
         else:
             self.listOfMapsets = []
-
+        
         disabled = []
         idx = 0
         try:

+ 2 - 2
gui/wxpython/gui_modules/gcmd.py

@@ -600,8 +600,8 @@ def RunCommand(prog, flags = "", overwrite = False, quiet = False, verbose = Fal
         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: 

+ 6 - 3
gui/wxpython/gui_modules/gselect.py

@@ -325,9 +325,11 @@ class TreeCtrlComboPopup(wx.combo.ComboPopup):
                 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 @@ class TreeCtrlComboPopup(wx.combo.ComboPopup):
                                 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')

+ 19 - 6
gui/wxpython/gui_modules/utils.py

@@ -619,16 +619,16 @@ def GetListOfMapsets(dbase, location, selectable = False):
         
         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 @@ def GetColorTables():
     
     return ret.splitlines()
 
-def EncodeString(string):
-    """!Return encoded string
+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 using system locales
+    
     @param string string to be encoded
-
+    
     @return encoded string
     """
     enc = locale.getdefaultlocale()[1]

+ 9 - 1
lib/python/core.py

@@ -30,6 +30,7 @@ import re
 import atexit
 import subprocess
 import shutil
+import locale
 
 # i18N
 import gettext
@@ -75,6 +76,13 @@ _popen_args = ["bufsize", "executable", "stdin", "stdout", "stderr",
 	       "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 @@ def read_command(*args, **kwargs):
     @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