Code

Ticket #6587: new_template_lib_loader_3.diff

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

Do not walk subdirs in os.walk

Line 
1diff --git a/django/template/__init__.py b/django/template/__init__.py
2index 29daa76..ad0e6da 100644
3--- a/django/template/__init__.py
4+++ b/django/template/__init__.py
5@@ -59,6 +59,7 @@ from django.utils.encoding import smart_unicode, force_unicode
6 from django.utils.translation import ugettext as _
7 from django.utils.safestring import SafeData, EscapeData, mark_safe, mark_for_escaping
8 from django.utils.html import escape
9+from django.templatetags import get_library_names
10 
11 __all__ = ('Template', 'Context', 'RequestContext', 'compile_string')
12 
13@@ -910,22 +911,28 @@ class Library(object):
14             return func
15         return dec
16 
17-def get_library(module_name):
18-    lib = libraries.get(module_name, None)
19+def get_library(library_name, full_path=False):
20+    lib = libraries.get(library_name, None)
21     if not lib:
22+        library_names = get_library_names()
23         try:
24+            module_name = full_path and library_name or library_names[library_name]
25+        except KeyError, e:
26+            raise InvalidTemplateLibrary("No such library %s, %s" % (library_name, e))
27+        try:
28+            module_name = full_path and library_name or library_names[library_name]
29             mod = __import__(module_name, {}, {}, [''])
30         except ImportError, e:
31             raise InvalidTemplateLibrary("Could not load template library from %s, %s" % (module_name, e))
32         try:
33             lib = mod.register
34-            libraries[module_name] = lib
35+            libraries[library_name] = lib
36         except AttributeError:
37             raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name)
38     return lib
39 
40 def add_to_builtins(module_name):
41-    builtins.append(get_library(module_name))
42+    builtins.append(get_library(module_name, full_path=True))
43 
44 add_to_builtins('django.template.defaulttags')
45 add_to_builtins('django.template.defaultfilters')
46diff --git a/django/templatetags/__init__.py b/django/templatetags/__init__.py
47index 9204535..f80c664 100644
48--- a/django/templatetags/__init__.py
49+++ b/django/templatetags/__init__.py
50@@ -1,7 +1,27 @@
51 from django.conf import settings
52+import os
53 
54-for a in settings.INSTALLED_APPS:
55-    try:
56-        __path__.extend(__import__(a + '.templatetags', {}, {}, ['']).__path__)
57-    except ImportError:
58-        pass
59+library_names = {}
60+
61+def get_library_names():
62+    if not library_names:
63+        library_names['i18n'] = 'django.templatetags.i18n'
64+        library_names['cache'] = 'django.templatetags.cache'
65+        for a in settings.INSTALLED_APPS:
66+            try:
67+                appname = a.find('.')!=-1 and a.split('.')[-1] or a
68+                name = a + '.templatetags'
69+                mod = __import__(name, {}, {}, [''])
70+                path = mod.__path__[0]
71+                for root, dirs, files in os.walk(path):
72+                    if root == path:
73+                        for file in files:
74+                            if not file.endswith('.pyc') and not file.startswith('__init__'):
75+                                library_name = os.path.splitext(file)[0]
76+                                module_path = '%s.%s' % (name, library_name)
77+                                if library_name not in library_names:
78+                                    library_names[library_name] = module_path
79+                                library_names[appname + '.' + library_name] = module_path
80+            except ImportError:
81+                pass
82+    return library_names