From 84ae5ce957e5e20b0db8564a2e0b8f4b70ac3d2f Mon Sep 17 00:00:00 2001 From: "Bess L. Walker" Date: Sat, 18 Apr 2020 15:11:06 -0600 Subject: [PATCH] Issue 554: Fix MultiFileChooser output formatting. Also adds MultiFileChooser to __ALL__. Includes tests for formatters.multiFileChooser. Other choosers may need similar fixes; I have not experimented with them. --- gooey/gui/components/widgets/choosers.py | 4 +++ gooey/tests/test_formatters.py | 45 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/gooey/gui/components/widgets/choosers.py b/gooey/gui/components/widgets/choosers.py index 674ed71..8e7a485 100644 --- a/gooey/gui/components/widgets/choosers.py +++ b/gooey/gui/components/widgets/choosers.py @@ -5,6 +5,7 @@ from gooey.gui.components.widgets.bases import TextContainer, BaseChooser __ALL__ = [ 'FileChooser', + 'MultiFileChooser', 'FileSaver', 'DirChooser', 'MultiDirChooser', @@ -21,6 +22,9 @@ class MultiFileChooser(BaseChooser): # todo: allow wildcard from argparse widget_class = core.MultiFileChooser + def formatOutput(self, metatdata, value): + return formatters.multiFileChooser(metatdata, value) + class FileSaver(BaseChooser): # todo: allow wildcard diff --git a/gooey/tests/test_formatters.py b/gooey/tests/test_formatters.py index 071108f..a3e175f 100644 --- a/gooey/tests/test_formatters.py +++ b/gooey/tests/test_formatters.py @@ -1,4 +1,6 @@ import argparse +import os +import shlex import unittest from gooey.gui import formatters @@ -42,3 +44,46 @@ class TestFormatters(unittest.TestCase): parser.add_argument('-v', '--verbose', action='count') parser.parse_args(result.split()) + def test_multifilechooser_formatter(self): + """ + Should return files (quoted), separated by spaces if there is more + than one, preceeded by optional command if the argument is optional. + + Assumes the argument has been created with some form of nargs, which + only makes sense for possibly choosing multiple values. + """ + + # Helper function to generalize the variants we need to test + def multifilechooser_helper(names): + # Note that the MultiFileChooser widget produces a single string with + # paths separated by os.pathsep. + if names: + prefix = names[0] + ' ' + else: + prefix = '' + + expected_outputs = [ + (names, None, ''), + (names, prefix + '"abc"', 'abc'), + (names, prefix + '"abc" "def"', os.pathsep.join(['abc', 'def'])), + # paths with spaces + (names, prefix + '"a b c"', 'a b c'), + (names, prefix + '"a b c" "d e f"', os.pathsep.join(['a b c', 'd e f'])), + ] + + for commands, expected, widget_result in expected_outputs: + result = formatters.multiFileChooser({'commands': commands}, widget_result) + self.assertEqual(result, expected) + # make sure that argparse actually accepts it as valid. + if result: + parser = argparse.ArgumentParser() + if not names: + names = ["file"] + parser.add_argument(names[0], nargs='+') + parser.parse_args(shlex.split(result)) + + # Positional argument, with nargs + multifilechooser_helper([]) + + # Optional argument, with nargs + multifilechooser_helper(["-f", "--file"])