Permalink
Browse files

Added a `stripws` parameter to the `parse_args` function.

  • Loading branch information...
1 parent 77521d5 commit cd5f4cee92b9361d0258467e66d4229fa3e80aaa rjollos committed May 7, 2013
Showing with 14 additions and 2 deletions.
  1. +14 −2 trac/wiki/api.py
View
@@ -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
@@ -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
@rjollos
rjollos May 8, 2013 owner

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

+ between the kw and the `=` character.
Example usage::
@@ -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 = [], {}
@@ -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

0 comments on commit cd5f4ce

Please sign in to comment.