Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] OWFile: Show error msg when file doesn't exists #2024

Merged
merged 1 commit into from
Feb 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions Orange/widgets/data/owfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ class Warning(widget.OWWidget.Warning):
file_too_big = widget.Msg("The file is too large to load automatically."
" Press Reload to load.")

class Error(widget.OWWidget.Error):
file_not_found = widget.Msg("File not found.")

def __init__(self):
super().__init__()
RecentPathsWComboMixin.__init__(self)
Expand Down Expand Up @@ -219,7 +222,8 @@ def __init__(self):

if self.source == self.LOCAL_FILE:
last_path = self.last_path()
if last_path and os.path.getsize(last_path) > self.SIZE_LIMIT:
if last_path and os.path.exists(last_path) and \
os.path.getsize(last_path) > self.SIZE_LIMIT:
self.Warning.file_too_big()
return

Expand Down Expand Up @@ -271,7 +275,13 @@ def load_data(self):
self.closeContext()
self.domain_editor.set_domain(None)
self.apply_button.setEnabled(False)
self.Warning.file_too_big.clear()
self.clear_messages()
self.set_file_list()
if self.last_path() and not os.path.exists(self.last_path()):
self.Error.file_not_found()
self.send("Data", None)
self.info.setText("No data.")
return

error = None
try:
Expand Down
32 changes: 30 additions & 2 deletions Orange/widgets/data/tests/test_owfile.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# Test methods with long descriptive names can omit docstrings
# pylint: disable=missing-docstring
from os import path
from os import path, remove
from unittest.mock import Mock

import numpy as np

from AnyQt.QtCore import QMimeData, QPoint, Qt, QUrl
from AnyQt.QtGui import QDragEnterEvent, QDropEvent

import Orange
from Orange.data import FileFormat, dataset_dirs, StringVariable
from Orange.data import FileFormat, dataset_dirs, StringVariable, Table, \
Domain, DiscreteVariable
from Orange.widgets.data.owfile import OWFile
from Orange.widgets.tests.base import WidgetTest

Expand Down Expand Up @@ -99,3 +102,28 @@ def test_no_last_path(self):
self.create_widget(OWFile, stored_settings={"recent_paths": []})
# Doesn't crash and contains a single item, (none).
self.assertEqual(self.widget.file_combo.count(), 1)

def test_file_not_found(self):
# Create a dummy file
file_name = "test_owfile_data.tab"
domainA = Domain([DiscreteVariable("d1", values=("a", "b"))],
DiscreteVariable("c1", values=("aaa", "bbb")))
dataA = Table(domainA, np.array([[0], [1], [0], [np.nan]]),
np.array([0, 1, 0, 1]))
dataA.save(file_name)

# Open the file with the widget
self.open_dataset(file_name)
self.assertEqual(self.get_output("Data").domain, dataA.domain)

# Delete the file and try to reload it
remove(file_name)
self.widget.load_data()
self.assertEqual(file_name, path.basename(self.widget.last_path()))
self.assertTrue(self.widget.Error.file_not_found.is_shown())
self.assertIsNone(self.get_output("Data"))
self.assertEqual(self.widget.info.text(), "No data.")

# Open a sample dataset
self.open_dataset("iris")
self.assertFalse(self.widget.Error.file_not_found.is_shown())
8 changes: 7 additions & 1 deletion Orange/widgets/utils/filedialogs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

from AnyQt.QtCore import QFileInfo
from AnyQt.QtCore import QFileInfo, Qt
from AnyQt.QtGui import QBrush
from AnyQt.QtWidgets import \
QMessageBox, QFileDialog, QFileIconProvider, QComboBox

Expand Down Expand Up @@ -281,6 +282,8 @@ def _relocate_recent_files(self):
rec.append(
RecentPath.create(recent.search(search_paths), search_paths, **kwargs)
)
else:
rec.append(recent)
# change the list in-place for the case the widgets wraps this list
# in some model (untested!)
self.recent_paths[:] = rec
Expand Down Expand Up @@ -351,6 +354,9 @@ def set_file_list(self):
for i, recent in enumerate(self.recent_paths):
self.file_combo.addItem(recent.basename)
self.file_combo.model().item(i).setToolTip(recent.abspath)
if not os.path.exists(recent.abspath):
self.file_combo.setItemData(i, QBrush(Qt.red),
Qt.TextColorRole)

def workflowEnvChanged(self, key, value, oldvalue):
super().workflowEnvChanged(key, value, oldvalue)
Expand Down