def load_template(self, template_name, template_dirs=None):
        # IMPORTANT NOTE:  We load the template, using the original loaders.
        #                  call compile, but still return the original,
        #                  unmodified result.  This causes the django to call
        #                  load_template again for every include node where of
        #                  course the validation may fail. (incomplete HTML
        #                  tree, maybe only javascript, etc...)
        # Load template
        template, origin = self.find_template(template_name, template_dirs)

        # Compile template as a test (could raise CompileException), throw away the compiled result.
        try:
            # Don't compile template when a parent frame was a 'render' method. Than it's probably a
            # runtime call from an IncludeNode or ExtendsNode.
            import inspect
            if not any(i[3] in ('render', 'do_include') for i in inspect.getouterframes(inspect.currentframe())):
                # Precompile command
                print 'compiling %s' % template_name
                execute_precompile_command()

                compile(template, loader = lambda path: self.find_template(path)[0], path=template_name,
                            options=get_options_for_path(origin.name) + _OVERRIDE_OPTIONS_FOR_VALIDATION )

        except Exception, e:
            # Print exception on console
            print '---'
            print 'Template: %s' % template_name
            print e
            print '-'
            import traceback
            traceback.print_exc()
            print '---'
            raise e
    def _compile_template(self, lang, input_path, output_path, no_html=False):
        try:
            # Create output directory
            self._create_dir(os.path.split(output_path)[0])

            try:
                # Open input file
                code = codecs.open(input_path, 'r', 'utf-8').read()
            except UnicodeDecodeError, e:
                raise CompileException(0, 0, input_path, str(e))

            # Compile
            if no_html:
                output = compile(code, loader=load_template_source, path=input_path,
                            options=get_options_for_path(input_path) + ['no-html'])
            else:
                output = compile(code, loader=load_template_source, path=input_path,
                            options=get_options_for_path(input_path))

            # Open output file
            codecs.open(output_path, 'w', 'utf-8').write(output)

            # Delete -c-recompile file (mark for recompilation) if one such exist.
            if os.path.exists(output_path + '-c-recompile'):
                os.remove(output_path + '-c-recompile')

            return True
    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, 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
                template, context = compile(
                    template,
                    loader=lambda path: self.find_template(path)[0],
                    path=template_name,
                    options=get_options_for_path(origin.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 process_template(self, template, input_path):
        # TODO: when HTML processing fails, the 'context' attribute is not
        #       retreived and no translations are failed.  so, translate again
        #       without html. (But we need to process html, in order to find
        #       gettext() in javascript.)

        try:
            try:
                # Open input file
                code = codecs.open(input_path, 'r', 'utf-8').read()
            except UnicodeDecodeError, e:
                raise CompileException(0, 0, input_path, str(e))

            # Compile
            output, context = compile(code, path=input_path, loader=load_template_source,
                        options=get_options_for_path(input_path))

            for entry in context.gettext_entries:
                line = '#: %s:%s:%s' % (entry.path, entry.line, entry.column)

                if not entry.text in self.strings:
                    self.strings[entry.text] = set()

                self.strings[entry.text].add(line)

                if self.verbosity >= 2:
                    sys.stderr.write(line + '\n')
                    sys.stderr.write('msgid "%s"\n\n' % entry.text.replace('"', r'\"'))
    def compile_template(self, original_source, template_dirs=None):
        template_source, context = compile(
            original_source,
            loader=lambda path: self.find_template(path)[0],
            options=self.options,
            context_class=self.context_class)

        return template_source
    def compile_template(self, original_source, template_dirs=None):
        template_source, context = compile(
            original_source,
            loader = lambda path: self.find_template(path)[0],
            options=self.options,
            context_class=self.context_class
        )

        return template_source
    def load_template(self, template_name, template_dirs=None):
        template, origin = self.find_template(template_name, template_dirs)

        # Compile template (we shouldn't compile anything at runtime.)
        template = compile(template, loader = lambda path: self.find_template(path)[0], path=template_name,
                        options=get_options_for_path(origin.name))

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

        # Return result
        return template, None
    def load_template(self, template_name, template_dirs=None):
        # IMPORTANT NOTE:  We load the template, using the original loaders.
        #                  call compile, but still return the original,
        #                  unmodified result.  This causes the django to call
        #                  load_template again for every include node where of
        #                  course the validation may fail. (incomplete HTML
        #                  tree, maybe only javascript, etc...)
        # Load template
        template, origin = self.find_template(template_name, template_dirs)

        # Compile template as a test (could raise CompileException), throw away the compiled result.
        try:
            # Don't compile template when a parent frame was a 'render' method. Than it's probably a
            # runtime call from an IncludeNode or ExtendsNode.
            import inspect
            if not any(
                    i[3] in ('render', 'do_include')
                    for i in inspect.getouterframes(inspect.currentframe())):
                # Precompile command
                print 'compiling %s' % template_name
                execute_precompile_command()

                compile(template,
                        loader=lambda path: self.find_template(path)[0],
                        path=template_name,
                        options=get_options_for_path(origin.name) +
                        _OVERRIDE_OPTIONS_FOR_VALIDATION)

        except Exception, e:
            # Print exception on console
            print '---'
            print 'Template: %s' % template_name
            print e
            print '-'
            import traceback
            traceback.print_exc()
            print '---'
            raise e
    def load_template(self, template_name, template_dirs=None):
        template, origin = self.find_template(template_name, template_dirs)

        # Precompile command
        execute_precompile_command()

        # Compile template
        template, context = compile(template, path=template_name, loader = lambda path: self.find_template(path)[0],
                        options=get_options_for_path(origin.name) + self.options,
                        context_class=self.context_class)

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

        # Return result
        return template, None
    def load_template(self, template_name, template_dirs=None):
        template, origin = self.find_template(template_name, template_dirs)

        # Precompile command
        execute_precompile_command()
        print 'compiling %s' % template_name

        # Compile template
        template, context = compile(
            template,
            path=template_name,
            loader=lambda path: self.find_template(path)[0],
            options=get_options_for_path(origin.name) + self.options,
            context_class=self.context_class)

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

        # Return result
        return template, None
        try:
            # Create output directory
            self._create_dir(os.path.split(output_path)[0])

            try:
                # Open input file
                code = codecs.open(input_path, 'r', 'utf-8').read()
            except UnicodeDecodeError, e:
                raise CompileException(0, 0, input_path, str(e))
            except IOError, e:
                raise CompileException(0, 0, input_path, str(e))

            # Compile
            if no_html:
                output, context = compile(code, path=input_path, loader=load_template_source,
                            options=get_options_for_path(input_path) + ['no-html'],
                            context_class=self.NiceContext)
            else:
                output, context = compile(code, path=input_path, loader=load_template_source,
                            options=get_options_for_path(input_path),
                            context_class=self.NiceContext)

            # store dependencies
            self._save_template_dependencies(lang, template, context.template_dependencies)
            self._save_first_level_template_dependencies(lang, template, context.include_dependencies,
                                                                context.extends_dependencies)

            # Open output file
            codecs.open(output_path, 'w', 'utf-8').write(output)

            # Delete -c-recompile file (mark for recompilation) if one such exist.
示例#12
0
            # Create output directory
            self._create_dir(os.path.split(output_path)[0])

            try:
                # Open input file
                code = codecs.open(input_path, 'r', 'utf-8').read()
            except UnicodeDecodeError, e:
                raise CompileException(0, 0, input_path, str(e))
            except IOError, e:
                raise CompileException(0, 0, input_path, str(e))

            # Compile
            if no_html:
                output, context = compile(
                    code,
                    path=input_path,
                    loader=load_template_source,
                    options=get_options_for_path(input_path) + ['no-html'],
                    context_class=self.NiceContext)
            else:
                output, context = compile(
                    code,
                    path=input_path,
                    loader=load_template_source,
                    options=get_options_for_path(input_path),
                    context_class=self.NiceContext)

            # store dependencies
            self._save_template_dependencies(lang, template,
                                             context.template_dependencies)
            self._save_first_level_template_dependencies(
                lang, template, context.include_dependencies,
示例#13
0
 def test_load_url_from_future(self):
     compiled, context = compile('{% load url from future %}')
     compiled = compiled.strip()
     self.assertEqual(compiled, '{% load url from future%}')
 def test_load_url_from_future(self):
     compiled, context = compile("{% load url from future %}")
     compiled = compiled.strip()
     self.assertEqual(compiled, "{% load url from future%}")