1
0

calendar.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # -*- coding: utf-8; indent-tabs-mode: nil; -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2008-2009 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. 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.encoding = "utf-8"
  32. self.owner = "lize"
  33. self.user = user
  34. self.cal = cal
  35. self.version = "2.0"
  36. self.ctag = hash_tag(self.vcalendar())
  37. def append(self, vcalendar):
  38. """Append vcalendar to the calendar."""
  39. self.ctag = hash_tag(self.vcalendar())
  40. support.append(self.cal, vcalendar)
  41. def remove(self, uid):
  42. """Remove object named ``uid`` from the calendar."""
  43. self.ctag = hash_tag(self.vcalendar())
  44. support.remove(self.cal, uid)
  45. def replace(self, uid, vcalendar):
  46. """Replace objet named ``uid`` by ``vcalendar`` in the calendar."""
  47. self.ctag = hash_tag(self.vcalendar())
  48. support.remove(self.cal, uid)
  49. support.append(self.cal, vcalendar)
  50. def vcalendar(self):
  51. """Return unicode calendar from the calendar."""
  52. return unicode(support.read(self.cal), self.encoding)
  53. class Event(object):
  54. """Internal event class."""
  55. def __init__(self, vcalendar):
  56. """Initialize event from ``vcalendar``."""
  57. self.text = vcalendar
  58. def etag(self):
  59. """Return etag from event."""
  60. return hash_tag(self.text)
  61. class Header(object):
  62. """Internal header class."""
  63. def __init__(self, vcalendar):
  64. """Initialize header from ``vcalendar``."""
  65. self.text = vcalendar
  66. class Timezone(object):
  67. """Internal timezone class."""
  68. def __init__(self, vcalendar):
  69. """Initialize timezone from ``vcalendar``."""
  70. lines = vcalendar.splitlines()
  71. for line in lines:
  72. if line.startswith("TZID:"):
  73. self.tzid = line.lstrip("TZID:")
  74. break
  75. self.text = vcalendar
  76. class Todo(object):
  77. """Internal todo class."""
  78. def __init__(self, vcalendar):
  79. """Initialize todo from ``vcalendar``."""
  80. self.text = vcalendar
  81. def etag(self):
  82. """Return etag from todo."""
  83. return hash_tag(self.text)