Skip to content

Commit

Permalink
Don't quote simple strings that don't need it
Browse files Browse the repository at this point in the history
  • Loading branch information
keis committed Feb 9, 2017
1 parent d2d732c commit 16f7ce4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -163,7 +163,7 @@ test-uzbl-core-sandbox: sandbox uzbl-core sandbox-install-uzbl-core sandbox-inst
rm -rf ./sandbox/usr

test-uzbl-browser-sandbox: sandbox uzbl-browser sandbox-install-uzbl-browser sandbox-install-example-data
./sandbox/env.sh ${PYTHON} -m uzbl.event_manager restart -navv &
./sandbox/env.sh ${PYTHON} -m uzbl.event_manager restart -navvv &
sleep 1
./sandbox/env.sh uzbl-browser http://www.uzbl.org --verbose
./sandbox/env.sh ${PYTHON} -m uzbl.event_manager stop -vv -o /dev/null
Expand Down
12 changes: 10 additions & 2 deletions tests/event-manager/testonevent.py
Expand Up @@ -15,15 +15,23 @@ def setUp(self):

def test_command(self):
oe = OnEventPlugin[self.uzbl]
event, command = 'FOO', "test 'test'"
event, command = 'FOO', 'test test'

oe.parse_on_event('FOO test test')
oe.event_handler('', on_event=event)
self.uzbl.send.assert_called_once_with(command)

def test_command_with_quotes(self):
oe = OnEventPlugin[self.uzbl]
event, command = 'FOO', "test 'string with spaces'"

oe.parse_on_event('FOO test "string with spaces"')
oe.event_handler('', on_event=event)
self.uzbl.send.assert_called_once_with(command)

def test_matching_pattern(self):
oe = OnEventPlugin[self.uzbl]
event, command = 'FOO', "test 'test'"
event, command = 'FOO', "test test"

oe.parse_on_event('FOO [ BAR ] test test')
oe.event_handler('BAR else', on_event=event)
Expand Down
15 changes: 13 additions & 2 deletions uzbl/plugins/cmd_expand.py
@@ -1,3 +1,8 @@
import re

SIMPLE = re.compile('^[a-zA-Z]+$')


def escape(str):
for (level, char) in [(1, '\\'), (1, "'"), (1, '"'), (1, '@')]:
str = str.replace(char, (level * '\\') + char)
Expand Down Expand Up @@ -38,17 +43,23 @@ def cmd_expand(cmd, args):
return cmd


def format_arg(a):
if SIMPLE.match(a):
return a
return repr(a)


def send_user_command(uzbl, cmd, args):
if cmd[0] == 'event':
has_var = any('@' in x for x in cmd)
event = cmd[1]
args = cmd_expand(' '.join(repr(c) for c in cmd[2:]), args)
args = cmd_expand(' '.join(format_arg(c) for c in cmd[2:]), args)
if not has_var:
# Bypass the roundtrip to uzbl and dispatch immediately
uzbl.event(event, args)
else:
uzbl.send(' '.join(('event', event, args)))
else:
cmd = ' '.join((cmd[0],) + tuple(repr(c) for c in cmd[1:]))
cmd = ' '.join((cmd[0],) + tuple(format_arg(c) for c in cmd[1:]))
cmd = cmd_expand(cmd, args)
uzbl.send(cmd)

0 comments on commit 16f7ce4

Please sign in to comment.