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 else: # POSIX shell
def quote(value): def quote(value):
return "'{}'".format('{}'.format(value).replace("'", "'\\''")) 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()) FileSaver = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.FileSaverPayload())
DateChooser = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.DateChooserPayload()) DateChooser = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.DateChooserPayload())
TextField = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.TextInputPayload()) 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()) Dropdown = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.DropdownPayload())
Counter = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.CounterPayload()) Counter = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.CounterPayload())
MultiDirChooser = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.MultiDirChooserPayload()) 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 from functools import partial
import re
from gooey.gui.lang import i18n from gooey.gui.lang import i18n
from gooey.gui.util.filedrop import FileDrop from gooey.gui.util.filedrop import FileDrop
from gooey.gui.util.quoting import maybe_quote from gooey.gui.util.quoting import quote
__author__ = 'Chris' __author__ = 'Chris'
@ -69,9 +68,9 @@ class BaseChooser(WidgetPack):
def getValue(self): def getValue(self):
value = self.text_box.GetValue() value = self.text_box.GetValue()
if self.option_string and value: 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: else:
return maybe_quote(value) if value else '' return quote(value) if value else ''
def onButton(self, evt): def onButton(self, evt):
raise NotImplementedError raise NotImplementedError
@ -86,6 +85,13 @@ class BaseFileChooser(BaseChooser):
BaseChooser.__init__(self) BaseChooser.__init__(self)
self.dialog = dialog 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): def onButton(self, evt):
dlg = self.dialog(self.parent) dlg = self.dialog(self.parent)
result = (self.get_path(dlg) result = (self.get_path(dlg)
@ -96,10 +102,9 @@ class BaseFileChooser(BaseChooser):
def get_path(self, dlg): def get_path(self, dlg):
if isinstance(dlg, wx.DirDialog) or isinstance(dlg, CalendarDlg): if isinstance(dlg, wx.DirDialog) or isinstance(dlg, CalendarDlg):
return maybe_quote(dlg.GetPath()) return dlg.GetPath()
else: else:
paths = dlg.GetPaths() return os.pathsep.join(dlg.GetPaths())
return maybe_quote(paths[0]) if len(paths) < 2 else ' '.join(map(maybe_quote, paths))
class MyMultiDirChooser(MDD.MultiDirDialog): class MyMultiDirChooser(MDD.MultiDirDialog):
def __init(self, *args, **kwargs): 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)) 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): class TextInputPayload(WidgetPack):
def __init__(self): def __init__(self, no_quoting=False):
self.widget = None self.widget = None
self.option_string = None self.option_string = None
self.no_quoting = no_quoting
def build(self, parent, data): def build(self, parent, data):
self.option_string = self.get_command(data) self.option_string = self.get_command(data)
@ -138,11 +144,15 @@ class TextInputPayload(WidgetPack):
return self.widget return self.widget
def getValue(self): def getValue(self):
if self.no_quoting:
_quote = lambda value: value
else:
_quote = lambda value: quote(value)
value = self.widget.GetValue() value = self.widget.GetValue()
if value and self.option_string: if value and self.option_string:
return '{} {}'.format(self.option_string, value) return '{} {}'.format(self.option_string, _quote(value))
else: else:
return '"{}"'.format(value) if value else '' return _quote(value) if value else ''
def _SetValue(self, text): def _SetValue(self, text):
# used for testing # used for testing
@ -168,12 +178,13 @@ class DropdownPayload(WidgetPack):
return self.widget return self.widget
def getValue(self): def getValue(self):
if self.widget.GetValue() == self.default_value: value = self.widget.GetValue()
if value == self.default_value:
return '' return ''
elif self.widget.GetValue() and self.option_string: elif value and self.option_string:
return '{} {}'.format(self.option_string, self.widget.GetValue()) return '{} {}'.format(self.option_string, quote(value))
else: else:
return self.widget.GetValue() return quote(value) if value else ''
def _SetValue(self, text): def _SetValue(self, text):
# used for testing # used for testing

2
gooey/gui/windows/layouts.py

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

|||||||
100:0
Loading…
Cancel
Save