multifilesystem.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2014 Jean-Marc Martins
  5. # Copyright © 2014-2015 Guillaume Ayoub
  6. #
  7. # This library is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  19. """
  20. Multi files per calendar filesystem storage backend.
  21. """
  22. import os
  23. import shutil
  24. import time
  25. import sys
  26. from . import filesystem
  27. from .. import ical
  28. class Collection(filesystem.Collection):
  29. """Collection stored in several files per calendar."""
  30. def _create_dirs(self):
  31. if not os.path.exists(self._path):
  32. os.makedirs(self._path)
  33. @property
  34. def headers(self):
  35. return (
  36. ical.Header("PRODID:-//Radicale//NONSGML Radicale Server//EN"),
  37. ical.Header("VERSION:%s" % self.version))
  38. def write(self):
  39. self._create_dirs()
  40. for component in self.components:
  41. text = ical.serialize(
  42. self.tag, self.headers, [component] + self.timezones)
  43. name = (
  44. component.name if sys.version_info[0] >= 3 else
  45. component.name.encode(filesystem.FILESYSTEM_ENCODING))
  46. path = os.path.join(self._path, name)
  47. with filesystem.open(path, "w") as fd:
  48. fd.write(text)
  49. def delete(self):
  50. shutil.rmtree(self._path)
  51. os.remove(self._props_path)
  52. def remove(self, name):
  53. if os.path.exists(os.path.join(self._path, name)):
  54. os.remove(os.path.join(self._path, name))
  55. @property
  56. def text(self):
  57. components = (
  58. ical.Timezone, ical.Event, ical.Todo, ical.Journal, ical.Card)
  59. items = set()
  60. try:
  61. for filename in os.listdir(self._path):
  62. with filesystem.open(os.path.join(self._path, filename)) as fd:
  63. items.update(self._parse(fd.read(), components))
  64. except IOError:
  65. return ""
  66. else:
  67. return ical.serialize(
  68. self.tag, self.headers, sorted(items, key=lambda x: x.name))
  69. @classmethod
  70. def is_node(cls, path):
  71. path = os.path.join(filesystem.FOLDER, path.replace("/", os.sep))
  72. return os.path.isdir(path) and not os.path.exists(path + ".props")
  73. @classmethod
  74. def is_leaf(cls, path):
  75. path = os.path.join(filesystem.FOLDER, path.replace("/", os.sep))
  76. return os.path.isdir(path) and os.path.exists(path + ".props")
  77. @property
  78. def last_modified(self):
  79. last = max([
  80. os.path.getmtime(os.path.join(self._path, filename))
  81. for filename in os.listdir(self._path)] or [0])
  82. return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(last))