multifilesystem.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 json
  24. import shutil
  25. import time
  26. import sys
  27. from contextlib import contextmanager
  28. from . import filesystem
  29. from .. import ical
  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._path):
  34. os.makedirs(self._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, headers=None, items=None):
  41. self._create_dirs()
  42. headers = headers or self.headers
  43. items = items if items is not None else self.items
  44. timezones = list(set(i for i in items if isinstance(i, ical.Timezone)))
  45. components = [i for i in items if isinstance(i, ical.Component)]
  46. for component in components:
  47. text = ical.serialize(self.tag, headers, [component] + timezones)
  48. name = (
  49. component.name if sys.version_info[0] >= 3 else
  50. component.name.encode(filesystem.FILESYSTEM_ENCODING))
  51. path = os.path.join(self._path, name)
  52. with filesystem.open(path, "w") as fd:
  53. fd.write(text)
  54. def delete(self):
  55. shutil.rmtree(self._path)
  56. def remove(self, name):
  57. if os.path.exists(os.path.join(self._path, name)):
  58. os.remove(os.path.join(self._path, name))
  59. @property
  60. def text(self):
  61. components = (
  62. ical.Timezone, ical.Event, ical.Todo, ical.Journal, ical.Card)
  63. items = set()
  64. try:
  65. for filename in os.listdir(self._path):
  66. with filesystem.open(os.path.join(self._path, filename)) as fd:
  67. items.update(self._parse(fd.read(), components))
  68. except IOError:
  69. return ""
  70. else:
  71. return ical.serialize(
  72. self.tag, self.headers, sorted(items, key=lambda x: x.name))
  73. @classmethod
  74. def is_node(cls, path):
  75. path = os.path.join(filesystem.FOLDER, path.replace("/", os.sep))
  76. return os.path.isdir(path) and not os.path.exists(path + ".props")
  77. @classmethod
  78. def is_leaf(cls, path):
  79. path = os.path.join(filesystem.FOLDER, path.replace("/", os.sep))
  80. return os.path.isdir(path) and os.path.exists(path + ".props")
  81. @property
  82. def last_modified(self):
  83. last = max([
  84. os.path.getmtime(os.path.join(self._path, filename))
  85. for filename in os.listdir(self._path)] or [0])
  86. return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(last))
  87. @property
  88. @contextmanager
  89. def props(self):
  90. # On enter
  91. properties = {}
  92. if os.path.exists(self._props_path):
  93. with open(self._props_path) as prop_file:
  94. properties.update(json.load(prop_file))
  95. old_properties = properties.copy()
  96. yield properties
  97. # On exit
  98. if os.path.exists(self._props_path):
  99. self._create_dirs()
  100. if old_properties != properties:
  101. with open(self._props_path, "w") as prop_file:
  102. json.dump(properties, prop_file)