filesystem.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2012-2013 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 posixpath
  24. import json
  25. import time
  26. import sys
  27. from contextlib import contextmanager
  28. from .. import config, ical
  29. FOLDER = os.path.expanduser(config.get("storage", "filesystem_folder"))
  30. FILESYSTEM_ENCODING = sys.getfilesystemencoding()
  31. try:
  32. from dulwich.repo import Repo
  33. GIT_REPOSITORY = Repo(FOLDER)
  34. except:
  35. GIT_REPOSITORY = None
  36. # This function overrides the builtin ``open`` function for this module
  37. # pylint: disable=W0622
  38. @contextmanager
  39. def open(path, mode="r"):
  40. """Open a file at ``path`` with encoding set in the configuration."""
  41. # On enter
  42. abs_path = os.path.join(FOLDER, path.replace("/", os.sep))
  43. with codecs.open(abs_path, mode, config.get("encoding", "stock")) as fd:
  44. yield fd
  45. # On exit
  46. if GIT_REPOSITORY and mode == "w":
  47. path = os.path.relpath(abs_path, FOLDER)
  48. GIT_REPOSITORY.stage([path])
  49. committer = config.get("git", "committer")
  50. GIT_REPOSITORY.do_commit("Commit by Radicale", committer=committer)
  51. # pylint: enable=W0622
  52. class Collection(ical.Collection):
  53. """Collection stored in a flat ical file."""
  54. @property
  55. def _path(self):
  56. """Absolute path of the file at local ``path``."""
  57. return os.path.join(FOLDER, self.path.replace("/", os.sep))
  58. @property
  59. def _props_path(self):
  60. """Absolute path of the file storing the collection properties."""
  61. return self._path + ".props"
  62. def _create_dirs(self):
  63. """Create folder storing the collection if absent."""
  64. if not os.path.exists(os.path.dirname(self._path)):
  65. os.makedirs(os.path.dirname(self._path))
  66. def save(self, text):
  67. self._create_dirs()
  68. with open(self._path, "w") as fd:
  69. fd.write(text)
  70. def delete(self):
  71. os.remove(self._path)
  72. os.remove(self._props_path)
  73. @property
  74. def text(self):
  75. try:
  76. with open(self._path) as fd:
  77. return fd.read()
  78. except IOError:
  79. return ""
  80. @classmethod
  81. def children(cls, path):
  82. abs_path = os.path.join(FOLDER, path.replace("/", os.sep))
  83. _, directories, files = next(os.walk(abs_path))
  84. for filename in directories + files:
  85. rel_filename = posixpath.join(path, filename)
  86. if cls.is_node(rel_filename) or cls.is_leaf(rel_filename):
  87. yield cls(rel_filename)
  88. @classmethod
  89. def is_node(cls, path):
  90. abs_path = os.path.join(FOLDER, path.replace("/", os.sep))
  91. return os.path.isdir(abs_path)
  92. @classmethod
  93. def is_leaf(cls, path):
  94. abs_path = os.path.join(FOLDER, path.replace("/", os.sep))
  95. return os.path.isfile(abs_path) and not abs_path.endswith(".props")
  96. @property
  97. def last_modified(self):
  98. modification_time = time.gmtime(os.path.getmtime(self._path))
  99. return time.strftime("%a, %d %b %Y %H:%M:%S +0000", modification_time)
  100. @property
  101. @contextmanager
  102. def props(self):
  103. # On enter
  104. properties = {}
  105. if os.path.exists(self._props_path):
  106. with open(self._props_path) as prop_file:
  107. properties.update(json.load(prop_file))
  108. old_properties = properties.copy()
  109. yield properties
  110. # On exit
  111. self._create_dirs()
  112. if old_properties != properties:
  113. with open(self._props_path, "w") as prop_file:
  114. json.dump(properties, prop_file)