ical.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # -*- coding: utf-8; indent-tabs-mode: nil; -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2008 The Radicale Team
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. # TODO: Manage filters (see xmlutils)
  19. import calendar
  20. def writeCalendar(headers=[calendar.Header("PRODID:-//The Radicale Team//NONSGML Radicale Server//EN"),
  21. calendar.Header("VERSION:2.0")],
  22. timezones=[], todos=[], events=[]):
  23. """
  24. Create calendar from headers, timezones, todos, events
  25. """
  26. # TODO: Manage encoding and EOL
  27. cal = u"\n".join((
  28. u"BEGIN:VCALENDAR",
  29. u"\n".join([header.text for header in headers]),
  30. u"\n".join([timezone.text for timezone in timezones]),
  31. u"\n".join([todo.text for todo in todos]),
  32. u"\n".join([event.text for event in events]),
  33. u"END:VCALENDAR"))
  34. return u"\n".join([line for line in cal.splitlines() if line])
  35. def headers(vcalendar):
  36. """
  37. Find Headers Items in vcalendar
  38. """
  39. headers = []
  40. lines = vcalendar.splitlines()
  41. for line in lines:
  42. if line.startswith("PRODID:"):
  43. headers.append(calendar.Header(line))
  44. for line in lines:
  45. if line.startswith("VERSION:"):
  46. headers.append(calendar.Header(line))
  47. return headers
  48. def _parse(vcalendar, tag, obj):
  49. items = []
  50. lines = vcalendar.splitlines()
  51. inItem = False
  52. itemLines = []
  53. for line in lines:
  54. if line.startswith("BEGIN:%s" % tag):
  55. inItem = True
  56. itemLines = []
  57. if inItem:
  58. # TODO: Manage encoding
  59. itemLines.append(line)
  60. if line.startswith("END:%s" % tag):
  61. items.append(obj("\n".join(itemLines)))
  62. return items
  63. events = lambda vcalendar: _parse(vcalendar, "VEVENT", calendar.Event)
  64. todos = lambda vcalendar: _parse(vcalendar, "VTODO", calendar.Todo)
  65. timezones = lambda vcalendar: _parse(vcalendar, "VTIMEZONE", calendar.Timezone)