filesystem.py 4.1 KB

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