| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- # -*- coding: utf-8; indent-tabs-mode: nil; -*-
- #
- # This file is part of Radicale Server - Calendar Server
- # Copyright © 2008 The Radicale Team
- #
- # 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/>.
- # TODO: Manage filters (see xmlutils)
- import calendar
- def writeCalendar(headers=[calendar.Header("PRODID:-//The Radicale Team//NONSGML Radicale Server//EN"),
- calendar.Header("VERSION:2.0")],
- timezones=[], todos=[], events=[]):
- """
- Create calendar from headers, timezones, todos, events
- """
- # TODO: Manage encoding and EOL
- cal = u"\n".join((
- u"BEGIN:VCALENDAR",
- u"\n".join([header.text for header in headers]),
- u"\n".join([timezone.text for timezone in timezones]),
- u"\n".join([todo.text for todo in todos]),
- u"\n".join([event.text for event in events]),
- u"END:VCALENDAR"))
- return u"\n".join([line for line in cal.splitlines() if line])
- def headers(vcalendar):
- """
- Find Headers Items in vcalendar
- """
- headers = []
- lines = vcalendar.splitlines()
- for line in lines:
- if line.startswith("PRODID:"):
- headers.append(calendar.Header(line))
- for line in lines:
- if line.startswith("VERSION:"):
- headers.append(calendar.Header(line))
- return headers
- def _parse(vcalendar, tag, obj):
- items = []
- lines = vcalendar.splitlines()
- inItem = False
- itemLines = []
- for line in lines:
- if line.startswith("BEGIN:%s" % tag):
- inItem = True
- itemLines = []
- if inItem:
- # TODO: Manage encoding
- itemLines.append(line)
- if line.startswith("END:%s" % tag):
- items.append(obj("\n".join(itemLines)))
- 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)
|