meta.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. import radicale.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. temp_meta = json.load(f)
  34. except FileNotFoundError:
  35. temp_meta = {}
  36. self._meta_cache = radicale_item.check_and_sanitize_props(
  37. temp_meta)
  38. except ValueError as e:
  39. raise RuntimeError("Failed to load properties of collection "
  40. "%r: %s" % (self.path, e)) from e
  41. return self._meta_cache if key is None else self._meta_cache.get(key)
  42. def set_meta(self, props):
  43. with self._atomic_write(self._props_path, "w") as f:
  44. json.dump(props, f, sort_keys=True)