1
0

calendar.py 3.3 KB

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