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.

autoreload.py 1.8KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from pathlib import Path
  2. from django.dispatch import receiver
  3. from django.template import engines
  4. from django.template.backends.django import DjangoTemplates
  5. from django.utils._os import to_path
  6. from django.utils.autoreload import autoreload_started, file_changed, is_django_path
  7. def get_template_directories():
  8. # Iterate through each template backend and find
  9. # any template_loader that has a 'get_dirs' method.
  10. # Collect the directories, filtering out Django templates.
  11. cwd = Path.cwd()
  12. items = set()
  13. for backend in engines.all():
  14. if not isinstance(backend, DjangoTemplates):
  15. continue
  16. items.update(cwd / to_path(dir) for dir in backend.engine.dirs if dir)
  17. for loader in backend.engine.template_loaders:
  18. if not hasattr(loader, "get_dirs"):
  19. continue
  20. items.update(
  21. cwd / to_path(directory)
  22. for directory in loader.get_dirs()
  23. if directory and not is_django_path(directory)
  24. )
  25. return items
  26. def reset_loaders():
  27. for backend in engines.all():
  28. if not isinstance(backend, DjangoTemplates):
  29. continue
  30. for loader in backend.engine.template_loaders:
  31. loader.reset()
  32. @receiver(autoreload_started, dispatch_uid="template_loaders_watch_changes")
  33. def watch_for_template_changes(sender, **kwargs):
  34. for directory in get_template_directories():
  35. sender.watch_dir(directory, "**/*")
  36. @receiver(file_changed, dispatch_uid="template_loaders_file_changed")
  37. def template_changed(sender, file_path, **kwargs):
  38. if file_path.suffix == ".py":
  39. return
  40. for template_dir in get_template_directories():
  41. if template_dir in file_path.parents:
  42. reset_loaders()
  43. return True