calendar.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. Radicale calendar classes.
  22. Define the main classes of a calendar as seen from the server.
  23. """
  24. from radicale import support
  25. def hash_tag(vcalendar):
  26. """Hash an vcalendar string."""
  27. return str(hash(vcalendar))
  28. class Calendar(object):
  29. """Internal calendar class."""
  30. def __init__(self, user, cal):
  31. """Initialize the calendar with ``cal`` and ``user`` parameters."""
  32. # TODO: Use properties from the calendar configuration
  33. self.support = support.load()
  34. self.encoding = "utf-8"
  35. self.owner = "radicale"
  36. self.user = user
  37. self.cal = cal
  38. self.version = "2.0"
  39. self.ctag = hash_tag(self.vcalendar)
  40. def append(self, vcalendar):
  41. """Append vcalendar to the calendar."""
  42. self.ctag = hash_tag(self.vcalendar)
  43. self.support.append(self.cal, vcalendar)
  44. def remove(self, uid):
  45. """Remove object named ``uid`` from the calendar."""
  46. self.ctag = hash_tag(self.vcalendar)
  47. self.support.remove(self.cal, uid)
  48. def replace(self, uid, vcalendar):
  49. """Replace objet named ``uid`` by ``vcalendar`` in the calendar."""
  50. self.ctag = hash_tag(self.vcalendar)
  51. self.support.remove(self.cal, uid)
  52. self.support.append(self.cal, vcalendar)
  53. @property
  54. def vcalendar(self):
  55. """Unicode calendar from the calendar."""
  56. return self.support.read(self.cal)
  57. @property
  58. def etag(self):
  59. """Etag from calendar."""
  60. return '"%s"' % hash_tag(self.vcalendar)
  61. class Event(object):
  62. """Internal event class."""
  63. def __init__(self, vcalendar):
  64. """Initialize event from ``vcalendar``."""
  65. self.text = vcalendar
  66. @property
  67. def etag(self):
  68. """Etag from event."""
  69. return '"%s"' % hash_tag(self.text)
  70. class Header(object):
  71. """Internal header class."""
  72. def __init__(self, vcalendar):
  73. """Initialize header from ``vcalendar``."""
  74. self.text = vcalendar
  75. class Timezone(object):
  76. """Internal timezone class."""
  77. def __init__(self, vcalendar):
  78. """Initialize timezone from ``vcalendar``."""
  79. lines = vcalendar.splitlines()
  80. for line in lines:
  81. if line.startswith("TZID:"):
  82. self.id = line.lstrip("TZID:")
  83. break
  84. self.text = vcalendar
  85. class Todo(object):
  86. """Internal todo class."""
  87. def __init__(self, vcalendar):
  88. """Initialize todo from ``vcalendar``."""
  89. self.text = vcalendar
  90. @property
  91. def etag(self):
  92. """Etag from todo."""
  93. return hash_tag(self.text)