Skip to content

Commit

Permalink
closes amoffat#43. did @fruch's suggestion from awhile back. repr() h…
Browse files Browse the repository at this point in the history
…ad hidden pitfalls
  • Loading branch information
Andrew Moffat committed Feb 24, 2012
1 parent a859df7 commit e61c3a5
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions pbs.py
Expand Up @@ -277,33 +277,55 @@ def _extract_call_args(kwargs):
return call_args, kwargs


@staticmethod
def _compile_args(args, kwargs):
def _format_arg(self, arg):
if IS_PY3: arg = str(arg)
else: arg = unicode(arg).encode("utf8")
arg = '"%s"' % arg

return arg

def _compile_args(self, args, kwargs):
processed_args = []

# aggregate positional args
for arg in args:
if isinstance(arg, (list, tuple)):
for sub_arg in arg: processed_args.append(repr(sub_arg))
else: processed_args.append(repr(arg))
for sub_arg in arg: processed_args.append(self._format_arg(sub_arg))
else: processed_args.append(self._format_arg(arg))

# aggregate the keyword arguments
for k,v in kwargs.items():
# we're passing a short arg as a kwarg, example:
# cut(d="\t")
if len(k) == 1:
if v is True: arg = "-"+k
else: arg = "-%s %r" % (k, v)
else: arg = "-%s %s" % (k, self._format_arg(v))

# we're doing a long arg
else:
k = k.replace("_", "-")

if v is True: arg = "--"+k
else: arg = "--%s=%r" % (k, v)
else: arg = "--%s=%s" % (k, self._format_arg(v))
processed_args.append(arg)

processed_args = shlex.split(" ".join(processed_args))
try: processed_args = shlex.split(" ".join(processed_args))
except ValueError, e:
if str(e) == "No closing quotation":
exc_msg = """No closing quotation. If you're trying to escape \
double quotes, please note that you need to escape the escape:
# incorrect
print pbs.echo('test print double quote: \"')
print pbs.echo('test print double quote: \\"')
print pbs.echo("test print double quote: \\"")
print pbs.echo("test print double quote: \\\\"")
# correct
print pbs.echo('test print double quote: \\\\"')
print pbs.echo("test print double quote: \\\\\\"")
"""
raise ValueError(exc_msg)
return processed_args


Expand Down

0 comments on commit e61c3a5

Please sign in to comment.