filesystem.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 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. # This function overrides the builtin ``open`` function for this module
  30. # pylint: disable=W0622
  31. def open(path, mode="r"):
  32. """Open a file at ``path`` with encoding set in the configuration."""
  33. abs_path = os.path.join(FOLDER, path.replace("/", os.sep))
  34. return codecs.open(abs_path, mode, config.get("encoding", "stock"))
  35. # pylint: enable=W0622
  36. class Collection(ical.Collection):
  37. """Collection stored in a flat ical file."""
  38. @property
  39. def _path(self):
  40. """Absolute path of the file at local ``path``."""
  41. return os.path.join(FOLDER, self.path.replace("/", os.sep))
  42. @property
  43. def _props_path(self):
  44. """Absolute path of the file storing the collection properties."""
  45. return self._path + ".props"
  46. def _create_dirs(self):
  47. """Create folder storing the collection if absent."""
  48. if not os.path.exists(os.path.dirname(self._path)):
  49. os.makedirs(os.path.dirname(self._path))
  50. def save(self, text):
  51. self._create_dirs()
  52. open(self._path, "w").write(text)
  53. def delete(self):
  54. os.remove(self._path)
  55. os.remove(self._props_path)
  56. @property
  57. def text(self):
  58. try:
  59. return open(self._path).read()
  60. except IOError:
  61. return ""
  62. @classmethod
  63. def children(cls, path):
  64. abs_path = os.path.join(FOLDER, path.replace("/", os.sep))
  65. _, directories, files = next(os.walk(abs_path))
  66. for filename in directories + files:
  67. rel_filename = posixpath.join(path, filename)
  68. if cls.is_node(rel_filename) or cls.is_leaf(rel_filename):
  69. yield cls(rel_filename)
  70. @classmethod
  71. def is_node(cls, path):
  72. abs_path = os.path.join(FOLDER, path.replace("/", os.sep))
  73. return os.path.isdir(abs_path)
  74. @classmethod
  75. def is_leaf(cls, path):
  76. abs_path = os.path.join(FOLDER, path.replace("/", os.sep))
  77. return os.path.isfile(abs_path) and not abs_path.endswith(".props")
  78. @property
  79. def last_modified(self):
  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. @contextmanager
  84. def props(self):
  85. # On enter
  86. properties = {}
  87. if os.path.exists(self._props_path):
  88. with open(self._props_path) as prop_file:
  89. properties.update(json.load(prop_file))
  90. yield properties
  91. # On exit
  92. self._create_dirs()
  93. with open(self._props_path, "w") as prop_file:
  94. json.dump(properties, prop_file)