Skip to content

Commit 90ecf9a

Browse files
committed
RecentFiles: Check for missing file in workflow dir
When opening saved workflow and file widget detects missing file, check if the file is placed in the same directory as workflow is saved.
1 parent 459044b commit 90ecf9a

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

Orange/widgets/data/tests/test_owfile.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Test methods with long descriptive names can omit docstrings
22
# pylint: disable=missing-docstring
3-
from os import path, remove
3+
from os import path, remove, getcwd
44
from unittest.mock import Mock, patch
55
import pickle
66
import tempfile
@@ -27,6 +27,7 @@
2727
from Orange.widgets.utils.domaineditor import ComboDelegate, VarTypeDelegate, VarTableModel
2828

2929
TITANIC_PATH = path.join(path.dirname(Orange.__file__), 'datasets', 'titanic.tab')
30+
orig_path_exists = path.exists
3031

3132

3233
class FailedSheetsFormat(FileFormat):
@@ -440,3 +441,26 @@ def test_adds_origin(self):
440441
attrs = data1.domain["image"].attributes
441442
self.assertIn("origin", attrs)
442443
self.assertIn("origin1", attrs["origin"])
444+
445+
@patch("Orange.widgets.widget.OWWidget.workflowEnv",
446+
Mock(return_value={"basedir": getcwd()}))
447+
def test_open_moved_workflow(self):
448+
"""Test opening workflow that has been moved to another location
449+
(i.e. sent by email), considering data file is stored in the same
450+
directory as the workflow.
451+
"""
452+
file = tempfile.NamedTemporaryFile(dir=getcwd(), delete=False)
453+
file_name = file.name
454+
base_name = path.basename(file_name)
455+
try:
456+
recent_path = RecentPath(
457+
path.join("temp/datasets", base_name), "",
458+
path.join("datasets", base_name)
459+
)
460+
stored_settings = {"recent_paths": [recent_path]}
461+
w = self.create_widget(OWFile, stored_settings=stored_settings)
462+
w.load_data()
463+
self.assertEqual(w.file_combo.count(), 1)
464+
self.assertFalse(w.Error.file_not_found.is_shown())
465+
finally:
466+
remove(file_name)

Orange/widgets/utils/filedialogs.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,17 @@ def search(self, searchpaths):
240240
def resolve(self, searchpaths):
241241
if self.prefix is None and os.path.exists(self.abspath):
242242
return self
243-
elif self.prefix is not None:
243+
else:
244244
for prefix, base in searchpaths:
245-
if self.prefix == prefix:
245+
path = None
246+
if self.prefix and self.prefix == prefix:
246247
path = os.path.join(base, self.relpath)
247-
if os.path.exists(path):
248-
return RecentPath(
249-
os.path.normpath(path), self.prefix, self.relpath,
250-
file_format=self.file_format)
248+
elif not self.prefix and prefix == "basedir":
249+
path = os.path.join(base, self.basename)
250+
if path and os.path.exists(path):
251+
return RecentPath(
252+
os.path.normpath(path), self.prefix, self.relpath,
253+
file_format=self.file_format)
251254
return None
252255

253256
@property
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import os
2+
import unittest
3+
from tempfile import NamedTemporaryFile
4+
5+
from Orange.widgets.utils.filedialogs import RecentPath
6+
7+
8+
class TestRecentPath(unittest.TestCase):
9+
def test_resolve(self):
10+
file = NamedTemporaryFile(dir=os.getcwd(), delete=False)
11+
file_name = file.name
12+
base_name = os.path.basename(file_name)
13+
try:
14+
recent_path = RecentPath(
15+
os.path.join("temp/datasets", base_name), "",
16+
os.path.join("datasets", base_name)
17+
)
18+
search_paths = [("basedir", os.getcwd())]
19+
self.assertIsNotNone(recent_path.resolve(search_paths))
20+
finally:
21+
os.remove(file_name)

0 commit comments

Comments
 (0)