Skip to content

Commit

Permalink
Added a stripws parameter to the parse_args function.
Browse files Browse the repository at this point in the history
  • Loading branch information
rjollos authored and rjollos committed May 8, 2013
1 parent 77521d5 commit cd5f4ce
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions trac/wiki/api.py
Expand Up @@ -202,7 +202,7 @@ def format(formatter, ns, target, label, fullmatch=None):
"""


def parse_args(args, strict=True):
def parse_args(args, strict=True, stripws=False):
"""Utility for parsing macro "content" and splitting them into arguments.
The content is split along commas, unless they are escaped with a
Expand All @@ -211,6 +211,10 @@ def parse_args(args, strict=True):
:param args: a string containing macros arguments
:param strict: if `True`, only Python-like identifiers will be
recognized as keyword arguments
:param stripws: if `True`, leading and trailing whitespace is stripped from
the arguments. However, regardless of this parameter,
:param: strict always governs whether whitespace is allowed

This comment has been minimized.

Copy link
@rjollos

rjollos May 8, 2013

Owner

FIX: :param: strict -> :param strict:

between the kw and the `=` character.
Example usage::
Expand All @@ -222,6 +226,9 @@ def parse_args(args, strict=True):
(['Some text', ' some other arg, with a comma.'], {'mode': ' 3'})
>>> parse_args('milestone=milestone1,status!=closed', strict=False)
([], {'status!': 'closed', 'milestone': 'milestone1'})
>>> parse_args(' Some text, mode= 3, arg , othermode= on ', \
stripws=True)
(['Some text', 'arg'], {'othermode': 'on', 'mode': '3'})
"""
largs, kwargs = [], {}
Expand All @@ -236,8 +243,13 @@ def parse_args(args, strict=True):
kw = arg[:m.end()-1].strip()
if strict:
kw = unicode(kw).encode('utf-8')
kwargs[kw] = arg[m.end():]
if stripws:
kwargs[kw] = arg[m.end():].strip()
else:
kwargs[kw] = arg[m.end():]
else:
if stripws:
arg = arg.strip()
largs.append(arg)
return largs, kwargs

Expand Down

0 comments on commit cd5f4ce

Please sign in to comment.