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 29, 2023
1 parent 219852d commit 69a7d1b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
11 changes: 10 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,14 @@ 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")
if os.path.splitext(path)[0] == ".dst":
return True
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 ren(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(ren("xlsx_files/distances_with_nans.xlsx")))
self.assertFalse(handler.canDropFile(ren("xlsx_files/distances_nonsquare.xlsx")))
self.assertFalse(handler.canDropFile(ren("datasets/lenses.tab")))
self.assertFalse(handler.canDropFile("test.bin"))

def test_parametersFromFile(self):
Expand Down

0 comments on commit 69a7d1b

Please sign in to comment.