def load_template(self, template_name, template_dirs=None):
        lang = translation.get_language() or 'en'
        key = '%s-%s' % (lang, template_name)

        if key not in self.template_cache:
            # Path in the cache directory
            output_path = os.path.join(self.__cache_dir, 'cache', lang, template_name)

            # Load template
            if os.path.exists(output_path):
                # Prefer precompiled version
                template = codecs.open(output_path, 'r', 'utf-8').read()
                origin = StringOrigin(template)
            else:
                template, origin = self.find_template(template_name, template_dirs)

            # Compile template
            output = compile_to_parse_tree(template, loader = lambda path: self.find_template(path)[0], path=template_name)

            # Compile to python
            output2 = compile_tree(output)
            template = Template(output2, template_name)

            # Turn into Template object
            #template = get_template_from_string(template, origin, template_name)

            # Save in cache
            self.template_cache[key] = template

        # Return result
        return self.template_cache[key], None
    def _compile_template(self, lang, input_path, output_path):
        try:
            # Open input file
            code = codecs.open(input_path, 'r', 'utf-8').read()

            # Compile
            output = compile_to_parse_tree(code, loader=load_template_source, path=input_path)
            output2 = compile_tree(output)

            # Open output file
            self._create_dir(os.path.split(output_path)[0])
            codecs.open(output_path, 'w', 'utf-8').write(output2)

        except CompileException, e:
            self.print_error(u'ERROR:  %s' % unicode(e))
Example #3
0
    def load_template(self, template_name, template_dirs=None):
        lang = translation.get_language() or 'en'
        key = '%s-%s' % (lang, template_name)

        if key not in self.template_cache:
            # Path in the cache directory
            output_path = os.path.join(self.__cache_dir, 'cache', lang,
                                       template_name)

            # Load template
            if os.path.exists(output_path):
                # Prefer precompiled version
                template = codecs.open(output_path, 'r', 'utf-8').read()
                origin = StringOrigin(template)
            else:
                template, origin = self.find_template(template_name,
                                                      template_dirs)

            # Compile template
            output = compile_to_parse_tree(
                template,
                loader=lambda path: self.find_template(path)[0],
                path=template_name)

            # Compile to python
            output2 = compile_tree(output)
            template = Template(output2, template_name)

            # Turn into Template object
            #template = get_template_from_string(template, origin, template_name)

            # Save in cache
            self.template_cache[key] = template

        # Return result
        return self.template_cache[key], None