types.py 2.0 KB

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