summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2005-10-14 22:22:12 +0000
committerAdrian Holovaty <adrian@holovaty.com>2005-10-14 22:22:12 +0000
commit7aefff78335c45edb8dd66aeebadad934fd062ca (patch)
tree74fc9dda46b06cf686c394b395a829dae308344b
parent083b4f9001f3968138c4d0c34ab741cf7be97f43 (diff)
Fixed #582 -- Added support for loading templates from Python eggs, and a TEMPLATE_LOADERS setting, which defines which loaders to use. Thanks, Sune
git-svn-id: http://code.djangoproject.com/svn/django/trunk@870 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/conf/global_settings.py8
-rw-r--r--django/core/template/loader.py47
-rw-r--r--django/core/template/loaders/eggs.py25
-rw-r--r--django/core/template/loaders/filesystem.py1
4 files changed, 79 insertions, 2 deletions
diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py
index ea2fc440de..dde5612038 100644
--- a/django/conf/global_settings.py
+++ b/django/conf/global_settings.py
@@ -61,6 +61,14 @@ TEMPLATE_DIRS = ()
# Extension on all templates.
TEMPLATE_FILE_EXTENSION = '.html'
+# List of callables that know how to import templates from various sources.
+# See the comments in django/core/template/loader.py for interface
+# documentation.
+TEMPLATE_LOADERS = (
+ 'django.core.template.loaders.filesystem.load_template_source',
+# 'django.core.template.loaders.eggs.load_template_source',
+)
+
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
diff --git a/django/core/template/loader.py b/django/core/template/loader.py
index 65a6c735b3..067b3fcfb8 100644
--- a/django/core/template/loader.py
+++ b/django/core/template/loader.py
@@ -1,7 +1,50 @@
-"Wrapper for loading templates from storage of some sort (e.g. files or db)"
+# Wrapper for loading templates from storage of some sort (e.g. filesystem, database).
+#
+# This uses the TEMPLATE_LOADERS setting, which is a list of loaders to use.
+# Each loader is expected to have this interface:
+#
+# callable(name, dirs=[])
+#
+# name is the template name.
+# dirs is an optional list of directories to search instead of TEMPLATE_DIRS.
+#
+# Each loader should have an "is_usable" attribute set. This is a boolean that
+# specifies whether the loader can be used in this Python installation. Each
+# loader is responsible for setting this when it's initialized.
+#
+# For example, the eggs loader (which is capable of loading templates from
+# Python eggs) sets is_usable to False if the "pkg_resources" module isn't
+# installed, because pkg_resources is necessary to read eggs.
+from django.core.exceptions import ImproperlyConfigured
from django.core.template import Template, Context, Node, TemplateDoesNotExist, TemplateSyntaxError, resolve_variable_with_filters, register_tag
-from django.core.template.loaders.filesystem import load_template_source
+from django.conf.settings import TEMPLATE_LOADERS
+
+template_source_loaders = []
+for path in TEMPLATE_LOADERS:
+ i = path.rfind('.')
+ module, attr = path[:i], path[i+1:]
+ try:
+ mod = __import__(module, globals(), locals(), [attr])
+ except ImportError, e:
+ raise ImproperlyConfigured, 'Error importing template source loader %s: "%s"' % (module, e)
+ try:
+ func = getattr(mod, attr)
+ except AttributeError:
+ raise ImproperlyConfigured, 'Module "%s" does not define a "%s" callable template source loader' % (module, attr)
+ if not func.is_usable:
+ import warnings
+ warnings.warn("Your TEMPLATE_LOADERS setting includes %r, but your Python installation doesn't support that type of template loading. Consider removing that line from TEMPLATE_LOADERS." % path)
+ else:
+ template_source_loaders.append(func)
+
+def load_template_source(name, dirs=None):
+ for loader in template_source_loaders:
+ try:
+ return loader(name, dirs)
+ except template.TemplateDoesNotExist:
+ pass
+ raise template.TemplateDoesNotExist, name
class ExtendsError(Exception):
pass
diff --git a/django/core/template/loaders/eggs.py b/django/core/template/loaders/eggs.py
new file mode 100644
index 0000000000..b1e221ee1c
--- /dev/null
+++ b/django/core/template/loaders/eggs.py
@@ -0,0 +1,25 @@
+# Wrapper for loading templates from eggs via pkg_resources.resource_string.
+
+try:
+ from pkg_resources import resource_string
+except ImportError:
+ resource_string = None
+
+from django.core.template import TemplateDoesNotExist
+from django.conf.settings import INSTALLED_APPS, TEMPLATE_FILE_EXTENSION
+
+def load_template_source(name, dirs=None):
+ """
+ Loads templates from Python eggs via pkg_resource.resource_string.
+
+ For every installed app, it tries to get the resource (app, name).
+ """
+ if resource_string is not None:
+ pkg_name = 'templates/' + name + TEMPLATE_FILE_EXTENSION
+ for app in INSTALLED_APPS:
+ try:
+ return resource_string(app, pkg_name)
+ except:
+ pass
+ raise TemplateDoesNotExist, name
+load_template_source.is_usable = resource_string is not None
diff --git a/django/core/template/loaders/filesystem.py b/django/core/template/loaders/filesystem.py
index 849f62b94e..1d42c6d841 100644
--- a/django/core/template/loaders/filesystem.py
+++ b/django/core/template/loaders/filesystem.py
@@ -19,3 +19,4 @@ def load_template_source(template_name, template_dirs=None):
else:
error_msg = "Your TEMPLATE_DIRS settings is empty. Change it to point to at least one template directory."
raise TemplateDoesNotExist, error_msg
+load_template_source.is_usable = True