diff options
author | Mathieu Pillard <diox@users.noreply.github.com> | 2022-10-28 19:01:54 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-28 19:01:54 +0200 |
commit | c9b75865c79b8008765b417147a9a6012e50de1c (patch) | |
tree | d2962abaff4df318c6ef92639c571d4146354029 /compressor/filters | |
parent | f99346cf86be43f89e8f42ddf50dc6a01bcf48f1 (diff) | |
download | django-compressor-c9b75865c79b8008765b417147a9a6012e50de1c.tar.gz |
Reformat with black (#1151)
* Reformat with black
* Update flake8 ignores
Diffstat (limited to 'compressor/filters')
-rw-r--r-- | compressor/filters/__init__.py | 9 | ||||
-rw-r--r-- | compressor/filters/base.py | 75 | ||||
-rw-r--r-- | compressor/filters/css_default.py | 62 | ||||
-rw-r--r-- | compressor/filters/cssmin/__init__.py | 5 | ||||
-rw-r--r-- | compressor/filters/datauri.py | 21 | ||||
-rw-r--r-- | compressor/filters/jsmin/__init__.py | 6 | ||||
-rw-r--r-- | compressor/filters/template.py | 1 | ||||
-rw-r--r-- | compressor/filters/yuglify.py | 6 | ||||
-rw-r--r-- | compressor/filters/yui.py | 8 |
9 files changed, 114 insertions, 79 deletions
diff --git a/compressor/filters/__init__.py b/compressor/filters/__init__.py index efeee86..0692071 100644 --- a/compressor/filters/__init__.py +++ b/compressor/filters/__init__.py @@ -1,3 +1,8 @@ # flake8: noqa -from compressor.filters.base import (FilterBase, CallbackOutputFilter, - CompilerFilter, CachedCompilerFilter, FilterError) +from compressor.filters.base import ( + FilterBase, + CallbackOutputFilter, + CompilerFilter, + CachedCompilerFilter, + FilterError, +) diff --git a/compressor/filters/base.py b/compressor/filters/base.py index d660768..767f0a8 100644 --- a/compressor/filters/base.py +++ b/compressor/filters/base.py @@ -9,10 +9,12 @@ if system() != "Windows": from shlex import quote as shell_quote else: from subprocess import list2cmdline + def shell_quote(s): # shlex.quote/pipes.quote is not compatible with Windows return list2cmdline([s]) + from django.core.exceptions import ImproperlyConfigured from django.core.files.temp import NamedTemporaryFile from django.utils.encoding import smart_str @@ -41,9 +43,17 @@ class FilterBase: # This flag allows those filters to do so. run_with_compression_disabled = False - def __init__(self, content, attrs=None, filter_type=None, filename=None, - verbose=0, charset=None, **kwargs): - self.type = filter_type or getattr(self, 'type', None) + def __init__( + self, + content, + attrs=None, + filter_type=None, + filename=None, + verbose=0, + charset=None, + **kwargs + ): + self.type = filter_type or getattr(self, "type", None) self.content = content self.verbose = verbose or settings.COMPRESS_VERBOSE self.logger = logger @@ -68,6 +78,7 @@ class CallbackOutputFilter(FilterBase): Callback should be a function which takes a string as first argument and returns a string. """ + callback = None args = [] kwargs = {} @@ -77,8 +88,9 @@ class CallbackOutputFilter(FilterBase): super().__init__(*args, **kwargs) if self.callback is None: raise ImproperlyConfigured( - "The callback filter %s must define a 'callback' attribute." % - self.__class__.__name__) + "The callback filter %s must define a 'callback' attribute." + % self.__class__.__name__ + ) try: mod_name, func_name = get_mod_func(self.callback) func = getattr(import_module(mod_name), func_name) @@ -87,16 +99,20 @@ class CallbackOutputFilter(FilterBase): if len(self.dependencies) == 1: warning = "dependency (%s) is" % self.dependencies[0] else: - warning = ("dependencies (%s) are" % - ", ".join([dep for dep in self.dependencies])) + warning = "dependencies (%s) are" % ", ".join( + [dep for dep in self.dependencies] + ) else: warning = "" raise ImproperlyConfigured( "The callback %s couldn't be imported. Make sure the %s " - "correctly installed." % (self.callback, warning)) + "correctly installed." % (self.callback, warning) + ) except AttributeError as e: - raise ImproperlyConfigured("An error occurred while importing the " - "callback filter %s: %s" % (self, e)) + raise ImproperlyConfigured( + "An error occurred while importing the " + "callback filter %s: %s" % (self, e) + ) else: self._callback_func = func @@ -111,11 +127,11 @@ class CompilerFilter(FilterBase): A filter subclass that is able to filter content via external commands. """ + command = None options = () default_encoding = ( - settings.FILE_CHARSET if settings.is_overridden('FILE_CHARSET') else - 'utf-8' + settings.FILE_CHARSET if settings.is_overridden("FILE_CHARSET") else "utf-8" ) def __init__(self, content, command=None, **kwargs): @@ -149,7 +165,7 @@ class CompilerFilter(FilterBase): if self.infile is None and "{infile}" in self.command: # create temporary input file if needed if self.filename is None: - self.infile = NamedTemporaryFile(mode='wb') + self.infile = NamedTemporaryFile(mode="wb") self.infile.write(self.content.encode(encoding)) self.infile.flush() options["infile"] = self.infile.name @@ -165,7 +181,7 @@ class CompilerFilter(FilterBase): if "{outfile}" in self.command and "outfile" not in options: # create temporary output file if needed ext = self.type and ".%s" % self.type or "" - self.outfile = NamedTemporaryFile(mode='r+', suffix=ext) + self.outfile = NamedTemporaryFile(mode="r+", suffix=ext) options["outfile"] = self.outfile.name # Quote infile and outfile for spaces etc. @@ -177,34 +193,42 @@ class CompilerFilter(FilterBase): try: command = self.command.format(**options) proc = subprocess.Popen( - command, shell=True, cwd=self.cwd, stdout=self.stdout, - stdin=self.stdin, stderr=self.stderr) + command, + shell=True, + cwd=self.cwd, + stdout=self.stdout, + stdin=self.stdin, + stderr=self.stderr, + ) if self.infile is None: # if infile is None then send content to process' stdin - filtered, err = proc.communicate( - self.content.encode(encoding)) + filtered, err = proc.communicate(self.content.encode(encoding)) else: filtered, err = proc.communicate() filtered, err = filtered.decode(encoding), err.decode(encoding) except (IOError, OSError) as e: - raise FilterError('Unable to apply %s (%r): %s' % - (self.__class__.__name__, self.command, e)) + raise FilterError( + "Unable to apply %s (%r): %s" + % (self.__class__.__name__, self.command, e) + ) else: if proc.wait() != 0: # command failed, raise FilterError exception if not err: - err = ('Unable to apply %s (%s)' % - (self.__class__.__name__, self.command)) + err = "Unable to apply %s (%s)" % ( + self.__class__.__name__, + self.command, + ) if filtered: - err += '\n%s' % filtered + err += "\n%s" % filtered raise FilterError(err) if self.verbose: self.logger.debug(err) - outfile_path = options.get('outfile') + outfile_path = options.get("outfile") if outfile_path: - with io.open(outfile_path, 'r', encoding=encoding) as file: + with io.open(outfile_path, "r", encoding=encoding) as file: filtered = file.read() finally: if self.infile is not None: @@ -215,7 +239,6 @@ class CompilerFilter(FilterBase): class CachedCompilerFilter(CompilerFilter): - def __init__(self, mimetype, *args, **kwargs): self.mimetype = mimetype super().__init__(*args, **kwargs) diff --git a/compressor/filters/css_default.py b/compressor/filters/css_default.py index 9a84fab..41091ac 100644 --- a/compressor/filters/css_default.py +++ b/compressor/filters/css_default.py @@ -6,16 +6,19 @@ from compressor.cache import get_hashed_mtime, get_hashed_content from compressor.conf import settings from compressor.filters import FilterBase, FilterError -URL_PATTERN = re.compile(r""" +URL_PATTERN = re.compile( + r""" url\( \s* # any amount of whitespace ([\'"]?) # optional quote (.*?) # any amount of anything, non-greedily (this is the actual url) \1 # matching quote (or nothing if there was none) \s* # any amount of whitespace - \)""", re.VERBOSE) + \)""", + re.VERBOSE, +) SRC_PATTERN = re.compile(r'src=([\'"])(.*?)\1') -SCHEMES = ('http://', 'https://', '/') +SCHEMES = ("http://", "https://", "/") class CssAbsoluteFilter(FilterBase): @@ -25,25 +28,26 @@ class CssAbsoluteFilter(FilterBase): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.root = settings.COMPRESS_ROOT - self.url = settings.COMPRESS_URL.rstrip('/') + self.url = settings.COMPRESS_URL.rstrip("/") self.url_path = self.url self.has_scheme = False def input(self, filename=None, basename=None, **kwargs): if not filename: return self.content - self.path = basename.replace(os.sep, '/') - self.path = self.path.lstrip('/') - if self.url.startswith(('http://', 'https://')): + self.path = basename.replace(os.sep, "/") + self.path = self.path.lstrip("/") + if self.url.startswith(("http://", "https://")): self.has_scheme = True - parts = self.url.split('/') - self.url = '/'.join(parts[2:]) - self.url_path = '/%s' % '/'.join(parts[3:]) - self.protocol = '%s/' % '/'.join(parts[:2]) + parts = self.url.split("/") + self.url = "/".join(parts[2:]) + self.url_path = "/%s" % "/".join(parts[3:]) + self.protocol = "%s/" % "/".join(parts[:2]) self.host = parts[2] - self.directory_name = '/'.join((self.url, os.path.dirname(self.path))) - return SRC_PATTERN.sub(self.src_converter, - URL_PATTERN.sub(self.url_converter, self.content)) + self.directory_name = "/".join((self.url, os.path.dirname(self.path))) + return SRC_PATTERN.sub( + self.src_converter, URL_PATTERN.sub(self.url_converter, self.content) + ) def guess_filename(self, url): local_path = url @@ -60,7 +64,7 @@ class CssAbsoluteFilter(FilterBase): if local_path.startswith(self.url_path): local_path = local_path.replace(self.url_path, "", 1) # Re-build the local full path by adding root - filename = os.path.join(self.root, local_path.lstrip('/')) + filename = os.path.join(self.root, local_path.lstrip("/")) return os.path.exists(filename) and filename def add_suffix(self, url): @@ -78,9 +82,10 @@ class CssAbsoluteFilter(FilterBase): elif settings.COMPRESS_CSS_HASHING_METHOD in ("hash", "content"): suffix = get_hashed_content(filename) else: - raise FilterError('COMPRESS_CSS_HASHING_METHOD is configured ' - 'with an unknown method (%s).' % - settings.COMPRESS_CSS_HASHING_METHOD) + raise FilterError( + "COMPRESS_CSS_HASHING_METHOD is configured " + "with an unknown method (%s)." % settings.COMPRESS_CSS_HASHING_METHOD + ) fragment = None if "#" in url: url, fragment = url.rsplit("#", 1) @@ -93,12 +98,11 @@ class CssAbsoluteFilter(FilterBase): return url def _converter(self, url): - if url.startswith(('#', 'data:')): + if url.startswith(("#", "data:")): return url elif url.startswith(SCHEMES): return self.add_suffix(url) - full_url = posixpath.normpath('/'.join([str(self.directory_name), - url])) + full_url = posixpath.normpath("/".join([str(self.directory_name), url])) if self.has_scheme: full_url = "%s%s" % (self.protocol, full_url) full_url = self.add_suffix(full_url) @@ -149,11 +153,15 @@ class CssRelativeFilter(CssAbsoluteFilter): """ old_prefix = self.url if self.has_scheme: - old_prefix = '{}{}'.format(self.protocol, old_prefix) + old_prefix = "{}{}".format(self.protocol, old_prefix) # One level up from 'css' / 'js' folder - new_prefix = '..' + new_prefix = ".." # N levels up from ``settings.COMPRESS_OUTPUT_DIR`` - new_prefix += '/..' * len(list(filter( - None, os.path.normpath(settings.COMPRESS_OUTPUT_DIR).split(os.sep) - ))) - return re.sub('^{}'.format(old_prefix), new_prefix, url) + new_prefix += "/.." * len( + list( + filter( + None, os.path.normpath(settings.COMPRESS_OUTPUT_DIR).split(os.sep) + ) + ) + ) + return re.sub("^{}".format(old_prefix), new_prefix, url) diff --git a/compressor/filters/cssmin/__init__.py b/compressor/filters/cssmin/__init__.py index cc23cd2..282d671 100644 --- a/compressor/filters/cssmin/__init__.py +++ b/compressor/filters/cssmin/__init__.py @@ -6,6 +6,7 @@ class CSSCompressorFilter(CallbackOutputFilter): A filter that utilizes Yury Selivanov's Python port of the YUI CSS compression algorithm: https://pypi.python.org/pypi/csscompressor """ + callback = "csscompressor.compress" dependencies = ["csscompressor"] @@ -13,9 +14,7 @@ class CSSCompressorFilter(CallbackOutputFilter): class rCSSMinFilter(CallbackOutputFilter): callback = "rcssmin.cssmin" dependencies = ["rcssmin"] - kwargs = { - "keep_bang_comments": True - } + kwargs = {"keep_bang_comments": True} # This is for backwards compatibility. diff --git a/compressor/filters/datauri.py b/compressor/filters/datauri.py index bd08a7d..ab9bca0 100644 --- a/compressor/filters/datauri.py +++ b/compressor/filters/datauri.py @@ -17,6 +17,7 @@ class DataUriFilter(FilterBase): Don't use this class directly. Use a subclass. """ + def input(self, filename=None, **kwargs): if not filename or not filename.startswith(settings.COMPRESS_ROOT): return self.content @@ -31,18 +32,19 @@ class DataUriFilter(FilterBase): url = url.split("?")[0] if "#" in url: url = url.split("#")[0] - return os.path.join( - settings.COMPRESS_ROOT, url[len(settings.COMPRESS_URL):]) + return os.path.join(settings.COMPRESS_ROOT, url[len(settings.COMPRESS_URL) :]) def data_uri_converter(self, matchobj): - url = matchobj.group(1).strip(' \'"') - if not url.startswith('data:') and not url.startswith('//'): + url = matchobj.group(1).strip(" '\"") + if not url.startswith("data:") and not url.startswith("//"): path = self.get_file_path(url) if os.stat(path).st_size <= settings.COMPRESS_DATA_URI_MAX_SIZE: - with open(path, 'rb') as file: - data = b64encode(file.read()).decode('ascii') + with open(path, "rb") as file: + data = b64encode(file.read()).decode("ascii") return 'url("data:%s;base64,%s")' % ( - mimetypes.guess_type(path)[0], data) + mimetypes.guess_type(path)[0], + data, + ) return 'url("%s")' % url @@ -51,6 +53,5 @@ class CssDataUriFilter(DataUriFilter): See DataUriFilter. """ - url_patterns = ( - re.compile(r'url\(([^\)]+)\)'), - ) + + url_patterns = (re.compile(r"url\(([^\)]+)\)"),) diff --git a/compressor/filters/jsmin/__init__.py b/compressor/filters/jsmin/__init__.py index c1a64cf..a517cfd 100644 --- a/compressor/filters/jsmin/__init__.py +++ b/compressor/filters/jsmin/__init__.py @@ -34,11 +34,11 @@ class SlimItFilter(CallbackOutputFilter): class CalmjsFilter(FilterBase): def __init__(self, *args, **kwargs): try: - self._parser = kwargs.pop('parser') + self._parser = kwargs.pop("parser") except KeyError: self._parser = None try: - self._unparser = kwargs.pop('unparser') + self._unparser = kwargs.pop("unparser") except KeyError: self._unparser = None super().__init__(*args, **kwargs) @@ -56,6 +56,6 @@ class CalmjsFilter(FilterBase): def output(self, **kwargs): program = self._parser(self.content) - minified = u''.join(part.text for part in self._unparser(program)) + minified = "".join(part.text for part in self._unparser(program)) assert isinstance(minified, str) return minified diff --git a/compressor/filters/template.py b/compressor/filters/template.py index 8bf7365..768cfab 100644 --- a/compressor/filters/template.py +++ b/compressor/filters/template.py @@ -5,7 +5,6 @@ from compressor.filters import FilterBase class TemplateFilter(FilterBase): - def input(self, filename=None, basename=None, **kwargs): template = Template(self.content) context = Context(settings.COMPRESS_TEMPLATE_FILTER_CONTEXT) diff --git a/compressor/filters/yuglify.py b/compressor/filters/yuglify.py index b3a6e4f..940037f 100644 --- a/compressor/filters/yuglify.py +++ b/compressor/filters/yuglify.py @@ -7,11 +7,11 @@ class YUglifyFilter(CompilerFilter): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.command += ' --type=%s' % self.type + self.command += " --type=%s" % self.type class YUglifyCSSFilter(YUglifyFilter): - type = 'css' + type = "css" options = ( ("binary", settings.COMPRESS_YUGLIFY_BINARY), ("args", settings.COMPRESS_YUGLIFY_CSS_ARGUMENTS), @@ -19,7 +19,7 @@ class YUglifyCSSFilter(YUglifyFilter): class YUglifyJSFilter(YUglifyFilter): - type = 'js' + type = "js" options = ( ("binary", settings.COMPRESS_YUGLIFY_BINARY), ("args", settings.COMPRESS_YUGLIFY_JS_ARGUMENTS), diff --git a/compressor/filters/yui.py b/compressor/filters/yui.py index f4beea3..5a91c04 100644 --- a/compressor/filters/yui.py +++ b/compressor/filters/yui.py @@ -7,13 +7,13 @@ class YUICompressorFilter(CompilerFilter): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.command += ' --type=%s' % self.type + self.command += " --type=%s" % self.type if self.verbose: - self.command += ' --verbose' + self.command += " --verbose" class YUICSSFilter(YUICompressorFilter): - type = 'css' + type = "css" options = ( ("binary", settings.COMPRESS_YUI_BINARY), ("args", settings.COMPRESS_YUI_CSS_ARGUMENTS), @@ -21,7 +21,7 @@ class YUICSSFilter(YUICompressorFilter): class YUIJSFilter(YUICompressorFilter): - type = 'js' + type = "js" options = ( ("binary", settings.COMPRESS_YUI_BINARY), ("args", settings.COMPRESS_YUI_JS_ARGUMENTS), |