multifilesystem.py 3.5 KB

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