Ticket #6587: new_template_lib_loader_9.diff

File new_template_lib_loader_9.diff, 4.5 KB (added by oyvind, 16 years ago)

removed os import

  • django/template/__init__.py

    diff --git a/django/template/__init__.py b/django/template/__init__.py
    index 5c4ab30..1d61387 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_templatetags_modules
    6263
    6364__all__ = ('Template', 'Context', 'RequestContext', 'compile_string')
    6465
    class Library(object):  
    913914            return func
    914915        return dec
    915916
    916 def get_library(module_name):
    917     lib = libraries.get(module_name, None)
     917def import_library(module_name):
     918    try:
     919        mod = __import__(module_name, {}, {}, [''])
     920    except ImportError:
     921        return None
     922    try:
     923        return mod.register
     924    except AttributeError:
     925        raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name)
     926
     927def get_library(library_name):
     928    lib = libraries.get(library_name, None)
    918929    if not lib:
    919         try:
    920             mod = __import__(module_name, {}, {}, [''])
    921         except ImportError, e:
    922             raise InvalidTemplateLibrary("Could not load template library from %s, %s" % (module_name, e))
    923         try:
    924             lib = mod.register
    925             libraries[module_name] = lib
    926         except AttributeError:
    927             raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name)
     930
     931        """
     932        If library is not already loaded loop over all templatetags modules to locate it.
     933
     934        {% load somelib %} and {% load someotherlib %} loops twice.
     935
     936        Subsequent loads eg. {% load somelib %} in the same thread will grab the cached
     937        module from libraries.
     938        """
     939        templatetags_modules = get_templatetags_modules()
     940        tried_modules = []
     941        for module in templatetags_modules:
     942            taglib_module = '%s.%s' % (module, library_name)
     943            tried_modules.append(taglib_module)
     944            lib = import_library(taglib_module)
     945            if lib:
     946                libraries[library_name] = lib
     947                break
     948        if not lib:
     949            raise InvalidTemplateLibrary("Template library %s not found, tried %s" % (library_name, ','.join(tried_modules)))
    928950    return lib
    929951
    930952def add_to_builtins(module_name):
    931     builtins.append(get_library(module_name))
     953    builtins.append(import_library(module_name))
    932954
    933955add_to_builtins('django.template.defaulttags')
    934956add_to_builtins('django.template.defaultfilters')
  • django/template/defaulttags.py

    diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
    index cf3b35b..665c30a 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..93a97b4 100644
    a b  
    11from django.conf import settings
    22
    3 for a in settings.INSTALLED_APPS:
    4     try:
    5         __path__.extend(__import__(a + '.templatetags', {}, {}, ['']).__path__)
    6     except ImportError:
    7         pass
     3templatetags_modules= []
     4
     5def get_templatetags_modules():
     6    if not templatetags_modules:
     7        """ Populate list once per thread. """
     8        for app_module in ['django'] + list(settings.INSTALLED_APPS):
     9            try:
     10                name = '%s.templatetags' % app_module
     11                mod = __import__(name, {}, {}, [''])
     12                templatetags_modules.append(name)
     13            except ImportError:
     14                pass
     15    return templatetags_modules
  • tests/regressiontests/templates/tests.py

    diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
    index 4effea5..dc61a8a 100644
    a b def do_echo(parser, token):  
    4848
    4949register.tag("echo", do_echo)
    5050
    51 template.libraries['django.templatetags.testtags'] = register
     51template.libraries['testtags'] = register
    5252
    5353#####################################
    5454# Helper objects for template tests #
Back to Top