Ver código fonte

g.gui.psmap: fixes for Python3

Anna Petrasova 5 anos atrás
pai
commit
8ad5e8e4ae

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

@@ -52,13 +52,6 @@ import six
 import wx
 
 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.filebrowsebutton as filebrowse
 
@@ -79,7 +72,7 @@ from core.settings import UserSettings
 from core.debug import Debug
 from gui_core.vselect import VectorSelectBase
 from gui_core.wrap import TreeCtrl, Button, StaticText, StaticBox, \
-    TextCtrl, Panel
+    TextCtrl, Panel, ComboPopup, ComboCtrl
 
 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:
     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):
     if wxPythonPhoenix:
@@ -453,3 +461,13 @@ class ColourSelect(csel.ColourSelect):
             csel.ColourSelect.SetToolTip(self, tipString=tip)
         else:
             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.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"))
 
             if scaleType == 1:
@@ -1244,7 +1244,7 @@ class MapFramePanel(wx.Panel):
                 stype = 'region'
                 self.select.SetElementList(type=stype)
                 self.mapText.SetLabel(self.mapOrRegionText[1])
-                self.select.SetToolTipString("")
+                self.select.SetToolTip("")
 
             for each in self.mapSizer.GetChildren():
                 each.GetWindow().Enable()
@@ -2230,8 +2230,8 @@ class VPropertiesDialog(PsmapDialog):
                 "Database connection is not defined in DB file."))
         text = StaticText(panel, id=wx.ID_ANY, label=_("Select layer:"))
         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)
 

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

@@ -243,7 +243,25 @@ class PsMapFrame(wx.Frame):
     def InstructionFile(self):
         """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):
         """Generate PostScript"""
@@ -282,8 +300,11 @@ class PsMapFrame(wx.Frame):
     def PSFile(self, filename=None, pdf=False):
         """Create temporary instructions file and run ps.map with output = filename"""
         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.close()
 
@@ -470,8 +491,11 @@ class PsMapFrame(wx.Frame):
         filename = self.getFile(
             wildcard="*.psmap|*.psmap|Text file(*.txt)|*.txt|All files(*.*)|*.*")
         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()
 
     def OnLoadFile(self, event):
@@ -928,8 +952,11 @@ class PsMapFrame(wx.Frame):
     def getInitMap(self):
         """Create default map frame when no map is selected, needed for coordinates in map units"""
         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.close()
 

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

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