ical.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. # TODO: Factorize code
  20. import calendar
  21. def writeCalendar(headers=[], timezones=[], todos=[], events=[]):
  22. """
  23. Create calendar from headers, timezones, todos, events
  24. """
  25. # TODO: Manage encoding and EOL
  26. return "\n".join((
  27. "BEGIN:VCALENDAR",
  28. "\n".join([header.text for header in headers]),
  29. "\n".join([timezone.text for timezone in timezones]),
  30. "\n".join([todo.text for todo in todos]),
  31. "\n".join([event.text for event in events]),
  32. "END:VCALENDAR"))
  33. def events(vcalendar):
  34. """
  35. Find VEVENT Items in vcalendar
  36. """
  37. events = []
  38. lines = vcalendar.splitlines()
  39. inEvent = False
  40. eventLines = []
  41. for line in lines:
  42. if line.startswith("BEGIN:VEVENT"):
  43. inEvent = True
  44. eventLines = []
  45. if inEvent:
  46. # TODO: Manage encoding
  47. eventLines.append(line)
  48. if line.startswith("END:VEVENT"):
  49. events.append(calendar.Event("\n".join(eventLines)))
  50. return events
  51. def headers(vcalendar):
  52. """
  53. Find Headers Items in vcalendar
  54. """
  55. headers = []
  56. lines = vcalendar.splitlines()
  57. for line in lines:
  58. if line.startswith("PRODID:"):
  59. headers.append(calendar.Header(line))
  60. for line in lines:
  61. if line.startswith("VERSION:"):
  62. headers.append(calendar.Header(line))
  63. return headers
  64. def timezones(vcalendar):
  65. """
  66. Find VTIMEZONE Items in vcalendar
  67. """
  68. timezones = []
  69. lines = vcalendar.splitlines()
  70. inTz = False
  71. tzLines = []
  72. for line in lines:
  73. if line.startswith("BEGIN:VTIMEZONE"):
  74. inTz = True
  75. tzLines = []
  76. if inTz:
  77. tzLines.append(line)
  78. if line.startswith("END:VTIMEZONE"):
  79. timezones.append(calendar.Timezone("\n".join(tzLines)))
  80. return timezones
  81. def todos(vcalendar):
  82. """
  83. Find VTODO Items in vcalendar
  84. """
  85. todos = []
  86. lines = vcalendar.splitlines()
  87. inTodo = False
  88. todoLines = []
  89. for line in lines:
  90. if line.startswith("BEGIN:VTODO"):
  91. inTodo = True
  92. todoLines = []
  93. if inTodo:
  94. # TODO: Manage encoding
  95. todoLines.append(line)
  96. if line.startswith("END:VTODO"):
  97. todos.append(calendar.Todo("\n".join(todoLines)))
  98. return todos