Ticket #6587: new_template_lib_loader_3.diff

File new_template_lib_loader_3.diff, 3.4 KB (added by oyvind, 16 years ago)

Do not walk subdirs in os.walk

  • 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/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
Back to Top