asyncsupport.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. # -*- coding: utf-8 -*-
  2. """The code for async support. Importing this patches Jinja on supported
  3. Python versions.
  4. """
  5. import asyncio
  6. import inspect
  7. from functools import update_wrapper
  8. from markupsafe import Markup
  9. from .environment import TemplateModule
  10. from .runtime import LoopContext
  11. from .utils import concat
  12. from .utils import internalcode
  13. from .utils import missing
  14. async def concat_async(async_gen):
  15. rv = []
  16. async def collect():
  17. async for event in async_gen:
  18. rv.append(event)
  19. await collect()
  20. return concat(rv)
  21. async def generate_async(self, *args, **kwargs):
  22. vars = dict(*args, **kwargs)
  23. try:
  24. async for event in self.root_render_func(self.new_context(vars)):
  25. yield event
  26. except Exception:
  27. yield self.environment.handle_exception()
  28. def wrap_generate_func(original_generate):
  29. def _convert_generator(self, loop, args, kwargs):
  30. async_gen = self.generate_async(*args, **kwargs)
  31. try:
  32. while 1:
  33. yield loop.run_until_complete(async_gen.__anext__())
  34. except StopAsyncIteration:
  35. pass
  36. def generate(self, *args, **kwargs):
  37. if not self.environment.is_async:
  38. return original_generate(self, *args, **kwargs)
  39. return _convert_generator(self, asyncio.get_event_loop(), args, kwargs)
  40. return update_wrapper(generate, original_generate)
  41. async def render_async(self, *args, **kwargs):
  42. if not self.environment.is_async:
  43. raise RuntimeError("The environment was not created with async mode enabled.")
  44. vars = dict(*args, **kwargs)
  45. ctx = self.new_context(vars)
  46. try:
  47. return await concat_async(self.root_render_func(ctx))
  48. except Exception:
  49. return self.environment.handle_exception()
  50. def wrap_render_func(original_render):
  51. def render(self, *args, **kwargs):
  52. if not self.environment.is_async:
  53. return original_render(self, *args, **kwargs)
  54. loop = asyncio.get_event_loop()
  55. return loop.run_until_complete(self.render_async(*args, **kwargs))
  56. return update_wrapper(render, original_render)
  57. def wrap_block_reference_call(original_call):
  58. @internalcode
  59. async def async_call(self):
  60. rv = await concat_async(self._stack[self._depth](self._context))
  61. if self._context.eval_ctx.autoescape:
  62. rv = Markup(rv)
  63. return rv
  64. @internalcode
  65. def __call__(self):
  66. if not self._context.environment.is_async:
  67. return original_call(self)
  68. return async_call(self)
  69. return update_wrapper(__call__, original_call)
  70. def wrap_macro_invoke(original_invoke):
  71. @internalcode
  72. async def async_invoke(self, arguments, autoescape):
  73. rv = await self._func(*arguments)
  74. if autoescape:
  75. rv = Markup(rv)
  76. return rv
  77. @internalcode
  78. def _invoke(self, arguments, autoescape):
  79. if not self._environment.is_async:
  80. return original_invoke(self, arguments, autoescape)
  81. return async_invoke(self, arguments, autoescape)
  82. return update_wrapper(_invoke, original_invoke)
  83. @internalcode
  84. async def get_default_module_async(self):
  85. if self._module is not None:
  86. return self._module
  87. self._module = rv = await self.make_module_async()
  88. return rv
  89. def wrap_default_module(original_default_module):
  90. @internalcode
  91. def _get_default_module(self):
  92. if self.environment.is_async:
  93. raise RuntimeError("Template module attribute is unavailable in async mode")
  94. return original_default_module(self)
  95. return _get_default_module
  96. async def make_module_async(self, vars=None, shared=False, locals=None):
  97. context = self.new_context(vars, shared, locals)
  98. body_stream = []
  99. async for item in self.root_render_func(context):
  100. body_stream.append(item)
  101. return TemplateModule(self, context, body_stream)
  102. def patch_template():
  103. from . import Template
  104. Template.generate = wrap_generate_func(Template.generate)
  105. Template.generate_async = update_wrapper(generate_async, Template.generate_async)
  106. Template.render_async = update_wrapper(render_async, Template.render_async)
  107. Template.render = wrap_render_func(Template.render)
  108. Template._get_default_module = wrap_default_module(Template._get_default_module)
  109. Template._get_default_module_async = get_default_module_async
  110. Template.make_module_async = update_wrapper(
  111. make_module_async, Template.make_module_async
  112. )
  113. def patch_runtime():
  114. from .runtime import BlockReference, Macro
  115. BlockReference.__call__ = wrap_block_reference_call(BlockReference.__call__)
  116. Macro._invoke = wrap_macro_invoke(Macro._invoke)
  117. def patch_filters():
  118. from .filters import FILTERS
  119. from .asyncfilters import ASYNC_FILTERS
  120. FILTERS.update(ASYNC_FILTERS)
  121. def patch_all():
  122. patch_template()
  123. patch_runtime()
  124. patch_filters()
  125. async def auto_await(value):
  126. if inspect.isawaitable(value):
  127. return await value
  128. return value
  129. async def auto_aiter(iterable):
  130. if hasattr(iterable, "__aiter__"):
  131. async for item in iterable:
  132. yield item
  133. return
  134. for item in iterable:
  135. yield item
  136. class AsyncLoopContext(LoopContext):
  137. _to_iterator = staticmethod(auto_aiter)
  138. @property
  139. async def length(self):
  140. if self._length is not None:
  141. return self._length
  142. try:
  143. self._length = len(self._iterable)
  144. except TypeError:
  145. iterable = [x async for x in self._iterator]
  146. self._iterator = self._to_iterator(iterable)
  147. self._length = len(iterable) + self.index + (self._after is not missing)
  148. return self._length
  149. @property
  150. async def revindex0(self):
  151. return await self.length - self.index
  152. @property
  153. async def revindex(self):
  154. return await self.length - self.index0
  155. async def _peek_next(self):
  156. if self._after is not missing:
  157. return self._after
  158. try:
  159. self._after = await self._iterator.__anext__()
  160. except StopAsyncIteration:
  161. self._after = missing
  162. return self._after
  163. @property
  164. async def last(self):
  165. return await self._peek_next() is missing
  166. @property
  167. async def nextitem(self):
  168. rv = await self._peek_next()
  169. if rv is missing:
  170. return self._undefined("there is no next item")
  171. return rv
  172. def __aiter__(self):
  173. return self
  174. async def __anext__(self):
  175. if self._after is not missing:
  176. rv = self._after
  177. self._after = missing
  178. else:
  179. rv = await self._iterator.__anext__()
  180. self.index0 += 1
  181. self._before = self._current
  182. self._current = rv
  183. return rv, self
  184. async def make_async_loop_context(iterable, undefined, recurse=None, depth0=0):
  185. import warnings
  186. warnings.warn(
  187. "This template must be recompiled with at least Jinja 2.11, or"
  188. " it will fail in 3.0.",
  189. DeprecationWarning,
  190. stacklevel=2,
  191. )
  192. return AsyncLoopContext(iterable, undefined, recurse, depth0)
  193. patch_all()