summaryrefslogtreecommitdiff
path: root/django/core/template
diff options
context:
space:
mode:
authorGeorg Bauer <gb@hugo.westfalen.de>2005-10-14 23:32:35 +0000
committerGeorg Bauer <gb@hugo.westfalen.de>2005-10-14 23:32:35 +0000
commitcde24debb7c91b7ace343f215e0cb8585470fdaa (patch)
treef9989efcedb5d2982f8cf19f25be764637264835 /django/core/template
parent2a2e2fb6aa7f46a7ada2a320c775a315045f8b83 (diff)
parent67e6252a1ecd6587693b2756bf4bf27c0b89c3e6 (diff)
i18n: merged to [873] from trunk
git-svn-id: http://code.djangoproject.com/svn/django/branches/i18n@874 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/core/template')
-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
3 files changed, 71 insertions, 2 deletions
diff --git a/django/core/template/loader.py b/django/core/template/loader.py
index 65a6c735b3..20df897d71 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 TemplateDoesNotExist:
+ pass
+ raise 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