Factorize code and remove encoding bugs

git-svn-id: http://svn.32rwr.info/radicale/trunk@8 74e4794c-479d-4a33-9dda-c6c359d70f12
This commit is contained in:
(no author) 2009-07-25 16:18:05 +00:00
parent 34d1d30998
commit e7a5ef8c5d
3 changed files with 31 additions and 74 deletions

View File

@ -20,6 +20,8 @@
# TODO: Manage depth and calendars/collections (see xmlutils) # TODO: Manage depth and calendars/collections (see xmlutils)
# TODO: Manage smart and configurable logs # TODO: Manage smart and configurable logs
# TODO: Manage authentication # TODO: Manage authentication
# TODO: Magage command-line options
# TODO: Forget twisted?
import sys import sys
from twisted.web import server from twisted.web import server

View File

@ -17,7 +17,6 @@
# along with Radicale. If not, see <http://www.gnu.org/licenses/>. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
# TODO: Manage filters (see xmlutils) # TODO: Manage filters (see xmlutils)
# TODO: Factorize code
import calendar import calendar
@ -28,37 +27,14 @@ def writeCalendar(headers=[calendar.Header("PRODID:-//The Radicale Team//NONSGML
Create calendar from headers, timezones, todos, events Create calendar from headers, timezones, todos, events
""" """
# TODO: Manage encoding and EOL # TODO: Manage encoding and EOL
cal = "\n".join(( cal = u"\n".join((
"BEGIN:VCALENDAR", u"BEGIN:VCALENDAR",
"\n".join([header.text for header in headers]), u"\n".join([header.text for header in headers]),
"\n".join([timezone.text for timezone in timezones]), u"\n".join([timezone.text for timezone in timezones]),
"\n".join([todo.text for todo in todos]), u"\n".join([todo.text for todo in todos]),
"\n".join([event.text for event in events]), u"\n".join([event.text for event in events]),
"END:VCALENDAR")) u"END:VCALENDAR"))
return "\n".join([line for line in cal.splitlines() if line]) return u"\n".join([line for line in cal.splitlines() if line])
def events(vcalendar):
"""
Find VEVENT Items in vcalendar
"""
events = []
lines = vcalendar.splitlines()
inEvent = False
eventLines = []
for line in lines:
if line.startswith("BEGIN:VEVENT"):
inEvent = True
eventLines = []
if inEvent:
# TODO: Manage encoding
eventLines.append(line)
if line.startswith("END:VEVENT"):
events.append(calendar.Event("\n".join(eventLines)))
return events
def headers(vcalendar): def headers(vcalendar):
""" """
@ -76,47 +52,26 @@ def headers(vcalendar):
return headers return headers
def timezones(vcalendar): def _parse(vcalendar, tag, obj):
""" items = []
Find VTIMEZONE Items in vcalendar
"""
timezones = []
lines = vcalendar.splitlines() lines = vcalendar.splitlines()
inTz = False inItem = False
tzLines = [] itemLines = []
for line in lines: for line in lines:
if line.startswith("BEGIN:VTIMEZONE"): if line.startswith("BEGIN:%s" % tag):
inTz = True inItem = True
tzLines = [] itemLines = []
if inTz: if inItem:
tzLines.append(line)
if line.startswith("END:VTIMEZONE"):
timezones.append(calendar.Timezone("\n".join(tzLines)))
return timezones
def todos(vcalendar):
"""
Find VTODO Items in vcalendar
"""
todos = []
lines = vcalendar.splitlines()
inTodo = False
todoLines = []
for line in lines:
if line.startswith("BEGIN:VTODO"):
inTodo = True
todoLines = []
if inTodo:
# TODO: Manage encoding # TODO: Manage encoding
todoLines.append(line) itemLines.append(line)
if line.startswith("END:VTODO"): if line.startswith("END:%s" % tag):
todos.append(calendar.Todo("\n".join(todoLines))) items.append(obj("\n".join(itemLines)))
return todos return items
events = lambda vcalendar: _parse(vcalendar, "VEVENT", calendar.Event)
todos = lambda vcalendar: _parse(vcalendar, "VTODO", calendar.Todo)
timezones = lambda vcalendar: _parse(vcalendar, "VTIMEZONE", calendar.Timezone)

View File

@ -44,7 +44,7 @@ def mkcalendar(name):
if not os.path.exists(os.path.join(_folder, user)): if not os.path.exists(os.path.join(_folder, user)):
os.makedirs(os.path.join(_folder, user)) os.makedirs(os.path.join(_folder, user))
fd = open(os.path.join(_folder, user, cal), "w") fd = open(os.path.join(_folder, user, cal), "w")
fd.write(ical.writeCalendar()) fd.write(ical.writeCalendar().encode(config.get("encoding", "stock")))
def read(cal): def read(cal):
""" """
@ -77,7 +77,7 @@ def append(cal, vcalendar):
fd.close() fd.close()
for i,line in enumerate(tz.text.splitlines()): for i,line in enumerate(tz.text.splitlines()):
lines.insert(2+i, line.encode("utf-8")+"\n") lines.insert(2+i, line.encode(config.get("encoding", "stock"))+"\n")
fd = open(path, "w") fd = open(path, "w")
fd.writelines(lines) fd.writelines(lines)
@ -91,7 +91,7 @@ def append(cal, vcalendar):
fd.close() fd.close()
for line in obj.text.splitlines(): for line in obj.text.splitlines():
lines.insert(-1, line.encode("utf-8")+"\n") lines.insert(-1, line.encode(config.get("encoding", "stock"))+"\n")
fd = open(path, "w") fd = open(path, "w")
fd.writelines(lines) fd.writelines(lines)
@ -111,7 +111,7 @@ def remove(cal, etag):
events = [event for event in ical.events(cal) if event.etag() != etag] events = [event for event in ical.events(cal) if event.etag() != etag]
fd = open(path, "w") fd = open(path, "w")
fd.write(ical.writeCalendar(headers, timezones, todos, events)) fd.write(ical.writeCalendar(headers, timezones, todos, events).encode(config.get("encoding", "stock")))
fd.close() fd.close()
if config.get("support", "defaultCalendar"): if config.get("support", "defaultCalendar"):