types.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2020-2023 Unrud <unrud@outlook.com>
  3. # Copyright © 2024-2025 Peter Bieringer <pb@bieringer.de>
  4. #
  5. # This library is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  17. import contextlib
  18. from typing import (Any, Callable, ContextManager, Iterator, List, Mapping,
  19. MutableMapping, Protocol, Sequence, Tuple, TypeVar, Union,
  20. runtime_checkable)
  21. WSGIResponseHeaders = Union[Mapping[str, str], Sequence[Tuple[str, str]]]
  22. WSGIResponse = Tuple[int, WSGIResponseHeaders, Union[None, str, bytes], Union[None, str]]
  23. WSGIEnviron = Mapping[str, Any]
  24. WSGIStartResponse = Callable[[str, List[Tuple[str, str]]], Any]
  25. CONFIG = Mapping[str, Mapping[str, Any]]
  26. MUTABLE_CONFIG = MutableMapping[str, MutableMapping[str, Any]]
  27. CONFIG_SCHEMA = Mapping[str, Mapping[str, Any]]
  28. _T = TypeVar("_T")
  29. def contextmanager(func: Callable[..., Iterator[_T]]
  30. ) -> Callable[..., ContextManager[_T]]:
  31. """Compatibility wrapper for `contextlib.contextmanager` with
  32. `typeguard`"""
  33. result = contextlib.contextmanager(func)
  34. result.__annotations__ = {**func.__annotations__,
  35. "return": ContextManager[_T]}
  36. return result
  37. @runtime_checkable
  38. class InputStream(Protocol):
  39. def read(self, size: int = ...) -> bytes: ...
  40. @runtime_checkable
  41. class ErrorStream(Protocol):
  42. def flush(self) -> object: ...
  43. def write(self, s: str) -> object: ...
  44. from radicale import item, storage # noqa:E402 isort:skip
  45. CollectionOrItem = Union[item.Item, storage.BaseCollection]