From 7b324f94d950470168fdc59d1b9a4f1e9e100c69 Mon Sep 17 00:00:00 2001 From: janezd Date: Fri, 20 Oct 2023 10:17:59 +0200 Subject: [PATCH] Distance File: Don't accept dropped .xlsx files --- Orange/widgets/unsupervised/owdistancefile.py | 9 ++++++++- Orange/widgets/unsupervised/tests/test_owdistancefile.py | 8 ++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Orange/widgets/unsupervised/owdistancefile.py b/Orange/widgets/unsupervised/owdistancefile.py index bed14aada18..c851f90656a 100644 --- a/Orange/widgets/unsupervised/owdistancefile.py +++ b/Orange/widgets/unsupervised/owdistancefile.py @@ -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: @@ -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 diff --git a/Orange/widgets/unsupervised/tests/test_owdistancefile.py b/Orange/widgets/unsupervised/tests/test_owdistancefile.py index 9e6232d3e1c..0fa526a7890 100644 --- a/Orange/widgets/unsupervised/tests/test_owdistancefile.py +++ b/Orange/widgets/unsupervised/tests/test_owdistancefile.py @@ -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):