A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
checking file types against parsers in the consumer.
jonaswinkler834352130c97
diff --git a/src/documents/consumer.py b/src/documents/consumer.pyindex e8f527974..7bae5c2a9 100755--- a/src/documents/consumer.py+++ b/src/documents/consumer.py@@ -41,21 +41,6 @@ class Consumer(LoggingMixin):raise ConsumerError("Cannot consume {}: It is not a file".format(self.path))- def pre_check_file_extension(self):- extensions = get_supported_file_extensions()- _, ext = os.path.splitext(self.filename)-- if not ext:- raise ConsumerError(- f"Not consuming {self.filename}: File type unknown."- )-- if ext not in extensions:- raise ConsumerError(- f"Not consuming {self.filename}: File extension {ext} does "- f"not map to any known file type ({str(extensions)})"- )-def pre_check_duplicate(self):with open(self.path, "rb") as f:checksum = hashlib.md5(f.read()).hexdigest()@@ -98,7 +83,6 @@ class Consumer(LoggingMixin):# Make sure that preconditions for consuming the file are met.self.pre_check_file_exists()- self.pre_check_file_extension()self.pre_check_directories()self.pre_check_duplicate()diff --git a/src/documents/management/commands/document_consumer.py b/src/documents/management/commands/document_consumer.pyindex ea63cd19d..5cecd6bf9 100644--- a/src/documents/management/commands/document_consumer.py+++ b/src/documents/management/commands/document_consumer.py@@ -11,6 +11,7 @@ from watchdog.events import FileSystemEventHandlerfrom watchdog.observers.polling import PollingObserverfrom documents.models import Tag+from documents.parsers import is_file_ext_supportedtry:from inotifyrecursive import INotify, flags@@ -37,11 +38,19 @@ def _tags_from_path(filepath):def _consume(filepath):+ if os.path.isdir(filepath):+ return+if not os.path.isfile(filepath):logger.debug(f"Not consuming file {filepath}: File has moved.")return+ if not is_file_ext_supported(os.path.splitext(filepath)[1]):+ logger.debug(+ f"Not consuming file {filepath}: Unknown file extension.")+ return+tag_ids = Nonetry:if settings.CONSUMER_SUBDIRS_AS_TAGS:@@ -181,7 +190,7 @@ class Command(BaseCommand):try:while not self.stop_flag:- for event in inotify.read(timeout=1000, read_delay=1000):+ for event in inotify.read(timeout=1000):if recursive:path = inotify.get_path(event.wd)else:diff --git a/src/documents/parsers.py b/src/documents/parsers.pyindex 7fc927c50..eeaaed886 100644--- a/src/documents/parsers.py+++ b/src/documents/parsers.py@@ -54,6 +54,13 @@ def get_default_file_extension(mime_type):return None+def is_file_ext_supported(ext):+ if ext:+ return ext.lower() in get_supported_file_extensions()+ else:+ return False++def get_supported_file_extensions():extensions = set()for response in document_consumer_declaration.send(None):diff --git a/src/documents/tests/test_consumer.py b/src/documents/tests/test_consumer.pyindex 99801923f..992d450db 100644--- a/src/documents/tests/test_consumer.py+++ b/src/documents/tests/test_consumer.py@@ -552,7 +552,7 @@ class TestConsumer(DirectoriesMixin, TestCase):try:self.consumer.try_consume_file(self.get_test_file())except ConsumerError as e:- self.assertTrue("File extension .pdf does not map to any" in str(e))+ self.assertTrue("No parsers abvailable for" in str(e))returnself.fail("Should throw exception")diff --git a/src/documents/tests/test_management_consumer.py b/src/documents/tests/test_management_consumer.pyindex c56b49a41..6973fdacf 100644--- a/src/documents/tests/test_management_consumer.py+++ b/src/documents/tests/test_management_consumer.py@@ -34,12 +34,12 @@ def chunked(size, source):yield source[i:i+size]-class TestConsumer(DirectoriesMixin, TransactionTestCase):+class ConsumerMixin:sample_file = os.path.join(os.path.dirname(__file__), "samples", "simple.pdf")def setUp(self) -> None:- super(TestConsumer, self).setUp()+ super(ConsumerMixin, self).setUp()self.t = Nonepatcher = mock.patch("documents.management.commands.document_consumer.async_task")self.task_mock = patcher.start()@@ -58,7 +58,7 @@ class TestConsumer(DirectoriesMixin, TransactionTestCase):# wait for the consumer to exit.self.t.join()- super(TestConsumer, self).tearDown()+ super(ConsumerMixin, self).tearDown()def wait_for_task_mock_call(self):n = 0@@ -69,7 +69,6 @@ class TestConsumer(DirectoriesMixin, TransactionTestCase):returnn += 1sleep(0.1)- self.fail("async_task was never called")# A bogus async_task that will simply check the file for# completeness and raise an exception otherwise.@@ -96,6 +95,9 @@ class TestConsumer(DirectoriesMixin, TransactionTestCase):sleep(0.1)print("file completed.")++class TestConsumer(DirectoriesMixin, ConsumerMixin, TransactionTestCase):+def test_consume_file(self):self.t_start()@@ -109,61 +111,26 @@ class TestConsumer(DirectoriesMixin, TransactionTestCase):args, kwargs = self.task_mock.call_argsself.assertEqual(args[1], f)- @override_settings(CONSUMER_POLLING=1)- def test_consume_file_polling(self):- self.test_consume_file()-- def test_consume_existing_file(self):- f = os.path.join(self.dirs.consumption_dir, "my_file.pdf")- shutil.copy(self.sample_file, f)-+ def test_consume_file_invalid_ext(self):self.t_start()- self.task_mock.assert_called_once()-- args, kwargs = self.task_mock.call_args- self.assertEqual(args[1], f)- @override_settings(CONSUMER_POLLING=1)- def test_consume_existing_file_polling(self):- self.test_consume_existing_file()-- @override_settings(CONSUMER_RECURSIVE=1)- @override_settings(CONSUMER_SUBDIRS_AS_TAGS=1)- def test_consume_file_with_path_tags(self):+ f = os.path.join(self.dirs.consumption_dir, "my_file.wow")+ shutil.copy(self.sample_file, f)- tag_names = ("existingTag", "Space Tag")- # Create a Tag prior to consuming a file using it in path- tag_ids = [Tag.objects.create(name=tag_names[0]).pk,]+ self.wait_for_task_mock_call()- self.t_start()+ self.task_mock.assert_not_called()- path = os.path.join(self.dirs.consumption_dir, *tag_names)- os.makedirs(path, exist_ok=True)- f = os.path.join(path, "my_file.pdf")- # Wait at least inotify read_delay for recursive watchers- # to be created for the new directories- sleep(1)+ def test_consume_existing_file(self):+ f = os.path.join(self.dirs.consumption_dir, "my_file.pdf")shutil.copy(self.sample_file, f)- self.wait_for_task_mock_call()-+ self.t_start()self.task_mock.assert_called_once()- # Add the pk of the Tag created by _consume()- tag_ids.append(Tag.objects.get(name=tag_names[1]).pk)-args, kwargs = self.task_mock.call_argsself.assertEqual(args[1], f)- # assertCountEqual has a bad name, but test that the first- # sequence contains the same elements as second, regardless of- # their order.- self.assertCountEqual(kwargs["override_tag_ids"], tag_ids)-- @override_settings(CONSUMER_POLLING=1)- def test_consume_file_with_path_tags_polling(self):- self.test_consume_file_with_path_tags()-@mock.patch("documents.management.commands.document_consumer.logger.error")def test_slow_write_pdf(self, error_logger):@@ -184,10 +151,6 @@ class TestConsumer(DirectoriesMixin, TransactionTestCase):args, kwargs = self.task_mock.call_argsself.assertEqual(args[1], fname)- @override_settings(CONSUMER_POLLING=1)- def test_slow_write_pdf_polling(self):- self.test_slow_write_pdf()-@mock.patch("documents.management.commands.document_consumer.logger.error")def test_slow_write_and_move(self, error_logger):@@ -210,10 +173,6 @@ class TestConsumer(DirectoriesMixin, TransactionTestCase):error_logger.assert_not_called()- @override_settings(CONSUMER_POLLING=1)- def test_slow_write_and_move_polling(self):- self.test_slow_write_and_move()-@mock.patch("documents.management.commands.document_consumer.logger.error")def test_slow_write_incomplete(self, error_logger):@@ -233,10 +192,6 @@ class TestConsumer(DirectoriesMixin, TransactionTestCase):# assert that we have an error logged with this invalid file.error_logger.assert_called_once()- @override_settings(CONSUMER_POLLING=1)- def test_slow_write_incomplete_polling(self):- self.test_slow_write_incomplete()-@override_settings(CONSUMPTION_DIR="does_not_exist")def test_consumption_directory_invalid(self):@@ -246,3 +201,62 @@ class TestConsumer(DirectoriesMixin, TransactionTestCase):def test_consumption_directory_unset(self):self.assertRaises(CommandError, call_command, 'document_consumer', '--oneshot')+++@override_settings(CONSUMER_POLLING=1)+class TestConsumerPolling(TestConsumer):+ # just do all the tests with polling+ pass+++@override_settings(CONSUMER_RECURSIVE=True)+class TestConsumerRecursive(TestConsumer):+ # just do all the tests with recursive+ pass+++@override_settings(CONSUMER_RECURSIVE=True)+@override_settings(CONSUMER_POLLING=1)+class TestConsumerRecursivePolling(TestConsumer):+ # just do all the tests with polling and recursive+ pass+++class TestConsumerTags(DirectoriesMixin, ConsumerMixin, TransactionTestCase):++ @override_settings(CONSUMER_RECURSIVE=True)+ @override_settings(CONSUMER_SUBDIRS_AS_TAGS=True)+ def test_consume_file_with_path_tags(self):++ tag_names = ("existingTag", "Space Tag")+ # Create a Tag prior to consuming a file using it in path+ tag_ids = [Tag.objects.create(name=tag_names[0]).pk,]++ self.t_start()++ path = os.path.join(self.dirs.consumption_dir, *tag_names)+ os.makedirs(path, exist_ok=True)+ f = os.path.join(path, "my_file.pdf")+ # Wait at least inotify read_delay for recursive watchers+ # to be created for the new directories+ sleep(1)+ shutil.copy(self.sample_file, f)++ self.wait_for_task_mock_call()++ self.task_mock.assert_called_once()++ # Add the pk of the Tag created by _consume()+ tag_ids.append(Tag.objects.get(name=tag_names[1]).pk)++ args, kwargs = self.task_mock.call_args+ self.assertEqual(args[1], f)++ # assertCountEqual has a bad name, but test that the first+ # sequence contains the same elements as second, regardless of+ # their order.+ self.assertCountEqual(kwargs["override_tag_ids"], tag_ids)++ @override_settings(CONSUMER_POLLING=1)+ def test_consume_file_with_path_tags_polling(self):+ self.test_consume_file_with_path_tags()