Fix compatibility between python2 and 3

This commit is contained in:
Jean-Marc Martins 2013-09-19 14:40:03 +02:00
parent 6c40f5e24a
commit a631c8c761
5 changed files with 18 additions and 11 deletions

View File

@ -272,9 +272,9 @@ class Application(object):
if authorization: if authorization:
authorization = \ authorization = \
authorization.lstrip("Basic").strip().encode("ascii") authorization.decode("ascii").lstrip("Basic").strip()
user, password = self.decode( user, password = self.decode(base64.b64decode(
base64.b64decode(authorization), environ).split(":", 1) authorization.encode("ascii")), environ).split(":", 1)
else: else:
user = password = None user = password = None

View File

@ -103,7 +103,8 @@ class Item(object):
self.text = self.text.replace( self.text = self.text.replace(
"\nEND:", "\nX-RADICALE-NAME:%s\nEND:" % self._name) "\nEND:", "\nX-RADICALE-NAME:%s\nEND:" % self._name)
else: 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( self.text = self.text.replace(
"\nEND:", "\nX-RADICALE-NAME:%s\nEND:" % self._name) "\nEND:", "\nX-RADICALE-NAME:%s\nEND:" % self._name)

View File

@ -36,7 +36,7 @@ FILESYSTEM_ENCODING = sys.getfilesystemencoding()
try: try:
from dulwich.repo import Repo from dulwich.repo import Repo
GIT_REPOSITORY = Repo(FOLDER).encode(FILESYSTEM_ENCODING) GIT_REPOSITORY = Repo(FOLDER)
except: except:
GIT_REPOSITORY = None GIT_REPOSITORY = None
@ -53,7 +53,7 @@ def open(path, mode="r"):
# On exit # On exit
if GIT_REPOSITORY and mode == "w": if GIT_REPOSITORY and mode == "w":
path = os.path.relpath(abs_path, FOLDER) path = os.path.relpath(abs_path, FOLDER)
GIT_REPOSITORY.stage([path.encode(FILESYSTEM_ENCODING)]) GIT_REPOSITORY.stage([path])
committer = config.get("git", "committer") committer = config.get("git", "committer")
GIT_REPOSITORY.do_commit("Commit by Radicale", committer=committer) GIT_REPOSITORY.do_commit("Commit by Radicale", committer=committer)
# pylint: enable=W0622 # pylint: enable=W0622

View File

@ -25,6 +25,7 @@ Multi files per calendar filesystem storage backend.
import os import os
import shutil import shutil
import time import time
import sys
from . import filesystem from . import filesystem
from .. import ical from .. import ical
@ -50,7 +51,10 @@ class Collection(filesystem.Collection):
components = [i for i in items if isinstance(i, ical.Component)] components = [i for i in items if isinstance(i, ical.Component)]
for component in components: for component in components:
text = ical.serialize(self.tag, headers, [component] + timezones) 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: with filesystem.open(path, "w") as fd:
fd.write(text) fd.write(text)

View File

@ -58,6 +58,8 @@ class BaseTest(object):
args["REQUEST_METHOD"] = method.upper() args["REQUEST_METHOD"] = method.upper()
args["PATH_INFO"] = path args["PATH_INFO"] = path
if data: if data:
if sys.version_info[0] >= 3:
data = data.encode("utf-8")
args["wsgi.input"] = BytesIO(data) args["wsgi.input"] = BytesIO(data)
args["CONTENT_LENGTH"] = str(len(data)) args["CONTENT_LENGTH"] = str(len(data))
self.application._answer = self.application(args, self.start_response) self.application._answer = self.application(args, self.start_response)
@ -128,11 +130,11 @@ class HtpasswdAuthSystem(BaseTest):
def setup(self): def setup(self):
self.colpath = tempfile.mkdtemp() self.colpath = tempfile.mkdtemp()
htpasswd_file_path = os.path.join(self.colpath, ".htpasswd") htpasswd_file_path = os.path.join(self.colpath, ".htpasswd")
with open(htpasswd_file_path, "w") as fd: with open(htpasswd_file_path, "wb") as fd:
fd.write('tmp:{SHA}' + base64.b64encode( fd.write(b"tmp:{SHA}" + base64.b64encode(
hashlib.sha1("po").digest())) hashlib.sha1(b"bepo").digest()))
config.set("auth", "type", "htpasswd") config.set("auth", "type", "htpasswd")
self.userpass = base64.b64encode("tmp:bépo") self.userpass = base64.b64encode(b"tmp:bepo")
self.application = radicale.Application() self.application = radicale.Application()
htpasswd.FILENAME = htpasswd_file_path htpasswd.FILENAME = htpasswd_file_path
htpasswd.ENCRYPTION = "sha1" htpasswd.ENCRYPTION = "sha1"