You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
3.3 KiB

4 years ago
  1. import asyncio
  2. from abc import ABC, abstractmethod
  3. from collections.abc import Iterable, Sized
  4. class AbstractRouter(ABC):
  5. def __init__(self):
  6. self._frozen = False
  7. def post_init(self, app):
  8. """Post init stage.
  9. Not an abstract method for sake of backward compatibility,
  10. but if the router wants to be aware of the application
  11. it can override this.
  12. """
  13. @property
  14. def frozen(self):
  15. return self._frozen
  16. def freeze(self):
  17. """Freeze router."""
  18. self._frozen = True
  19. @abstractmethod
  20. async def resolve(self, request):
  21. """Return MATCH_INFO for given request"""
  22. class AbstractMatchInfo(ABC):
  23. @abstractmethod
  24. async def handler(self, request):
  25. """Execute matched request handler"""
  26. @abstractmethod
  27. async def expect_handler(self, request):
  28. """Expect handler for 100-continue processing"""
  29. @property # pragma: no branch
  30. @abstractmethod
  31. def http_exception(self):
  32. """HTTPException instance raised on router's resolving, or None"""
  33. @abstractmethod # pragma: no branch
  34. def get_info(self):
  35. """Return a dict with additional info useful for introspection"""
  36. @property # pragma: no branch
  37. @abstractmethod
  38. def apps(self):
  39. """Stack of nested applications.
  40. Top level application is left-most element.
  41. """
  42. @abstractmethod
  43. def add_app(self, app):
  44. """Add application to the nested apps stack."""
  45. @abstractmethod
  46. def freeze(self):
  47. """Freeze the match info.
  48. The method is called after route resolution.
  49. After the call .add_app() is forbidden.
  50. """
  51. class AbstractView(ABC):
  52. """Abstract class based view."""
  53. def __init__(self, request):
  54. self._request = request
  55. @property
  56. def request(self):
  57. """Request instance."""
  58. return self._request
  59. @abstractmethod
  60. def __await__(self):
  61. """Execute the view handler."""
  62. class AbstractResolver(ABC):
  63. """Abstract DNS resolver."""
  64. @abstractmethod
  65. async def resolve(self, hostname):
  66. """Return IP address for given hostname"""
  67. @abstractmethod
  68. async def close(self):
  69. """Release resolver"""
  70. class AbstractCookieJar(Sized, Iterable):
  71. """Abstract Cookie Jar."""
  72. def __init__(self, *, loop=None):
  73. self._loop = loop or asyncio.get_event_loop()
  74. @abstractmethod
  75. def clear(self):
  76. """Clear all cookies."""
  77. @abstractmethod
  78. def update_cookies(self, cookies, response_url=None):
  79. """Update cookies."""
  80. @abstractmethod
  81. def filter_cookies(self, request_url):
  82. """Return the jar's cookies filtered by their attributes."""
  83. class AbstractStreamWriter(ABC):
  84. """Abstract stream writer."""
  85. @abstractmethod
  86. async def write(self, chunk):
  87. """Write chunk into stream."""
  88. @abstractmethod
  89. async def write_eof(self, chunk=b''):
  90. """Write last chunk."""
  91. @abstractmethod
  92. async def drain(self):
  93. """Flush the write buffer."""
  94. class AbstractAccessLogger(ABC):
  95. """Abstract writer to access log."""
  96. def __init__(self, logger, log_format):
  97. self.logger = logger
  98. self.log_format = log_format
  99. @abstractmethod
  100. def log(self, request, response, time):
  101. """Emit log to logger."""