Allow modifier % on styledtext

svn: r17578
This commit is contained in:
Benny Malengier 2011-05-27 09:06:35 +00:00
parent b75e636f33
commit a71636ec4e
2 changed files with 40 additions and 4 deletions

View File

@ -126,6 +126,43 @@ class StyledText(object):
def __ne__(self, other):
return self._string != other._string or self._tags != other._tags
def __mod__(self, other):
"""Implement '%' operation on the class."""
# test whether the formatting operation is valid at all
self._string % other
result = self.__class__(self._string, self._tags)
i0 = 0
while True:
i1 = result._string.find('%', i0)
if i1 < 0:
break
if result._string[i1+1] == '(':
i2 = result._string.find(')', i1+3)
param_name = result._string[i1+2:i2]
else:
i2 = i1
param_name = None
for i3 in range(i2+1, len(result._string)):
if result._string[i3] in 'diouxXeEfFgGcrs%':
break
if param_name is not None:
param = other[param_name]
elif isinstance(other, tuple):
param = other[0]
other = other[1:]
else:
param = other
if not isinstance(param, StyledText):
param = StyledText('%' + result._string[i2+1:i3+1] % param)
(before, after) = result.split(result._string[i1:i3+1], 1)
result = before + param + after
i0 = i3 + 1
return result
# private methods

View File

@ -215,10 +215,9 @@ def _find_records(db, filter, callname):
father = db.get_person_from_handle(father_handle)
mother = db.get_person_from_handle(mother_handle)
name = StyledText("").join([
_Person_get_styled_primary_name(father, callname),
_(" and "),
_Person_get_styled_primary_name(mother, callname)])
name = StyledText(_("%(father)s and %(mother)s")) % {
'father': _Person_get_styled_primary_name(father, callname),
'mother': _Person_get_styled_primary_name(mother, callname)}
_record(None, family_mostchildren,
len(family.get_child_ref_list()),