multifilesystem.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2013 Guillaume Ayoub
  5. # Copyright © 2013 Jean-Marc Martins
  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, headers=None, items=None):
  39. self._create_dirs()
  40. headers = headers or self.headers
  41. items = items if items is not None else self.items
  42. timezones = list(set(i for i in items if isinstance(i, ical.Timezone)))
  43. components = [i for i in items if isinstance(i, ical.Component)]
  44. for component in components:
  45. text = ical.serialize(self.tag, headers, [component] + timezones)
  46. name = (
  47. component.name if sys.version_info[0] >= 3 else
  48. component.name.encode(filesystem.FILESYSTEM_ENCODING))
  49. path = os.path.join(self._path, name)
  50. with filesystem.open(path, "w") as fd:
  51. fd.write(text)
  52. def delete(self):
  53. shutil.rmtree(self._path)
  54. os.remove(self._props_path)
  55. def remove(self, name):
  56. if os.path.exists(os.path.join(self._path, name)):
  57. os.remove(os.path.join(self._path, name))
  58. @property
  59. def text(self):
  60. components = (
  61. ical.Timezone, ical.Event, ical.Todo, ical.Journal, ical.Card)
  62. items = set()
  63. try:
  64. for filename in os.listdir(self._path):
  65. with filesystem.open(os.path.join(self._path, filename)) as fd:
  66. items.update(self._parse(fd.read(), components))
  67. except IOError:
  68. return ""
  69. else:
  70. return ical.serialize(
  71. self.tag, self.headers, sorted(items, key=lambda x: x.name))
  72. @classmethod
  73. def is_node(cls, path):
  74. path = os.path.join(filesystem.FOLDER, path.replace("/", os.sep))
  75. return os.path.isdir(path) and not os.path.exists(path + ".props")
  76. @classmethod
  77. def is_leaf(cls, path):
  78. path = os.path.join(filesystem.FOLDER, path.replace("/", os.sep))
  79. return os.path.isdir(path) and os.path.exists(path + ".props")
  80. @property
  81. def last_modified(self):
  82. last = max([
  83. os.path.getmtime(os.path.join(self._path, filename))
  84. for filename in os.listdir(self._path)] or [0])
  85. return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(last))