new pattern matcher - partially done
authorandy
Wed, 15 Mar 2006 12:22:10 +0000
changeset 2573 273f77e79536
parent 2572 d652d0a4f34e
child 2574 d81949596070
new pattern matcher - partially done
reportlab/lib/validators.py
--- a/reportlab/lib/validators.py	Mon Mar 13 23:33:51 2006 +0000
+++ b/reportlab/lib/validators.py	Wed Mar 15 12:22:10 2006 +0000
@@ -7,7 +7,7 @@
 used in an attribute map.
 """
 
-import string, sys
+import string, sys,re
 from types import *
 _SequenceTypes = (ListType,TupleType)
 _NumberTypes = (FloatType,IntType)
@@ -239,6 +239,20 @@
     def test(self,x):
         return isinstance(x,self._klass)
 
+class matchesPattern(Validator):
+    """Matches value, or its string representation, against regex"""
+    def __init__(self, pattern):
+        self._pattern = re.compile(pattern)
+
+    def test(self,x):
+        print 'testing %s against %s' % (x, self._pattern)
+        if type(x) is StringType:
+            text = x
+        else:
+            text = str(x)
+        return (self._pattern.match(text) <> None)
+
+
 class DerivedValue:
     """This is used for magic values which work themselves out.
     An example would be an "inherit" property, so that one can have
@@ -257,6 +271,7 @@
         a correct stack of parent nodes."""
         return None
 
+
 class Inherit(DerivedValue):
     def __repr__(self):
         return "inherit"