plain.py 3.9 KB

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