[Zope-CVS] SVN: ldapadapter/trunk/ Added field for validate LDAP uri

Roger Ineichen roger at projekt01.ch
Sun Oct 10 04:12:09 EDT 2004


Log message for revision 27875:
  Added field for validate LDAP uri
  Added exceptions for not valid ldap uri
  Implement field and exception
  --> should move to the zope3 core later
  --> TODO correct regex for validation


Changed:
  U   ldapadapter/trunk/TODO.txt
  A   ldapadapter/trunk/__init__.py
  A   ldapadapter/trunk/exceptions.py
  A   ldapadapter/trunk/field.py
  U   ldapadapter/trunk/interfaces.py
  A   ldapadapter/trunk/tests/test_field.py
  U   ldapadapter/trunk/tests/test_ldapadapter.py
  U   ldapadapter/trunk/utility.py


-=-
Modified: ldapadapter/trunk/TODO.txt
===================================================================
--- ldapadapter/trunk/TODO.txt	2004-10-10 07:57:18 UTC (rev 27874)
+++ ldapadapter/trunk/TODO.txt	2004-10-10 08:12:08 UTC (rev 27875)
@@ -7,5 +7,13 @@
 ================================================================================
 Add a generic validating input widget for URLs | torsten/roger | 10/10/04 | [ ]
 -----------------------------------------------+---------------+----------+-----
+Complete regex for validation LDAP uri         | torsten/roger | 10/10/04 | [ ]
+-----------------------------------------------+---------------+----------+-----
+Add exceptions                                 | torsten/roger | 10/10/04 | [ ]
+-----------------------------------------------+---------------+----------+-----
+Add i18n translation files                     | torsten/roger | 10/10/04 | [ ]
+-----------------------------------------------+---------------+----------+-----
+Move LDAPURI field to zope.schema._fields.py   | torsten/roger | 10/10/04 | [ ]
+-----------------------------------------------+---------------+----------+-----
 Connection tracer (for validating connections) | torsten/roger | 10/10/04 | [ ]
 ================================================================================

Added: ldapadapter/trunk/__init__.py
===================================================================
--- ldapadapter/trunk/__init__.py	2004-10-10 07:57:18 UTC (rev 27874)
+++ ldapadapter/trunk/__init__.py	2004-10-10 08:12:08 UTC (rev 27875)
@@ -0,0 +1,17 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation 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
+#
+##############################################################################
+"""LDAPAdapter packages.
+
+$Id: __init__.py 27826 2004-10-09 13:39:27Z rogerineichen $
+"""


Property changes on: ldapadapter/trunk/__init__.py
___________________________________________________________________
Name: svn:eol-style
   + native

Added: ldapadapter/trunk/exceptions.py
===================================================================
--- ldapadapter/trunk/exceptions.py	2004-10-10 07:57:18 UTC (rev 27874)
+++ ldapadapter/trunk/exceptions.py	2004-10-10 08:12:08 UTC (rev 27875)
@@ -0,0 +1,35 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation 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.
+#
+##############################################################################
+"""LDAP Adapter utility.
+
+$Id:$
+"""
+
+from zope.exceptions import NotFoundError
+
+from zope.i18nmessageid import MessageIDFactory
+_ = MessageIDFactory("ldapadapter")
+
+from zope.schema._bootstrapinterfaces import ValidationError
+
+
+class URLFormatError(Exception):
+    """The given ldap uri is not valid."""
+
+LDAP_url_format_error = _(u'The uri is not a valid LDAP uri.')
+
+
+
+class InvalidLDAPURI(ValidationError):
+    __doc__ = _("""The specified LDAP URI is not valid.""")

Added: ldapadapter/trunk/field.py
===================================================================
--- ldapadapter/trunk/field.py	2004-10-10 07:57:18 UTC (rev 27874)
+++ ldapadapter/trunk/field.py	2004-10-10 08:12:08 UTC (rev 27875)
@@ -0,0 +1,91 @@
+##############################################################################
+#
+# Copyright (c) 2002 Zope Corporation 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.
+#
+##############################################################################
+"""LDAP Schema Fields
+
+$Id: $
+"""
+import re
+
+from zope.interface import implements
+
+from zope.schema.interfaces import IURI
+from zope.schema.interfaces import IFromUnicode
+from zope.schema.interfaces import InvalidURI
+from zope.schema import URI
+
+from ldapadapter.exceptions import InvalidLDAPURI
+
+
+_isldapuri = re.compile(
+    r"^ldap[s]{0,1}://"           # protocol
+    r"[a-zA-Z0-9\-\.]+"   # host
+    r"[\:]{0,1}[\d]{0,5}"         # port
+    ).match
+
+
+class LDAPURI(URI):
+    """LDAPURI schema field
+    """
+
+    implements(IURI, IFromUnicode)
+
+    def _validate(self, value):
+        """
+        >>> from ldapadapter.field import LDAPURI
+        >>> uri = LDAPURI(__name__='test')
+        >>> uri.validate("ldap://www.python.org:389")
+        >>> uri.validate("ldaps://www.python.org:389")
+        >>> uri.validate("www.python.org")
+        Traceback (most recent call last):
+        ...
+        InvalidLDAPURI: www.python.org
+        
+        >>> uri.validate("http://www.python.org")
+        Traceback (most recent call last):
+        ...
+        InvalidLDAPURI: http://www.python.org
+        
+        >>> uri.validate("ldap://www.python.org/foo")
+        Traceback (most recent call last):
+        ...
+        InvalidLDAPURI: ldap://www.python.org/foo
+        
+        """
+        
+        #super(LDAPURI, self)._validate(value)
+        if _isldapuri(value):
+            return
+
+        raise InvalidLDAPURI, value
+
+    def fromUnicode(self, value):
+        """
+        >>> from ldapadapter.field import LDAPURI
+        >>> uri = LDAPURI(__name__='test')
+        >>> uri.fromUnicode("ldap://www.python.org:389")
+        'ldap://www.python.org:389'
+        >>> uri.fromUnicode("ldaps://www.python.org:389")
+        'ldaps://www.python.org:389'
+        >>> uri.fromUnicode("          ldap://www.python.org:389")
+        'ldap://www.python.org:389'
+        >>> uri.fromUnicode("      \\n    ldap://www.python.org:389\\n")
+        'ldap://www.python.org:389'
+        >>> uri.fromUnicode("ldap://www.pyt hon.org:389")
+        Traceback (most recent call last):
+        ...
+        InvalidLDAPURI: ldap://www.pyt hon.org:389
+        """
+        v = str(value.strip())
+        self.validate(v)
+        return v


Property changes on: ldapadapter/trunk/field.py
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: ldapadapter/trunk/interfaces.py
===================================================================
--- ldapadapter/trunk/interfaces.py	2004-10-10 07:57:18 UTC (rev 27874)
+++ ldapadapter/trunk/interfaces.py	2004-10-10 08:12:08 UTC (rev 27875)
@@ -20,8 +20,11 @@
 from zope.schema import Int
 from zope.schema import Bool
 from zope.schema import TextLine
+from zope.schema import URI
+from ldapadapter.field import LDAPURI
 
-from zope.app.i18n import ZopeMessageIDFactory as _
+from zope.i18nmessageid import MessageIDFactory
+_ = MessageIDFactory("ldapadapter")
 
 
 class ILDAPAdapter(Interface):
@@ -56,7 +59,17 @@
         """
 
 class ILDAPAdapterManagement(Interface):
-    serverURL = TextLine(
+    #serverURL = TextLine(
+    #    title=_("Server URL"),
+    #    description=_(
+    #        "Specify the LDAP URL of the server. Examples:\n"
+    #        "\n"
+    #        "ldap:///\n",
+    #        "ldaps://localhost:389/\n",
+    #        ),
+    #    default=u"ldap://localhost",
+    #    )
+    serverURL = LDAPURI(
         title=_("Server URL"),
         description=_(
             "Specify the LDAP URL of the server. Examples:\n"
@@ -64,7 +77,7 @@
             "ldap:///\n",
             "ldaps://localhost:389/\n",
             ),
-        default=u"ldap://localhost",
+        default="ldap://localhost",
         )
     bindDN = TextLine(
         title=_("Bind DN"),

Added: ldapadapter/trunk/tests/test_field.py
===================================================================
--- ldapadapter/trunk/tests/test_field.py	2004-10-10 07:57:18 UTC (rev 27874)
+++ ldapadapter/trunk/tests/test_field.py	2004-10-10 08:12:08 UTC (rev 27875)
@@ -0,0 +1,33 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation 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.
+#
+##############################################################################
+"""LDAPAdapter tests
+
+$Id:$
+"""
+__docformat__ = "reStructuredText"
+
+import sys
+
+import unittest
+
+from zope.testing import doctest
+
+
+def test_suite():
+    return unittest.TestSuite((
+        doctest.DocFileSuite('../field.py'),
+        ))
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')


Property changes on: ldapadapter/trunk/tests/test_field.py
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: ldapadapter/trunk/tests/test_ldapadapter.py
===================================================================
--- ldapadapter/trunk/tests/test_ldapadapter.py	2004-10-10 07:57:18 UTC (rev 27874)
+++ ldapadapter/trunk/tests/test_ldapadapter.py	2004-10-10 08:12:08 UTC (rev 27875)
@@ -48,8 +48,7 @@
 
 def test_suite():
     return unittest.TestSuite((
-        doctest.DocFileSuite('../README.txt',
-                             setUp=setUp, tearDown=tearDown),
+        doctest.DocFileSuite('../README.txt', setUp=setUp, tearDown=tearDown),
         ))
 
 if __name__ == '__main__':

Modified: ldapadapter/trunk/utility.py
===================================================================
--- ldapadapter/trunk/utility.py	2004-10-10 07:57:18 UTC (rev 27874)
+++ ldapadapter/trunk/utility.py	2004-10-10 08:12:08 UTC (rev 27875)
@@ -32,6 +32,9 @@
 from zope.interface import implements
 from zope.app.container.contained import Contained
 
+from exceptions import URLFormatError
+from exceptions import LDAP_url_format_error
+
 from interfaces import ILDAPAdapter
 from interfaces import ILDAPConnection
 from interfaces import IManageableLDAPAdapter
@@ -100,13 +103,9 @@
                 if len(urlList) == 3:
                     port = int(urlList[2])
             else:
-                # raise ERROR
-                print "to small url"
-                return None
+                URLFormatError(LDAP_url_format_error)
         else:
-            # raise ERROR
-            print "ERROR"
-            return None
+            raise URLFormatError(LDAP_url_format_error)
          
         self.host = host
         self.port = port



More information about the Zope-CVS mailing list