Ticket #6587: new_template_lib_loader_4.diff

File new_template_lib_loader_4.diff, 4.5 KB (added by oyvind, 17 years ago)

get_library no longer uses django.templatetags.module

  • django/template/__init__.py

    diff --git a/django/template/__init__.py b/django/template/__init__.py
    index 29daa76..ad0e6da 100644
    a b from django.utils.encoding import smart_unicode, force_unicode  
    5959from django.utils.translation import ugettext as _
    6060from django.utils.safestring import SafeData, EscapeData, mark_safe, mark_for_escaping
    6161from django.utils.html import escape
     62from django.templatetags import get_library_names
    6263
    6364__all__ = ('Template', 'Context', 'RequestContext', 'compile_string')
    6465
    class Library(object):  
    910911            return func
    911912        return dec
    912913
    913 def get_library(module_name):
    914     lib = libraries.get(module_name, None)
     914def get_library(library_name, full_path=False):
     915    lib = libraries.get(library_name, None)
    915916    if not lib:
     917        library_names = get_library_names()
    916918        try:
     919            module_name = full_path and library_name or library_names[library_name]
     920        except KeyError, e:
     921            raise InvalidTemplateLibrary("No such library %s, %s" % (library_name, e))
     922        try:
     923            module_name = full_path and library_name or library_names[library_name]
    917924            mod = __import__(module_name, {}, {}, [''])
    918925        except ImportError, e:
    919926            raise InvalidTemplateLibrary("Could not load template library from %s, %s" % (module_name, e))
    920927        try:
    921928            lib = mod.register
    922             libraries[module_name] = lib
     929            libraries[library_name] = lib
    923930        except AttributeError:
    924931            raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name)
    925932    return lib
    926933
    927934def add_to_builtins(module_name):
    928     builtins.append(get_library(module_name))
     935    builtins.append(get_library(module_name, full_path=True))
    929936
    930937add_to_builtins('django.template.defaulttags')
    931938add_to_builtins('django.template.defaultfilters')
  • django/template/defaulttags.py

    diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
    index e5a8e66..cd671b9 100644
    a b def load(parser, token):  
    850850    for taglib in bits[1:]:
    851851        # add the library to the parser
    852852        try:
    853             lib = get_library("django.templatetags.%s" % taglib)
     853            lib = get_library(taglib)
    854854            parser.add_library(lib)
    855855        except InvalidTemplateLibrary, e:
    856856            raise TemplateSyntaxError("'%s' is not a valid tag library: %s" %
  • django/templatetags/__init__.py

    diff --git a/django/templatetags/__init__.py b/django/templatetags/__init__.py
    index 9204535..f80c664 100644
    a b  
    11from django.conf import settings
     2import os
    23
    3 for a in settings.INSTALLED_APPS:
    4     try:
    5         __path__.extend(__import__(a + '.templatetags', {}, {}, ['']).__path__)
    6     except ImportError:
    7         pass
     4library_names = {}
     5
     6def get_library_names():
     7    if not library_names:
     8        library_names['i18n'] = 'django.templatetags.i18n'
     9        library_names['cache'] = 'django.templatetags.cache'
     10        for a in settings.INSTALLED_APPS:
     11            try:
     12                appname = a.find('.')!=-1 and a.split('.')[-1] or a
     13                name = a + '.templatetags'
     14                mod = __import__(name, {}, {}, [''])
     15                path = mod.__path__[0]
     16                for root, dirs, files in os.walk(path):
     17                    if root == path:
     18                        for file in files:
     19                            if not file.endswith('.pyc') and not file.startswith('__init__'):
     20                                library_name = os.path.splitext(file)[0]
     21                                module_path = '%s.%s' % (name, library_name)
     22                                if library_name not in library_names:
     23                                    library_names[library_name] = module_path
     24                                library_names[appname + '.' + library_name] = module_path
     25            except ImportError:
     26                pass
     27    return library_names
  • tests/regressiontests/templates/tests.py

    diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
    index aef6f50..0490f2a 100644
    a b def do_echo(parser, token):  
    4545
    4646register.tag("echo", do_echo)
    4747
    48 template.libraries['django.templatetags.testtags'] = register
     48template.libraries['testtags'] = register
    4949
    5050#####################################
    5151# Helper objects for template tests #
Back to Top