Skip to content

Commit

Permalink
Distance File: Don't accept dropped .xlsx files
Browse files Browse the repository at this point in the history
  • Loading branch information
janezd committed Oct 20, 2023
1 parent 0597b34 commit 44ac0ef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 8 additions & 1 deletion Orange/widgets/unsupervised/owdistancefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ def open_file(self):
err = str(exc)
self.Error.invalid_file(" \n"[len(err) > 40] + err)
else:
# If you add any other checks before accepting the file,
# you should probably mirror them in canDropFile
if distances.shape[0] != distances.shape[1]:
self.Error.non_square_matrix()
else:
Expand Down Expand Up @@ -158,7 +160,12 @@ def parametersFromFile(self, path):
return {"recent_paths": stored_recent_paths_prepend(self.WIDGET, r)}

def canDropFile(self, path: str) -> bool:
return os.path.splitext(path)[1].lower() in (".dst", ".xlsx")
try:
distances = DistMatrix.from_file(path)
except Exception: # pylint: disable=broad-except
return False
else:
return distances.shape[0] == distances.shape[1]


if __name__ == "__main__": # pragma: no cover
Expand Down
8 changes: 6 additions & 2 deletions Orange/widgets/unsupervised/tests/test_owdistancefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ def test_nan_to_num(self):

class TestOWDistanceFileDropHandler(unittest.TestCase):
def test_canDropFile(self):
def fn(name):
return os.path.join(os.path.split(Orange.tests.__file__)[0], name)

handler = OWDistanceFileDropHandler()
self.assertTrue(handler.canDropFile("test.dst"))
self.assertTrue(handler.canDropFile("test.xlsx"))
self.assertTrue(handler.canDropFile(fn("xlsx_files/distances_with_nans.xlsx")))
self.assertFalse(handler.canDropFile(fn("xlsx_files/distances_nonsquare.xlsx")))
self.assertFalse(handler.canDropFile(fn("datasets/lenses.tab")))
self.assertFalse(handler.canDropFile("test.bin"))

def test_parametersFromFile(self):
Expand Down

0 comments on commit 44ac0ef

Please sign in to comment.