Example #1
0
 def output(self, mode='file', forced=False):
     """
     The general output method, override in subclass if you need to do
     any custom modification. Calls other mode specific methods or simply
     returns the content directly.
     """
     # First check whether we should do the full compression,
     # including precompilation (or if it's forced)
     if settings.COMPRESS_ENABLED or forced:
         content = self.combined
     elif settings.COMPRESS_PRECOMPILERS:
         # or concatting it, if pre-compilation is enabled
         content = self.concat
     else:
         # or just doing nothing, when neither
         # compression nor compilation is enabled
         return self.content
     # Shortcurcuit in case the content is empty.
     if not content:
         return ''
     # Then check for the appropriate output method and call it
     output_func = getattr(self, "output_%s" % mode, None)
     if callable(output_func):
         return output_func(mode, content, forced)
     # Total failure, raise a general exception
     raise CompressorError("Couldn't find output method for mode '%s'" %
                           mode)
Example #2
0
 def handle_output(self, mode, content, forced, basename=None):
     # Then check for the appropriate output method and call it
     output_func = getattr(self, "output_%s" % mode, None)
     if callable(output_func):
         return output_func(mode, content, forced, basename)
     # Total failure, raise a general exception
     raise CompressorError("Couldn't find output method for mode '%s'" % mode)
Example #3
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
Example #4
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)
Example #5
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:
         command = self.all_mimetypes.get(mimetype)
         if 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:
             return True, CompilerFilter(content, filter_type=self.type,
                 command=command, filename=filename).input(**kwargs)
     return False, content