Updated the ValueComment validator so that the comment has to be on the right side...
authorphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Sun, 27 Nov 2011 17:19:10 +0000 (17:19 +0000)
committerphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Sun, 27 Nov 2011 17:19:10 +0000 (17:19 +0000)
git-svn-id: http://www.winterrodeln.org/svn/servermediawiki/trunk/wrpylib@1015 7aebc617-e5e2-0310-91dc-80fb5f6d2477

wrpylib/wrvalidators.py

index bad2e857ea1ce12616255413df2c0f817a6bba6e..825050e15b9a2073d83547ea78ac24c9333ccf84 100644 (file)
@@ -126,7 +126,7 @@ class Loop(formencode.FancyValidator):
     
     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))
 
@@ -185,10 +185,14 @@ class GermanTristateFloat(GermanTristateTuple):
 
 
 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
@@ -200,16 +204,16 @@ class ValueComment(formencode.FancyValidator):
             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):