Skip to content

Commit

Permalink
Merge pull request #278 from janezd/open_filename_dialog-all-files
Browse files Browse the repository at this point in the history
open_filename_dialog: Allow reading *.*
  • Loading branch information
markotoplak authored Sep 20, 2024
2 parents 9ae212f + 7c6f189 commit f84655f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
3 changes: 3 additions & 0 deletions i18n/si/msgs.jaml
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,8 @@ utils/filedialogs.py:
Save as...: Shrani kot...
def `open_filename_dialog`:
Open...: Odpri...
*: false
All files (*.*): Vse datoteke (*.*)
All readable files (*{}): Vse znane datoteke (*{})
' *': false
;;: false
Expand Down Expand Up @@ -1080,6 +1082,7 @@ utils/matplotlib_export.py:
\n: false
def `scene_code`:
import matplotlib.pyplot as plt: false
import numpy as np: false
from numpy import array: false
plt.clf(): false
bottom: false
Expand Down
8 changes: 6 additions & 2 deletions orangewidget/utils/filedialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ def open_filename_dialog(start_dir: str, start_filter: str, file_formats,
start_dir (str): initial directory, optionally including the filename
start_filter (str): initial filter
file_formats (a list of FileFormat): file formats
add_all (bool): add a filter for all supported extensions
add_all (False, True, or `"*"`; default True): add a filter for all supported extensions.
If set to `"*"`, show an option to read all files, *.*.
title (str): title of the dialog
dialog: a function that creates a QT dialog
Returns:
Expand All @@ -172,7 +173,10 @@ def open_filename_dialog(start_dir: str, start_filter: str, file_formats,
filters = [format_filter(f) for f in file_formats]

# add all readable files option
if add_all:
if add_all == "*":
file_formats.insert(0, None)
filters.insert(0, "All files (*.*)")
elif add_all and len(file_formats) > 1:
all_extensions = set()
for f in file_formats:
all_extensions.update(f.EXTENSIONS)
Expand Down
48 changes: 48 additions & 0 deletions orangewidget/utils/tests/test_filedialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,54 @@ class ABCFormat:
self.assertEqual(file_format, None)
self.assertEqual(file_filter, None)

def test_add_all(self):
class ABCFormat:
EXTENSIONS = ('.abc', '.jkl')
DESCRIPTION = 'abc file'
PRIORITY = 30

class DEFFormat:
EXTENSIONS = ('.def', )
DESCRIPTION = 'def file'
PRIORITY = 40

# Add all known extensions
dialog = Mock(return_value=("foo.abc", ""))
open_filename_dialog(".", "", [ABCFormat, DEFFormat], dialog=dialog)
self.assertIn("(*.abc *.def *.jkl)", dialog.call_args[0][3])
self.assertIn("(*.abc *.jkl)", dialog.call_args[0][3])
self.assertIn("(*.def)", dialog.call_args[0][3])

# Add all extensions (*.*)
dialog = Mock(return_value=("foo.abc", ""))
open_filename_dialog(".", "", [ABCFormat, DEFFormat],
add_all="*", dialog=dialog)
self.assertIn("(*.*)", dialog.call_args[0][3])
self.assertIn("(*.abc *.jkl)", dialog.call_args[0][3])
self.assertIn("(*.def)", dialog.call_args[0][3])

# Don't add any extensions
dialog = Mock(return_value=("foo.abc", ""))
open_filename_dialog(".", "", [ABCFormat, DEFFormat],
add_all=False, dialog=dialog)
self.assertNotIn("(*.abc *.def *.jkl)", dialog.call_args[0][3])
self.assertIn("(*.abc *.jkl)", dialog.call_args[0][3])
self.assertIn("(*.def)", dialog.call_args[0][3])

# With a single format, add all known extensions is ignored
dialog = Mock(return_value=("foo.abc", ""))
open_filename_dialog(".", "", [ABCFormat],
dialog=dialog)
self.assertNotIn("(*.*)", dialog.call_args[0][3])
self.assertEqual(dialog.call_args[0][3].count("(*.abc *.jkl)"), 1)

# With a single format, add all extensions (*.*) still applies
dialog = Mock(return_value=("foo.abc", ""))
open_filename_dialog(".", "", [ABCFormat],
add_all="*", dialog=dialog)
self.assertIn("(*.*)", dialog.call_args[0][3])
self.assertIn("(*.abc *.jkl)", dialog.call_args[0][3])


if __name__ == "__main__":
unittest.main()

0 comments on commit f84655f

Please sign in to comment.