Skip to content

Commit

Permalink
Drop python2 support
Browse files Browse the repository at this point in the history
Python 2 went EOL on 1/1/2020 and we receive no more support from the
Python Core Team after Python 2.7.18 even for known security issues.

This removes the six package from dependencies.
  • Loading branch information
danigm authored and tobixen committed Feb 18, 2023
1 parent db004b8 commit 6b44014
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 96 deletions.
6 changes: 1 addition & 5 deletions caldav/davclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import re

import requests
import six
from caldav.elements import cdav
from caldav.elements import dav
from caldav.elements import ical
Expand All @@ -22,10 +21,7 @@
from caldav.requests import HTTPBearerAuth
from lxml import etree

if six.PY3:
from urllib.parse import unquote
else:
from urlparse import unquote
from urllib.parse import unquote


class DAVResponse:
Expand Down
5 changes: 1 addition & 4 deletions caldav/elements/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from caldav.lib.namespace import nsmap
from caldav.lib.python_utilities import to_unicode
from lxml import etree
from six import PY3


class BaseElement(object):
Expand All @@ -30,9 +29,7 @@ def __str__(self):
utf8 = etree.tostring(
self.xmlelement(), encoding="utf-8", xml_declaration=True, pretty_print=True
)
if PY3:
return str(utf8, "utf-8")
return utf8
return str(utf8, "utf-8")

def xmlelement(self):
root = etree.Element(self.tag, nsmap=nsmap)
Expand Down
34 changes: 5 additions & 29 deletions caldav/lib/python_utilities.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
from six import PY3
from six import string_types

assert PY3 ## Still using python2? Write an issue on the github issue tracker, and I will consider to offer some limited python2-support for the 1.x-series of caldav - otherwise I will start cleaning away code for supporting python2 in the 1.1-release


def isPython3():
"""Deprecated. Use six.PY3"""
return PY3


def to_wire(text):
if text is None:
return None
if isinstance(text, string_types) and PY3:
if isinstance(text, str):
text = bytes(text, "utf-8")
elif not PY3:
text = to_unicode(text).encode("utf-8")
text = text.replace(b"\n", b"\r\n")
text = text.replace(b"\r\r\n", b"\r\n")
return text
Expand All @@ -24,7 +11,7 @@ def to_wire(text):
def to_local(text):
if text is None:
return None
if not isinstance(text, string_types):
if not isinstance(text, str):
text = text.decode("utf-8")
text = text.replace("\r\n", "\n")
return text
Expand All @@ -35,28 +22,17 @@ def to_local(text):

def to_normal_str(text):
"""
A str object is a unicode on python3 and a byte string on python2.
Make sure we return a normal string, no matter what version of
python ...
Make sure we return a normal string
"""
if text is None:
return text
if PY3 and not isinstance(text, str):
if not isinstance(text, str):
text = text.decode("utf-8")
elif not PY3 and not isinstance(text, str):
text = text.encode("utf-8")
text = text.replace("\r\n", "\n")
return text


def to_unicode(text):
if (
text
and isinstance(text, string_types)
and not PY3
and not isinstance(text, unicode)
):
return unicode(text, "utf-8")
if PY3 and text and isinstance(text, bytes):
if text and isinstance(text, bytes):
return text.decode("utf-8")
return text
36 changes: 11 additions & 25 deletions caldav/lib/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,15 @@
# -*- encoding: utf-8 -*-
from caldav.lib.python_utilities import to_normal_str
from caldav.lib.python_utilities import to_unicode
from six import PY3

if PY3:
from urllib.parse import (
ParseResult,
SplitResult,
urlparse,
unquote,
quote,
urlunparse,
)
else:
from urlparse import ParseResult, SplitResult
from urlparse import urlparse, urlunparse
from urllib import unquote, quote


def uc2utf8(input):
# argh! this feels wrong, but seems to be needed.
if not PY3 and type(input) == unicode:
return input.encode("utf-8")
else:
return input

from urllib.parse import (
ParseResult,
SplitResult,
urlparse,
unquote,
quote,
urlunparse,
)


class URL:
Expand Down Expand Up @@ -195,12 +181,12 @@ def join(self, path):
raise ValueError("%s can't be joined with %s" % (self, path))

if path.path[0] == "/":
ret_path = uc2utf8(path.path)
ret_path = path.path
else:
sep = "/"
if self.path.endswith("/"):
sep = ""
ret_path = "%s%s%s" % (self.path, sep, uc2utf8(path.path))
ret_path = "%s%s%s" % (self.path, sep, path.path)
return URL(
ParseResult(
self.scheme or path.scheme,
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
"vobject",
"lxml",
"requests",
"six",
"icalendar",
"recurring-ical-events>=2.0.0",
]
Expand Down
17 changes: 5 additions & 12 deletions tests/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,11 @@

from caldav.lib.python_utilities import to_local
from caldav.lib.python_utilities import to_wire
from six import PY3

if PY3:
from urllib import parse
from urllib.parse import urlparse, urlunparse
from http.server import BaseHTTPRequestHandler, HTTPServer
from socketserver import ThreadingMixIn
else:
from urlparse import urlparse as parse
from urlparse import urlparse, urlunparse
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from SocketServer import ThreadingMixIn

from urllib import parse
from urllib.parse import urlparse, urlunparse
from http.server import BaseHTTPRequestHandler, HTTPServer
from socketserver import ThreadingMixIn

__version__ = "0.3.1"

Expand Down
6 changes: 1 addition & 5 deletions tests/test_caldav.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
from caldav.objects import Principal
from caldav.objects import Todo
from requests.packages import urllib3
from six import PY3

from . import compatibility_issues
from .conf import caldav_servers
Expand Down Expand Up @@ -70,10 +69,7 @@
import radicale.server
import socket

if PY3:
from urllib.parse import urlparse
else:
from urlparse import urlparse
from urllib.parse import urlparse

log = logging.getLogger("caldav")

Expand Down
22 changes: 7 additions & 15 deletions tests/test_caldav_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,10 @@
from caldav.objects import Journal
from caldav.objects import Principal
from caldav.objects import Todo
from six import PY3


if PY3:
from urllib.parse import urlparse
from unittest import mock
else:
from urlparse import urlparse
import mock
from urllib.parse import urlparse
from unittest import mock

## Some example icalendar data partly copied from test_caldav.py
ev1 = """BEGIN:VCALENDAR
Expand Down Expand Up @@ -263,14 +258,11 @@ def testRequestNonAscii(self, mocked):
assert response.status == 200
assert response.tree is None

if PY3:
response = client.put(
"/foo/møøh/bar".encode("utf-8"),
"bringebærsyltetøy 北京 пиво".encode("utf-8"),
{},
)
else:
response = client.put(u"/foo/møøh/bar", "bringebærsyltetøy 北京 пиво", {}) # fmt: skip
response = client.put(
"/foo/møøh/bar".encode("utf-8"),
"bringebærsyltetøy 北京 пиво".encode("utf-8"),
{},
)
assert response.status == 200
assert response.tree is None

Expand Down

0 comments on commit 6b44014

Please sign in to comment.