calendar.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. def vcalendar(self):
  52. """Return unicode calendar from the calendar."""
  53. return self.support.read(self.cal)
  54. def etag(self):
  55. """Return etag from calendar."""
  56. return '"%s"' % hash_tag(self.vcalendar())
  57. class Event(object):
  58. """Internal event class."""
  59. def __init__(self, vcalendar):
  60. """Initialize event from ``vcalendar``."""
  61. self.text = vcalendar
  62. def etag(self):
  63. """Return etag from event."""
  64. return '"%s"' % hash_tag(self.text)
  65. class Header(object):
  66. """Internal header class."""
  67. def __init__(self, vcalendar):
  68. """Initialize header from ``vcalendar``."""
  69. self.text = vcalendar
  70. class Timezone(object):
  71. """Internal timezone class."""
  72. def __init__(self, vcalendar):
  73. """Initialize timezone from ``vcalendar``."""
  74. lines = vcalendar.splitlines()
  75. for line in lines:
  76. if line.startswith("TZID:"):
  77. self.tzid = line.lstrip("TZID:")
  78. break
  79. self.text = vcalendar
  80. class Todo(object):
  81. """Internal todo class."""
  82. def __init__(self, vcalendar):
  83. """Initialize todo from ``vcalendar``."""
  84. self.text = vcalendar
  85. def etag(self):
  86. """Return etag from todo."""
  87. return hash_tag(self.text)