filesystem.py 3.6 KB

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