Commits

Anonymous committed 8d90063

Added RegexPattern type

RegexPattern returns a compiled re object.

Comments (0)

Files changed (2)

docs/schema/types.rst

 .. autoclass:: dotconf.schema.types.String
 .. autoclass:: dotconf.schema.types.Regex
 .. autoclass:: dotconf.schema.types.NamedRegex
+.. autoclass:: dotconf.schema.types.RegexPattern
 .. autoclass:: dotconf.schema.types.IPAddress
 .. autoclass:: dotconf.schema.types.IPNetwork
 .. autoclass:: dotconf.schema.types.Url

dotconf/schema/types.py

         return value.groupdict()
 
 
+class RegexPattern(String):
+
+    """ A re Python object.
+
+    :param flags: python re compile `flag <http://docs.python.org/library/re.html#re.compile>`_
+
+    Example in configuration::
+
+        match = '/[a-z]+(-[a-z]+)?\.css'
+    """
+
+    def __init__(self, flags=0):
+        super(String, self).__init__()
+        self.flags = flags
+
+    def validate(self, value):
+        value = super(RegexPattern, self).validate(value)
+        # Try to compile regex object:
+        try:
+            value = re.compile(value, self.flags)
+        except re.error:
+            raise ValidationError('Bad format for regular expression')
+        return value
+
+
 class IPAddress(String):
 
     """ A string based type representing an ipv4 or ipv6 address.