ical.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. from radicale 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 given parameters."""
  30. cal = "\n".join((
  31. "BEGIN:VCALENDAR",
  32. "\n".join([header.text for header in headers]),
  33. "\n".join([timezone.text for timezone in timezones]),
  34. "\n".join([todo.text for todo in todos]),
  35. "\n".join([event.text for event in events]),
  36. "END:VCALENDAR"))
  37. return "\n".join([line for line in cal.splitlines() if line])
  38. def _parse(vcalendar, tag, obj):
  39. """Find ``tag`` items in ``vcalendar``.
  40. Return a list of items of type ``obj``.
  41. """
  42. items = []
  43. lines = vcalendar.splitlines()
  44. in_item = False
  45. item_lines = []
  46. for line in lines:
  47. if line.startswith("BEGIN:%s" % tag):
  48. in_item = True
  49. item_lines = []
  50. if in_item:
  51. item_lines.append(line)
  52. if line.startswith("END:%s" % tag):
  53. items.append(obj("\n".join(item_lines)))
  54. return items
  55. def headers(vcalendar):
  56. """Find Headers items in ``vcalendar``."""
  57. header_lines = []
  58. lines = vcalendar.splitlines()
  59. for line in lines:
  60. if line.startswith("PRODID:"):
  61. header_lines.append(calendar.Header(line))
  62. for line in lines:
  63. if line.startswith("VERSION:"):
  64. header_lines.append(calendar.Header(line))
  65. return header_lines
  66. def events(vcalendar):
  67. """Get list of ``Event`` from VEVENTS items in ``vcalendar``."""
  68. return _parse(vcalendar, "VEVENT", calendar.Event)
  69. def todos(vcalendar):
  70. """Get list of ``Todo`` from VTODO items in ``vcalendar``."""
  71. return _parse(vcalendar, "VTODO", calendar.Todo)
  72. def timezones(vcalendar):
  73. """Get list of ``Timezome`` from VTIMEZONE items in ``vcalendar``."""
  74. return _parse(vcalendar, "VTIMEZONE", calendar.Timezone)