瀏覽代碼

g.gui.psmap: fixes for Python3

Anna Petrasova 5 年之前
父節點
當前提交
8ad5e8e4ae

+ 1 - 8
gui/wxpython/gui_core/gselect.py

@@ -52,13 +52,6 @@ import six
 import wx
 import wx
 
 
 from core import globalvar
 from core import globalvar
-if globalvar.wxPythonPhoenix:
-    ComboPopup = wx.ComboPopup
-    ComboCtrl = wx.ComboCtrl
-else:
-    import wx.combo
-    ComboPopup = wx.combo.ComboPopup
-    ComboCtrl = wx.combo.ComboCtrl
 import wx.lib.buttons as buttons
 import wx.lib.buttons as buttons
 import wx.lib.filebrowsebutton as filebrowse
 import wx.lib.filebrowsebutton as filebrowse
 
 
@@ -79,7 +72,7 @@ from core.settings import UserSettings
 from core.debug import Debug
 from core.debug import Debug
 from gui_core.vselect import VectorSelectBase
 from gui_core.vselect import VectorSelectBase
 from gui_core.wrap import TreeCtrl, Button, StaticText, StaticBox, \
 from gui_core.wrap import TreeCtrl, Button, StaticText, StaticBox, \
-    TextCtrl, Panel
+    TextCtrl, Panel, ComboPopup, ComboCtrl
 
 
 from grass.pydispatch.signal import Signal
 from grass.pydispatch.signal import Signal
 
 

+ 18 - 0
gui/wxpython/gui_core/wrap.py

@@ -27,6 +27,14 @@ from core.globalvar import gtk3, wxPythonPhoenix
 if wxPythonPhoenix:
 if wxPythonPhoenix:
     import wx.adv
     import wx.adv
 
 
+if wxPythonPhoenix:
+    ComboPopup = wx.ComboPopup
+    wxComboCtrl = wx.ComboCtrl
+else:
+    import wx.combo
+    ComboPopup = wx.combo.ComboPopup
+    wxComboCtrl = wx.combo.ComboCtrl
+
 
 
 def BitmapFromImage(image, depth=-1):
 def BitmapFromImage(image, depth=-1):
     if wxPythonPhoenix:
     if wxPythonPhoenix:
@@ -453,3 +461,13 @@ class ColourSelect(csel.ColourSelect):
             csel.ColourSelect.SetToolTip(self, tipString=tip)
             csel.ColourSelect.SetToolTip(self, tipString=tip)
         else:
         else:
             csel.ColourSelect.SetToolTipString(self, tip)
             csel.ColourSelect.SetToolTipString(self, tip)
+
+class ComboCtrl(wxComboCtrl):
+    def __init__(self, *args, **kwargs):
+        wxComboCtrl.__init__(self, *args, **kwargs)
+
+    def SetToolTip(self, tip):
+        if wxPythonPhoenix:
+            wxComboCtrl.SetToolTip(self, tipString=tip)
+        else:
+            wxComboCtrl.SetToolTipString(self, tip)

+ 4 - 4
gui/wxpython/psmap/dialogs.py

@@ -1232,7 +1232,7 @@ class MapFramePanel(wx.Panel):
 
 
                 self.select.SetElementList(type=stype)
                 self.select.SetElementList(type=stype)
                 self.mapText.SetLabel(self.mapOrRegionText[0])
                 self.mapText.SetLabel(self.mapOrRegionText[0])
-                self.select.SetToolTipString(
+                self.select.SetToolTip(
                     _("Region is set to match this map,\nraster or vector map must be added later"))
                     _("Region is set to match this map,\nraster or vector map must be added later"))
 
 
             if scaleType == 1:
             if scaleType == 1:
@@ -1244,7 +1244,7 @@ class MapFramePanel(wx.Panel):
                 stype = 'region'
                 stype = 'region'
                 self.select.SetElementList(type=stype)
                 self.select.SetElementList(type=stype)
                 self.mapText.SetLabel(self.mapOrRegionText[1])
                 self.mapText.SetLabel(self.mapOrRegionText[1])
-                self.select.SetToolTipString("")
+                self.select.SetToolTip("")
 
 
             for each in self.mapSizer.GetChildren():
             for each in self.mapSizer.GetChildren():
                 each.GetWindow().Enable()
                 each.GetWindow().Enable()
@@ -2230,8 +2230,8 @@ class VPropertiesDialog(PsmapDialog):
                 "Database connection is not defined in DB file."))
                 "Database connection is not defined in DB file."))
         text = StaticText(panel, id=wx.ID_ANY, label=_("Select layer:"))
         text = StaticText(panel, id=wx.ID_ANY, label=_("Select layer:"))
         self.layerChoice = wx.Choice(
         self.layerChoice = wx.Choice(
-            panel, id=wx.ID_ANY, choices=map(
-                str, self.layers), size=self.spinCtrlSize)
+            panel, id=wx.ID_ANY, choices=[str(each) for each in self.layers],
+            size=self.spinCtrlSize)
 
 
         self.layerChoice.SetStringSelection(self.currLayer)
         self.layerChoice.SetStringSelection(self.currLayer)
 
 

+ 34 - 7
gui/wxpython/psmap/frame.py

@@ -243,7 +243,25 @@ class PsMapFrame(wx.Frame):
     def InstructionFile(self):
     def InstructionFile(self):
         """Creates mapping instructions"""
         """Creates mapping instructions"""
 
 
-        return str(self.instruction)
+        text = str(self.instruction)
+        try:
+            text = text.encode('Latin_1')
+        except UnicodeEncodeError as err:
+            try:
+                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 text
 
 
     def OnPSFile(self, event):
     def OnPSFile(self, event):
         """Generate PostScript"""
         """Generate PostScript"""
@@ -282,8 +300,11 @@ class PsMapFrame(wx.Frame):
     def PSFile(self, filename=None, pdf=False):
     def PSFile(self, filename=None, pdf=False):
         """Create temporary instructions file and run ps.map with output = filename"""
         """Create temporary instructions file and run ps.map with output = filename"""
         instrFile = grass.tempfile()
         instrFile = grass.tempfile()
-        instrFileFd = open(instrFile, mode='w')
-        instrFileFd.write(self.InstructionFile())
+        instrFileFd = open(instrFile, mode='wb')
+        content = self.InstructionFile()
+        if not content:
+            return
+        instrFileFd.write(content)
         instrFileFd.flush()
         instrFileFd.flush()
         instrFileFd.close()
         instrFileFd.close()
 
 
@@ -470,8 +491,11 @@ class PsMapFrame(wx.Frame):
         filename = self.getFile(
         filename = self.getFile(
             wildcard="*.psmap|*.psmap|Text file(*.txt)|*.txt|All files(*.*)|*.*")
             wildcard="*.psmap|*.psmap|Text file(*.txt)|*.txt|All files(*.*)|*.*")
         if filename:
         if filename:
-            instrFile = open(filename, "w")
-            instrFile.write(self.InstructionFile())
+            instrFile = open(filename, "wb")
+            content = self.InstructionFile()
+            if not content:
+                return
+            instrFile.write(content)
             instrFile.close()
             instrFile.close()
 
 
     def OnLoadFile(self, event):
     def OnLoadFile(self, event):
@@ -928,8 +952,11 @@ class PsMapFrame(wx.Frame):
     def getInitMap(self):
     def getInitMap(self):
         """Create default map frame when no map is selected, needed for coordinates in map units"""
         """Create default map frame when no map is selected, needed for coordinates in map units"""
         instrFile = grass.tempfile()
         instrFile = grass.tempfile()
-        instrFileFd = open(instrFile, mode='w')
-        instrFileFd.write(self.InstructionFile())
+        instrFileFd = open(instrFile, mode='wb')
+        content = self.InstructionFile()
+        if not content:
+            return
+        instrFileFd.write(content)
         instrFileFd.flush()
         instrFileFd.flush()
         instrFileFd.close()
         instrFileFd.close()
 
 

+ 4 - 38
gui/wxpython/psmap/instructions.py

@@ -37,6 +37,7 @@ import string
 import six
 import six
 from math import ceil
 from math import ceil
 from time import strftime, localtime
 from time import strftime, localtime
+from io import open
 
 
 import wx
 import wx
 import grass.script as grass
 import grass.script as grass
@@ -135,7 +136,7 @@ class Instruction:
         self.filename = filename
         self.filename = filename
         # open file
         # open file
         try:
         try:
-            file = open(filename, 'r')
+            file = open(filename, encoding='Latin_1', errors='ignore')
         except IOError:
         except IOError:
             GError(message=_("Unable to open file\n%s") % filename)
             GError(message=_("Unable to open file\n%s") % filename)
             return
             return
@@ -926,24 +927,6 @@ class Text(InstructionObject):
                 string.Template("    xoffset $xoffset\n    yoffset $yoffset\n"). substitute(
                 string.Template("    xoffset $xoffset\n    yoffset $yoffset\n"). substitute(
                     self.instruction))
                     self.instruction))
         instr += "    end"
         instr += "    end"
-        try:
-            instr = instr.encode('latin1')
-        except UnicodeEncodeError as err:
-            try:
-                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 instr
         return instr
 
 
     def Read(self, instruction, text, **kwargs):
     def Read(self, instruction, text, **kwargs):
@@ -963,7 +946,7 @@ class Text(InstructionObject):
                         instr['XY'] = False
                         instr['XY'] = False
                         instr['east'], instr['north'] = float(e), float(n)
                         instr['east'], instr['north'] = float(e), float(n)
 
 
-                    instr['text'] = line.split(None, 3)[3].decode('latin_1')
+                    instr['text'] = line.split(None, 3)[3]
 
 
                 elif sub == 'font':
                 elif sub == 'font':
                     instr['font'] = line.split(None, 1)[1]
                     instr['font'] = line.split(None, 1)[1]
@@ -1996,23 +1979,6 @@ class VProperties(InstructionObject):
             "    label $label\n    lpos $lpos\n").substitute(dic)
             "    label $label\n    lpos $lpos\n").substitute(dic)
 
 
         vInstruction += "    end"
         vInstruction += "    end"
-        try:
-            vInstruction = vInstruction.encode('Latin_1')
-        except UnicodeEncodeError as err:
-            try:
-                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
         return vInstruction
 
 
     def Read(self, instruction, text, **kwargs):
     def Read(self, instruction, text, **kwargs):
@@ -2106,7 +2072,7 @@ class VProperties(InstructionObject):
             if line.startswith('lpos'):
             if line.startswith('lpos'):
                 instr['lpos'] = int(line.split()[1])
                 instr['lpos'] = int(line.split()[1])
             elif line.startswith('label'):
             elif line.startswith('label'):
-                instr['label'] = line.split(None, 1)[1].decode('latin_1')
+                instr['label'] = line.split(None, 1)[1]
             elif line.startswith('layer'):
             elif line.startswith('layer'):
                 instr['layer'] = line.split()[1]
                 instr['layer'] = line.split()[1]
             elif line.startswith('masked'):
             elif line.startswith('masked'):