django-chameleon-templates / djchameleon / loaders /

Full commit
from chameleon.zpt.template import PageTemplateFile
from django.core.urlresolvers import reverse
from django.utils.importlib import import_module
from django.core.exceptions import ImproperlyConfigured
from django.conf import settings
from django.utils import six
import ast
import os
import sys

if not six.PY3:
    fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()

MY_ROOT = os.path.normpath(os.path.dirname(__file__))

def url_expression(string):
    def compiler(target, engine):
        the_url = reverse(string)
        value = ast.Str(the_url)
        return [ast.Assign(targets=[target], value=value)]
    return compiler

djangotags = PageTemplateFile(os.path.join(MY_ROOT, 'defaultmacros', ''))

app_macros_dirs = []
for app in settings.INSTALLED_APPS:
        mod = import_module(app)
    except ImportError as e:
        raise ImproperlyConfigured('ImportError %s: %s' % (app, e.args[0]))
    macro_dir = os.path.join(os.path.dirname(mod.__file__), 'macros')
    if os.path.isdir(macro_dir):
        if not six.PY3:
            macro_dir = macro_dir.decode(fs_encoding)

# It won't change, so convert it to a tuple to save memory.
app_macros_dirs = tuple(app_macros_dirs)

class Template(PageTemplateFile):
    expression_types = PageTemplateFile.expression_types.copy()
    expression_types['url'] = url_expression
    def gather_macros(self):
        macro_pt = {}
        for d in app_macros_dirs:
            for pt in os.listdir(d):
                fn = pt.split('.')
                if len(fn) > 1 and fn[-1] == 'pt':
                    macro_pt.update({fn[0]: PageTemplateFile(os.path.join(d, pt))})
        return macro_pt
    def render(self, context):
        context_dict = {'django': djangotags}
        for d in context.dicts:
        return super(Template, self).render(**context_dict)