types.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. import sys
  18. from typing import (Any, Callable, ContextManager, Iterator, List, Mapping,
  19. MutableMapping, Sequence, Tuple, TypeVar, Union)
  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. if sys.version_info >= (3, 8):
  37. from typing import Protocol, runtime_checkable
  38. @runtime_checkable
  39. class InputStream(Protocol):
  40. def read(self, size: int = ...) -> bytes: ...
  41. @runtime_checkable
  42. class ErrorStream(Protocol):
  43. def flush(self) -> None: ...
  44. def write(self, s: str) -> None: ...
  45. else:
  46. ErrorStream = Any
  47. InputStream = Any
  48. from radicale import item, storage # noqa:E402 isort:skip
  49. CollectionOrItem = Union[item.Item, storage.BaseCollection]