From a631c8c761b72b30ddf1f04ae8c3e834a18a77af Mon Sep 17 00:00:00 2001 From: Jean-Marc Martins Date: Thu, 19 Sep 2013 14:40:03 +0200 Subject: [PATCH] =?UTF-8?q?Fix=20compatibility=20between=20python2=C2=A0an?= =?UTF-8?q?d=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- radicale/__init__.py | 6 +++--- radicale/ical.py | 3 ++- radicale/storage/filesystem.py | 4 ++-- radicale/storage/multifilesystem.py | 6 +++++- tests/__init__.py | 10 ++++++---- 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/radicale/__init__.py b/radicale/__init__.py index 92a4ea33..41bab4f0 100644 --- a/radicale/__init__.py +++ b/radicale/__init__.py @@ -272,9 +272,9 @@ class Application(object): if authorization: authorization = \ - authorization.lstrip("Basic").strip().encode("ascii") - user, password = self.decode( - base64.b64decode(authorization), environ).split(":", 1) + authorization.decode("ascii").lstrip("Basic").strip() + user, password = self.decode(base64.b64decode( + authorization.encode("ascii")), environ).split(":", 1) else: user = password = None diff --git a/radicale/ical.py b/radicale/ical.py index 166bffa9..3b6bd681 100644 --- a/radicale/ical.py +++ b/radicale/ical.py @@ -103,7 +103,8 @@ class Item(object): self.text = self.text.replace( "\nEND:", "\nX-RADICALE-NAME:%s\nEND:" % self._name) else: - self._name = str(uuid4()) + # workaround to get unicode on both python2 and 3 + self._name = uuid4().hex.encode("ascii").decode("ascii") self.text = self.text.replace( "\nEND:", "\nX-RADICALE-NAME:%s\nEND:" % self._name) diff --git a/radicale/storage/filesystem.py b/radicale/storage/filesystem.py index 15e8e8b4..238f6eca 100644 --- a/radicale/storage/filesystem.py +++ b/radicale/storage/filesystem.py @@ -36,7 +36,7 @@ FILESYSTEM_ENCODING = sys.getfilesystemencoding() try: from dulwich.repo import Repo - GIT_REPOSITORY = Repo(FOLDER).encode(FILESYSTEM_ENCODING) + GIT_REPOSITORY = Repo(FOLDER) except: GIT_REPOSITORY = None @@ -53,7 +53,7 @@ def open(path, mode="r"): # On exit if GIT_REPOSITORY and mode == "w": path = os.path.relpath(abs_path, FOLDER) - GIT_REPOSITORY.stage([path.encode(FILESYSTEM_ENCODING)]) + GIT_REPOSITORY.stage([path]) committer = config.get("git", "committer") GIT_REPOSITORY.do_commit("Commit by Radicale", committer=committer) # pylint: enable=W0622 diff --git a/radicale/storage/multifilesystem.py b/radicale/storage/multifilesystem.py index 2a605afb..ca2d70c1 100644 --- a/radicale/storage/multifilesystem.py +++ b/radicale/storage/multifilesystem.py @@ -25,6 +25,7 @@ Multi files per calendar filesystem storage backend. import os import shutil import time +import sys from . import filesystem from .. import ical @@ -50,7 +51,10 @@ class Collection(filesystem.Collection): components = [i for i in items if isinstance(i, ical.Component)] for component in components: text = ical.serialize(self.tag, headers, [component] + timezones) - path = os.path.join(self._path, component.name) + name = ( + component.name if sys.version_info[0] >= 3 else + component.name.encode(filesystem.FILESYSTEM_ENCODING)) + path = os.path.join(self._path, name) with filesystem.open(path, "w") as fd: fd.write(text) diff --git a/tests/__init__.py b/tests/__init__.py index b5bd194c..5b067ba1 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -58,6 +58,8 @@ class BaseTest(object): args["REQUEST_METHOD"] = method.upper() args["PATH_INFO"] = path if data: + if sys.version_info[0] >= 3: + data = data.encode("utf-8") args["wsgi.input"] = BytesIO(data) args["CONTENT_LENGTH"] = str(len(data)) self.application._answer = self.application(args, self.start_response) @@ -128,11 +130,11 @@ class HtpasswdAuthSystem(BaseTest): def setup(self): self.colpath = tempfile.mkdtemp() htpasswd_file_path = os.path.join(self.colpath, ".htpasswd") - with open(htpasswd_file_path, "w") as fd: - fd.write('tmp:{SHA}' + base64.b64encode( - hashlib.sha1("bépo").digest())) + with open(htpasswd_file_path, "wb") as fd: + fd.write(b"tmp:{SHA}" + base64.b64encode( + hashlib.sha1(b"bepo").digest())) config.set("auth", "type", "htpasswd") - self.userpass = base64.b64encode("tmp:bépo") + self.userpass = base64.b64encode(b"tmp:bepo") self.application = radicale.Application() htpasswd.FILENAME = htpasswd_file_path htpasswd.ENCRYPTION = "sha1"