1
0
Fork 0
mirror of https://github.com/Kozea/Radicale.git synced 2025-07-23 17:48:30 +00:00

Python 3 support and minor fixes.

This commit is contained in:
Guillaume Ayoub 2010-01-15 16:04:03 +01:00
parent 4a0d3936e8
commit 4ee09cf817
11 changed files with 66 additions and 70 deletions

View file

@ -24,12 +24,15 @@ Plain text storage.
import os
import posixpath
import codecs
from .. import ical
from .. import config
from radicale import config, ical
_folder = os.path.expanduser(config.get("support", "folder"))
def _open(path, mode="r"):
return codecs.open(path, mode, config.get("encoding", "stock"))
def calendars():
"""List available calendars paths."""
calendars = []
@ -45,17 +48,17 @@ def mkcalendar(name):
user, cal = name.split(posixpath.sep)
if not os.path.exists(os.path.join(_folder, user)):
os.makedirs(os.path.join(_folder, user))
fd = open(os.path.join(_folder, user, cal), "w")
fd.write(ical.write_calendar().encode(config.get("encoding", "stock")))
fd = _open(os.path.join(_folder, user, cal), "w")
fd.write(ical.write_calendar())
def read(cal):
"""Read calendar ``cal``."""
path = os.path.join(_folder, cal.replace(posixpath.sep, os.path.sep))
return open(path).read()
return _open(path).read()
def append(cal, vcalendar):
"""Append ``vcalendar`` to ``cal``."""
old_calendar = unicode(read(cal), config.get("encoding", "stock"))
old_calendar = read(cal)
old_tzs = [tz.tzid for tz in ical.timezones(old_calendar)]
path = os.path.join(_folder, cal.replace(posixpath.sep, os.path.sep))
@ -69,29 +72,29 @@ def append(cal, vcalendar):
for tz in ical.timezones(vcalendar):
if tz.tzid not in old_tzs:
# TODO: Manage position, encoding and EOL
fd = open(path)
# TODO: Manage position and EOL
fd = _open(path)
lines = [line for line in fd.readlines() if line]
fd.close()
for i,line in enumerate(tz.text.splitlines()):
lines.insert(2+i, line.encode(config.get("encoding", "stock"))+"\n")
lines.insert(2 + i, line + "\n")
fd = open(path, "w")
fd = _open(path, "w")
fd.writelines(lines)
fd.close()
for obj in objects:
if obj.etag() not in old_objects:
# TODO: Manage position, encoding and EOL
fd = open(path)
# TODO: Manage position and EOL
fd = _open(path)
lines = [line for line in fd.readlines() if line]
fd.close()
for line in obj.text.splitlines():
lines.insert(-1, line.encode(config.get("encoding", "stock"))+"\n")
lines.insert(-1, line + "\n")
fd = open(path, "w")
fd = _open(path, "w")
fd.writelines(lines)
fd.close()
@ -99,15 +102,15 @@ def remove(cal, etag):
"""Remove object named ``etag`` from ``cal``."""
path = os.path.join(_folder, cal.replace(posixpath.sep, os.path.sep))
cal = unicode(read(cal), config.get("encoding", "stock"))
cal = read(cal)
headers = ical.headers(cal)
timezones = ical.timezones(cal)
todos = [todo for todo in ical.todos(cal) if todo.etag() != etag]
events = [event for event in ical.events(cal) if event.etag() != etag]
fd = open(path, "w")
fd.write(ical.write_calendar(headers, timezones, todos, events).encode(config.get("encoding", "stock")))
fd = _open(path, "w")
fd.write(ical.write_calendar(headers, timezones, todos, events))
fd.close()
if config.get("support", "calendar"):