def from_python(self, value):
# we don't call self.validator.to_python(self.validator.from_python(value))
- # here because our to_python implementation basically leaves the input untouches
+ # here because our to_python implementation basically leaves the input untouched
# and so should from_python do.
return self.validator.from_python(self.validator.to_python(value))
class ValueComment(formencode.FancyValidator):
- """Converts value with a potentially optional comment to a python tuple:
- u'' <=> (None, None)
- u'value' <=> (u'value', None)
- u'value (comment)' <=> (u'value', u'comment')"""
+ """Converts value with a potentially optional comment to a python tuple. If a comment is present, the
+ closing bracket has to be the rightmost character.
+ u'' <=> (None, None)
+ u'value' <=> (u'value', None)
+ u'value (comment)' <=> (u'value', u'comment')
+ u'[[link (linkcomment)]]' <=> (u'[[link (linkcomment)]]', None)
+ u'[[link (linkcomment)]] (comment)' <=> (u'[[link (linkcomment)]]', comment)
+ """
def __init__(self, value_validator=UnicodeNone(), comment_validator=UnicodeNone(), comment_is_optional=True):
self.value_validator = value_validator
self.comment_validator = comment_validator
v = value
c = value
else:
- left = value.find('(')
right = value.rfind(')')
- if left < 0 and right < 0:
+ if right+1 != len(value):
if not self.comment_is_optional: raise formencode.Invalid(u'Mandatory comment not present', value, None)
v = value
c = u''
- elif left >= 0 and right >= 0 and left < right:
+ else:
+ left = value.rfind('(')
+ if left < 0: raise formencode.Invalid(u'Invalid format', value, None)
v = value[:left].strip()
c = value[left+1:right].strip()
- else: raise formencode.Invalid(u'Invalid format', value, None)
return self.value_validator.to_python(v), self.comment_validator.to_python(c)
def from_python(self, value):