Browse Source

make quoting implicit

pull/119/head
Alexander Gordeyev 9 years ago
parent
commit
ac25d0e34d
4 changed files with 27 additions and 19 deletions
  1. 4
      gooey/gui/util/quoting.py
  2. 1
      gooey/gui/widgets/components.py
  3. 39
      gooey/gui/widgets/widget_pack.py
  4. 2
      gooey/gui/windows/layouts.py

4
gooey/gui/util/quoting.py

@ -7,7 +7,3 @@ if sys.platform.startswith("win"):
else: # POSIX shell
def quote(value):
return "'{}'".format('{}'.format(value).replace("'", "'\\''"))
def maybe_quote(string):
return '"{}"'.format(string) if not re.match(r'^".*"$', string) else string

1
gooey/gui/widgets/components.py

@ -234,6 +234,7 @@ DirChooser = lambda data: BaseGuiComponent(data=data, widget_pack=widget_
FileSaver = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.FileSaverPayload())
DateChooser = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.DateChooserPayload())
TextField = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.TextInputPayload())
CommandField = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.TextInputPayload(no_qouting=True))
Dropdown = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.DropdownPayload())
Counter = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.CounterPayload())
MultiDirChooser = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.MultiDirChooserPayload())

39
gooey/gui/widgets/widget_pack.py

@ -1,8 +1,7 @@
from functools import partial
import re
from gooey.gui.lang import i18n
from gooey.gui.util.filedrop import FileDrop
from gooey.gui.util.quoting import maybe_quote
from gooey.gui.util.quoting import quote
__author__ = 'Chris'
@ -69,9 +68,9 @@ class BaseChooser(WidgetPack):
def getValue(self):
value = self.text_box.GetValue()
if self.option_string and value:
return '{0} {1}'.format(self.option_string, maybe_quote(value))
return '{0} {1}'.format(self.option_string, quote(value))
else:
return maybe_quote(value) if value else ''
return quote(value) if value else ''
def onButton(self, evt):
raise NotImplementedError
@ -86,6 +85,13 @@ class BaseFileChooser(BaseChooser):
BaseChooser.__init__(self)
self.dialog = dialog
def getValue(self):
value = ' '.join(quote(x) for x in self.text_box.GetValue().split(os.pathsep))
if self.option_string and value:
return '{} {}'.format(self.option_string, value)
else:
return value or ''
def onButton(self, evt):
dlg = self.dialog(self.parent)
result = (self.get_path(dlg)
@ -96,10 +102,9 @@ class BaseFileChooser(BaseChooser):
def get_path(self, dlg):
if isinstance(dlg, wx.DirDialog) or isinstance(dlg, CalendarDlg):
return maybe_quote(dlg.GetPath())
return dlg.GetPath()
else:
paths = dlg.GetPaths()
return maybe_quote(paths[0]) if len(paths) < 2 else ' '.join(map(maybe_quote, paths))
return os.pathsep.join(dlg.GetPaths())
class MyMultiDirChooser(MDD.MultiDirDialog):
def __init(self, *args, **kwargs):
@ -123,9 +128,10 @@ DateChooserPayload = partial(BaseFileChooser, dialog=CalendarDlg)
MultiDirChooserPayload = partial(BaseFileChooser, dialog=lambda parent: MyMultiDirChooser(parent, title="Select Directories", defaultPath=os.getcwd(), agwStyle=MDD.DD_MULTIPLE|MDD.DD_DIR_MUST_EXIST))
class TextInputPayload(WidgetPack):
def __init__(self):
def __init__(self, no_quoting=False):
self.widget = None
self.option_string = None
self.no_quoting = no_quoting
def build(self, parent, data):
self.option_string = self.get_command(data)
@ -138,11 +144,15 @@ class TextInputPayload(WidgetPack):
return self.widget
def getValue(self):
if self.no_quoting:
_quote = lambda value: value
else:
_quote = lambda value: quote(value)
value = self.widget.GetValue()
if value and self.option_string:
return '{} {}'.format(self.option_string, value)
return '{} {}'.format(self.option_string, _quote(value))
else:
return '"{}"'.format(value) if value else ''
return _quote(value) if value else ''
def _SetValue(self, text):
# used for testing
@ -168,12 +178,13 @@ class DropdownPayload(WidgetPack):
return self.widget
def getValue(self):
if self.widget.GetValue() == self.default_value:
value = self.widget.GetValue()
if value == self.default_value:
return ''
elif self.widget.GetValue() and self.option_string:
return '{} {}'.format(self.option_string, self.widget.GetValue())
elif value and self.option_string:
return '{} {}'.format(self.option_string, quote(value))
else:
return self.widget.GetValue()
return quote(value) if value else ''
def _SetValue(self, text):
# used for testing

2
gooey/gui/windows/layouts.py

@ -9,7 +9,7 @@ from gooey.gui.util import wx_util
basic_config = {
'required': [{
'type': 'TextField',
'type': 'CommandField',
'data': {
'display_name': 'Enter Commands',
'help': 'Enter command line arguments',

Loading…
Cancel
Save