From 4d927963f3965a38a2b1a796f6dae07ac524d2d4 Mon Sep 17 00:00:00 2001 From: Alexandre Detiste Date: Fri, 7 Jul 2023 23:11:38 +0200 Subject: [PATCH 1/3] janitor: Python2 removal --- gui/wxpython/core/gconsole.py | 12 ++----- gui/wxpython/core/globalvar.py | 2 -- gui/wxpython/core/ws.py | 5 ++- gui/wxpython/nviz/workspace.py | 23 ++++---------- lib/init/grass.py | 20 +++--------- python/grass/gunittest/multirunner.py | 19 ++---------- python/grass/imaging/images2swf.py | 5 --- .../interface/testsuite/test_modules.py | 11 ++----- .../raster/testsuite/test_raster_region.py | 10 +++--- python/grass/script/task.py | 5 +-- python/grass/script/utils.py | 31 ++++--------------- scripts/r.in.wms/wms_cap_parsers.py | 5 +-- scripts/r.in.wms/wms_drv.py | 14 ++------- 13 files changed, 35 insertions(+), 127 deletions(-) diff --git a/gui/wxpython/core/gconsole.py b/gui/wxpython/core/gconsole.py index 077042f9682..e25137c3556 100644 --- a/gui/wxpython/core/gconsole.py +++ b/gui/wxpython/core/gconsole.py @@ -21,18 +21,13 @@ @author Wolf Bergenheim (#962) """ -from __future__ import print_function - import os import sys import re import time import threading -if sys.version_info.major == 2: - import Queue -else: - import queue as Queue +import queue as Queue import codecs import locale @@ -570,10 +565,7 @@ def RunCmd( message=_("Module <%s> not found.") % command[0], ) pymodule = imp.load_source(command[0].replace(".", "_"), pyPath) - try: # PY3 - pymain = inspect.getfullargspec(pymodule.main) - except AttributeError: - pymain = inspect.getargspec(pymodule.main) + pymain = inspect.getfullargspec(pymodule.main) if pymain and "giface" in pymain.args: pymodule.main(self._giface) return diff --git a/gui/wxpython/core/globalvar.py b/gui/wxpython/core/globalvar.py index e4cc9c16c6d..4c73431f5fe 100644 --- a/gui/wxpython/core/globalvar.py +++ b/gui/wxpython/core/globalvar.py @@ -11,8 +11,6 @@ @author Martin Landa """ -from __future__ import print_function - import os import sys import locale diff --git a/gui/wxpython/core/ws.py b/gui/wxpython/core/ws.py index eecd951863c..32dec87a05d 100644 --- a/gui/wxpython/core/ws.py +++ b/gui/wxpython/core/ws.py @@ -19,7 +19,6 @@ import sys import copy import time -import six import wx @@ -230,7 +229,7 @@ def _getRegionDict(self, env): def _createRegionStr(self, region): """Create string for GRASS_REGION env variable from dict created by _getRegionDict.""" regionStr = "" - for k, v in six.iteritems(region): + for k, v in region.items(): item = k + ": " + str(v) if regionStr: regionStr += "; " @@ -364,7 +363,7 @@ def AddRasterBands(self, sourceFile, sTBands): if sXsize < 1 or sYsize < 1: return - for sBandNnum, tBandNum in six.iteritems(sTBands): + for sBandNnum, tBandNum in sTBands.items(): bandData = sDataset.GetRasterBand(sBandNnum).ReadRaster( sXoff, sYoff, sXsize, sYsize, tXsize, tYsize, gdal.GDT_Byte ) diff --git a/gui/wxpython/nviz/workspace.py b/gui/wxpython/nviz/workspace.py index 3ca1aa0472b..9b7c912f056 100644 --- a/gui/wxpython/nviz/workspace.py +++ b/gui/wxpython/nviz/workspace.py @@ -15,7 +15,6 @@ """ import copy -import six from core.settings import UserSettings @@ -32,7 +31,7 @@ def __init__(self): def SetConstantDefaultProp(self): """Set default constant data properties""" data = dict() - for key, value in six.iteritems(UserSettings.Get(group="nviz", key="constant")): + for key, value in UserSettings.Get(group="nviz", key="constant").items(): data[key] = value color = ( str(data["color"][0]) @@ -57,9 +56,7 @@ def SetSurfaceDefaultProp(self, data=None): # for attrb in ("shine",): data["attribute"][attrb] = {} - for key, value in six.iteritems( - UserSettings.Get(group="nviz", key="surface", subkey=attrb) - ): + for key, value in UserSettings.Get(group="nviz", key="surface", subkey=attrb).items(): data["attribute"][attrb][key] = value data["attribute"][attrb]["update"] = None @@ -67,9 +64,7 @@ def SetSurfaceDefaultProp(self, data=None): # draw # data["draw"]["all"] = False # apply only for current surface - for control, value in six.iteritems( - UserSettings.Get(group="nviz", key="surface", subkey="draw") - ): + for control, value in UserSettings.Get(group="nviz", key="surface", subkey="draw").items(): if control[:3] == "res": if "resolution" not in data["draw"]: data["draw"]["resolution"] = {} @@ -115,9 +110,7 @@ def SetVolumeDefaultProp(self): # # draw # - for control, value in six.iteritems( - UserSettings.Get(group="nviz", key="volume", subkey="draw") - ): + for control, value in UserSettings.Get(group="nviz", key="volume", subkey="draw").items(): if control == "shading": sel = UserSettings.Get( group="nviz", key="volume", subkey=["draw", "shading"] @@ -164,9 +157,7 @@ def SetVolumeDefaultProp(self): # for attrb in ("shine",): data["attribute"][attrb] = {} - for key, value in six.iteritems( - UserSettings.Get(group="nviz", key="volume", subkey=attrb) - ): + for key, value in UserSettings.Get(group="nviz", key="volume", subkey=attrb).items(): data["attribute"][attrb][key] = value return data @@ -180,9 +171,7 @@ def SetIsosurfaceDefaultProp(self): if attr == "inout": data[attr]["value"] = 0 continue - for key, value in six.iteritems( - UserSettings.Get(group="nviz", key="volume", subkey=attr) - ): + for key, value in UserSettings.Get(group="nviz", key="volume", subkey=attr).items(): data[attr][key] = value return data diff --git a/lib/init/grass.py b/lib/init/grass.py index ae5814d7ebf..4ab9a54bb26 100755 --- a/lib/init/grass.py +++ b/lib/init/grass.py @@ -37,7 +37,6 @@ # (this makes it more stable since we have to set up paths first) # pylint: disable=too-many-lines -from __future__ import print_function import sys import os import errno @@ -49,7 +48,6 @@ import string import subprocess import re -import six import platform import tempfile import locale @@ -118,9 +116,7 @@ def decode(bytes_, encoding=ENCODING): :param encoding: encoding to be used, default value is the system's default encoding or, if that cannot be determined, 'UTF-8'. """ - if sys.version_info.major >= 3: - unicode = str - if isinstance(bytes_, unicode): + if isinstance(bytes_, str): return bytes_ elif isinstance(bytes_, bytes): return bytes_.decode(encoding) @@ -140,12 +136,9 @@ def encode(string, encoding=ENCODING): :param encoding: encoding to be used, default value is the system's default encoding or, if that cannot be determined, 'UTF-8'. """ - if sys.version_info.major >= 3: - unicode = str if isinstance(string, bytes): return string - # this also tests str in Py3: - elif isinstance(string, unicode): + elif isinstance(string, str): return string.encode(encoding) else: # if something else than text @@ -155,12 +148,7 @@ def encode(string, encoding=ENCODING): # see https://trac.osgeo.org/grass/ticket/3508 def to_text_string(obj, encoding=ENCODING): """Convert `obj` to (unicode) text string""" - if six.PY2: - # Python 2 - return encode(obj, encoding=encoding) - else: - # Python 3 - return decode(obj, encoding=encoding) + return decode(obj, encoding=encoding) def try_remove(path): @@ -281,7 +269,7 @@ def count_wide_chars(s): """ return sum( unicodedata.east_asian_width(c) in "WF" - for c in (s if sys.version_info.major >= 3 else unicode(s)) + for c in s ) diff --git a/python/grass/gunittest/multirunner.py b/python/grass/gunittest/multirunner.py index 3bfbb6ad67f..fd648406332 100644 --- a/python/grass/gunittest/multirunner.py +++ b/python/grass/gunittest/multirunner.py @@ -9,22 +9,12 @@ :authors: Vaclav Petras """ -from __future__ import print_function - import sys import os import argparse import subprocess import locale -try: - from itertools import izip as zip -except ImportError: # will be 3.x series - pass - -if sys.version_info.major >= 3: - unicode = str - def _get_encoding(): try: @@ -45,7 +35,7 @@ def decode(bytes_, encoding=None): def encode(string, encoding=None): - if isinstance(string, unicode): + if isinstance(string, str): return string.encode(_get_encoding()) else: return string @@ -55,12 +45,7 @@ def text_to_string(text): """Convert text to str. Useful when passing text into environments, in Python 2 it needs to be bytes on Windows, in Python 3 in needs unicode. """ - if sys.version[0] == "2": - # Python 2 - return encode(text) - else: - # Python 3 - return decode(text) + return decode(text) def main(): diff --git a/python/grass/imaging/images2swf.py b/python/grass/imaging/images2swf.py index 2e97f6af7c1..c01e6fdf99c 100644 --- a/python/grass/imaging/images2swf.py +++ b/python/grass/imaging/images2swf.py @@ -67,7 +67,6 @@ """ import os -import sys import zlib try: @@ -81,10 +80,6 @@ PIL = None -# True if we are running on Python 3. -# Code taken from six.py by Benjamin Peterson (MIT licensed) -PY3 = sys.version_info[0] == 3 - string_types = (str,) integer_types = (int,) class_types = (type,) diff --git a/python/grass/pygrass/modules/interface/testsuite/test_modules.py b/python/grass/pygrass/modules/interface/testsuite/test_modules.py index 41dc27a88b8..10b4c3f2529 100644 --- a/python/grass/pygrass/modules/interface/testsuite/test_modules.py +++ b/python/grass/pygrass/modules/interface/testsuite/test_modules.py @@ -3,8 +3,9 @@ @author: pietro """ -import sys from fnmatch import fnmatch +from io import BytesIO + from grass.gunittest.case import TestCase from grass.gunittest.main import test @@ -12,12 +13,6 @@ from grass.exceptions import ParameterError from grass.pygrass.modules.interface import Module -PY2 = sys.version_info[0] == 2 -if PY2: - from StringIO import StringIO -else: - from io import BytesIO as StringIO - SKIP = [ "g.parser", @@ -66,7 +61,7 @@ def test_rsun(self): """Test if a Module instance is pickable""" import pickle - out = StringIO() + out = BytesIO() pickle.dump(Module("r.sun"), out) out.close() diff --git a/python/grass/pygrass/raster/testsuite/test_raster_region.py b/python/grass/pygrass/raster/testsuite/test_raster_region.py index cf7628dd32c..6e7d86ba436 100644 --- a/python/grass/pygrass/raster/testsuite/test_raster_region.py +++ b/python/grass/pygrass/raster/testsuite/test_raster_region.py @@ -1,4 +1,4 @@ -import six +from unittest import assertCountEqual from grass.gunittest.case import TestCase from grass.gunittest.main import test @@ -42,10 +42,10 @@ def test_resampling_1(self): rast.set_region(region) rast.open(mode="r") - six.assertCountEqual( + assertCountEqual( self, rast[0].tolist(), [22, 22, 22, 22, 22, 32, 32, 32, 32, 32] ) - six.assertCountEqual( + assertCountEqual( self, rast[5].tolist(), [23, 23, 23, 23, 23, 33, 33, 33, 33, 33] ) @@ -77,8 +77,8 @@ def test_resampling_2(self): [nan, nan, nan, nan, nan, nan, nan, nan] """ - six.assertCountEqual(self, rast[2].tolist()[2:6], [11.0, 21.0, 31.0, 41.0]) - six.assertCountEqual(self, rast[5].tolist()[2:6], [14.0, 24.0, 34.0, 44.0]) + assertCountEqual(self, rast[2].tolist()[2:6], [11.0, 21.0, 31.0, 41.0]) + assertCountEqual(self, rast[5].tolist()[2:6], [14.0, 24.0, 34.0, 44.0]) rast.close() diff --git a/python/grass/script/task.py b/python/grass/script/task.py index 370454de223..7b73b9dfcd4 100644 --- a/python/grass/script/task.py +++ b/python/grass/script/task.py @@ -25,10 +25,7 @@ from .utils import decode, split from .core import Popen, PIPE, get_real_command -try: - import xml.etree.ElementTree as etree -except ImportError: - import elementtree.ElementTree as etree # Python <= 2.4 +import xml.etree.ElementTree as etree from xml.parsers import expat # TODO: works for any Python? # Get the XML parsing exceptions to catch. The behavior chnaged with Python 2.7 diff --git a/python/grass/script/utils.py b/python/grass/script/utils.py index 4182a6eb53b..45948d5a1bf 100644 --- a/python/grass/script/utils.py +++ b/python/grass/script/utils.py @@ -30,9 +30,6 @@ import string -if sys.version_info.major >= 3: - unicode = str - def float_or_dms(s): """Convert DMS to float. @@ -191,7 +188,7 @@ def decode(bytes_, encoding=None): >>> decode(1234) u'1234' """ - if isinstance(bytes_, unicode): + if isinstance(bytes_, str): return bytes_ if isinstance(bytes_, bytes): if encoding is None: @@ -199,13 +196,8 @@ def decode(bytes_, encoding=None): else: enc = encoding return bytes_.decode(enc) - # if something else than text - if sys.version_info.major >= 3: - # only text should be used - raise TypeError("can only accept types str and bytes") - else: - # for backwards compatibility - return unicode(bytes_) + # only text should be used + raise TypeError("can only accept types str and bytes") def encode(string, encoding=None): @@ -229,32 +221,21 @@ def encode(string, encoding=None): """ if isinstance(string, bytes): return string - # this also tests str in Py3: - if isinstance(string, unicode): + if isinstance(string, str): if encoding is None: enc = _get_encoding() else: enc = encoding return string.encode(enc) # if something else than text - if sys.version_info.major >= 3: - # only text should be used - raise TypeError("can only accept types str and bytes") - else: - # for backwards compatibility - return bytes(string) + raise TypeError("can only accept types str and bytes") def text_to_string(text, encoding=None): """Convert text to str. Useful when passing text into environments, in Python 2 it needs to be bytes on Windows, in Python 3 in needs unicode. """ - if sys.version[0] == "2": - # Python 2 - return encode(text, encoding=encoding) - else: - # Python 3 - return decode(text, encoding=encoding) + return decode(text, encoding=encoding) def parse_key_val(s, sep="=", dflt=None, val_type=None, vsep=None): diff --git a/scripts/r.in.wms/wms_cap_parsers.py b/scripts/r.in.wms/wms_cap_parsers.py index 504481f978f..c41ea7fff22 100644 --- a/scripts/r.in.wms/wms_cap_parsers.py +++ b/scripts/r.in.wms/wms_cap_parsers.py @@ -18,10 +18,7 @@ """ import pathlib -try: - from xml.etree.ElementTree import ParseError -except ImportError: # < Python 2.7 - from xml.parsers.expat import ExpatError as ParseError +from xml.etree.ElementTree import ParseError import xml.etree.ElementTree as etree import grass.script as grass diff --git a/scripts/r.in.wms/wms_drv.py b/scripts/r.in.wms/wms_drv.py index 1380ce03a5b..fc65853a80c 100644 --- a/scripts/r.in.wms/wms_drv.py +++ b/scripts/r.in.wms/wms_drv.py @@ -36,18 +36,10 @@ from math import pi, floor -try: - from urllib2 import HTTPError - from httplib import HTTPException -except ImportError: - # python3 - from urllib.error import HTTPError - from http.client import HTTPException +from urllib.error import HTTPError +from http.client import HTTPException -try: - from xml.etree.ElementTree import ParseError -except ImportError: # < Python 2.7 - from xml.parsers.expat import ExpatError as ParseError +from xml.etree.ElementTree import ParseError from wms_base import GetEpsg, GetSRSParamVal, WMSBase From 92a1294794353baf1cca0e931890e385877e183d Mon Sep 17 00:00:00 2001 From: Alexandre Detiste Date: Sat, 8 Jul 2023 13:18:18 +0200 Subject: [PATCH 2/3] first use of black --- gui/wxpython/nviz/workspace.py | 20 +++++++++++++++----- lib/init/grass.py | 5 +---- python/grass/script/utils.py | 1 - 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/gui/wxpython/nviz/workspace.py b/gui/wxpython/nviz/workspace.py index 9b7c912f056..853c64b49a4 100644 --- a/gui/wxpython/nviz/workspace.py +++ b/gui/wxpython/nviz/workspace.py @@ -56,7 +56,9 @@ def SetSurfaceDefaultProp(self, data=None): # for attrb in ("shine",): data["attribute"][attrb] = {} - for key, value in UserSettings.Get(group="nviz", key="surface", subkey=attrb).items(): + for key, value in UserSettings.Get( + group="nviz", key="surface", subkey=attrb + ).items(): data["attribute"][attrb][key] = value data["attribute"][attrb]["update"] = None @@ -64,7 +66,9 @@ def SetSurfaceDefaultProp(self, data=None): # draw # data["draw"]["all"] = False # apply only for current surface - for control, value in UserSettings.Get(group="nviz", key="surface", subkey="draw").items(): + for control, value in UserSettings.Get( + group="nviz", key="surface", subkey="draw" + ).items(): if control[:3] == "res": if "resolution" not in data["draw"]: data["draw"]["resolution"] = {} @@ -110,7 +114,9 @@ def SetVolumeDefaultProp(self): # # draw # - for control, value in UserSettings.Get(group="nviz", key="volume", subkey="draw").items(): + for control, value in UserSettings.Get( + group="nviz", key="volume", subkey="draw" + ).items(): if control == "shading": sel = UserSettings.Get( group="nviz", key="volume", subkey=["draw", "shading"] @@ -157,7 +163,9 @@ def SetVolumeDefaultProp(self): # for attrb in ("shine",): data["attribute"][attrb] = {} - for key, value in UserSettings.Get(group="nviz", key="volume", subkey=attrb).items(): + for key, value in UserSettings.Get( + group="nviz", key="volume", subkey=attrb + ).items(): data["attribute"][attrb][key] = value return data @@ -171,7 +179,9 @@ def SetIsosurfaceDefaultProp(self): if attr == "inout": data[attr]["value"] = 0 continue - for key, value in UserSettings.Get(group="nviz", key="volume", subkey=attr).items(): + for key, value in UserSettings.Get( + group="nviz", key="volume", subkey=attr + ).items(): data[attr][key] = value return data diff --git a/lib/init/grass.py b/lib/init/grass.py index 4ab9a54bb26..69cec20b3b3 100755 --- a/lib/init/grass.py +++ b/lib/init/grass.py @@ -267,10 +267,7 @@ def count_wide_chars(s): :param str s: string """ - return sum( - unicodedata.east_asian_width(c) in "WF" - for c in s - ) + return sum(unicodedata.east_asian_width(c) in "WF" for c in s) def f(fmt, *args): diff --git a/python/grass/script/utils.py b/python/grass/script/utils.py index 45948d5a1bf..df7ee11f525 100644 --- a/python/grass/script/utils.py +++ b/python/grass/script/utils.py @@ -30,7 +30,6 @@ import string - def float_or_dms(s): """Convert DMS to float. From f975f3a9d026834992519d0ce9e83b844a951669 Mon Sep 17 00:00:00 2001 From: Alexandre Detiste Date: Sat, 8 Jul 2023 14:53:13 +0200 Subject: [PATCH 3/3] fix failing test --- .../pygrass/raster/testsuite/test_raster_region.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/python/grass/pygrass/raster/testsuite/test_raster_region.py b/python/grass/pygrass/raster/testsuite/test_raster_region.py index 6e7d86ba436..cb8cc8c30c8 100644 --- a/python/grass/pygrass/raster/testsuite/test_raster_region.py +++ b/python/grass/pygrass/raster/testsuite/test_raster_region.py @@ -1,4 +1,3 @@ -from unittest import assertCountEqual from grass.gunittest.case import TestCase from grass.gunittest.main import test @@ -42,11 +41,11 @@ def test_resampling_1(self): rast.set_region(region) rast.open(mode="r") - assertCountEqual( - self, rast[0].tolist(), [22, 22, 22, 22, 22, 32, 32, 32, 32, 32] + self.assertCountEqual( + rast[0].tolist(), [22, 22, 22, 22, 22, 32, 32, 32, 32, 32] ) - assertCountEqual( - self, rast[5].tolist(), [23, 23, 23, 23, 23, 33, 33, 33, 33, 33] + self.assertCountEqual( + rast[5].tolist(), [23, 23, 23, 23, 23, 33, 33, 33, 33, 33] ) rast.close() @@ -77,8 +76,8 @@ def test_resampling_2(self): [nan, nan, nan, nan, nan, nan, nan, nan] """ - assertCountEqual(self, rast[2].tolist()[2:6], [11.0, 21.0, 31.0, 41.0]) - assertCountEqual(self, rast[5].tolist()[2:6], [14.0, 24.0, 34.0, 44.0]) + self.assertCountEqual(rast[2].tolist()[2:6], [11.0, 21.0, 31.0, 41.0]) + self.assertCountEqual(rast[5].tolist()[2:6], [14.0, 24.0, 34.0, 44.0]) rast.close()