Skip to content

Commit cd5f4ce

Browse files
rjollosrjollos
authored andcommitted
Added a stripws parameter to the parse_args function.
1 parent 77521d5 commit cd5f4ce

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

trac/wiki/api.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def format(formatter, ns, target, label, fullmatch=None):
202202
"""
203203

204204

205-
def parse_args(args, strict=True):
205+
def parse_args(args, strict=True, stripws=False):
206206
"""Utility for parsing macro "content" and splitting them into arguments.
207207
208208
The content is split along commas, unless they are escaped with a
@@ -211,6 +211,10 @@ def parse_args(args, strict=True):
211211
:param args: a string containing macros arguments
212212
:param strict: if `True`, only Python-like identifiers will be
213213
recognized as keyword arguments
214+
:param stripws: if `True`, leading and trailing whitespace is stripped from
215+
the arguments. However, regardless of this parameter,
216+
:param: strict always governs whether whitespace is allowed
217+
between the kw and the `=` character.
214218
215219
Example usage::
216220
@@ -222,6 +226,9 @@ def parse_args(args, strict=True):
222226
(['Some text', ' some other arg, with a comma.'], {'mode': ' 3'})
223227
>>> parse_args('milestone=milestone1,status!=closed', strict=False)
224228
([], {'status!': 'closed', 'milestone': 'milestone1'})
229+
>>> parse_args(' Some text, mode= 3, arg , othermode= on ', \
230+
stripws=True)
231+
(['Some text', 'arg'], {'othermode': 'on', 'mode': '3'})
225232
226233
"""
227234
largs, kwargs = [], {}
@@ -236,8 +243,13 @@ def parse_args(args, strict=True):
236243
kw = arg[:m.end()-1].strip()
237244
if strict:
238245
kw = unicode(kw).encode('utf-8')
239-
kwargs[kw] = arg[m.end():]
246+
if stripws:
247+
kwargs[kw] = arg[m.end():].strip()
248+
else:
249+
kwargs[kw] = arg[m.end():]
240250
else:
251+
if stripws:
252+
arg = arg.strip()
241253
largs.append(arg)
242254
return largs, kwargs
243255

0 commit comments

Comments
 (0)