Exemplo n.º 1
0
 def precompile(self, content, kind=None, elem=None, filename=None, **kwargs):
     if not kind:
         return False, content
     attrs = self.parser.elem_attribs(elem)
     mimetype = attrs.get("type", None)
     if mimetype:
         filter_or_command = self.all_mimetypes.get(mimetype)
         if filter_or_command is None:
             if mimetype not in ("text/css", "text/javascript"):
                 raise CompressorError("Couldn't find any precompiler in "
                                       "COMPRESS_PRECOMPILERS setting for "
                                       "mimetype '%s'." % mimetype)
         else:
             mod_name, cls_name = get_mod_func(filter_or_command)
             try:
                 mod = import_module(mod_name)
             except ImportError:
                 return True, CompilerFilter(content, filter_type=self.type,
                         command=filter_or_command, filename=filename).input(
                             **kwargs)
             try:
                 precompiler_class = getattr(mod, cls_name)
             except AttributeError:
                 raise FilterDoesNotExist('Could not find "%s".' %
                         filter_or_command)
             else:
                 return True, precompiler_class(content, attrs,
                         filter_type=self.type, filename=filename).input(
                             **kwargs)
     return False, content
Exemplo n.º 2
0
    def precompile(
        self, content, kind=None, elem=None, filename=None, charset=None, **kwargs
    ):
        """
        Processes file using a pre compiler.

        This is the place where files like coffee script are processed.
        """
        if not kind:
            return False, content
        attrs = self.parser.elem_attribs(elem)
        mimetype = attrs.get("type", None)
        if mimetype is None:
            return False, content

        filter_or_command = self.precompiler_mimetypes.get(mimetype)
        if filter_or_command is None:
            if mimetype in ("text/css", "text/javascript"):
                return False, content
            raise CompressorError(
                "Couldn't find any precompiler in "
                "COMPRESS_PRECOMPILERS setting for "
                "mimetype '%s'." % mimetype
            )

        mod_name, cls_name = get_mod_func(filter_or_command)
        try:
            mod = import_module(mod_name)
        except (ImportError, TypeError):
            filter = CachedCompilerFilter(
                content=content,
                filter_type=self.type,
                filename=filename,
                charset=charset,
                command=filter_or_command,
                mimetype=mimetype,
            )
            return True, filter.input(**kwargs)
        try:
            precompiler_class = getattr(mod, cls_name)
        except AttributeError:
            raise FilterDoesNotExist('Could not find "%s".' % filter_or_command)
        filter = precompiler_class(
            content,
            attrs=attrs,
            filter_type=self.type,
            charset=charset,
            filename=filename,
        )
        return True, filter.input(**kwargs)