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.

1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from django.core.exceptions import ImproperlyConfigured, SuspiciousFileOperation
  2. from django.template.utils import get_app_template_dirs
  3. from django.utils._os import safe_join
  4. from django.utils.functional import cached_property
  5. class BaseEngine:
  6. # Core methods: engines have to provide their own implementation
  7. # (except for from_string which is optional).
  8. def __init__(self, params):
  9. """
  10. Initialize the template engine.
  11. `params` is a dict of configuration settings.
  12. """
  13. params = params.copy()
  14. self.name = params.pop("NAME")
  15. self.dirs = list(params.pop("DIRS"))
  16. self.app_dirs = params.pop("APP_DIRS")
  17. if params:
  18. raise ImproperlyConfigured(
  19. "Unknown parameters: {}".format(", ".join(params))
  20. )
  21. @property
  22. def app_dirname(self):
  23. raise ImproperlyConfigured(
  24. "{} doesn't support loading templates from installed "
  25. "applications.".format(self.__class__.__name__)
  26. )
  27. def from_string(self, template_code):
  28. """
  29. Create and return a template for the given source code.
  30. This method is optional.
  31. """
  32. raise NotImplementedError(
  33. "subclasses of BaseEngine should provide a from_string() method"
  34. )
  35. def get_template(self, template_name):
  36. """
  37. Load and return a template for the given name.
  38. Raise TemplateDoesNotExist if no such template exists.
  39. """
  40. raise NotImplementedError(
  41. "subclasses of BaseEngine must provide a get_template() method"
  42. )
  43. # Utility methods: they are provided to minimize code duplication and
  44. # security issues in third-party backends.
  45. @cached_property
  46. def template_dirs(self):
  47. """
  48. Return a list of directories to search for templates.
  49. """
  50. # Immutable return value because it's cached and shared by callers.
  51. template_dirs = tuple(self.dirs)
  52. if self.app_dirs:
  53. template_dirs += get_app_template_dirs(self.app_dirname)
  54. return template_dirs
  55. def iter_template_filenames(self, template_name):
  56. """
  57. Iterate over candidate files for template_name.
  58. Ignore files that don't lie inside configured template dirs to avoid
  59. directory traversal attacks.
  60. """
  61. for template_dir in self.template_dirs:
  62. try:
  63. yield safe_join(template_dir, template_name)
  64. except SuspiciousFileOperation:
  65. # The joined path was located outside of this template_dir
  66. # (it might be inside another one, so this isn't fatal).
  67. pass