Skip to content

Commit

Permalink
Merge pull request #3700 from janezd/owsave-remove-extra-extensions
Browse files Browse the repository at this point in the history
[FIX] Save Data: Remove extra file extensions
  • Loading branch information
lanzagar authored Mar 29, 2019
2 parents f1dff6c + 78f26e5 commit d3ec31f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
20 changes: 16 additions & 4 deletions Orange/widgets/data/owsave.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Orange/widgets/data/tests/test_owsave.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)"]
Expand Down

0 comments on commit d3ec31f

Please sign in to comment.