diff --git a/pyproject.toml b/pyproject.toml index 7d60988ac..140e3c979 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ exclude = [ line-length = 100 [tool.ruff.lint] -select = ["B", "C901", "E", "F", "W"] +select = ["B", "C901", "E", "F", "I", "W"] ignore = ["B904", "B017"] [tool.ruff.lint.mccabe] diff --git a/setup.py b/setup.py index 4c2761d13..548cfa96a 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ -from setuptools import setup, find_packages +from setuptools import find_packages, setup setup( name='sorl-thumbnail', diff --git a/sorl/__init__.py b/sorl/__init__.py index 6bfd3696a..955934be6 100644 --- a/sorl/__init__.py +++ b/sorl/__init__.py @@ -1,4 +1,4 @@ -from importlib.metadata import version, PackageNotFoundError +from importlib.metadata import PackageNotFoundError, version try: __version__ = version("sorl-thumbnail") diff --git a/sorl/thumbnail/admin/current.py b/sorl/thumbnail/admin/current.py index db26a1e1a..586c338e1 100644 --- a/sorl/thumbnail/admin/current.py +++ b/sorl/thumbnail/admin/current.py @@ -2,10 +2,10 @@ from django import forms from django.utils.safestring import mark_safe + from sorl.thumbnail.fields import ImageField from sorl.thumbnail.shortcuts import get_thumbnail - logger = logging.getLogger(__name__) diff --git a/sorl/thumbnail/base.py b/sorl/thumbnail/base.py index 64155afaa..2589db54e 100644 --- a/sorl/thumbnail/base.py +++ b/sorl/thumbnail/base.py @@ -2,13 +2,13 @@ import os import re -from sorl.thumbnail.conf import settings, defaults as default_settings -from sorl.thumbnail.helpers import tokey, serialize -from sorl.thumbnail.images import ImageFile, DummyImageFile from sorl.thumbnail import default +from sorl.thumbnail.conf import defaults as default_settings +from sorl.thumbnail.conf import settings +from sorl.thumbnail.helpers import serialize, tokey +from sorl.thumbnail.images import DummyImageFile, ImageFile from sorl.thumbnail.parsers import parse_geometry - logger = logging.getLogger(__name__) EXTENSIONS = { diff --git a/sorl/thumbnail/conf/__init__.py b/sorl/thumbnail/conf/__init__.py index ef7e1bcab..3d4bdbace 100644 --- a/sorl/thumbnail/conf/__init__.py +++ b/sorl/thumbnail/conf/__init__.py @@ -1,4 +1,5 @@ from django.conf import settings as user_settings + from sorl.thumbnail.conf import defaults diff --git a/sorl/thumbnail/engines/base.py b/sorl/thumbnail/engines/base.py index 433f2fce5..18d8cdab0 100644 --- a/sorl/thumbnail/engines/base.py +++ b/sorl/thumbnail/engines/base.py @@ -1,7 +1,6 @@ from sorl.thumbnail.conf import settings from sorl.thumbnail.helpers import toint -from sorl.thumbnail.parsers import parse_crop -from sorl.thumbnail.parsers import parse_cropbox +from sorl.thumbnail.parsers import parse_crop, parse_cropbox class EngineBase: diff --git a/sorl/thumbnail/engines/convert_engine.py b/sorl/thumbnail/engines/convert_engine.py index 7af2d874c..320148885 100644 --- a/sorl/thumbnail/engines/convert_engine.py +++ b/sorl/thumbnail/engines/convert_engine.py @@ -1,11 +1,11 @@ -import re +import logging import os +import re import subprocess -import logging from collections import OrderedDict -from django.utils.encoding import smart_str from django.core.files.temp import NamedTemporaryFile +from django.utils.encoding import smart_str from sorl.thumbnail.base import EXTENSIONS from sorl.thumbnail.conf import settings diff --git a/sorl/thumbnail/engines/pgmagick_engine.py b/sorl/thumbnail/engines/pgmagick_engine.py index 547733eef..147525e2c 100644 --- a/sorl/thumbnail/engines/pgmagick_engine.py +++ b/sorl/thumbnail/engines/pgmagick_engine.py @@ -1,5 +1,5 @@ -from pgmagick import Blob, Geometry, Image, ImageType -from pgmagick import InterlaceType, OrientationType +from pgmagick import Blob, Geometry, Image, ImageType, InterlaceType, OrientationType + from sorl.thumbnail.engines.base import EngineBase try: diff --git a/sorl/thumbnail/engines/pil_engine.py b/sorl/thumbnail/engines/pil_engine.py index 5371be42c..ea754fd77 100644 --- a/sorl/thumbnail/engines/pil_engine.py +++ b/sorl/thumbnail/engines/pil_engine.py @@ -3,11 +3,11 @@ from sorl.thumbnail.engines.base import EngineBase try: - from PIL import Image, ImageFile, ImageDraw, ImageFilter, ImageMode + from PIL import Image, ImageDraw, ImageFile, ImageFilter, ImageMode except ImportError: import Image - import ImageFile import ImageDraw + import ImageFile import ImageMode if hasattr(Image, 'Resampling'): @@ -36,7 +36,7 @@ def color_count(image): def histogram_entropy_py(image): """ Calculate the entropy of an images' histogram. """ - from math import log2, fsum + from math import fsum, log2 histosum = float(color_count(image)) histonorm = (histocol / histosum for histocol in image.histogram()) return -fsum(p * log2(p) for p in histonorm if p != 0.0) diff --git a/sorl/thumbnail/engines/vipsthumbnail_engine.py b/sorl/thumbnail/engines/vipsthumbnail_engine.py index cb9f53cc0..a63467d7d 100644 --- a/sorl/thumbnail/engines/vipsthumbnail_engine.py +++ b/sorl/thumbnail/engines/vipsthumbnail_engine.py @@ -1,16 +1,15 @@ -import re import os +import re import subprocess from collections import OrderedDict -from django.utils.encoding import smart_str from django.core.files.temp import NamedTemporaryFile +from django.utils.encoding import smart_str from sorl.thumbnail.base import EXTENSIONS from sorl.thumbnail.conf import settings from sorl.thumbnail.engines.base import EngineBase - size_re = re.compile(r'^(?:.+) (?P\d+)x(?P\d+)') diff --git a/sorl/thumbnail/engines/wand_engine.py b/sorl/thumbnail/engines/wand_engine.py index 575a1fd20..45e90fc2f 100644 --- a/sorl/thumbnail/engines/wand_engine.py +++ b/sorl/thumbnail/engines/wand_engine.py @@ -2,9 +2,10 @@ Wand (>=v0.3.0) engine for Sorl-thumbnail ''' +from wand import exceptions from wand.image import Image from wand.version import MAGICK_VERSION_NUMBER -from wand import exceptions + from sorl.thumbnail.engines.base import EngineBase diff --git a/sorl/thumbnail/fields.py b/sorl/thumbnail/fields.py index 4777864ea..4d84e1703 100644 --- a/sorl/thumbnail/fields.py +++ b/sorl/thumbnail/fields.py @@ -1,11 +1,10 @@ +from django import forms from django.db import models from django.db.models import Q -from django import forms from django.utils.translation import gettext_lazy as _ from sorl.thumbnail import default - __all__ = ('ImageField', 'ImageFormField') diff --git a/sorl/thumbnail/images.py b/sorl/thumbnail/images.py index a11390275..a656ec2b9 100644 --- a/sorl/thumbnail/images.py +++ b/sorl/thumbnail/images.py @@ -5,16 +5,17 @@ from functools import lru_cache from urllib.error import URLError from urllib.parse import quote, quote_plus, urlsplit, urlunsplit -from urllib.request import urlopen, Request +from urllib.request import Request, urlopen -from django.core.files.base import File, ContentFile +from django.core.files.base import ContentFile, File from django.core.files.storage import Storage # , default_storage from django.utils.encoding import force_str from django.utils.functional import LazyObject, empty + from sorl.thumbnail import default from sorl.thumbnail.conf import settings from sorl.thumbnail.default import storage as default_storage -from sorl.thumbnail.helpers import ThumbnailError, tokey, get_module_class, deserialize +from sorl.thumbnail.helpers import ThumbnailError, deserialize, get_module_class, tokey from sorl.thumbnail.parsers import parse_geometry url_pat = re.compile(r'^(https?|ftp):\/\/') diff --git a/sorl/thumbnail/kvstores/base.py b/sorl/thumbnail/kvstores/base.py index eef411d71..97958be14 100644 --- a/sorl/thumbnail/kvstores/base.py +++ b/sorl/thumbnail/kvstores/base.py @@ -1,6 +1,6 @@ from sorl.thumbnail.conf import settings -from sorl.thumbnail.helpers import serialize, deserialize, ThumbnailError -from sorl.thumbnail.images import serialize_image_file, deserialize_image_file +from sorl.thumbnail.helpers import ThumbnailError, deserialize, serialize +from sorl.thumbnail.images import deserialize_image_file, serialize_image_file def add_prefix(key, identity='image'): diff --git a/sorl/thumbnail/kvstores/cached_db_kvstore.py b/sorl/thumbnail/kvstores/cached_db_kvstore.py index 635cfb47f..f536ee748 100644 --- a/sorl/thumbnail/kvstores/cached_db_kvstore.py +++ b/sorl/thumbnail/kvstores/cached_db_kvstore.py @@ -1,6 +1,7 @@ -from django.core.cache import cache, caches, InvalidCacheBackendError -from sorl.thumbnail.kvstores.base import KVStoreBase +from django.core.cache import InvalidCacheBackendError, cache, caches + from sorl.thumbnail.conf import settings +from sorl.thumbnail.kvstores.base import KVStoreBase from sorl.thumbnail.models import KVStore as KVStoreModel diff --git a/sorl/thumbnail/kvstores/dbm_kvstore.py b/sorl/thumbnail/kvstores/dbm_kvstore.py index 4b529454e..bda4c80a2 100644 --- a/sorl/thumbnail/kvstores/dbm_kvstore.py +++ b/sorl/thumbnail/kvstores/dbm_kvstore.py @@ -1,8 +1,7 @@ import os -from sorl.thumbnail.kvstores.base import KVStoreBase from sorl.thumbnail.conf import settings - +from sorl.thumbnail.kvstores.base import KVStoreBase try: import anydbm as dbm diff --git a/sorl/thumbnail/kvstores/dynamodb_kvstore.py b/sorl/thumbnail/kvstores/dynamodb_kvstore.py index 1f42df69c..fed9e24a2 100644 --- a/sorl/thumbnail/kvstores/dynamodb_kvstore.py +++ b/sorl/thumbnail/kvstores/dynamodb_kvstore.py @@ -1,7 +1,8 @@ -from boto.dynamodb2.table import Table import boto -from sorl.thumbnail.kvstores.base import KVStoreBase +from boto.dynamodb2.table import Table + from sorl.thumbnail.conf import settings +from sorl.thumbnail.kvstores.base import KVStoreBase class KVStore(KVStoreBase): diff --git a/sorl/thumbnail/kvstores/redis_kvstore.py b/sorl/thumbnail/kvstores/redis_kvstore.py index 96fbd0405..4ab0e77d5 100644 --- a/sorl/thumbnail/kvstores/redis_kvstore.py +++ b/sorl/thumbnail/kvstores/redis_kvstore.py @@ -1,6 +1,7 @@ import redis -from sorl.thumbnail.kvstores.base import KVStoreBase + from sorl.thumbnail.conf import settings +from sorl.thumbnail.kvstores.base import KVStoreBase class KVStore(KVStoreBase): diff --git a/sorl/thumbnail/management/commands/thumbnail.py b/sorl/thumbnail/management/commands/thumbnail.py index f7b7468a2..34f72dfbc 100644 --- a/sorl/thumbnail/management/commands/thumbnail.py +++ b/sorl/thumbnail/management/commands/thumbnail.py @@ -3,7 +3,6 @@ from sorl.thumbnail import default from sorl.thumbnail.images import delete_all_thumbnails - VALID_LABELS = ['cleanup', 'clear', 'clear_delete_referenced', 'clear_delete_all'] diff --git a/sorl/thumbnail/parsers.py b/sorl/thumbnail/parsers.py index 87a985465..3ad1bde16 100644 --- a/sorl/thumbnail/parsers.py +++ b/sorl/thumbnail/parsers.py @@ -2,7 +2,6 @@ from sorl.thumbnail.helpers import ThumbnailError, toint - bgpos_pat = re.compile(r'^(?P\d+)(?P%|px)$') geometry_pat = re.compile(r'^(?P\d+)?(?:x(?P\d+))?$') diff --git a/sorl/thumbnail/templatetags/thumbnail.py b/sorl/thumbnail/templatetags/thumbnail.py index f97e0eb0d..59c0636a5 100644 --- a/sorl/thumbnail/templatetags/thumbnail.py +++ b/sorl/thumbnail/templatetags/thumbnail.py @@ -1,21 +1,20 @@ import decimal import logging -import sys -import re import os +import re +import sys from functools import wraps +from django.conf import settings from django.template import Library, Node, NodeList, TemplateSyntaxError from django.utils.encoding import smart_str -from django.conf import settings -from sorl.thumbnail.conf import settings as sorl_settings from sorl.thumbnail import default -from sorl.thumbnail.images import ImageFile, DummyImageFile +from sorl.thumbnail.conf import settings as sorl_settings +from sorl.thumbnail.images import DummyImageFile, ImageFile from sorl.thumbnail.parsers import parse_geometry from sorl.thumbnail.shortcuts import get_thumbnail - register = Library() kw_pat = re.compile(r'^(?P[\w]+)=(?P.+)$') logger = logging.getLogger('sorl.thumbnail') diff --git a/tests/thumbnail_tests/test_alternative_resolutions.py b/tests/thumbnail_tests/test_alternative_resolutions.py index 8a27ffdcc..439191878 100644 --- a/tests/thumbnail_tests/test_alternative_resolutions.py +++ b/tests/thumbnail_tests/test_alternative_resolutions.py @@ -4,12 +4,11 @@ from sorl.thumbnail import get_thumbnail from sorl.thumbnail.conf import settings -from sorl.thumbnail.images import ImageFile from sorl.thumbnail.engines.pil_engine import Engine as PILEngine +from sorl.thumbnail.images import ImageFile from .utils import BaseStorageTestCase - pytestmark = pytest.mark.django_db diff --git a/tests/thumbnail_tests/test_backends.py b/tests/thumbnail_tests/test_backends.py index d20dd4516..a0101090a 100644 --- a/tests/thumbnail_tests/test_backends.py +++ b/tests/thumbnail_tests/test_backends.py @@ -1,23 +1,23 @@ -from io import StringIO import os import platform -import sys import shutil +import sys import unittest -from PIL import Image +from io import StringIO import pytest from django.test import TestCase from django.test.utils import override_settings +from PIL import Image from sorl.thumbnail import default, delete, get_thumbnail from sorl.thumbnail.base import ThumbnailBackend from sorl.thumbnail.conf import settings from sorl.thumbnail.helpers import get_module_class from sorl.thumbnail.images import ImageFile -from .utils import BaseTestCase, FakeFile, same_open_fd_count -from .models import Item +from .models import Item +from .utils import BaseTestCase, FakeFile, same_open_fd_count pytestmark = pytest.mark.django_db diff --git a/tests/thumbnail_tests/test_commands.py b/tests/thumbnail_tests/test_commands.py index 41a1ded0b..4b506b75e 100644 --- a/tests/thumbnail_tests/test_commands.py +++ b/tests/thumbnail_tests/test_commands.py @@ -1,14 +1,14 @@ -from io import StringIO import os +from io import StringIO import pytest from django.core import management from sorl.thumbnail.conf import settings + from .models import Item from .utils import BaseTestCase - pytestmark = pytest.mark.django_db diff --git a/tests/thumbnail_tests/test_engines.py b/tests/thumbnail_tests/test_engines.py index f3ef9e7a4..0f1837bbc 100644 --- a/tests/thumbnail_tests/test_engines.py +++ b/tests/thumbnail_tests/test_engines.py @@ -1,25 +1,25 @@ import os import platform import unittest -from subprocess import Popen, PIPE +from subprocess import PIPE, Popen import pytest -from PIL import Image from django.core.files.storage import default_storage from django.template.loader import render_to_string +from PIL import Image from sorl.thumbnail import default from sorl.thumbnail.base import ThumbnailBackend from sorl.thumbnail.conf import settings +from sorl.thumbnail.engines.pil_engine import Engine as PILEngine from sorl.thumbnail.helpers import get_module_class from sorl.thumbnail.images import ImageFile from sorl.thumbnail.parsers import parse_geometry from sorl.thumbnail.templatetags.thumbnail import margin -from sorl.thumbnail.engines.pil_engine import Engine as PILEngine + from .models import Item from .utils import BaseTestCase - pytestmark = pytest.mark.django_db diff --git a/tests/thumbnail_tests/test_filters.py b/tests/thumbnail_tests/test_filters.py index 1e1acd6e3..6b239b7f4 100644 --- a/tests/thumbnail_tests/test_filters.py +++ b/tests/thumbnail_tests/test_filters.py @@ -3,7 +3,6 @@ from tests.thumbnail_tests.utils import BaseTestCase - pytestmark = pytest.mark.django_db diff --git a/tests/thumbnail_tests/test_storage.py b/tests/thumbnail_tests/test_storage.py index e8b67feab..751fd6c2c 100644 --- a/tests/thumbnail_tests/test_storage.py +++ b/tests/thumbnail_tests/test_storage.py @@ -1,12 +1,12 @@ import unittest + import pytest -from sorl.thumbnail import get_thumbnail, default +from sorl.thumbnail import default, get_thumbnail from sorl.thumbnail.helpers import get_module_class from .utils import BaseStorageTestCase - pytestmark = pytest.mark.django_db diff --git a/tests/thumbnail_tests/test_templatetags.py b/tests/thumbnail_tests/test_templatetags.py index 90ed5ebc0..72ba5cff4 100644 --- a/tests/thumbnail_tests/test_templatetags.py +++ b/tests/thumbnail_tests/test_templatetags.py @@ -1,18 +1,18 @@ import os import re -from subprocess import Popen, PIPE -from PIL import Image +from subprocess import PIPE, Popen +import pytest from django.template.loader import render_to_string from django.test import Client, TestCase from django.test.utils import override_settings -import pytest +from PIL import Image from sorl.thumbnail.conf import settings from sorl.thumbnail.engines.pil_engine import Engine as PILEngine -from .models import Item -from .utils import BaseTestCase, DATA_DIR +from .models import Item +from .utils import DATA_DIR, BaseTestCase pytestmark = pytest.mark.django_db diff --git a/tests/thumbnail_tests/urls.py b/tests/thumbnail_tests/urls.py index 0fae41c84..fa13531f9 100644 --- a/tests/thumbnail_tests/urls.py +++ b/tests/thumbnail_tests/urls.py @@ -7,7 +7,6 @@ from .views import direct_to_template - urlpatterns = [ url(r'^media/(?P.+)$', serve, {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}), diff --git a/tests/thumbnail_tests/utils.py b/tests/thumbnail_tests/utils.py index 264642623..443c35cac 100644 --- a/tests/thumbnail_tests/utils.py +++ b/tests/thumbnail_tests/utils.py @@ -1,7 +1,7 @@ +import logging import os import shutil import unittest -import logging from contextlib import contextmanager from subprocess import check_output @@ -11,6 +11,7 @@ from sorl.thumbnail.helpers import get_module_class from sorl.thumbnail.images import ImageFile from sorl.thumbnail.log import ThumbnailLogHandler + from .models import Item from .storage import MockLoggingHandler diff --git a/tests/thumbnail_tests/views.py b/tests/thumbnail_tests/views.py index eb9fe10ef..93bf03557 100644 --- a/tests/thumbnail_tests/views.py +++ b/tests/thumbnail_tests/views.py @@ -1,5 +1,5 @@ -from django.template import loader from django.http import HttpResponse +from django.template import loader def direct_to_template(request, template, mimetype=None, **kwargs):