[Zope3-checkins] CVS: Zope3/src/zope/configuration - fields.py:1.5
Stephan Richter
srichter@cosmos.phy.tufts.edu
Fri, 1 Aug 2003 15:46:07 -0400
Update of /cvs-repository/Zope3/src/zope/configuration
In directory cvs.zope.org:/tmp/cvs-serv32158/src/zope/configuration
Modified Files:
fields.py
Log Message:
Created new URI and PythonIdentifier field for configuration. philiKON and
I needed it.
=== Zope3/src/zope/configuration/fields.py 1.4 => 1.5 ===
--- Zope3/src/zope/configuration/fields.py:1.4 Thu Jul 31 10:56:47 2003
+++ Zope3/src/zope/configuration/fields.py Fri Aug 1 15:46:03 2003
@@ -9,17 +9,73 @@
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
"""Configuration-specific schema fields
$Id$
"""
-
-import os
+import os, re
from zope import schema
from zope.schema.interfaces import IFromUnicode
from zope.configuration.exceptions import ConfigurationError
from zope.interface import implements
+PYIDENTIFIER_REGEX = u'\A[a-zA-Z_]+[a-zA-Z0-9_]*\Z'
+pyidentifierPattern = re.compile(PYIDENTIFIER_REGEX)
+# This regex is originally from 4Suite/Ft/Lib/Uri.py
+URI_REGEX = r"\A(?:(?:[a-zA-Z][0-9a-zA-Z+\-\.]*:)?" \
+ r"/{0,2}[0-9a-zA-Z;/?:@&=+$\.\-_!~*'()%]+)?\Z"
+uriPattern = re.compile(URI_REGEX)
+
+class PythonIdentifier(schema.TextLine):
+ r"""This field describes a python identifier, i.e. a variable name.
+
+ Let's look at an example:
+
+ >>> class FauxContext:
+ ... pass
+ >>> context = FauxContext()
+ >>> field = PythonIdentifier().bind(context)
+
+ Let's test the fromUnicode method:
+
+ >>> field.fromUnicode(u'foo')
+ u'foo'
+ >>> field.fromUnicode(u'foo3')
+ u'foo3'
+ >>> field.fromUnicode(u'_foo3')
+ u'_foo3'
+
+ Now let's see whether validation works alright
+
+ >>> for value in (u'foo', u'foo3', u'foo_', u'_foo3', u'foo_3', u'foo3_'):
+ ... field._validate(value)
+ >>>
+ >>> from zope import schema
+ >>>
+ >>> for value in (u'3foo', u'foo:', u'\\', u''):
+ ... try:
+ ... field._validate(value)
+ ... except schema.ValidationError:
+ ... print 'Validation Error'
+ Validation Error
+ Validation Error
+ Validation Error
+ Validation Error
+
+ """
+
+ implements(IFromUnicode)
+
+ def fromUnicode(self, u):
+ return u.strip()
+
+ def _validate(self, value):
+ super(PythonIdentifier, self)._validate(value)
+ if pyidentifierPattern.match(value) is None:
+ raise schema.ValidationError(value)
+
class GlobalObject(schema.Field):
"""An object that can be accesses as a module global
@@ -194,6 +250,46 @@
return os.path.normpath(u)
return self.context.path(u)
+
+class URI(schema.TextLine):
+ r"""This field describes URIs, and validates the input accordingly.
+
+ Let's look at an example:
+
+ >>> class FauxContext:
+ ... pass
+ >>> context = FauxContext()
+ >>> field = URI().bind(context)
+
+ Let's test the fromUnicode method:
+
+ >>> field.fromUnicode(u'http://www.zope3.org')
+ u'http://www.zope3.org'
+
+ Now let's see whether validation works alright
+
+ >>> res = field._validate(u'http://www.zope3.org')
+ >>> res # Result should be None
+ >>>
+ >>> from zope import schema
+ >>> try:
+ ... res = field._validate(u'http:/\\www.zope3.org')
+ ... except schema.ValidationError:
+ ... print 'Validation Error'
+ Validation Error
+
+ """
+
+ implements(IFromUnicode)
+
+ def fromUnicode(self, u):
+ return u.strip()
+
+ def _validate(self, value):
+ super(URI, self)._validate(value)
+ if uriPattern.match(value) is None:
+ raise schema.ValidationError(value)
+
class Bool(schema.Bool):
"""A boolean value