PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/google/appengine/_internal/django/template/loaders/app_directories.py

https://github.com/theosp/google_appengine
Python | 74 lines | 73 code | 0 blank | 1 comment | 0 complexity | adb594f5b61c1389c77126d3f3d0a0a4 MD5 | raw file
  1. """
  2. Wrapper for loading templates from "templates" directories in INSTALLED_APPS
  3. packages.
  4. """
  5. import os
  6. import sys
  7. from google.appengine._internal.django.conf import settings
  8. from google.appengine._internal.django.core.exceptions import ImproperlyConfigured
  9. from google.appengine._internal.django.template import TemplateDoesNotExist
  10. from google.appengine._internal.django.template.loader import BaseLoader
  11. from google.appengine._internal.django.utils._os import safe_join
  12. from google.appengine._internal.django.utils.importlib import import_module
  13. # At compile time, cache the directories to search.
  14. fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
  15. app_template_dirs = []
  16. for app in settings.INSTALLED_APPS:
  17. try:
  18. mod = import_module(app)
  19. except ImportError, e:
  20. raise ImproperlyConfigured('ImportError %s: %s' % (app, e.args[0]))
  21. template_dir = os.path.join(os.path.dirname(mod.__file__), 'templates')
  22. if os.path.isdir(template_dir):
  23. app_template_dirs.append(template_dir.decode(fs_encoding))
  24. # It won't change, so convert it to a tuple to save memory.
  25. app_template_dirs = tuple(app_template_dirs)
  26. class Loader(BaseLoader):
  27. is_usable = True
  28. def get_template_sources(self, template_name, template_dirs=None):
  29. """
  30. Returns the absolute paths to "template_name", when appended to each
  31. directory in "template_dirs". Any paths that don't lie inside one of the
  32. template dirs are excluded from the result set, for security reasons.
  33. """
  34. if not template_dirs:
  35. template_dirs = app_template_dirs
  36. for template_dir in template_dirs:
  37. try:
  38. yield safe_join(template_dir, template_name)
  39. except UnicodeDecodeError:
  40. # The template dir name was a bytestring that wasn't valid UTF-8.
  41. raise
  42. except ValueError:
  43. # The joined path was located outside of template_dir.
  44. pass
  45. def load_template_source(self, template_name, template_dirs=None):
  46. for filepath in self.get_template_sources(template_name, template_dirs):
  47. try:
  48. file = open(filepath)
  49. try:
  50. return (file.read().decode(settings.FILE_CHARSET), filepath)
  51. finally:
  52. file.close()
  53. except IOError:
  54. pass
  55. raise TemplateDoesNotExist(template_name)
  56. _loader = Loader()
  57. def load_template_source(template_name, template_dirs=None):
  58. # For backwards compatibility
  59. import warnings
  60. warnings.warn(
  61. "'django.template.loaders.app_directories.load_template_source' is deprecated; use 'django.template.loaders.app_directories.Loader' instead.",
  62. PendingDeprecationWarning
  63. )
  64. return _loader.load_template_source(template_name, template_dirs)
  65. load_template_source.is_usable = True