From f41ed38fce21450ce68b707ab7c398cf0f021936 Mon Sep 17 00:00:00 2001
From: Timm Fitschen <t.fitschen@indiscale.com>
Date: Wed, 6 Jul 2022 22:32:48 +0200
Subject: [PATCH] MAINT: remove python2.7 code

---
 src/caosdb/common/models.py       |  7 +----
 src/caosdb/common/utils.py        |  1 -
 src/caosdb/connection/__init__.py | 29 -------------------
 src/caosdb/connection/encode.py   | 41 +++------------------------
 src/caosdb/connection/mockup.py   |  6 +---
 src/caosdb/connection/utils.py    | 47 +++----------------------------
 6 files changed, 10 insertions(+), 121 deletions(-)

diff --git a/src/caosdb/common/models.py b/src/caosdb/common/models.py
index 3421f9ce..f974060f 100644
--- a/src/caosdb/common/models.py
+++ b/src/caosdb/common/models.py
@@ -44,7 +44,6 @@ from hashlib import sha512
 from os import listdir
 from os.path import isdir
 from random import randint
-from sys import hexversion
 from tempfile import NamedTemporaryFile
 from warnings import warn
 
@@ -1402,11 +1401,7 @@ def _log_request(request, xml_body=None):
 def _log_response(body):
     if Container._debug() > 0:
         print("\n======== Response body ========\n")
-
-        if hexversion < 0x03000000:
-            print(body)
-        else:
-            print(body.decode())
+        print(body.decode())
         print("\n===============================\n")
 
 
diff --git a/src/caosdb/common/utils.py b/src/caosdb/common/utils.py
index eabce313..f0ce740d 100644
--- a/src/caosdb/common/utils.py
+++ b/src/caosdb/common/utils.py
@@ -26,7 +26,6 @@
 from lxml import etree
 from multiprocessing import Lock
 from uuid import uuid4
-from sys import hexversion
 _uuid_lock = Lock()
 
 
diff --git a/src/caosdb/connection/__init__.py b/src/caosdb/connection/__init__.py
index 638d4f0a..e69de29b 100644
--- a/src/caosdb/connection/__init__.py
+++ b/src/caosdb/connection/__init__.py
@@ -1,29 +0,0 @@
-# -*- encoding: utf-8 -*-
-#
-# ** header v3.0
-# This file is a part of the CaosDB Project.
-#
-# Copyright (C) 2018 Research Group Biomedical Physics,
-# Max-Planck-Institute for Dynamics and Self-Organization Göttingen
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU Affero General Public License as
-# published by the Free Software Foundation, either version 3 of the
-# License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU Affero General Public License for more details.
-#
-# You should have received a copy of the GNU Affero General Public License
-# along with this program. If not, see <https://www.gnu.org/licenses/>.
-#
-# ** end header
-#
-from sys import hexversion
-from .utils import check_python_ssl_version
-
-check_python_ssl_version(hexversion)
-del check_python_ssl_version
-del hexversion
diff --git a/src/caosdb/connection/encode.py b/src/caosdb/connection/encode.py
index 970c4191..7b092aae 100644
--- a/src/caosdb/connection/encode.py
+++ b/src/caosdb/connection/encode.py
@@ -53,19 +53,9 @@ __all__ = [
     'gen_boundary', 'encode_and_quote', 'MultipartParam', 'encode_string',
     'encode_file_header', 'get_body_size', 'get_headers', 'multipart_encode'
 ]
-from sys import hexversion
-try:
-    from urllib.parse import quote_plus
-except ImportError:
-    from urllib import quote_plus
-
-try:
-    from io import UnsupportedOperation
-except ImportError:
-    UnsupportedOperation = None
-
+from urllib.parse import quote_plus
+from io import UnsupportedOperation
 import uuid
-
 import re
 import os
 import mimetypes
@@ -83,24 +73,9 @@ def encode_and_quote(data):
     if data is None:
         return None
 
-    if hexversion < 0x03000000:
-        if isinstance(data, unicode):
-            data = data.encode("utf-8")
     return quote_plus(data)
 
 
-def _strify(string):
-    """If string is a unicode string, encode it to UTF-8 and return the
-    results, otherwise return str(s), or None if s is None."""
-    if string is None:
-        return None
-    if hexversion < 0x03000000:
-        if isinstance(string, unicode):
-            return string.encode("utf-8")
-        return str(string)
-    return str(string)
-
-
 class MultipartParam(object):
     """Represents a single parameter in a multipart/form-data request.
 
@@ -143,22 +118,14 @@ class MultipartParam(object):
                  fileobj=None,
                  callback=None):
         self.name = Header(name).encode()
-        self.value = _strify(value)
+        self.value = value
         if filename is None:
             self.filename = None
-        elif hexversion < 0x03000000:
-            if isinstance(filename, unicode):
-                # Encode with XML entities
-                self.filename = filename.encode("ascii", "xmlcharrefreplace")
-            else:
-                self.filename = str(filename)
-            self.filename = self.filename.encode("string_escape").\
-                replace('"', '\\"')
         else:
             bfilename = filename.encode("ascii", "xmlcharrefreplace")
             self.filename = bfilename.decode("UTF-8").replace('"', '\\"')
 
-        self.filetype = _strify(filetype)
+        self.filetype = filetype
 
         self.filesize = filesize
         self.fileobj = fileobj
diff --git a/src/caosdb/connection/mockup.py b/src/caosdb/connection/mockup.py
index aaa9e09b..b37670b8 100644
--- a/src/caosdb/connection/mockup.py
+++ b/src/caosdb/connection/mockup.py
@@ -26,7 +26,6 @@ up a tcp connection."""
 
 from __future__ import unicode_literals, print_function
 from io import StringIO
-from sys import hexversion
 from .connection import CaosDBServerConnection, CaosDBHTTPResponse
 
 
@@ -57,10 +56,7 @@ class MockUpResponse(CaosDBHTTPResponse):
 
     def read(self, size=-1):
         """Return the body of the response."""
-        if hexversion < 0x03000000:
-            return self.response.read(size)
-        else:
-            return self.response.read(size).encode()
+        return self.response.read(size).encode()
 
     def getheader(self, name, default=None):
         """Get the contents of the header `name`, or `default` if there is no
diff --git a/src/caosdb/connection/utils.py b/src/caosdb/connection/utils.py
index 8c1518c1..9056bf9d 100644
--- a/src/caosdb/connection/utils.py
+++ b/src/caosdb/connection/utils.py
@@ -23,19 +23,9 @@
 #
 """Utility functions for the connection module."""
 from __future__ import unicode_literals, print_function
-try:
-    from builtins import str, unicode  # pylint: disable=redefined-builtin
-except ImportError:
-    from builtins import str as unicode
-try:  # pragma: no cover
-    # python3
-    from urllib.parse import (urlencode as _urlencode, quote as _quote,
-                              urlparse, urlunparse, unquote as _unquote)
-except ImportError:  # pragma: no cover
-    # python2
-    from urllib import (urlencode as _urlencode, quote as _quote, unquote as
-                        _unquote)
-    from urlparse import urlparse, urlunparse
+from builtins import str as unicode
+from urllib.parse import (urlencode as _urlencode, quote as _quote,
+                          urlparse, urlunparse, unquote as _unquote)
 import re
 
 
@@ -122,7 +112,7 @@ def make_uri_path(segments=None, query=None):
 
 
 def quote(string):
-    enc = unicode(string).encode('utf-8')
+    enc = string.encode('utf-8')
     return _quote(enc).replace('/', '%2F')
 
 
@@ -136,35 +126,6 @@ def parse_url(url):
     return fullurl
 
 
-def check_python_ssl_version(hexversion):
-    """Check the python version.
-
-    If `version < 2.7.9` or `3.0 <= version < 3.2` the ssl library does not
-    actually verify the ssl certificates send by the server. That is evil and
-    these versions shall not be used.
-
-    Parameters
-    ----------
-    hexversion : int
-        A python version.
-
-    Raises
-    ------
-    Exception
-        If the version does not fully support ssl encryption.
-    """
-    if hexversion < 0x02070900:
-        raise Exception(
-            "version " + str(hex(hexversion)) +
-            "\nPython version is smaller than 2.7.9. It is not does not fully support SSL encryption. Please update your Python to 2.7.9 or greater, or 3.2 or greater."
-        )
-    elif hexversion >= 0x03000000 and hexversion < 0x03020000:
-        raise Exception(
-            "version " + str(hex(hexversion)) +
-            "\nPython 3 version is smaller than 3.2. It is not does not fully support SSL encryption. Please update your Python to 2.7.9 or greater, or 3.2 or greater."
-        )
-
-
 _PATTERN = re.compile(r"^SessionToken=([^;]*);.*$")
 
 
-- 
GitLab