multifilesystem.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. from .. import log
  29. from .. import pathutils
  30. class Collection(filesystem.Collection):
  31. """Collection stored in several files per calendar."""
  32. def _create_dirs(self):
  33. if not os.path.exists(self._filesystem_path):
  34. os.makedirs(self._filesystem_path)
  35. @property
  36. def headers(self):
  37. return (
  38. ical.Header("PRODID:-//Radicale//NONSGML Radicale Server//EN"),
  39. ical.Header("VERSION:%s" % self.version))
  40. def write(self):
  41. self._create_dirs()
  42. for component in self.components:
  43. text = ical.serialize(
  44. self.tag, self.headers, [component] + self.timezones)
  45. name = (
  46. component.name if sys.version_info[0] >= 3 else
  47. component.name.encode(filesystem.FILESYSTEM_ENCODING))
  48. if not pathutils.is_safe_filesystem_path_component(name):
  49. log.LOGGER.debug(
  50. "Can't tranlate name safely to filesystem, "
  51. "skipping component: %s", name)
  52. continue
  53. filesystem_path = os.path.join(self._filesystem_path, name)
  54. with filesystem.open(filesystem_path, "w") as fd:
  55. fd.write(text)
  56. def delete(self):
  57. shutil.rmtree(self._filesystem_path)
  58. os.remove(self._props_path)
  59. def remove(self, name):
  60. if not pathutils.is_safe_filesystem_path_component(name):
  61. log.LOGGER.debug(
  62. "Can't tranlate name safely to filesystem, "
  63. "skipping component: %s", name)
  64. return
  65. filesystem_path = os.path.join(self._filesystem_path, name)
  66. if os.path.exists(filesystem_path):
  67. os.remove(filesystem_path)
  68. @property
  69. def text(self):
  70. components = (
  71. ical.Timezone, ical.Event, ical.Todo, ical.Journal, ical.Card)
  72. items = set()
  73. try:
  74. filenames = os.listdir(self._filesystem_path)
  75. except (OSError, IOError) as e:
  76. log.LOGGER.info(
  77. 'Error while reading collection %r: %r' % (
  78. self._filesystem_path, e))
  79. return ""
  80. for filename in filenames:
  81. path = os.path.join(self._filesystem_path, filename)
  82. try:
  83. with filesystem.open(path) as fd:
  84. items.update(self._parse(fd.read(), components))
  85. except (OSError, IOError) as e:
  86. log.LOGGER.warning(
  87. 'Error while reading item %r: %r' % (path, e))
  88. return ical.serialize(
  89. self.tag, self.headers, sorted(items, key=lambda x: x.name))
  90. @classmethod
  91. def is_node(cls, path):
  92. filesystem_path = pathutils.path_to_filesystem(path, filesystem.FOLDER)
  93. return (
  94. os.path.isdir(filesystem_path) and
  95. not os.path.exists(filesystem_path + ".props"))
  96. @classmethod
  97. def is_leaf(cls, path):
  98. filesystem_path = pathutils.path_to_filesystem(path, filesystem.FOLDER)
  99. return (
  100. os.path.isdir(filesystem_path) and os.path.exists(path + ".props"))
  101. @property
  102. def last_modified(self):
  103. last = max([
  104. os.path.getmtime(os.path.join(self._filesystem_path, filename))
  105. for filename in os.listdir(self._filesystem_path)] or [0])
  106. return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(last))