Ver código fonte

wxGUI: wx.psmap - fix reading and writing configuration file with non ascii characters

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@49667 15284696-431f-4ddb-bdfa-cd5b030d7da7
Anna Petrášová 13 anos atrás
pai
commit
7f2d568cc6
1 arquivos alterados com 42 adições e 22 exclusões
  1. 42 22
      gui/wxpython/psmap/dialogs.py

+ 42 - 22
gui/wxpython/psmap/dialogs.py

@@ -951,11 +951,30 @@ class Text(InstructionObject):
         
     def __str__(self):
         text = self.instruction['text'].replace('\n','\\n')
-        instr = "text %s %s" % (self.instruction['east'], self.instruction['north'])
+        instr = u"text %s %s" % (self.instruction['east'], self.instruction['north'])
+        instr += " %s\n" % text
+        instr += (string.Template("    font $font\n    fontsize $fontsize\n    color $color\n").
+                                                                   substitute(self.instruction))
+        instr += string.Template("    hcolor $hcolor\n").substitute(self.instruction)
+        if self.instruction['hcolor'] != 'none':
+            instr += string.Template("    hwidth $hwidth\n").substitute(self.instruction)
+        instr += string.Template("    border $border\n").substitute(self.instruction)
+        if self.instruction['border'] != 'none':
+            instr += string.Template("    width $width\n").substitute(self.instruction)
+        instr += string.Template("    background $background\n").substitute(self.instruction)
+        if self.instruction["ref"] != '0':
+            instr += string.Template("    ref $ref\n").substitute(self.instruction)
+        if self.instruction["rotate"]:
+            instr += string.Template("    rotate $rotate\n").substitute(self.instruction)
+        if float(self.instruction["xoffset"]) or float(self.instruction["yoffset"]):
+            instr += (string.Template("    xoffset $xoffset\n    yoffset $yoffset\n").
+                                                            substitute(self.instruction))
+        instr += "    end"
         try:
-            instr += " %s\n" % text.encode('latin_1')
+            instr = instr.encode('latin1')
         except UnicodeEncodeError, err:
             try:
+                print err
                 pos = str(err).split('position')[1].split(':')[0].strip()
             except IndexError:
                 pos = ''
@@ -969,24 +988,7 @@ class Text(InstructionObject):
                             "which is required by module ps.map.")
             GMessage(message = message)
             return ''
-        instr += (string.Template("    font $font\n    fontsize $fontsize\n    color $color\n").
-                                                                   substitute(self.instruction).
-                                                                   encode('latin_1'))
-        instr += string.Template("    hcolor $hcolor\n").substitute(self.instruction).encode('latin_1')
-        if self.instruction['hcolor'] != 'none':
-            instr += string.Template("    hwidth $hwidth\n").substitute(self.instruction).encode('latin_1')
-        instr += string.Template("    border $border\n").substitute(self.instruction).encode('latin_1')
-        if self.instruction['border'] != 'none':
-            instr += string.Template("    width $width\n").substitute(self.instruction).encode('latin_1')
-        instr += string.Template("    background $background\n").substitute(self.instruction).encode('latin_1')
-        if self.instruction["ref"] != '0':
-            instr += string.Template("    ref $ref\n").substitute(self.instruction).encode('latin_1')
-        if self.instruction["rotate"]:
-            instr += string.Template("    rotate $rotate\n").substitute(self.instruction).encode('latin_1')
-        if float(self.instruction["xoffset"]) or float(self.instruction["yoffset"]):
-            instr += (string.Template("    xoffset $xoffset\n    yoffset $yoffset\n").
-                                                            substitute(self.instruction).encode('latin_1'))
-        instr += "    end"
+        
         return instr
     
     def Read(self, instruction, text, **kwargs):
@@ -1005,7 +1007,7 @@ class Text(InstructionObject):
                         instr['XY'] = False
                         instr['east'], instr['north'] = float(e), float(n)
                         
-                    instr['text'] = line.split(None, 3)[3]
+                    instr['text'] = line.split(None, 3)[3].decode('latin_1')
                 
                 elif sub == 'font':
                     instr['font'] = line.split(None, 1)[1]
@@ -1681,6 +1683,24 @@ class VProperties(InstructionObject):
         vInstruction += string.Template("    label $label\n    lpos $lpos\n").substitute(dic)
         
         vInstruction += "    end"
+        try:
+            vInstruction = vInstruction.encode('Latin_1')
+        except UnicodeEncodeError, err:
+            try:
+                print err
+                pos = str(err).split('position')[1].split(':')[0].strip()
+            except IndexError:
+                pos = ''
+            if pos:
+                message = _("Characters on position %s are not supported "
+                            "by ISO-8859-1 (Latin 1) encoding "
+                            "which is required by module ps.map.") % pos
+            else:
+                message = _("Not all characters are supported "
+                            "by ISO-8859-1 (Latin 1) encoding "
+                            "which is required by module ps.map.")
+            GMessage(message = message)
+            return ''
         return vInstruction
     
     def Read(self, instruction, text, **kwargs):
@@ -1773,7 +1793,7 @@ class VProperties(InstructionObject):
             if line.startswith('lpos'):
                 instr['lpos'] = int(line.split()[1])
             elif line.startswith('label'):
-                instr['label'] = line.split(None, 1)[1]
+                instr['label'] = line.split(None, 1)[1].decode('latin_1')
             elif line.startswith('layer'):
                 instr['layer'] = line.split()[1]
             elif line.startswith('masked'):