|
@@ -14,6 +14,7 @@ Classes:
|
|
|
|
|
|
Functions:
|
|
|
- RunCommand
|
|
|
+ - GetDefaultEncoding
|
|
|
|
|
|
(C) 2007-2008, 2010-2011 by the GRASS Development Team
|
|
|
|
|
@@ -29,8 +30,8 @@ import sys
|
|
|
import time
|
|
|
import errno
|
|
|
import signal
|
|
|
-import locale
|
|
|
import traceback
|
|
|
+import locale
|
|
|
import subprocess
|
|
|
if subprocess.mswindows:
|
|
|
from win32file import ReadFile, WriteFile
|
|
@@ -67,13 +68,8 @@ def DecodeString(string):
|
|
|
"""
|
|
|
if not string:
|
|
|
return string
|
|
|
-
|
|
|
- try:
|
|
|
- enc = locale.getdefaultlocale()[1]
|
|
|
- except ValueError, e:
|
|
|
- sys.stderr.write(_("ERROR: %s\n") % str(e))
|
|
|
- return string
|
|
|
|
|
|
+ enc = GetDefaultEncoding()
|
|
|
if enc:
|
|
|
Debug.msg(5, "DecodeString(): enc=%s" % enc)
|
|
|
return string.decode(enc)
|
|
@@ -89,7 +85,8 @@ def EncodeString(string):
|
|
|
"""
|
|
|
if not string:
|
|
|
return string
|
|
|
- enc = locale.getdefaultlocale()[1]
|
|
|
+
|
|
|
+ enc = GetDefaultEncoding()
|
|
|
if enc:
|
|
|
Debug.msg(5, "EncodeString(): enc=%s" % enc)
|
|
|
return string.encode(enc)
|
|
@@ -467,14 +464,13 @@ class Command:
|
|
|
"""!Get error message or ''"""
|
|
|
if not self.cmdThread.module:
|
|
|
return _("Unable to exectute command: '%s'") % ' '.join(self.cmd)
|
|
|
-
|
|
|
+
|
|
|
for type, msg in self.__ProcessStdErr():
|
|
|
if type == 'ERROR':
|
|
|
- enc = locale.getdefaultlocale()[1]
|
|
|
+ enc = GetDefaultEncoding()
|
|
|
if enc:
|
|
|
return unicode(msg, enc)
|
|
|
- else:
|
|
|
- return msg
|
|
|
+ return msg
|
|
|
|
|
|
return ''
|
|
|
|
|
@@ -704,3 +700,17 @@ def RunCommand(prog, flags = "", overwrite = False, quiet = False, verbose = Fal
|
|
|
|
|
|
Debug.msg(2, "gcmd.RunCommand(): return result")
|
|
|
return stdout, _formatMsg(stderr)
|
|
|
+
|
|
|
+def GetDefaultEncoding(forceUTF8 = False):
|
|
|
+ """!Get default system encoding
|
|
|
+
|
|
|
+ @param forceUTF8 return UTF8 when encoding is not defined
|
|
|
+
|
|
|
+ @return system encoding (can be None)
|
|
|
+ """
|
|
|
+ enc = locale.getdefaultlocale()[1]
|
|
|
+ if forceUTF8 and (enc is None or enc != 'UTF8'):
|
|
|
+ return 'UTF-8'
|
|
|
+
|
|
|
+ Debug.msg(1, "GetSystemEncoding(): %s" % enc)
|
|
|
+ return enc
|