#---- addons/template_loader.py ---- """ Wrapper for loading templates from "template" directories in arbitrary modules. """ import os from django.conf import settings from django.template import TemplateDoesNotExist from addons import get_module_dir def get_template_sources(template_name, template_dirs=None): delimiter = ':' if not delimiter in template_name: return [] # Find the delimiter in the name and split by it i = template_name.find(delimiter) app, template_path = template_name[:i], template_name[i+1:] # Get the directory for the app and make sure that is indeed a directory app_dir = get_module_dir(app) parts = (app_dir, 'templates', template_path) # Normalize the path template_path = '/'.join(parts) # Return it as a list for iteration return [os.path.normpath(template_path)] def load_template_source(template_name, template_dirs=None): for filepath in get_template_sources(template_name, template_dirs): try: return (open(filepath).read().decode(settings.FILE_CHARSET), filepath) except IOError: pass raise TemplateDoesNotExist, template_name load_template_source.is_usable = True #---- addons/util.py ---- import inspect from os import path def import_module(module_name): module = __import__(module_name, globals(), locals(), [], -1) components = module_name.split('.') for component in components[1:]: module = getattr(module, component) return module def get_module_path(module_name): module = import_module(module_name) return inspect.getsourcefile(module) def get_module_dir(module_name): return path.dirname(get_module_path(module_name))