Exemple #1
0
    def compress(self, log=None, **options):
        """
        Searches templates containing 'compress' nodes and compresses them
        "offline" -- outside of the request/response cycle.

        The result is cached with a cache-key derived from the content of the
        compress nodes (not the content of the possibly linked files!).
        """
        extensions = options.get('extensions')
        extensions = self.handle_extensions(extensions or ['html'])
        verbosity = int(options.get("verbosity", 0))
        if not log:
            log = StringIO()
        if not settings.TEMPLATE_LOADERS:
            raise OfflineGenerationError("No template loaders defined. You "
                                         "must set TEMPLATE_LOADERS in your "
                                         "settings.")
        paths = set()
        for loader in self.get_loaders():
            try:
                module = import_module(loader.__module__)
                get_template_sources = getattr(module, 'get_template_sources',
                                               None)
                if get_template_sources is None:
                    get_template_sources = loader.get_template_sources
                paths.update(list(get_template_sources('')))
            except (ImportError, AttributeError):
                # Yeah, this didn't work out so well, let's move on
                pass
        if not paths:
            raise OfflineGenerationError("No template paths found. None of "
                                         "the configured template loaders "
                                         "provided template paths. See "
                                         "http://django.me/template-loaders "
                                         "for more information on template "
                                         "loaders.")
        if verbosity > 1:
            log.write("Considering paths:\n\t" + "\n\t".join(paths) + "\n")
        templates = set()
        for path in paths:
            for root, dirs, files in walk(path,
                                          followlinks=options.get(
                                              'followlinks', False)):
                templates.update(
                    os.path.join(root, name) for name in files
                    if not name.startswith('.') and any(
                        fnmatch(name, "*%s" % glob) for glob in extensions))
        if not templates:
            raise OfflineGenerationError("No templates found. Make sure your "
                                         "TEMPLATE_LOADERS and TEMPLATE_DIRS "
                                         "settings are correct.")
        if verbosity > 1:
            log.write("Found templates:\n\t" + "\n\t".join(templates) + "\n")

        compressor_nodes = SortedDict()
        for template_name in templates:
            try:
                template_file = open(template_name)
                try:
                    template = Template(template_file.read().decode(
                        settings.FILE_CHARSET))
                finally:
                    template_file.close()
            except IOError:  # unreadable file -> ignore
                if verbosity > 0:
                    log.write("Unreadable template at: %s\n" % template_name)
                continue
            except TemplateSyntaxError:  # broken template -> try jinja -> ignore if still broken
                try:
                    template_file = open(template_name)
                    template = jinja_env.parse(template_file.read().decode(
                        settings.FILE_CHARSET))
                    template.is_jinja = True
                    template.name = template_name
                except jinja2.exceptions.TemplateSyntaxError:
                    if verbosity > 0:
                        log.write("Invalid template at: %s\n" % template_name)
                    continue
                finally:
                    template_file.close()
            except UnicodeDecodeError:
                if verbosity > 0:
                    log.write("UnicodeDecodeError while trying to read "
                              "template %s\n" % template_name)
            if getattr(template, 'is_jinja', False):
                nodes = template.find_all(jinja2.nodes.CallBlock)
                for node in nodes:
                    try:
                        compress_node = node.call.node
                        if (compress_node.identifier ==
                                'compressor.contrib.jinja2ext.CompressorExtension'
                                and compress_node.name == '_compress'):
                            template.template_name = template_name
                            compressor_nodes.setdefault(template,
                                                        []).append(node)
                    except AttributeError, IndexError:
                        pass
            else:
                nodes = list(self.walk_nodes(template))
                if nodes:
                    template.template_name = template_name
                    compressor_nodes.setdefault(template, []).extend(nodes)
    def compress(self, log=None, **options):
        """
        Searches templates containing 'compress' nodes and compresses them
        "offline" -- outside of the request/response cycle.

        The result is cached with a cache-key derived from the content of the
        compress nodes (not the content of the possibly linked files!).
        """
        extensions = options.get("extensions")
        extensions = self.handle_extensions(extensions or ["html"])
        verbosity = int(options.get("verbosity", 0))
        if not log:
            log = StringIO()
        if not settings.TEMPLATE_LOADERS:
            raise OfflineGenerationError(
                "No template loaders defined. You " "must set TEMPLATE_LOADERS in your " "settings."
            )
        paths = set()
        for loader in self.get_loaders():
            try:
                module = import_module(loader.__module__)
                get_template_sources = getattr(module, "get_template_sources", None)
                if get_template_sources is None:
                    get_template_sources = loader.get_template_sources
                paths.update(list(get_template_sources("")))
            except (ImportError, AttributeError):
                # Yeah, this didn't work out so well, let's move on
                pass
        if not paths:
            raise OfflineGenerationError(
                "No template paths found. None of "
                "the configured template loaders "
                "provided template paths. See "
                "http://django.me/template-loaders "
                "for more information on template "
                "loaders."
            )
        if verbosity > 1:
            log.write("Considering paths:\n\t" + "\n\t".join(paths) + "\n")
        templates = set()
        for path in paths:
            for root, dirs, files in walk(path, followlinks=options.get("followlinks", False)):
                templates.update(
                    os.path.join(root, name)
                    for name in files
                    if not name.startswith(".") and any(fnmatch(name, "*%s" % glob) for glob in extensions)
                )
        if not templates:
            raise OfflineGenerationError(
                "No templates found. Make sure your " "TEMPLATE_LOADERS and TEMPLATE_DIRS " "settings are correct."
            )
        if verbosity > 1:
            log.write("Found templates:\n\t" + "\n\t".join(templates) + "\n")

        compressor_nodes = SortedDict()
        for template_name in templates:
            try:
                template_file = open(template_name)
                try:
                    template = Template(template_file.read().decode(settings.FILE_CHARSET))
                finally:
                    template_file.close()
            except IOError:  # unreadable file -> ignore
                if verbosity > 0:
                    log.write("Unreadable template at: %s\n" % template_name)
                continue
            except TemplateSyntaxError:  # broken template -> try jinja -> ignore if still broken
                try:
                    template_file = open(template_name)
                    template = jinja_env.parse(template_file.read().decode(settings.FILE_CHARSET))
                    template.is_jinja = True
                    template.name = template_name
                except jinja2.exceptions.TemplateSyntaxError:
                    if verbosity > 0:
                        log.write("Invalid template at: %s\n" % template_name)
                    continue
                finally:
                    template_file.close()
            except UnicodeDecodeError:
                if verbosity > 0:
                    log.write("UnicodeDecodeError while trying to read " "template %s\n" % template_name)
            if getattr(template, "is_jinja", False):
                nodes = template.find_all(jinja2.nodes.CallBlock)
                for node in nodes:
                    try:
                        compress_node = node.call.node
                        if (
                            compress_node.identifier == "compressor.contrib.jinja2ext.CompressorExtension"
                            and compress_node.name == "_compress"
                        ):
                            template.template_name = template_name
                            compressor_nodes.setdefault(template, []).append(node)
                    except AttributeError, IndexError:
                        pass
            else:
                nodes = list(self.walk_nodes(template))
                if nodes:
                    template.template_name = template_name
                    compressor_nodes.setdefault(template, []).extend(nodes)