filesystem.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2012 Guillaume Ayoub
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. Filesystem storage backend.
  20. """
  21. import codecs
  22. import os
  23. import json
  24. import time
  25. from contextlib import contextmanager
  26. from radicale import config, ical
  27. FOLDER = os.path.expanduser(config.get("storage", "filesystem_folder"))
  28. class Calendar(ical.Calendar):
  29. @staticmethod
  30. def open(path, mode="r"):
  31. abs_path = os.path.join(FOLDER, path.replace("/", os.sep))
  32. return codecs.open(abs_path, mode, config.get("encoding", "stock"))
  33. @classmethod
  34. def is_collection(cls, path):
  35. abs_path = os.path.join(FOLDER, path.replace("/", os.sep))
  36. return os.path.isdir(abs_path)
  37. @classmethod
  38. def is_item(cls, path):
  39. abs_path = os.path.join(FOLDER, path.replace("/", os.sep))
  40. return os.path.isfile(abs_path)
  41. @classmethod
  42. def children(cls, path):
  43. abs_path = os.path.join(FOLDER, path.replace("/", os.sep))
  44. for filename in next(os.walk(abs_path))[2]:
  45. if cls.is_collection(path):
  46. yield cls(path)
  47. def delete(self):
  48. os.remove(self._path)
  49. os.remove(self._props_path)
  50. @property
  51. def _path(self):
  52. """Absolute path of the file at local ``path``."""
  53. return os.path.join(FOLDER, self.path.replace("/", os.sep))
  54. @property
  55. def _props_path(self):
  56. """Absolute path of the file storing the calendar properties."""
  57. return self._path + ".props"
  58. def _create_dirs(self):
  59. """Create folder storing the calendar if absent."""
  60. if not os.path.exists(os.path.dirname(self._path)):
  61. os.makedirs(os.path.dirname(self._path))
  62. @property
  63. @contextmanager
  64. def props(self):
  65. # On enter
  66. properties = {}
  67. if os.path.exists(self._props_path):
  68. with open(self._props_path) as prop_file:
  69. properties.update(json.load(prop_file))
  70. yield properties
  71. # On exit
  72. self._create_dirs()
  73. with open(self._props_path, 'w') as prop_file:
  74. json.dump(properties, prop_file)
  75. @property
  76. def last_modified(self):
  77. # Create calendar if needed
  78. if not os.path.exists(self._path):
  79. self.write()
  80. modification_time = time.gmtime(os.path.getmtime(self._path))
  81. return time.strftime("%a, %d %b %Y %H:%M:%S +0000", modification_time)
  82. @property
  83. def text(self):
  84. try:
  85. return open(self._path).read()
  86. except IOError:
  87. return ""
  88. def save(self, text):
  89. self._create_dirs()
  90. self.open(self._path, "w").write(text)
  91. ical.Calendar = Calendar