meta.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2014 Jean-Marc Martins
  3. # Copyright © 2012-2017 Guillaume Ayoub
  4. # Copyright © 2017-2021 Unrud <unrud@outlook.com>
  5. # Copyright © 2024-2025 Peter Bieringer <pb@bieringer.de>
  6. #
  7. # This library is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  19. import json
  20. import os
  21. from typing import Mapping, Optional, TextIO, Union, cast, overload
  22. import radicale.item as radicale_item
  23. from radicale.storage import multifilesystem
  24. from radicale.storage.multifilesystem.base import CollectionBase
  25. class CollectionPartMeta(CollectionBase):
  26. _meta_cache: Optional[Mapping[str, str]]
  27. _props_path: str
  28. def __init__(self, storage_: "multifilesystem.Storage", path: str,
  29. filesystem_path: Optional[str] = None) -> None:
  30. super().__init__(storage_, path, filesystem_path)
  31. self._meta_cache = None
  32. self._props_path = os.path.join(
  33. self._filesystem_path, ".Radicale.props")
  34. @overload
  35. def get_meta(self, key: None = None) -> Mapping[str, str]: ...
  36. @overload
  37. def get_meta(self, key: str) -> Optional[str]: ...
  38. def get_meta(self, key: Optional[str] = None) -> Union[Mapping[str, str],
  39. Optional[str]]:
  40. # reuse cached value if the storage is read-only
  41. if self._storage._lock.locked == "w" or self._meta_cache is None:
  42. try:
  43. try:
  44. with open(self._props_path, encoding=self._encoding) as f:
  45. temp_meta = json.load(f)
  46. except FileNotFoundError:
  47. temp_meta = {}
  48. self._meta_cache = radicale_item.check_and_sanitize_props(
  49. temp_meta)
  50. except ValueError as e:
  51. raise RuntimeError("Failed to load properties of collection "
  52. "%r: %s" % (self.path, e)) from e
  53. return self._meta_cache if key is None else self._meta_cache.get(key)
  54. def set_meta(self, props: Mapping[str, str]) -> None:
  55. # TODO: better fix for "mypy"
  56. try:
  57. with self._atomic_write(self._props_path, "w") as fo: # type: ignore
  58. f = cast(TextIO, fo)
  59. json.dump(props, f, sort_keys=True)
  60. except OSError as e:
  61. raise ValueError("Failed to write meta data %r %s" % (self._props_path, e)) from e