Skip to content

Commit

Permalink
Merge pull request #626 from VesnaT/fix_import_doc
Browse files Browse the repository at this point in the history
[FIX] Import Documents: Handle loading folder with no readable files
  • Loading branch information
ajdapretnar authored Mar 11, 2021
2 parents b62673a + 49c6029 commit b02d221
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
7 changes: 7 additions & 0 deletions orangecontrib/text/import_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
log = logging.getLogger(__name__)


class NoDocumentsException(Exception):
pass


class Reader(metaclass=Registry):
def __init__(self, path, replace_white_space=False):
self.path = path
Expand Down Expand Up @@ -168,6 +172,9 @@ def run(self):
n_paths = len(paths)
batch = []

if n_paths == 0:
raise NoDocumentsException()

for path in paths:
if len(batch) == 1 and self._report_progress is not None:
self._report_progress(
Expand Down
12 changes: 8 additions & 4 deletions orangecontrib/text/widgets/owimportdocuments.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
from Orange.widgets.widget import Output

from orangecontrib.text.corpus import Corpus
from orangecontrib.text.import_documents import ImportDocuments
from orangecontrib.text.import_documents import ImportDocuments, \
NoDocumentsException

try:
from orangecanvas.preview.previewbrowser import TextLabel
Expand Down Expand Up @@ -526,13 +527,15 @@ def __onRunFinished(self):
task = self.__pendingTask
self.__pendingTask = None

corpus, errors = None, []
try:
corpus, errors = task.future.result()
except NoDocumentsException:
state = State.Error
self.error("Folder contains no readable files.")
except Exception:
sys.excepthook(*sys.exc_info())
state = State.Error
corpus = None
errors = []
self.error(traceback.format_exc())
else:
state = State.Done
Expand All @@ -544,7 +547,8 @@ def __onRunFinished(self):
if corpus.domain.class_var else 0

self.corpus = corpus
self.corpus.name = "Documents"
if self.corpus:
self.corpus.name = "Documents"
self.skipped_documents = errors

if len(errors):
Expand Down
11 changes: 11 additions & 0 deletions orangecontrib/text/widgets/tests/test_owimportdocuments.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import unittest
from unittest.mock import patch, Mock

from Orange.widgets.tests.base import WidgetTest
from orangecontrib.text.widgets.owimportdocuments import OWImportDocuments
Expand Down Expand Up @@ -81,6 +82,16 @@ def test_info_box(self):
"No document set selected", self.widget.info_area.text()
)

@patch("orangecontrib.text.import_documents.ImportDocuments.scan",
Mock(return_value=[]))
def test_load_empty_folder(self):
widget = self.create_widget(OWImportDocuments)
path = os.path.join(os.path.dirname(__file__), "data/documents")
widget.setCurrentPath(path)
widget.reload()
self.wait_until_finished(widget=widget)
self.assertIsNone(self.get_output(widget.Outputs.data))


if __name__ == "__main__":
unittest.main()

0 comments on commit b02d221

Please sign in to comment.