A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
Chore: Adds additional rules for Ruff linter (#5660)
Trenton H4813a7bc70d8
diff --git a/.ruff.toml b/.ruff.tomlindex c69de0ee1..497d3f1eb 100644--- a/.ruff.toml+++ b/.ruff.toml@@ -1,7 +1,29 @@-# https://beta.ruff.rs/docs/settings/-# https://beta.ruff.rs/docs/rules/-extend-select = ["I", "W", "UP", "COM", "DJ", "EXE", "ISC", "ICN", "G201", "INP", "PIE", "RSE", "SIM", "TID", "PLC", "PLE", "RUF"]-# TODO PTH+# https://docs.astral.sh/ruff/settings/+# https://docs.astral.sh/ruff/rules/+extend-select = [+ "W", # https://docs.astral.sh/ruff/rules/#pycodestyle-e-w+ "I", # https://docs.astral.sh/ruff/rules/#isort-i+ "UP", # https://docs.astral.sh/ruff/rules/#pyupgrade-up+ "COM", # https://docs.astral.sh/ruff/rules/#flake8-commas-com+ "DJ", # https://docs.astral.sh/ruff/rules/#flake8-django-dj+ "EXE", # https://docs.astral.sh/ruff/rules/#flake8-executable-exe+ "ISC", # https://docs.astral.sh/ruff/rules/#flake8-implicit-str-concat-isc+ "ICN", # https://docs.astral.sh/ruff/rules/#flake8-import-conventions-icn+ "G201", # https://docs.astral.sh/ruff/rules/#flake8-logging-format-g+ "INP", # https://docs.astral.sh/ruff/rules/#flake8-no-pep420-inp+ "PIE", # https://docs.astral.sh/ruff/rules/#flake8-pie-pie+ "Q", # https://docs.astral.sh/ruff/rules/#flake8-quotes-q+ "RSE", # https://docs.astral.sh/ruff/rules/#flake8-raise-rse+ "T20", # https://docs.astral.sh/ruff/rules/#flake8-print-t20+ "SIM", # https://docs.astral.sh/ruff/rules/#flake8-simplify-sim+ "TID", # https://docs.astral.sh/ruff/rules/#flake8-tidy-imports-tid+ "TCH", # https://docs.astral.sh/ruff/rules/#flake8-type-checking-tch+ "PLC", # https://docs.astral.sh/ruff/rules/#pylint-pl+ "PLE", # https://docs.astral.sh/ruff/rules/#pylint-pl+ "RUF", # https://docs.astral.sh/ruff/rules/#ruff-specific-rules-ruf+ "FLY", # https://docs.astral.sh/ruff/rules/#flynt-fly+]+# TODO PTH https://docs.astral.sh/ruff/rules/#flake8-use-pathlib-pthignore = ["DJ001", "SIM105", "RUF012"]fix = trueline-length = 88@@ -13,9 +35,9 @@ show-fixes = true[per-file-ignores]".github/scripts/*.py" = ["E501", "INP001", "SIM117"]-"docker/wait-for-redis.py" = ["INP001"]+"docker/wait-for-redis.py" = ["INP001", "T201"]"*/tests/*.py" = ["E501", "SIM117"]-"*/migrations/*.py" = ["E501", "SIM"]+"*/migrations/*.py" = ["E501", "SIM", "T201"]"src/paperless_tesseract/tests/test_parser.py" = ["RUF001"]"src/documents/models.py" = ["SIM115"]diff --git a/src/documents/caching.py b/src/documents/caching.pyindex 9b8607dd8..d80f319f7 100644--- a/src/documents/caching.py+++ b/src/documents/caching.py@@ -90,7 +90,6 @@ def set_suggestions_cache("""if classifier is not None:doc_key = get_suggestion_cache_key(document_id)- print(classifier.last_auto_type_hash)cache.set(doc_key,SuggestionCacheData(diff --git a/src/documents/classifier.py b/src/documents/classifier.pyindex 6180a8671..aa0eb70b6 100644--- a/src/documents/classifier.py+++ b/src/documents/classifier.py@@ -4,11 +4,14 @@ import pickleimport reimport warningsfrom collections.abc import Iterator-from datetime import datetimefrom hashlib import sha256-from pathlib import Path+from typing import TYPE_CHECKINGfrom typing import Optional+if TYPE_CHECKING:+ from datetime import datetime+ from pathlib import Path+from django.conf import settingsfrom django.core.cache import cachefrom sklearn.exceptions import InconsistentVersionWarningdiff --git a/src/documents/management/commands/document_retagger.py b/src/documents/management/commands/document_retagger.pyindex dda3ecebc..10bb54b71 100644--- a/src/documents/management/commands/document_retagger.py+++ b/src/documents/management/commands/document_retagger.py@@ -69,8 +69,6 @@ class Command(ProgressBarMixin, BaseCommand):def handle(self, *args, **options):self.handle_progress_bar_mixin(**options)- # Detect if we support color- color = self.style.ERROR("test") != "test"if options["inbox_only"]:queryset = Document.objects.filter(tags__is_inbox_tag=True)@@ -96,7 +94,8 @@ class Command(ProgressBarMixin, BaseCommand):use_first=options["use_first"],suggest=options["suggest"],base_url=options["base_url"],- color=color,+ stdout=self.stdout,+ style_func=self.style,)if options["document_type"]:@@ -108,7 +107,8 @@ class Command(ProgressBarMixin, BaseCommand):use_first=options["use_first"],suggest=options["suggest"],base_url=options["base_url"],- color=color,+ stdout=self.stdout,+ style_func=self.style,)if options["tags"]:@@ -119,7 +119,8 @@ class Command(ProgressBarMixin, BaseCommand):replace=options["overwrite"],suggest=options["suggest"],base_url=options["base_url"],- color=color,+ stdout=self.stdout,+ style_func=self.style,)if options["storage_path"]:set_storage_path(@@ -130,5 +131,6 @@ class Command(ProgressBarMixin, BaseCommand):use_first=options["use_first"],suggest=options["suggest"],base_url=options["base_url"],- color=color,+ stdout=self.stdout,+ style_func=self.style,)diff --git a/src/documents/management/commands/document_thumbnails.py b/src/documents/management/commands/document_thumbnails.pyindex ecd265102..d4653f0b3 100644--- a/src/documents/management/commands/document_thumbnails.py+++ b/src/documents/management/commands/document_thumbnails.py@@ -19,7 +19,7 @@ def _process_document(doc_id):if parser_class:parser = parser_class(logging_group=None)else:- print(f"{document} No parser for mime type {document.mime_type}")+ print(f"{document} No parser for mime type {document.mime_type}") # noqa: T201returntry:diff --git a/src/documents/plugins/helpers.py b/src/documents/plugins/helpers.pyindex 92fe1255b..27d03f30f 100644--- a/src/documents/plugins/helpers.py+++ b/src/documents/plugins/helpers.py@@ -5,7 +5,9 @@ from typing import Unionfrom asgiref.sync import async_to_syncfrom channels.layers import get_channel_layer-from channels_redis.pubsub import RedisPubSubChannelLayer++if TYPE_CHECKING:+ from channels_redis.pubsub import RedisPubSubChannelLayerclass ProgressStatusOptions(str, enum.Enum):diff --git a/src/documents/signals/handlers.py b/src/documents/signals/handlers.pyindex 2b717e042..c8657ce1d 100644--- a/src/documents/signals/handlers.py+++ b/src/documents/signals/handlers.py@@ -18,7 +18,6 @@ from django.db import close_old_connectionsfrom django.db import modelsfrom django.db.models import Qfrom django.dispatch import receiver-from django.utils import termcolorsfrom django.utils import timezonefrom filelock import FileLock@@ -54,6 +53,26 @@ def add_inbox_tags(sender, document: Document, logging_group=None, **kwargs):document.tags.add(*inbox_tags)+def _suggestion_printer(+ stdout,+ style_func,+ suggestion_type: str,+ document: Document,+ selected: MatchingModel,+ base_url: Optional[str] = None,+):+ """+ Smaller helper to reduce duplication when just outputting suggestions to the console+ """+ doc_str = str(document)+ if base_url is not None:+ stdout.write(style_func.SUCCESS(doc_str))+ stdout.write(style_func.SUCCESS(f"{base_url}/documents/{document.pk}"))+ else:+ stdout.write(style_func.SUCCESS(f"{doc_str} [{document.pk}]"))+ stdout.write(f"Suggest {suggestion_type}: {selected}")++def set_correspondent(sender,document: Document,@@ -63,7 +82,8 @@ def set_correspondent(use_first=True,suggest=False,base_url=None,- color=False,+ stdout=None,+ style_func=None,**kwargs,):if document.correspondent and not replace:@@ -90,23 +110,14 @@ def set_correspondent(if selected or replace:if suggest:- if base_url:- print(- termcolors.colorize(str(document), fg="green")- if color- else str(document),- )- print(f"{base_url}/documents/{document.pk}")- else:- print(- (- termcolors.colorize(str(document), fg="green")- if color- else str(document)- )- + f" [{document.pk}]",- )- print(f"Suggest correspondent {selected}")+ _suggestion_printer(+ stdout,+ style_func,+ "correspondent",+ document,+ selected,+ base_url,+ )else:logger.info(f"Assigning correspondent {selected} to {document}",@@ -126,7 +137,8 @@ def set_document_type(use_first=True,suggest=False,base_url=None,- color=False,+ stdout=None,+ style_func=None,**kwargs,):if document.document_type and not replace:@@ -154,23 +166,14 @@ def set_document_type(if selected or replace:if suggest:- if base_url:- print(- termcolors.colorize(str(document), fg="green")- if color- else str(document),- )- print(f"{base_url}/documents/{document.pk}")- else:- print(- (- termcolors.colorize(str(document), fg="green")- if color- else str(document)- )- + f" [{document.pk}]",- )- print(f"Suggest document type {selected}")+ _suggestion_printer(+ stdout,+ style_func,+ "document type",+ document,+ selected,+ base_url,+ )else:logger.info(f"Assigning document type {selected} to {document}",@@ -189,7 +192,8 @@ def set_tags(replace=False,suggest=False,base_url=None,- color=False,+ stdout=None,+ style_func=None,**kwargs,):if replace:@@ -212,26 +216,16 @@ def set_tags(]if not relevant_tags and not extra_tags:return+ doc_str = style_func.SUCCESS(str(document))if base_url:- print(- termcolors.colorize(str(document), fg="green")- if color- else str(document),- )- print(f"{base_url}/documents/{document.pk}")+ stdout.write(doc_str)+ stdout.write(f"{base_url}/documents/{document.pk}")else:- print(- (- termcolors.colorize(str(document), fg="green")- if color- else str(document)- )- + f" [{document.pk}]",- )+ stdout.write(doc_str + style_func.SUCCESS(f" [{document.pk}]"))if relevant_tags:- print("Suggest tags: " + ", ".join([t.name for t in relevant_tags]))+ stdout.write("Suggest tags: " + ", ".join([t.name for t in relevant_tags]))if extra_tags:- print("Extra tags: " + ", ".join([t.name for t in extra_tags]))+ stdout.write("Extra tags: " + ", ".join([t.name for t in extra_tags]))else:if not relevant_tags:return@@ -254,7 +248,8 @@ def set_storage_path(use_first=True,suggest=False,base_url=None,- color=False,+ stdout=None,+ style_func=None,**kwargs,):if document.storage_path and not replace:@@ -285,23 +280,14 @@ def set_storage_path(if selected or replace:if suggest:- if base_url:- print(- termcolors.colorize(str(document), fg="green")- if color- else str(document),- )- print(f"{base_url}/documents/{document.pk}")- else:- print(- (- termcolors.colorize(str(document), fg="green")- if color- else str(document)- )- + f" [{document.pk}]",- )- print(f"Suggest storage directory {selected}")+ _suggestion_printer(+ stdout,+ style_func,+ "storage directory",+ document,+ selected,+ base_url,+ )else:logger.info(f"Assigning storage path {selected} to {document}",diff --git a/src/documents/tests/test_api_bulk_download.py b/src/documents/tests/test_api_bulk_download.pyindex 57912c65c..43299b77d 100644--- a/src/documents/tests/test_api_bulk_download.py+++ b/src/documents/tests/test_api_bulk_download.py@@ -246,8 +246,6 @@ class TestBulkDownload(DirectoriesMixin, APITestCase):self.doc3.title = "Title 2 - Doc 3"self.doc3.save()- print(self.doc3.archive_path)- print(self.doc3.archive_filename)response = self.client.post(self.ENDPOINT,diff --git a/src/documents/tests/test_management_consumer.py b/src/documents/tests/test_management_consumer.pyindex 99d5d410e..7e2707403 100644--- a/src/documents/tests/test_management_consumer.py+++ b/src/documents/tests/test_management_consumer.py@@ -88,10 +88,10 @@ class ConsumerThreadMixin(DocumentConsumeDelayMixin):):eq = filecmp.cmp(input_doc.original_file, self.sample_file, shallow=False)if not eq:- print("Consumed an INVALID file.")+ print("Consumed an INVALID file.") # noqa: T201raise ConsumerError("Incomplete File READ FAILED")else:- print("Consumed a perfectly valid file.")+ print("Consumed a perfectly valid file.") # noqa: T201def slow_write_file(self, target, incomplete=False):with open(self.sample_file, "rb") as f:@@ -102,11 +102,11 @@ class ConsumerThreadMixin(DocumentConsumeDelayMixin):with open(target, "wb") as f:# this will take 2 seconds, since the file is about 20k.- print("Start writing file.")+ print("Start writing file.") # noqa: T201for b in chunked(1000, pdf_bytes):f.write(b)sleep(0.1)- print("file completed.")+ print("file completed.") # noqa: T201@override_settings(diff --git a/src/documents/tests/test_management_fuzzy.py b/src/documents/tests/test_management_fuzzy.pyindex c215c43ca..7cc1f265e 100644--- a/src/documents/tests/test_management_fuzzy.py+++ b/src/documents/tests/test_management_fuzzy.py@@ -196,7 +196,7 @@ class TestFuzzyMatchCommand(TestCase):self.assertEqual(Document.objects.count(), 3)stdout, _ = self.call_command("--delete")- print(stdout)+lines = [x.strip() for x in stdout.split("\n") if len(x.strip())]self.assertEqual(len(lines), 3)self.assertEqual(diff --git a/src/documents/tests/test_workflows.py b/src/documents/tests/test_workflows.pyindex ba5c53a78..95f903239 100644--- a/src/documents/tests/test_workflows.py+++ b/src/documents/tests/test_workflows.py@@ -1,16 +1,19 @@from datetime import timedeltafrom pathlib import Path+from typing import TYPE_CHECKINGfrom unittest import mockfrom django.contrib.auth.models import Groupfrom django.contrib.auth.models import User-from django.db.models import QuerySetfrom django.utils import timezonefrom guardian.shortcuts import assign_permfrom guardian.shortcuts import get_groups_with_permsfrom guardian.shortcuts import get_users_with_permsfrom rest_framework.test import APITestCase+if TYPE_CHECKING:+ from django.db.models import QuerySet+from documents import tasksfrom documents.data_models import ConsumableDocumentfrom documents.data_models import DocumentSourcediff --git a/src/documents/tests/utils.py b/src/documents/tests/utils.pyindex 4c3305d13..ba435d5c3 100644--- a/src/documents/tests/utils.py+++ b/src/documents/tests/utils.py@@ -340,7 +340,6 @@ class DummyProgressManager:def __init__(self, filename: str, task_id: Optional[str] = None) -> None:self.filename = filenameself.task_id = task_id- print("hello world")self.payloads = []def __enter__(self):diff --git a/src/paperless/auth.py b/src/paperless/auth.pyindex 98e2a8b30..ba9320b5d 100644--- a/src/paperless/auth.py+++ b/src/paperless/auth.py@@ -1,3 +1,5 @@+import logging+from django.conf import settingsfrom django.contrib import authfrom django.contrib.auth.middleware import PersistentRemoteUserMiddleware@@ -6,6 +8,8 @@ from django.http import HttpRequestfrom django.utils.deprecation import MiddlewareMixinfrom rest_framework import authentication+logger = logging.getLogger("paperless.auth")+class AutoLoginMiddleware(MiddlewareMixin):def process_request(self, request: HttpRequest):@@ -35,7 +39,7 @@ class AngularApiAuthenticationOverride(authentication.BaseAuthentication):and request.headers["Referer"].startswith("http://localhost:4200/")):user = User.objects.filter(is_staff=True).first()- print(f"Auto-Login with user {user}")+ logger.debug(f"Auto-Login with user {user}")return (user, None)else:return None