multifilesystem.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. def remove(self, name):
  55. if os.path.exists(os.path.join(self._path, name)):
  56. os.remove(os.path.join(self._path, name))
  57. @property
  58. def text(self):
  59. components = (
  60. ical.Timezone, ical.Event, ical.Todo, ical.Journal, ical.Card)
  61. items = set()
  62. try:
  63. for filename in os.listdir(self._path):
  64. with filesystem.open(os.path.join(self._path, filename)) as fd:
  65. items.update(self._parse(fd.read(), components))
  66. except IOError:
  67. return ""
  68. else:
  69. return ical.serialize(
  70. self.tag, self.headers, sorted(items, key=lambda x: x.name))
  71. @classmethod
  72. def is_node(cls, path):
  73. path = os.path.join(filesystem.FOLDER, path.replace("/", os.sep))
  74. return os.path.isdir(path) and not os.path.exists(path + ".props")
  75. @classmethod
  76. def is_leaf(cls, path):
  77. path = os.path.join(filesystem.FOLDER, path.replace("/", os.sep))
  78. return os.path.isdir(path) and os.path.exists(path + ".props")
  79. @property
  80. def last_modified(self):
  81. last = max([
  82. os.path.getmtime(os.path.join(self._path, filename))
  83. for filename in os.listdir(self._path)] or [0])
  84. return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(last))