[Checkins] SVN: z3c.schema/trunk/src/z3c/schema/regex/ added
regular expression field
Bernd Dorn
bernd.dorn at lovelysystems.com
Tue Dec 12 10:35:51 EST 2006
Log message for revision 71538:
added regular expression field
Changed:
A z3c.schema/trunk/src/z3c/schema/regex/
A z3c.schema/trunk/src/z3c/schema/regex/README.txt
A z3c.schema/trunk/src/z3c/schema/regex/__init__.py
A z3c.schema/trunk/src/z3c/schema/regex/field.py
A z3c.schema/trunk/src/z3c/schema/regex/interfaces.py
A z3c.schema/trunk/src/z3c/schema/regex/tests.py
-=-
Added: z3c.schema/trunk/src/z3c/schema/regex/README.txt
===================================================================
--- z3c.schema/trunk/src/z3c/schema/regex/README.txt 2006-12-11 23:58:05 UTC (rev 71537)
+++ z3c.schema/trunk/src/z3c/schema/regex/README.txt 2006-12-12 15:35:50 UTC (rev 71538)
@@ -0,0 +1,21 @@
+========================
+Regular expression field
+========================
+
+Let's first create the regex field.
+
+ >>> from z3c.schema.regex import Regex
+ >>> regex = Regex()
+
+The regex field only allows compilable regular expressions.
+
+ >>> regex.validate(r'.*')
+ >>> regex.validate(r'^\s+$')
+
+It does not validate regular expressions that do not compile.
+
+ >>> regex.validate('(i')
+ Traceback (most recent call last):
+ ...
+ InvalidRegex: '(i', unbalanced parenthesis
+
Property changes on: z3c.schema/trunk/src/z3c/schema/regex/README.txt
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.schema/trunk/src/z3c/schema/regex/__init__.py
===================================================================
--- z3c.schema/trunk/src/z3c/schema/regex/__init__.py 2006-12-11 23:58:05 UTC (rev 71537)
+++ z3c.schema/trunk/src/z3c/schema/regex/__init__.py 2006-12-12 15:35:50 UTC (rev 71538)
@@ -0,0 +1,19 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id$
+"""
+
+from z3c.schema.regex.interfaces import *
+from z3c.schema.regex.field import Regex
Property changes on: z3c.schema/trunk/src/z3c/schema/regex/__init__.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: z3c.schema/trunk/src/z3c/schema/regex/field.py
===================================================================
--- z3c.schema/trunk/src/z3c/schema/regex/field.py 2006-12-11 23:58:05 UTC (rev 71537)
+++ z3c.schema/trunk/src/z3c/schema/regex/field.py 2006-12-12 15:35:50 UTC (rev 71538)
@@ -0,0 +1,36 @@
+###############################################################################
+#
+# Copyright 2006 by refline (Schweiz) AG, CH-5630 Muri
+#
+###############################################################################
+"""Special Fields
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+import re
+import zope.interface
+import zope.schema
+import zope.schema.interfaces
+from z3c.schema.regex import interfaces
+
+class Regex(zope.schema.ASCIILine):
+ """Regex schema field.
+
+ Must be a compilable regular expression
+ """
+
+ zope.interface.implements(interfaces.IRegex)
+
+ def _validate(self, value):
+ super(Regex, self)._validate(value)
+ try:
+ re.compile(value)
+ except re.error, e:
+ raise interfaces.InvalidRegex, '%r, %s' % (value, e)
+
+ def fromUnicode(self, value):
+ v = str(value.strip())
+ self.validate(v)
+ return v
Property changes on: z3c.schema/trunk/src/z3c/schema/regex/field.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: z3c.schema/trunk/src/z3c/schema/regex/interfaces.py
===================================================================
--- z3c.schema/trunk/src/z3c/schema/regex/interfaces.py 2006-12-11 23:58:05 UTC (rev 71537)
+++ z3c.schema/trunk/src/z3c/schema/regex/interfaces.py 2006-12-12 15:35:50 UTC (rev 71538)
@@ -0,0 +1,28 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+import zope.schema
+import zope.schema.interfaces
+
+from z3c.i18n import MessageFactory as _
+
+class IRegex(zope.schema.interfaces.IASCIILine):
+ """Regular Expression field"""
+
+class InvalidRegex(zope.schema.ValidationError):
+ __doc__ = _("""The specified Regular Expression is not valid.""")
Property changes on: z3c.schema/trunk/src/z3c/schema/regex/interfaces.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: z3c.schema/trunk/src/z3c/schema/regex/tests.py
===================================================================
--- z3c.schema/trunk/src/z3c/schema/regex/tests.py 2006-12-11 23:58:05 UTC (rev 71537)
+++ z3c.schema/trunk/src/z3c/schema/regex/tests.py 2006-12-12 15:35:50 UTC (rev 71538)
@@ -0,0 +1,61 @@
+###############################################################################
+#
+# Copyright 2006 by refline (Schweiz) AG, CH-5630 Muri
+#
+###############################################################################
+"""Refline Recruiter Tests
+
+$Id$
+"""
+__docformat__ = 'restructuredtext'
+
+import unittest
+from zope.schema.interfaces import RequiredMissing
+from zope.schema.tests.test_field import FieldTestBase
+from zope.testing import doctest
+from zope.testing.doctestunit import DocFileSuite
+
+from z3c.schema.hostname import HostName
+from z3c.schema.hostname import InvalidHostName
+
+
+class HostNameTest(FieldTestBase):
+
+ _Field_Factory = HostName
+ _convert = str
+
+ def testValidate(self):
+ field = self._Field_Factory(title=u'HostName field', description=u'',
+ readonly=False, required=False)
+ field.validate(None)
+ field.validate(self._convert('host'))
+ field.validate(self._convert('123.123.123.123'))
+ field.validate(self._convert('host:123'))
+ field.validate(self._convert('www.host.com:123'))
+ field.validate(self._convert('123.123.123.123:123'))
+
+ def testValidateRequired(self):
+ field = self._Field_Factory(title=u'HostName field', description=u'',
+ readonly=False, required=True)
+ field.validate(self._convert('host'))
+ self.assertRaises(RequiredMissing, field.validate, None)
+
+ def testBadStringType(self):
+ field = self._Field_Factory(title=u'HostName field')
+ self.assertRaises(InvalidHostName, field.validate, u'123.123')
+
+ def test_newlines(self):
+ field = self._Field_Factory(title=u'HostName field')
+ self.assertRaises(InvalidHostName, field.validate,
+ self._convert('host\nfoo'))
+
+
+def test_suite():
+ return unittest.TestSuite((
+ DocFileSuite('README.txt',
+ optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,),
+ unittest.makeSuite(HostNameTest),
+ ))
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
Property changes on: z3c.schema/trunk/src/z3c/schema/regex/tests.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
More information about the Checkins
mailing list