Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Save Data: Remove extra file extensions #3700

Merged
merged 1 commit into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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