ical.py 2.7 KB

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