From 78f26e5ca31733887fd7f6674755330f718a5aa1 Mon Sep 17 00:00:00 2001 From: janezd Date: Thu, 28 Mar 2019 21:47:30 +0100 Subject: [PATCH] OWSave: Remove extra file extensions --- Orange/widgets/data/owsave.py | 20 ++++++++++++++++---- Orange/widgets/data/tests/test_owsave.py | 3 +++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Orange/widgets/data/owsave.py b/Orange/widgets/data/owsave.py index bb95c4b2f14..96bc3db8243 100644 --- a/Orange/widgets/data/owsave.py +++ b/Orange/widgets/data/owsave.py @@ -185,11 +185,23 @@ def _initial_start_dir(self): @staticmethod def _replace_extension(filename, extension): - known_extensions = map(OWSave._extension_from_filter, OWSave.filters) - for known_ext in sorted(known_extensions, key=len, reverse=True): - if filename.endswith(known_ext): - filename = filename[:-len(known_ext)] + """ + Remove all extensions that appear in any filter. + + Double extensions are broken in different weird ways across all systems, + including omitting some, like turning iris.tab.gz to iris.gz. This + function removes anything that can appear anywhere. + """ + known_extensions = set() + for filt in OWSave.filters: + known_extensions |= \ + set(OWSave._extension_from_filter(filt).split(".")) + known_extensions.remove("") + while True: + base, ext = os.path.splitext(filename) + if ext[1:] not in known_extensions: break + filename = base return filename + extension @staticmethod diff --git a/Orange/widgets/data/tests/test_owsave.py b/Orange/widgets/data/tests/test_owsave.py index 2c323124ade..ea11eb836ea 100644 --- a/Orange/widgets/data/tests/test_owsave.py +++ b/Orange/widgets/data/tests/test_owsave.py @@ -431,6 +431,9 @@ def test_save_file_dialog_enforces_extension_linux(self): dialog.selectFile("high.tab.gz") self.assertTrue(dialog.selectedFiles()[0].endswith("/high.csv")) + dialog.selectFile("high.tab.gz.tab.tab.gz") + self.assertTrue(dialog.selectedFiles()[0].endswith("/high.csv")) + def test_save_file_dialog_uses_valid_filters_linux(self): widget = self.widget widget._valid_filters = lambda: ["a (*.a)", "b (*.b)"]