Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.

engine.py 7.6KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import functools
  2. from django.core.exceptions import ImproperlyConfigured
  3. from django.utils.functional import cached_property
  4. from django.utils.module_loading import import_string
  5. from .base import Template
  6. from .context import Context, _builtin_context_processors
  7. from .exceptions import TemplateDoesNotExist
  8. from .library import import_library
  9. class Engine:
  10. default_builtins = [
  11. "django.template.defaulttags",
  12. "django.template.defaultfilters",
  13. "django.template.loader_tags",
  14. ]
  15. def __init__(
  16. self,
  17. dirs=None,
  18. app_dirs=False,
  19. context_processors=None,
  20. debug=False,
  21. loaders=None,
  22. string_if_invalid="",
  23. file_charset="utf-8",
  24. libraries=None,
  25. builtins=None,
  26. autoescape=True,
  27. ):
  28. if dirs is None:
  29. dirs = []
  30. if context_processors is None:
  31. context_processors = []
  32. if loaders is None:
  33. loaders = ["django.template.loaders.filesystem.Loader"]
  34. if app_dirs:
  35. loaders += ["django.template.loaders.app_directories.Loader"]
  36. loaders = [("django.template.loaders.cached.Loader", loaders)]
  37. else:
  38. if app_dirs:
  39. raise ImproperlyConfigured(
  40. "app_dirs must not be set when loaders is defined."
  41. )
  42. if libraries is None:
  43. libraries = {}
  44. if builtins is None:
  45. builtins = []
  46. self.dirs = dirs
  47. self.app_dirs = app_dirs
  48. self.autoescape = autoescape
  49. self.context_processors = context_processors
  50. self.debug = debug
  51. self.loaders = loaders
  52. self.string_if_invalid = string_if_invalid
  53. self.file_charset = file_charset
  54. self.libraries = libraries
  55. self.template_libraries = self.get_template_libraries(libraries)
  56. self.builtins = self.default_builtins + builtins
  57. self.template_builtins = self.get_template_builtins(self.builtins)
  58. def __repr__(self):
  59. return (
  60. "<%s:%s app_dirs=%s%s debug=%s loaders=%s string_if_invalid=%s "
  61. "file_charset=%s%s%s autoescape=%s>"
  62. ) % (
  63. self.__class__.__qualname__,
  64. "" if not self.dirs else " dirs=%s" % repr(self.dirs),
  65. self.app_dirs,
  66. ""
  67. if not self.context_processors
  68. else " context_processors=%s" % repr(self.context_processors),
  69. self.debug,
  70. repr(self.loaders),
  71. repr(self.string_if_invalid),
  72. repr(self.file_charset),
  73. "" if not self.libraries else " libraries=%s" % repr(self.libraries),
  74. "" if not self.builtins else " builtins=%s" % repr(self.builtins),
  75. repr(self.autoescape),
  76. )
  77. @staticmethod
  78. @functools.lru_cache
  79. def get_default():
  80. """
  81. Return the first DjangoTemplates backend that's configured, or raise
  82. ImproperlyConfigured if none are configured.
  83. This is required for preserving historical APIs that rely on a
  84. globally available, implicitly configured engine such as:
  85. >>> from django.template import Context, Template
  86. >>> template = Template("Hello {{ name }}!")
  87. >>> context = Context({'name': "world"})
  88. >>> template.render(context)
  89. 'Hello world!'
  90. """
  91. # Since Engine is imported in django.template and since
  92. # DjangoTemplates is a wrapper around this Engine class,
  93. # local imports are required to avoid import loops.
  94. from django.template import engines
  95. from django.template.backends.django import DjangoTemplates
  96. for engine in engines.all():
  97. if isinstance(engine, DjangoTemplates):
  98. return engine.engine
  99. raise ImproperlyConfigured("No DjangoTemplates backend is configured.")
  100. @cached_property
  101. def template_context_processors(self):
  102. context_processors = _builtin_context_processors
  103. context_processors += tuple(self.context_processors)
  104. return tuple(import_string(path) for path in context_processors)
  105. def get_template_builtins(self, builtins):
  106. return [import_library(x) for x in builtins]
  107. def get_template_libraries(self, libraries):
  108. loaded = {}
  109. for name, path in libraries.items():
  110. loaded[name] = import_library(path)
  111. return loaded
  112. @cached_property
  113. def template_loaders(self):
  114. return self.get_template_loaders(self.loaders)
  115. def get_template_loaders(self, template_loaders):
  116. loaders = []
  117. for template_loader in template_loaders:
  118. loader = self.find_template_loader(template_loader)
  119. if loader is not None:
  120. loaders.append(loader)
  121. return loaders
  122. def find_template_loader(self, loader):
  123. if isinstance(loader, (tuple, list)):
  124. loader, *args = loader
  125. else:
  126. args = []
  127. if isinstance(loader, str):
  128. loader_class = import_string(loader)
  129. return loader_class(self, *args)
  130. else:
  131. raise ImproperlyConfigured(
  132. "Invalid value in template loaders configuration: %r" % loader
  133. )
  134. def find_template(self, name, dirs=None, skip=None):
  135. tried = []
  136. for loader in self.template_loaders:
  137. try:
  138. template = loader.get_template(name, skip=skip)
  139. return template, template.origin
  140. except TemplateDoesNotExist as e:
  141. tried.extend(e.tried)
  142. raise TemplateDoesNotExist(name, tried=tried)
  143. def from_string(self, template_code):
  144. """
  145. Return a compiled Template object for the given template code,
  146. handling template inheritance recursively.
  147. """
  148. return Template(template_code, engine=self)
  149. def get_template(self, template_name):
  150. """
  151. Return a compiled Template object for the given template name,
  152. handling template inheritance recursively.
  153. """
  154. template, origin = self.find_template(template_name)
  155. if not hasattr(template, "render"):
  156. # template needs to be compiled
  157. template = Template(template, origin, template_name, engine=self)
  158. return template
  159. def render_to_string(self, template_name, context=None):
  160. """
  161. Render the template specified by template_name with the given context.
  162. For use in Django's test suite.
  163. """
  164. if isinstance(template_name, (list, tuple)):
  165. t = self.select_template(template_name)
  166. else:
  167. t = self.get_template(template_name)
  168. # Django < 1.8 accepted a Context in `context` even though that's
  169. # unintended. Preserve this ability but don't rewrap `context`.
  170. if isinstance(context, Context):
  171. return t.render(context)
  172. else:
  173. return t.render(Context(context, autoescape=self.autoescape))
  174. def select_template(self, template_name_list):
  175. """
  176. Given a list of template names, return the first that can be loaded.
  177. """
  178. if not template_name_list:
  179. raise TemplateDoesNotExist("No template names provided")
  180. not_found = []
  181. for template_name in template_name_list:
  182. try:
  183. return self.get_template(template_name)
  184. except TemplateDoesNotExist as exc:
  185. if exc.args[0] not in not_found:
  186. not_found.append(exc.args[0])
  187. continue
  188. # If we get here, none of the templates could be loaded
  189. raise TemplateDoesNotExist(", ".join(not_found))