filesystem.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. # This function overrides the builtin ``open`` function for this module
  29. # pylint: disable=W0622
  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. # pylint: enable=W0622
  34. class Calendar(ical.Calendar):
  35. @property
  36. def _path(self):
  37. """Absolute path of the file at local ``path``."""
  38. return os.path.join(FOLDER, self.path.replace("/", os.sep))
  39. @property
  40. def _props_path(self):
  41. """Absolute path of the file storing the calendar properties."""
  42. return self._path + ".props"
  43. def _create_dirs(self):
  44. """Create folder storing the calendar if absent."""
  45. if not os.path.exists(os.path.dirname(self._path)):
  46. os.makedirs(os.path.dirname(self._path))
  47. def save(self, text):
  48. self._create_dirs()
  49. open(self._path, "w").write(text)
  50. def delete(self):
  51. os.remove(self._path)
  52. os.remove(self._props_path)
  53. @property
  54. def text(self):
  55. try:
  56. return open(self._path).read()
  57. except IOError:
  58. return ""
  59. @classmethod
  60. def children(cls, path):
  61. abs_path = os.path.join(FOLDER, path.replace("/", os.sep))
  62. for filename in next(os.walk(abs_path))[2]:
  63. if cls.is_collection(path):
  64. yield cls(path)
  65. @classmethod
  66. def is_collection(cls, path):
  67. abs_path = os.path.join(FOLDER, path.replace("/", os.sep))
  68. return os.path.isdir(abs_path)
  69. @classmethod
  70. def is_item(cls, path):
  71. abs_path = os.path.join(FOLDER, path.replace("/", os.sep))
  72. return os.path.isfile(abs_path)
  73. @property
  74. def last_modified(self):
  75. # Create calendar if needed
  76. if not os.path.exists(self._path):
  77. self.write()
  78. modification_time = time.gmtime(os.path.getmtime(self._path))
  79. return time.strftime("%a, %d %b %Y %H:%M:%S +0000", modification_time)
  80. @property
  81. @contextmanager
  82. def props(self):
  83. # On enter
  84. properties = {}
  85. if os.path.exists(self._props_path):
  86. with open(self._props_path) as prop_file:
  87. properties.update(json.load(prop_file))
  88. yield properties
  89. # On exit
  90. self._create_dirs()
  91. with open(self._props_path, 'w') as prop_file:
  92. json.dump(properties, prop_file)
  93. ical.Calendar = Calendar