1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 | #---- 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))
|
Comments