def _reset_templatetags_cache(self): """Clears the Django templatetags_modules cache.""" # We'll import templatetags_modules here because # we want the most recent copy of templatetags_modules from django.template.base import (get_templatetags_modules, templatetags_modules) # Wipe out the contents del(templatetags_modules[:]) # And reload the cache get_templatetags_modules()
def _reset_templatetags_cache(self): """Clears the Django templatetags_modules cache.""" # We'll import templatetags_modules here because # we want the most recent copy of templatetags_modules from django.template.base import get_templatetags_modules, \ templatetags_modules # Wipe out the contents del (templatetags_modules[:]) # And reload the cache get_templatetags_modules()
def get_templateinfo(): modules = get_templatetags_modules() template_info = {} for module in modules: try: templatetag_mod = __import__(module, {}, {}, ['']) except Exception as err: logger.debug("Can't import %r: %s" % (module, err)) continue mod_path = os.path.dirname(inspect.getabsfile(templatetag_mod)) tag_files = [ os.path.splitext(fn)[0] for fn in os.listdir(mod_path) if fnmatch.fnmatch(fn, "*.py") and not fn.startswith("_") ] lib_info = {} for taglib in tag_files: try: lib = get_library(taglib) except: continue lib_info[taglib] = { "tags": collect_docs(lib.tags), "filters": collect_docs(lib.filters), } template_info[module.split(".")[0]] = lib_info return template_info
def _reset_templatetags_cache(self): """Clears the Django templatetags_modules cache.""" # We'll import templatetags_modules here because # we want the most recent copy of templatetags_modules from django.template.base import get_templatetags_modules # Wipe out the contents. if hasattr(get_templatetags_modules, 'cache_clear'): # Django >= 1.7 get_templatetags_modules.cache_clear() else: # Django < 1.7 from django.template.base import templatetags_modules del (templatetags_modules[:]) # And reload the cache get_templatetags_modules()
def _reset_templatetags_cache(self): """Clears the Django templatetags_modules cache.""" # We'll import templatetags_modules here because # we want the most recent copy of templatetags_modules from django.template.base import get_templatetags_modules # Wipe out the contents. if hasattr(get_templatetags_modules, 'cache_clear'): # Django >= 1.7 get_templatetags_modules.cache_clear() else: # Django < 1.7 from django.template.base import templatetags_modules del(templatetags_modules[:]) # And reload the cache get_templatetags_modules()
def _check_state(enabled): if enabled: if get_templatetags_modules: self.assertIn(templatetags_module, get_templatetags_modules()) self.assertEqual( Template(template_str).render(Context({})), 'Hello, world!') else: if get_templatetags_modules: self.assertNotIn(templatetags_module, get_templatetags_modules()) with self.assertRaisesRegexp(TemplateSyntaxError, 'is not a (valid|registered) tag ' 'library'): Template(template_str).render(Context({}))
def _check_state(enabled): if enabled: if get_templatetags_modules is not None: self.assertIn(templatetags_module_name, get_templatetags_modules()) self.assertEqual( Template(template_str).render(Context({})), 'Hello, world!') else: if get_templatetags_modules is not None: self.assertNotIn(templatetags_module_name, get_templatetags_modules()) with self.assertRaisesRegexp(TemplateSyntaxError, 'is not a (valid|registered) tag ' 'library'): Template(template_str).render(Context({}))
def get_installed_libraries(): for module in tplbase.get_templatetags_modules(): try: pkg = tplbase.import_module(module) except: pass for entry in walk_packages(pkg.__path__, pkg.__name__ + '.'): try: module = tplbase.import_module(entry[1]) if hasattr(module, 'register'): yield module.register except (ImproperlyConfigured, ImportError): pass
def get_installed_libraries(): for module in tplbase.get_templatetags_modules(): try: pkg = tplbase.import_module(module) except: pass for entry in walk_packages(pkg.__path__, pkg.__name__ + '.'): try: module = tplbase.import_module(entry[1]) if hasattr(module, 'register'): yield module.register except (ImproperlyConfigured, ImportError): pass
def get_library(library_name, app_name=None): """ (Forked from django.template.get_library) Load the template library module with the given name. If library is not already loaded loop over all templatetags modules to locate it. {% load somelib %} and {% load someotherlib %} loops twice. """ #TODO: add in caching. (removed when forked from django.template.get_library). templatetags_modules = get_templatetags_modules() tried_modules = [] best_match_lib = None last_found_lib = None app_name_parts = 0 if app_name: app_name_parts = app_name.count('.') for module in templatetags_modules: taglib_module = '%s.%s' % (module, library_name) tried_modules.append(taglib_module) lib = import_library(taglib_module) if not lib: continue last_found_lib = lib if not app_name: continue module_list = module.split('.') module_list.pop() # remove the last part 'templetags' current_app = '.'.join(module_list) if current_app == app_name: break start = len(module_list) - app_name_parts - 1 if start < 0: continue partial_app = '.'.join(module_list[start:]) if partial_app == app_name: best_match_lib = lib if best_match_lib: last_found_lib = best_match_lib if not last_found_lib: raise InvalidTemplateLibrary("Template library %s not found, tried %s" % (library_name, ','.join(tried_modules))) return last_found_lib
def load_all_installed_template_libraries(): # Load/register all template tag libraries from installed apps. for module_name in get_templatetags_modules(): mod = import_module(module_name) if not hasattr(mod, '__file__'): # e.g. packages installed as eggs continue try: libraries = [ os.path.splitext(p)[0] for p in os.listdir(os.path.dirname(upath(mod.__file__))) if p.endswith('.py') and p[0].isalpha() ] except OSError: continue else: for library_name in libraries: try: get_library(library_name) except InvalidTemplateLibrary: pass
def load_all_installed_template_libraries(): # Load/register all template tag libraries from installed apps. for module_name in get_templatetags_modules(): mod = import_module(module_name) if not hasattr(mod, '__file__'): # e.g. packages installed as eggs continue try: libraries = [ os.path.splitext(p)[0] for p in os.listdir(os.path.dirname(upath(mod.__file__))) if p.endswith('.py') and p[0].isalpha() ] except OSError: continue else: for library_name in libraries: try: get_library(library_name) except InvalidTemplateLibrary: pass
def get_library(library_name): """ An extension to Django's django.template.base.get_library, which allows tags to be loaded from specific apps using the 'app:taglib' syntax. So if in 'app1' you had templatetags/foo.py, you could load it as: {% import app1:foo %} or if you wanted to load a specific tag: {% import mytag from app1:foo %} This uses the same syntax as referencing URL patterns from specific apps, and does not clash with the '.' notation already used for loading modules within templatetags (e.g. templatetags/news/posts.py as news.posts). Without this functionality, Django template tags become a global namespace issue, where different apps can easily clash with one another. """ # Extract the specific information regarding the app and module from # the library name, and calculate the fully qualified name. app_name = "" tagparts = library_name.split(":") if len(tagparts) > 1: app_name = tagparts[0] library_name = tagparts[1] qualified_library_name = "%s:%s" % (app_name, library_name) else: library_name = tagparts[0] qualified_library_name = library_name # See if it exists in the cache lib = libraries.get(qualified_library_name, None) # If it isn't, we're going to look. Even though we know which app we want # to load it from, we're going to loop over the all modules to avoid # introducing significant new code, and since the result is going to be # cached. if not lib: templatetags_modules = get_templatetags_modules() tried_modules = [] for module in templatetags_modules: taglib_module = '%s.%s' % (module, library_name) tried_modules.append(taglib_module) lib = import_library(taglib_module) if lib: # We managed to load a library, but now we need to see if it is # from the right app. We can do that by finding out which app # it came from. if app_name: lib_app = taglib_module[:taglib_module.index('.')] lib_name = "%s:%s" % (lib_app, library_name) else: lib_name = library_name # OK, is it the right one? If so, store it in the cache if lib_name == qualified_library_name: libraries[lib_name] = lib break # Haven't found it yet, keep going. lib = None # If we don't find any, we throw an exception with the ones we've tried. if not lib: raise InvalidTemplateLibrary("Template library %s not found, " "tried %s" % (library_name, ','.join(tried_modules))) return lib
def get_library(library_name): """ An extension to Django's django.template.base.get_library, which allows tags to be loaded from specific apps using the 'app:taglib' syntax. So if in 'app1' you had templatetags/foo.py, you could load it as: {% import app1:foo %} or if you wanted to load a specific tag: {% import mytag from app1:foo %} This uses the same syntax as referencing URL patterns from specific apps, and does not clash with the '.' notation already used for loading modules within templatetags (e.g. templatetags/news/posts.py as news.posts). Without this functionality, Django template tags become a global namespace issue, where different apps can easily clash with one another. """ # Extract the specific information regarding the app and module from # the library name, and calculate the fully qualified name. app_name = "" tagparts = library_name.split(":") if len(tagparts) > 1: app_name = tagparts[0] library_name = tagparts[1] qualified_library_name = "%s:%s" % (app_name, library_name) else: library_name = tagparts[0] qualified_library_name = library_name # See if it exists in the cache lib = libraries.get(qualified_library_name, None) # If it isn't, we're going to look. Even though we know which app we want # to load it from, we're going to loop over the all modules to avoid # introducing significant new code, and since the result is going to be # cached. if not lib: templatetags_modules = get_templatetags_modules() tried_modules = [] for module in templatetags_modules: taglib_module = '%s.%s' % (module, library_name) tried_modules.append(taglib_module) lib = import_library(taglib_module) if lib: # We managed to load a library, but now we need to see if it is # from the right app. We can do that by finding out which app # it came from. if app_name: lib_app = taglib_module[:taglib_module.index('.')] lib_name = "%s:%s" % (lib_app, library_name) else: lib_name = library_name # OK, is it the right one? If so, store it in the cache if lib_name == qualified_library_name: libraries[lib_name] = lib break # Haven't found it yet, keep going. lib = None # If we don't find any, we throw an exception with the ones we've tried. if not lib: raise InvalidTemplateLibrary( "Template library %s not found, " "tried %s" % (library_name, ','.join(tried_modules))) return lib