Skip to content

Commit

Permalink
wmltools: refine comma_split function
Browse files Browse the repository at this point in the history
First: minor change of the argument name passed to the function
from "value" to "csstring", since the string might not be an
attribute value.

Second: more testing pointed out that an empty list would not
meet the "if list:" condition and would not be extended. So
make it explicitly "!= None".

Third: when asking on IRC about trailing whitespace, it was
noted that utils::split also removed empty entries, which
reminded me that I should do that as well.

The biggest change is that the function will now rstrip() by
default, with other options for dealing with trailing
whitespace offered.
  • Loading branch information
groggyd88 committed Mar 13, 2015
1 parent 134df26 commit 13c9885
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions data/tools/wesnoth/wmltools.py
Expand Up @@ -52,12 +52,20 @@ def attr_strip(value):
value = value.strip()
return string_strip(value)

def comma_split(value, list=None):
"Split a comma-separated value, and append the entries to a list if specified."
vallist = [x.lstrip()for x in value.split(",")]
# lstrip: wml-tags.lua split function will remove leading whitespace
# of items in comma-separated lists but not trailing whitespace
if list:
def comma_split(csstring, list=None, strip="r"):
"Split a comma-separated string, and append the entries to a list if specified."
vallist = [x.lstrip() for x in csstring.split(",") if x.lstrip()]
# strip=: utils::split will remove trailing whitespace from items in comma-
# separated lists but the wml-tags.lua split function only removes leading
# whitespace. So two flags are offered to change default behavior: one to
# lstrip() only, the other to warn about trailing whitespace.
if 'w' in strip:
for item in vallist:
if re.search('\s$', item):
print 'Trailing whitespace may be problematic: "%s" in "%s"' % (item, csstring)
if 'l' not in strip:
vallist = [x.rstrip() for x in vallist]
if list != None:
list.extend(vallist)
else:
return vallist
Expand Down

0 comments on commit 13c9885

Please sign in to comment.