2010-01-15 00:15:41 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2008-12-30 16:25:42 +00:00
|
|
|
#
|
|
|
|
# This file is part of Radicale Server - Calendar Server
|
2010-01-15 00:15:41 +01:00
|
|
|
# Copyright © 2008-2010 Guillaume Ayoub
|
2009-07-27 15:04:54 +00:00
|
|
|
# Copyright © 2008 Nicolas Kandel
|
|
|
|
# Copyright © 2008 Pascal Halter
|
2008-12-30 16:25:42 +00:00
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Radicale. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2009-07-27 15:04:54 +00:00
|
|
|
"""
|
|
|
|
iCal parsing functions.
|
2010-02-10 18:57:21 +01:00
|
|
|
|
2009-07-27 15:04:54 +00:00
|
|
|
"""
|
|
|
|
|
2008-12-30 16:25:42 +00:00
|
|
|
# TODO: Manage filters (see xmlutils)
|
|
|
|
|
2010-01-15 16:04:03 +01:00
|
|
|
from radicale import calendar
|
2008-12-30 16:25:42 +00:00
|
|
|
|
2010-02-10 18:57:21 +01:00
|
|
|
|
|
|
|
def write_calendar(headers=(
|
2009-07-27 15:04:54 +00:00
|
|
|
calendar.Header("PRODID:-//Radicale//NONSGML Radicale Server//EN"),
|
2010-02-10 18:57:21 +01:00
|
|
|
calendar.Header("VERSION:2.0")),
|
|
|
|
timezones=(), todos=(), events=()):
|
|
|
|
"""Create calendar from given parameters."""
|
2010-01-15 16:04:03 +01:00
|
|
|
cal = "\n".join((
|
|
|
|
"BEGIN:VCALENDAR",
|
|
|
|
"\n".join([header.text for header in headers]),
|
|
|
|
"\n".join([timezone.text for timezone in timezones]),
|
|
|
|
"\n".join([todo.text for todo in todos]),
|
|
|
|
"\n".join([event.text for event in events]),
|
|
|
|
"END:VCALENDAR"))
|
|
|
|
return "\n".join([line for line in cal.splitlines() if line])
|
2008-12-30 16:25:42 +00:00
|
|
|
|
|
|
|
|
2009-07-25 16:18:05 +00:00
|
|
|
def _parse(vcalendar, tag, obj):
|
2009-07-27 15:04:54 +00:00
|
|
|
"""Find ``tag`` items in ``vcalendar``.
|
|
|
|
|
|
|
|
Return a list of items of type ``obj``.
|
2010-02-10 18:57:21 +01:00
|
|
|
|
2009-07-27 15:04:54 +00:00
|
|
|
"""
|
2009-07-25 16:18:05 +00:00
|
|
|
items = []
|
2008-12-30 16:25:42 +00:00
|
|
|
|
|
|
|
lines = vcalendar.splitlines()
|
2010-02-10 18:57:21 +01:00
|
|
|
in_item = False
|
|
|
|
item_lines = []
|
2008-12-30 16:25:42 +00:00
|
|
|
|
|
|
|
for line in lines:
|
2009-07-25 16:18:05 +00:00
|
|
|
if line.startswith("BEGIN:%s" % tag):
|
2010-02-10 18:57:21 +01:00
|
|
|
in_item = True
|
|
|
|
item_lines = []
|
2008-12-30 16:25:42 +00:00
|
|
|
|
2010-02-10 18:57:21 +01:00
|
|
|
if in_item:
|
|
|
|
item_lines.append(line)
|
2009-07-25 16:18:05 +00:00
|
|
|
if line.startswith("END:%s" % tag):
|
2010-02-10 18:57:21 +01:00
|
|
|
items.append(obj("\n".join(item_lines)))
|
2009-07-25 16:18:05 +00:00
|
|
|
|
|
|
|
return items
|
2008-12-30 16:25:42 +00:00
|
|
|
|
2010-02-10 18:57:21 +01:00
|
|
|
|
|
|
|
def headers(vcalendar):
|
|
|
|
"""Find Headers items in ``vcalendar``."""
|
|
|
|
header_lines = []
|
|
|
|
|
|
|
|
lines = vcalendar.splitlines()
|
|
|
|
for line in lines:
|
|
|
|
if line.startswith("PRODID:"):
|
|
|
|
header_lines.append(calendar.Header(line))
|
|
|
|
for line in lines:
|
|
|
|
if line.startswith("VERSION:"):
|
|
|
|
header_lines.append(calendar.Header(line))
|
|
|
|
|
|
|
|
return header_lines
|
|
|
|
|
|
|
|
|
|
|
|
def events(vcalendar):
|
|
|
|
"""Get list of ``Event`` from VEVENTS items in ``vcalendar``."""
|
|
|
|
return _parse(vcalendar, "VEVENT", calendar.Event)
|
|
|
|
|
|
|
|
|
|
|
|
def todos(vcalendar):
|
|
|
|
"""Get list of ``Todo`` from VTODO items in ``vcalendar``."""
|
|
|
|
return _parse(vcalendar, "VTODO", calendar.Todo)
|
|
|
|
|
|
|
|
|
|
|
|
def timezones(vcalendar):
|
|
|
|
"""Get list of ``Timezome`` from VTIMEZONE items in ``vcalendar``."""
|
|
|
|
return _parse(vcalendar, "VTIMEZONE", calendar.Timezone)
|