Commit f02f36ed authored by Adrien Di Mascio's avatar Adrien Di Mascio
Browse files

new regexp constraint for yams

parent 8ca3979064a3
ChangeLog for yams
------------------
-- 0.13.0
* new RegexpBasedConstraint
2007-06-25 -- 0.12.0
* IntervalBoundConstraint replaces BoundConstraint
* new import_erschema method available in schema definition files
......
......@@ -21,7 +21,7 @@
modname = 'yams'
# release version
numversion = (0, 12, 0)
numversion = (0, 13, 0)
version = '.'.join([str(num) for num in numversion])
# license and copyright
......
......@@ -8,6 +8,8 @@
__docformat__ = "restructuredtext en"
import warnings
import re
from yams.interfaces import IConstraint, IVocabularyConstraint
class BaseConstraint(object):
......@@ -88,6 +90,33 @@ class SizeConstraint(BaseConstraint):
return cls(**kwargs)
deserialize = classmethod(deserialize)
class RegexpConstraint(BaseConstraint):
"""specifies a set of allowed patterns for a string value
"""
__implements__ = IConstraint
def __init__(self, regexp, flags=0):
self.regexp = regexp
self.flags = flags
self._rgx = re.compile(regexp, flags)
def check(self, entity, rtype, value):
"""return true if the value satisfy the constraint, else false"""
return self._rgx.match(value, self.flags)
def __str__(self):
return 'regexp %s' % self.serialize()
def serialize(self):
"""simple text serialization"""
return u'%s,%s' % (self.regexp, self.flags)
def deserialize(cls, value):
"""simple text deserialization"""
regexp, flags = value.rsplit(',', 1)
return cls(regexp, int(flags))
deserialize = classmethod(deserialize)
class BoundConstraint(BaseConstraint):
"""the int/float bound constraint :
......
......@@ -33,6 +33,16 @@ class ConstraintTC(TestCase):
cstr = IntervalBoundConstraint.deserialize('12.13;13.14')
self.assertEquals(cstr.minvalue, 12.13)
self.assertEquals(cstr.maxvalue, 13.14)
def test_regexp_serialization(self):
cstr = RegexpConstraint('[a-z]+,[A-Z]+', 12)
self.assertEquals(cstr.serialize(), '[a-z]+,[A-Z]+,12')
def test_regexp_deserialization(self):
cstr = RegexpConstraint.deserialize('[a-z]+,[A-Z]+,12')
self.assertEquals(cstr.regexp, '[a-z]+,[A-Z]+')
self.assertEquals(cstr.flags, 12)
if __name__ == '__main__':
......
......@@ -65,7 +65,7 @@ class BaseSchemaTC(TestCase):
enote.set_rproperty('date', 'cardinality', '11')
eaffaire.set_rproperty('sujet', 'constraints', [SizeConstraint(128)])
eaffaire.set_rproperty('ref', 'constraints', [SizeConstraint(12)])
eaffaire.set_rproperty('ref', 'constraints', [SizeConstraint(12), RegexpConstraint(r'[A-Z]+\d+')])
eperson.set_rproperty('nom', 'constraints', [SizeConstraint(20, 10)])
eperson.set_rproperty('prenom', 'constraints', [SizeConstraint(64)])
eperson.set_rproperty('tel', 'constraints', [IntervalBoundConstraint(maxvalue=999999)])
......@@ -111,6 +111,8 @@ ATTRIBUTE_BAD_VALUES = (
('type', 'bof, je suis pas unicode, alors...'),
('date', None),
]),
('Affaire', [('ref', 'ginco01'), ('ref', 'GINCO'),],
),
)
ATTRIBUTE_GOOD_VALUES = (
......@@ -120,7 +122,8 @@ ATTRIBUTE_GOOD_VALUES = (
('tel', 83433), ('fax', None), ('fax', 12),
('TEST', True), ('TEST', False)]),
('Note', [('date', '2229-01-31 00:00')]),
('Affaire', [('starton', '00:00')]),
('Affaire', [('starton', '00:00'),
('ref', u'GINCO01')]),
)
RELATIONS_BAD_VALUES = {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment