meta.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2014 Jean-Marc Martins
  3. # Copyright © 2012-2017 Guillaume Ayoub
  4. # Copyright © 2017-2018 Unrud <unrud@outlook.com>
  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. import json
  19. import os
  20. from radicale import item as radicale_item
  21. class CollectionMetaMixin:
  22. def __init__(self):
  23. super().__init__()
  24. self._meta_cache = None
  25. self._props_path = os.path.join(
  26. self._filesystem_path, ".Radicale.props")
  27. def get_meta(self, key=None):
  28. # reuse cached value if the storage is read-only
  29. if self._storage._lock.locked == "w" or self._meta_cache is None:
  30. try:
  31. try:
  32. with open(self._props_path, encoding=self._encoding) as f:
  33. self._meta_cache = json.load(f)
  34. except FileNotFoundError:
  35. self._meta_cache = {}
  36. radicale_item.check_and_sanitize_props(self._meta_cache)
  37. except ValueError as e:
  38. raise RuntimeError("Failed to load properties of collection "
  39. "%r: %s" % (self.path, e)) from e
  40. return self._meta_cache.get(key) if key else self._meta_cache
  41. def set_meta(self, props):
  42. with self._atomic_write(self._props_path, "w") as f:
  43. json.dump(props, f, sort_keys=True)