summaryrefslogtreecommitdiffstats
path: root/ipalib
diff options
context:
space:
mode:
authorJason Gerard DeRose <jderose@redhat.com>2009-02-06 12:36:49 -0700
committerRob Crittenden <rcritten@redhat.com>2009-02-17 16:03:08 -0500
commitb5b2e55be589bc97ba030d0ad070d787c646ddd0 (patch)
tree87a9509b9ef3843a841252ed9d60537e072381b3 /ipalib
parent18cecdc515f04853a316fa74b1247878ae77ba15 (diff)
downloadfreeipa-b5b2e55be589bc97ba030d0ad070d787c646ddd0.tar.gz
freeipa-b5b2e55be589bc97ba030d0ad070d787c646ddd0.tar.xz
freeipa-b5b2e55be589bc97ba030d0ad070d787c646ddd0.zip
Add pattern matching to Str and Bytes
Diffstat (limited to 'ipalib')
-rw-r--r--ipalib/parameters.py31
1 files changed, 25 insertions, 6 deletions
diff --git a/ipalib/parameters.py b/ipalib/parameters.py
index fe32c92f..e07808de 100644
--- a/ipalib/parameters.py
+++ b/ipalib/parameters.py
@@ -31,6 +31,7 @@ TODO:
* Add maxvalue, minvalue kwargs and rules to `Int` and `Float`
"""
+import re
from types import NoneType
from util import make_repr
from request import ugettext
@@ -38,6 +39,7 @@ from plugable import ReadOnly, lock, check_name
from errors2 import ConversionError, RequirementError, ValidationError
from constants import NULLS, TYPE_ERROR, CALLABLE_ERROR
+
class DefaultFrom(ReadOnly):
"""
Derive a default value from other supplied values.
@@ -779,6 +781,7 @@ class Data(Param):
('minlength', int, None),
('maxlength', int, None),
('length', int, None),
+ ('pattern', (basestring,), None),
)
def __init__(self, name, *rules, **kw):
@@ -814,6 +817,16 @@ class Data(Param):
self.nice, self.minlength)
)
+ def _rule_pattern(self, _, value):
+ """
+ Check pattern (regex) contraint.
+ """
+ assert type(value) is self.type
+ if self.re.match(value) is None:
+ return _('must match pattern "%(pattern)s"') % dict(
+ pattern=self.pattern,
+ )
+
class Bytes(Data):
"""
@@ -830,9 +843,12 @@ class Bytes(Data):
type = str
type_error = _('must be binary data')
- kwargs = Data.kwargs + (
- ('pattern', str, None),
- )
+ def __init__(self, name, *rules, **kw):
+ if kw.get('pattern', None) is None:
+ self.re = None
+ else:
+ self.re = re.compile(kw['pattern'])
+ super(Bytes, self).__init__(name, *rules, **kw)
def _rule_minlength(self, _, value):
"""
@@ -880,9 +896,12 @@ class Str(Data):
type = unicode
type_error = _('must be Unicode text')
- kwargs = Data.kwargs + (
- ('pattern', unicode, None),
- )
+ def __init__(self, name, *rules, **kw):
+ if kw.get('pattern', None) is None:
+ self.re = None
+ else:
+ self.re = re.compile(kw['pattern'], re.UNICODE)
+ super(Str, self).__init__(name, *rules, **kw)
def _convert_scalar(self, value, index=None):
"""