plain.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. import os
  19. import posixpath
  20. from .. import ical
  21. from .. import config
  22. def calendars():
  23. """
  24. List Available Calendars Paths
  25. """
  26. calendars = []
  27. for folder in os.listdir(config.get("support", "folder")):
  28. for cal in os.listdir(os.path.join(config.get("support", "folder"), folder)):
  29. calendars.append(posixpath.join(folder, cal))
  30. return calendars
  31. def read(cal):
  32. """
  33. Read cal
  34. """
  35. path = os.path.join(config.get("support", "folder"), cal.replace(posixpath.sep, os.path.sep))
  36. return open(path).read()
  37. def append(cal, vcalendar):
  38. """
  39. Append vcalendar to cal
  40. """
  41. oldCalendar = read(cal)
  42. oldTzs = [tz.tzid for tz in ical.timezones(oldCalendar)]
  43. path = os.path.join(config.get("support", "folder"), cal.replace(posixpath.sep, os.path.sep))
  44. oldObjects = []
  45. oldObjects.extend([event.etag() for event in ical.events(oldCalendar)])
  46. oldObjects.extend([todo.etag() for todo in ical.todos(oldCalendar)])
  47. objects = []
  48. objects.extend(ical.events(vcalendar))
  49. objects.extend(ical.todos(vcalendar))
  50. for tz in ical.timezones(vcalendar):
  51. if tz.tzid not in oldTzs:
  52. # TODO: Manage position, encoding and EOL
  53. fd = open(path)
  54. lines = [line for line in fd.readlines() if line]
  55. fd.close()
  56. for i,line in enumerate(tz.text.splitlines()):
  57. lines.insert(2+i, line.encode("utf-8")+"\n")
  58. fd = open(path, "w")
  59. fd.writelines(lines)
  60. fd.close()
  61. for obj in objects:
  62. if obj.etag() not in oldObjects:
  63. # TODO: Manage position, encoding and EOL
  64. fd = open(path)
  65. lines = [line for line in fd.readlines() if line]
  66. fd.close()
  67. for line in obj.text.splitlines():
  68. lines.insert(-1, line.encode("utf-8")+"\n")
  69. fd = open(path, "w")
  70. fd.writelines(lines)
  71. fd.close()
  72. def remove(cal, etag):
  73. """
  74. Remove object named uid from cal
  75. """
  76. path = os.path.join(config.get("support", "folder"), cal.replace(posixpath.sep, os.path.sep))
  77. cal = read(cal)
  78. headers = ical.headers(cal)
  79. timezones = ical.timezones(cal)
  80. todos = [todo for todo in ical.todos(cal) if todo.etag() != etag]
  81. events = [event for event in ical.events(cal) if event.etag() != etag]
  82. fd = open(path, "w")
  83. fd.write(ical.writeCalendar(headers, timezones, todos, events))
  84. fd.close()