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.

119 lines
3.6KB

  1. import typing as t
  2. from ast import literal_eval
  3. from itertools import chain
  4. from itertools import islice
  5. from . import nodes
  6. from .compiler import CodeGenerator
  7. from .compiler import Frame
  8. from .compiler import has_safe_repr
  9. from .environment import Environment
  10. from .environment import Template
  11. def native_concat(values: t.Iterable[t.Any]) -> t.Optional[t.Any]:
  12. """Return a native Python type from the list of compiled nodes. If
  13. the result is a single node, its value is returned. Otherwise, the
  14. nodes are concatenated as strings. If the result can be parsed with
  15. :func:`ast.literal_eval`, the parsed value is returned. Otherwise,
  16. the string is returned.
  17. :param values: Iterable of outputs to concatenate.
  18. """
  19. head = list(islice(values, 2))
  20. if not head:
  21. return None
  22. if len(head) == 1:
  23. raw = head[0]
  24. if not isinstance(raw, str):
  25. return raw
  26. else:
  27. raw = "".join([str(v) for v in chain(head, values)])
  28. try:
  29. return literal_eval(raw)
  30. except (ValueError, SyntaxError, MemoryError):
  31. return raw
  32. class NativeCodeGenerator(CodeGenerator):
  33. """A code generator which renders Python types by not adding
  34. ``str()`` around output nodes.
  35. """
  36. @staticmethod
  37. def _default_finalize(value: t.Any) -> t.Any:
  38. return value
  39. def _output_const_repr(self, group: t.Iterable[t.Any]) -> str:
  40. return repr("".join([str(v) for v in group]))
  41. def _output_child_to_const(
  42. self, node: nodes.Expr, frame: Frame, finalize: CodeGenerator._FinalizeInfo
  43. ) -> t.Any:
  44. const = node.as_const(frame.eval_ctx)
  45. if not has_safe_repr(const):
  46. raise nodes.Impossible()
  47. if isinstance(node, nodes.TemplateData):
  48. return const
  49. return finalize.const(const) # type: ignore
  50. def _output_child_pre(
  51. self, node: nodes.Expr, frame: Frame, finalize: CodeGenerator._FinalizeInfo
  52. ) -> None:
  53. if finalize.src is not None:
  54. self.write(finalize.src)
  55. def _output_child_post(
  56. self, node: nodes.Expr, frame: Frame, finalize: CodeGenerator._FinalizeInfo
  57. ) -> None:
  58. if finalize.src is not None:
  59. self.write(")")
  60. class NativeEnvironment(Environment):
  61. """An environment that renders templates to native Python types."""
  62. code_generator_class = NativeCodeGenerator
  63. class NativeTemplate(Template):
  64. environment_class = NativeEnvironment
  65. def render(self, *args: t.Any, **kwargs: t.Any) -> t.Any:
  66. """Render the template to produce a native Python type. If the
  67. result is a single node, its value is returned. Otherwise, the
  68. nodes are concatenated as strings. If the result can be parsed
  69. with :func:`ast.literal_eval`, the parsed value is returned.
  70. Otherwise, the string is returned.
  71. """
  72. ctx = self.new_context(dict(*args, **kwargs))
  73. try:
  74. return native_concat(self.root_render_func(ctx)) # type: ignore
  75. except Exception:
  76. return self.environment.handle_exception()
  77. async def render_async(self, *args: t.Any, **kwargs: t.Any) -> t.Any:
  78. if not self.environment.is_async:
  79. raise RuntimeError(
  80. "The environment was not created with async mode enabled."
  81. )
  82. ctx = self.new_context(dict(*args, **kwargs))
  83. try:
  84. return native_concat(
  85. [n async for n in self.root_render_func(ctx)] # type: ignore
  86. )
  87. except Exception:
  88. return self.environment.handle_exception()
  89. NativeEnvironment.template_class = NativeTemplate