Skip to content

Commit 60395fb

Browse files
authored
Merge pull request #2024 from VesnaT/owfile_not_found
[FIX] OWFile: Show error msg when file doesn't exists
2 parents 70f0e1b + 99b7b42 commit 60395fb

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

Orange/widgets/data/owfile.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ class Warning(widget.OWWidget.Warning):
117117
file_too_big = widget.Msg("The file is too large to load automatically."
118118
" Press Reload to load.")
119119

120+
class Error(widget.OWWidget.Error):
121+
file_not_found = widget.Msg("File not found.")
122+
120123
def __init__(self):
121124
super().__init__()
122125
RecentPathsWComboMixin.__init__(self)
@@ -219,7 +222,8 @@ def __init__(self):
219222

220223
if self.source == self.LOCAL_FILE:
221224
last_path = self.last_path()
222-
if last_path and os.path.getsize(last_path) > self.SIZE_LIMIT:
225+
if last_path and os.path.exists(last_path) and \
226+
os.path.getsize(last_path) > self.SIZE_LIMIT:
223227
self.Warning.file_too_big()
224228
return
225229

@@ -271,7 +275,13 @@ def load_data(self):
271275
self.closeContext()
272276
self.domain_editor.set_domain(None)
273277
self.apply_button.setEnabled(False)
274-
self.Warning.file_too_big.clear()
278+
self.clear_messages()
279+
self.set_file_list()
280+
if self.last_path() and not os.path.exists(self.last_path()):
281+
self.Error.file_not_found()
282+
self.send("Data", None)
283+
self.info.setText("No data.")
284+
return
275285

276286
error = None
277287
try:

Orange/widgets/data/tests/test_owfile.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# Test methods with long descriptive names can omit docstrings
22
# pylint: disable=missing-docstring
3-
from os import path
3+
from os import path, remove
44
from unittest.mock import Mock
55

6+
import numpy as np
7+
68
from AnyQt.QtCore import QMimeData, QPoint, Qt, QUrl
79
from AnyQt.QtGui import QDragEnterEvent, QDropEvent
810

911
import Orange
10-
from Orange.data import FileFormat, dataset_dirs, StringVariable
12+
from Orange.data import FileFormat, dataset_dirs, StringVariable, Table, \
13+
Domain, DiscreteVariable
1114
from Orange.widgets.data.owfile import OWFile
1215
from Orange.widgets.tests.base import WidgetTest
1316

@@ -99,3 +102,28 @@ def test_no_last_path(self):
99102
self.create_widget(OWFile, stored_settings={"recent_paths": []})
100103
# Doesn't crash and contains a single item, (none).
101104
self.assertEqual(self.widget.file_combo.count(), 1)
105+
106+
def test_file_not_found(self):
107+
# Create a dummy file
108+
file_name = "test_owfile_data.tab"
109+
domainA = Domain([DiscreteVariable("d1", values=("a", "b"))],
110+
DiscreteVariable("c1", values=("aaa", "bbb")))
111+
dataA = Table(domainA, np.array([[0], [1], [0], [np.nan]]),
112+
np.array([0, 1, 0, 1]))
113+
dataA.save(file_name)
114+
115+
# Open the file with the widget
116+
self.open_dataset(file_name)
117+
self.assertEqual(self.get_output("Data").domain, dataA.domain)
118+
119+
# Delete the file and try to reload it
120+
remove(file_name)
121+
self.widget.load_data()
122+
self.assertEqual(file_name, path.basename(self.widget.last_path()))
123+
self.assertTrue(self.widget.Error.file_not_found.is_shown())
124+
self.assertIsNone(self.get_output("Data"))
125+
self.assertEqual(self.widget.info.text(), "No data.")
126+
127+
# Open a sample dataset
128+
self.open_dataset("iris")
129+
self.assertFalse(self.widget.Error.file_not_found.is_shown())

Orange/widgets/utils/filedialogs.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22

3-
from AnyQt.QtCore import QFileInfo
3+
from AnyQt.QtCore import QFileInfo, Qt
4+
from AnyQt.QtGui import QBrush
45
from AnyQt.QtWidgets import \
56
QMessageBox, QFileDialog, QFileIconProvider, QComboBox
67

@@ -281,6 +282,8 @@ def _relocate_recent_files(self):
281282
rec.append(
282283
RecentPath.create(recent.search(search_paths), search_paths, **kwargs)
283284
)
285+
else:
286+
rec.append(recent)
284287
# change the list in-place for the case the widgets wraps this list
285288
# in some model (untested!)
286289
self.recent_paths[:] = rec
@@ -351,6 +354,9 @@ def set_file_list(self):
351354
for i, recent in enumerate(self.recent_paths):
352355
self.file_combo.addItem(recent.basename)
353356
self.file_combo.model().item(i).setToolTip(recent.abspath)
357+
if not os.path.exists(recent.abspath):
358+
self.file_combo.setItemData(i, QBrush(Qt.red),
359+
Qt.TextColorRole)
354360

355361
def workflowEnvChanged(self, key, value, oldvalue):
356362
super().workflowEnvChanged(key, value, oldvalue)

0 commit comments

Comments
 (0)