[Zope-CVS] SVN: ldapadapter/trunk/ Updated and cleaned the code.

Stephan Richter srichter at cosmos.phy.tufts.edu
Thu May 19 16:10:12 EDT 2005


Log message for revision 30431:
  Updated and cleaned the code.
  

Changed:
  U   ldapadapter/trunk/README.txt
  U   ldapadapter/trunk/__init__.py
  U   ldapadapter/trunk/browser/check.py
  U   ldapadapter/trunk/browser/configure.zcml
  U   ldapadapter/trunk/check.py
  D   ldapadapter/trunk/exceptions.py
  U   ldapadapter/trunk/field.py
  U   ldapadapter/trunk/interfaces.py
  U   ldapadapter/trunk/tests/test_ldapadapter.py
  U   ldapadapter/trunk/utility.py

-=-
Modified: ldapadapter/trunk/README.txt
===================================================================
--- ldapadapter/trunk/README.txt	2005-05-19 20:03:16 UTC (rev 30430)
+++ ldapadapter/trunk/README.txt	2005-05-19 20:10:11 UTC (rev 30431)
@@ -12,8 +12,6 @@
 You get an LDAP adapter by calling LDAPAdapter, which implements
 ILDAPAdapter:
 
-  >>> from pprint import PrettyPrinter
-  >>> pprint = PrettyPrinter(width=60).pprint
   >>> from ldapadapter.utility import LDAPAdapter
   >>> from ldapadapter.interfaces import ILDAPAdapter
   >>> from zope.interface.verify import verifyClass, verifyObject
@@ -118,9 +116,9 @@
 
   >>> res = conn.search('dc=test', attrs=['givenName'])
   >>> pprint(res)
-  [(u'dc=test', {}),
-   (u'cn=foo,dc=test', {'givenName': [u'John']}),
+  [(u'cn=foo,dc=test', {'givenName': [u'John']}),
    (u'cn=bar,dc=test', {'givenName': [u'Joey']}),
+   (u'dc=test', {}),
    (u'cn=baz,dc=test', {'givenName': [u'Raoul']})]
 
 You can use a search filter to filter the entries returned:

Modified: ldapadapter/trunk/__init__.py
===================================================================
--- ldapadapter/trunk/__init__.py	2005-05-19 20:03:16 UTC (rev 30430)
+++ ldapadapter/trunk/__init__.py	2005-05-19 20:10:11 UTC (rev 30431)
@@ -1,17 +1 @@
-##############################################################################
-#
-# 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 $
-"""
+# Make this a package.

Modified: ldapadapter/trunk/browser/check.py
===================================================================
--- ldapadapter/trunk/browser/check.py	2005-05-19 20:03:16 UTC (rev 30430)
+++ ldapadapter/trunk/browser/check.py	2005-05-19 20:10:11 UTC (rev 30431)
@@ -15,7 +15,6 @@
 
 $Id:$
 """
-
 from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
 from zope.app.publisher.browser import BrowserView
 from zope.app.i18n import ZopeMessageIDFactory as _
@@ -24,12 +23,10 @@
 from ldapadapter.interfaces import ICheckLDAPAdapter
 
 
-
 class CheckLDAPAdapterView(BrowserView):
 
     __used_for__ = IManageableLDAPAdapter
 
-
     def __init__(self, context, request):
         self.context = context
         self.request = request
@@ -53,7 +50,9 @@
             dn = self.context.bindDN
             pw = self.context.bindPassword
             testadapter = ICheckLDAPAdapter(self.context)
-            self._addInfo("<strong>Test python connection and LDAP server binding</strong>")
+            self._addInfo("<strong>"
+                          "Test python connection and LDAP server binding"
+                          "</strong>")
             self.report = self.report + testadapter.testConnection(dn, pw)
             self._addInfo("<strong>Tests done</strong>")
             self._addInfo(" ")
@@ -64,7 +63,9 @@
             dn = ''
             pw = ''
             testadapter = ICheckLDAPAdapter(self.context)
-            self._addInfo("<strong>Test python connection and LDAP server binding</strong>")
+            self._addInfo("<strong>"
+                          "Test python connection and LDAP server binding"
+                          "</strong>")
             self.report = self.report + testadapter.testConnection(dn, pw)
             self._addInfo("<strong>Tests done</strong>")
             self._addInfo(" ")

Modified: ldapadapter/trunk/browser/configure.zcml
===================================================================
--- ldapadapter/trunk/browser/configure.zcml	2005-05-19 20:03:16 UTC (rev 30430)
+++ ldapadapter/trunk/browser/configure.zcml	2005-05-19 20:10:11 UTC (rev 30431)
@@ -1,52 +1,46 @@
 <configure
-  xmlns:zope="http://namespaces.zope.org/zope"
-  xmlns="http://namespaces.zope.org/browser"
-  i18n_domain="ldapadapter">
+    xmlns:zope="http://namespaces.zope.org/zope"
+    xmlns="http://namespaces.zope.org/browser"
+    i18n_domain="ldapadapter">
 
-    <!-- LDAPAdapter -->
-    <tool
-        interface="ldapadapter.interfaces.ILDAPAdapter"
-        title="LDAP Adapter"
-        description="A LDAP adapter."
-        />
+  <!-- LDAPAdapter -->
+  <tool
+      interface="ldapadapter.interfaces.ILDAPAdapter"
+      title="LDAP Adapter"
+      description="A LDAP adapter."
+      />
 
-    <addform
-        label="LDAP Adapter"
-        for= "ldapadapter.interfaces.ILDAPAdapterManagement"
-        name="addLDAPAdapter.html"
-        schema="zope.app.utility.interfaces.IUtilityRegistration"
-        class="zope.app.utility.browser.AddRegistration"
-        permission="zope.ManageServices"
-        content_factory=
-            "ldapadapter.utility.LDAPAdapter"
-        arguments="name interface"
-        set_after_add="status"
-        fields="name interface permission status"
-        />
+  <addform
+      name="AddLDAPAdapter.html"
+      label="Add a LDAP Adapter"
+      schema="ldapadapter.interfaces.ILDAPAdapterManagement"
+      permission="zope.ManageSite"
+      content_factory="ldapadapter.utility.LDAPAdapter"
+      />
+  
+  <addMenuItem
+      class="ldapadapter.utility.ManageableLDAPAdapter"
+      title="LDAP Adapter"
+      description="A LDAP adapter"
+      permission="zope.ManageSite"
+      />
+  
+  <editform
+      for="ldapadapter.interfaces.IManageableLDAPAdapter"
+      schema="ldapadapter.interfaces.ILDAPAdapterManagement"
+      name="edit.html"
+      menu="zmi_views" title="Edit"
+      permission="zope.ManageContent"
+      />
+  
+  <!-- LDAPAdapter connetion test view -->
+  <page
+      for="ldapadapter.interfaces.IManageableLDAPAdapter"
+      name="checkLDAPAdapter.html"
+      attribute="check"
+      class=".check.CheckLDAPAdapterView"
+      menu="zmi_views" title="Test LDAP Adapter"
+      permission="zope.ManageServices"
+      />
 
-    <addMenuItem
-        class="ldapadapter.utility.ManageableLDAPAdapter"
-        title="LDAP Adapter"
-        description="A LDAP adapter"
-        permission="zope.ManageServices"
-        />
-
-    <editform
-        for="ldapadapter.interfaces.IManageableLDAPAdapter"
-        schema="ldapadapter.interfaces.ILDAPAdapterManagement"
-        name="edit.html"
-        menu="zmi_views" title="Edit"
-        permission="zope.ManageContent"
-        />
-
-    <!-- LDAPAdapter connetion test view -->
-    <page
-        for="ldapadapter.interfaces.IManageableLDAPAdapter"
-        name="checkLDAPAdapter.html"
-        attribute="check"
-        class=".check.CheckLDAPAdapterView"
-        menu="zmi_views" title="Test LDAP Adapter"
-        permission="zope.ManageServices"
-        />
-
 </configure>

Modified: ldapadapter/trunk/check.py
===================================================================
--- ldapadapter/trunk/check.py	2005-05-19 20:03:16 UTC (rev 30430)
+++ ldapadapter/trunk/check.py	2005-05-19 20:10:11 UTC (rev 30431)
@@ -11,20 +11,19 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-"""A plugable authentication module for LDAP.
+"""A LDAP Adapter Test Facility
 
 $Id:$
 """
-
-from zope.security.proxy import removeSecurityProxy
+import zope.interface
 from zope.interface import implements
 
-from interfaces import ICheckLDAPAdapter
+from ldapadapter.interfaces import ICheckLDAPAdapter
 
 class CheckLDAPAdapter:
     """A LDAP connection test adapter."""
 
-    implements(ICheckLDAPAdapter)
+    zope.interface.implements(ICheckLDAPAdapter)
 
     def __init__(self, context):
         self.context = context

Deleted: ldapadapter/trunk/exceptions.py
===================================================================
--- ldapadapter/trunk/exceptions.py	2005-05-19 20:03:16 UTC (rev 30430)
+++ ldapadapter/trunk/exceptions.py	2005-05-19 20:10:11 UTC (rev 30431)
@@ -1,51 +0,0 @@
-##############################################################################
-#
-# 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 InvalidLDAPURI(ValidationError):
-    __doc__ = _("""The specified LDAP URI is not valid.""")
-
-
-# LDAP Adapter management errors
-
-class LDAPURIParseError(Exception):
-    """The given ldap uri is not valid."""
-
-LDAP_uri_parse_error = _(u'The LDAP URI could not be parsed.')
-
-
-# LDAP Adapter exceptions
-
-class LDAPException(Exception):
-    """LDAP exception"""
-
-class ServerDown(LDAPException):
-    """The server doesn't answer"""
-
-class InvalidCredentials(LDAPException):
-    """The credentials are incorrect"""
-
-class NoSuchObject(LDAPException):
-    """The base object doesn't exist"""

Modified: ldapadapter/trunk/field.py
===================================================================
--- ldapadapter/trunk/field.py	2005-05-19 20:03:16 UTC (rev 30430)
+++ ldapadapter/trunk/field.py	2005-05-19 20:10:11 UTC (rev 30431)
@@ -17,14 +17,16 @@
 """
 import re
 
-from zope.interface import implements
+import zope.interface
 
 from zope.schema.interfaces import IURI
 from zope.schema.interfaces import IFromUnicode
 from zope.schema.interfaces import InvalidURI
+from zope.schema._bootstrapinterfaces import ValidationError
 from zope.schema import URI
 
-from ldapadapter.exceptions import InvalidLDAPURI
+from zope.i18nmessageid import MessageIDFactory
+_ = MessageIDFactory("ldapadapter")
 
 
 """
@@ -60,11 +62,17 @@
     ).match
 
 
+# LDAP-Adapter Exeptions
+# Note: Located here to avoid circular references.
+class InvalidLDAPURI(ValidationError):
+    __doc__ = _("""The specified LDAP URI is not valid.""")
+
+
 class LDAPURI(URI):
     """LDAPURI schema field
     """
 
-    implements(IURI, IFromUnicode)
+    zope.interface.implements(IURI, IFromUnicode)
 
     def _validate(self, value):
         """

Modified: ldapadapter/trunk/interfaces.py
===================================================================
--- ldapadapter/trunk/interfaces.py	2005-05-19 20:03:16 UTC (rev 30430)
+++ ldapadapter/trunk/interfaces.py	2005-05-19 20:10:11 UTC (rev 30431)
@@ -15,40 +15,60 @@
 
 $Id$
 """
-from zope.interface import Interface
-from zope.interface import Attribute
-from zope.schema import Int
-from zope.schema import Bool
-from zope.schema import TextLine
-from zope.schema import URI
+import zope.interface
+import zope.schema
+
 from ldapadapter.field import LDAPURI
 
 from zope.i18nmessageid import MessageIDFactory
 _ = MessageIDFactory("ldapadapter")
 
 
-class ILDAPAdapter(Interface):
+# LDAP Adapter management errors
+
+class LDAPURIParseError(Exception):
+    """The given ldap uri is not valid."""
+
+LDAP_uri_parse_error = _(u'The LDAP URI could not be parsed.')
+
+
+# LDAP Adapter exceptions
+
+class LDAPException(Exception):
+    """LDAP exception"""
+
+class ServerDown(LDAPException):
+    """The server doesn't answer"""
+
+class InvalidCredentials(LDAPException):
+    """The credentials are incorrect"""
+
+class NoSuchObject(LDAPException):
+    """The base object doesn't exist"""
+
+
+class ILDAPAdapter(zope.interface.Interface):
     """Adapter to an LDAP server."""
-    host = TextLine(
+    host = zope.schema.TextLine(
         title=_("Host"),
         default=u'localhost',
         required=True,
         )
-    port = Int(
+    port = zope.schema.Int(
         title=_("Port"),
         default=389,
         required=True,
         )
-    useSSL = Bool(
+    useSSL = zope.schema.Bool(
         title=_("Use SSL"),
         default=False,
         )
-    bindDN = TextLine(
+    bindDN = zope.schema.TextLine(
         title=_("Bind DN"),
         default=u'',
         required=False,
         )
-    bindPassword = TextLine(
+    bindPassword = zope.schema.TextLine(
         title=_("Bind password"),
         default=u'',
         required=False,
@@ -62,7 +82,7 @@
         May raise InvalidCredentials.
         """
 
-class ILDAPAdapterManagement(Interface):
+class ILDAPAdapterManagement(zope.interface.Interface):
     serverURL = LDAPURI(
         title=_("Server URL"),
         description=_(
@@ -73,12 +93,12 @@
             ),
         default="ldap://localhost",
         )
-    bindDN = TextLine(
+    bindDN = zope.schema.TextLine(
         title=_("Bind DN"),
         default=u'',
         required=False,
         )
-    bindPassword = TextLine(
+    bindPassword = zope.schema.TextLine(
         title=_("Bind password"),
         default=u'',
         required=False,
@@ -89,7 +109,7 @@
     """LDAP Adapter with management functions."""
 
 
-class ILDAPConnection(Interface):
+class ILDAPConnection(zope.interface.Interface):
     """LDAP connection to a server, bound to a user."""
 
     def add(dn, entry):
@@ -136,7 +156,7 @@
         # TODO: some values are binary and should not be converted to unicode
 
 
-class ICheckLDAPAdapter(Interface):
+class ICheckLDAPAdapter(zope.interface.Interface):
     """A test adapter for to test the connection between Zope and LDAP."""
 
     def testConnection():

Modified: ldapadapter/trunk/tests/test_ldapadapter.py
===================================================================
--- ldapadapter/trunk/tests/test_ldapadapter.py	2005-05-19 20:03:16 UTC (rev 30430)
+++ ldapadapter/trunk/tests/test_ldapadapter.py	2005-05-19 20:10:11 UTC (rev 30431)
@@ -18,7 +18,7 @@
 __docformat__ = "reStructuredText"
 import sys
 import unittest
-from zope.testing import doctest
+from zope.testing import doctest, doctestunit
 from zope.app.tests import placelesssetup, ztapi
 from zope.app.event.tests.placelesssetup import getEvents
 
@@ -48,7 +48,10 @@
 
 def test_suite():
     return unittest.TestSuite((
-        doctest.DocFileSuite('../README.txt', setUp=setUp, tearDown=tearDown),
+        doctest.DocFileSuite('../README.txt',
+                             setUp=setUp, tearDown=tearDown,
+                             globs={'pprint': doctestunit.pprint},
+                             optionflags=doctest.NORMALIZE_WHITESPACE),
         ))
 
 if __name__ == '__main__':

Modified: ldapadapter/trunk/utility.py
===================================================================
--- ldapadapter/trunk/utility.py	2005-05-19 20:03:16 UTC (rev 30430)
+++ ldapadapter/trunk/utility.py	2005-05-19 20:10:11 UTC (rev 30431)
@@ -15,23 +15,14 @@
 
 $Id$
 """
-
 import re
 import ldap
-from persistent import Persistent
-from zope.interface import implements
+import persistent
+import zope.interface
 from zope.app.container.contained import Contained
 
-from exceptions import LDAPURIParseError
-from exceptions import LDAP_uri_parse_error
-from exceptions import ServerDown
-from exceptions import InvalidCredentials
-from exceptions import NoSuchObject
+from ldapadapter import interfaces
 
-from interfaces import ILDAPAdapter
-from interfaces import ILDAPConnection
-from interfaces import IManageableLDAPAdapter
-
 SCOPES = {'base': ldap.SCOPE_BASE,
           'one': ldap.SCOPE_ONELEVEL,
           'sub': ldap.SCOPE_SUBTREE,
@@ -45,7 +36,7 @@
 
 
 class LDAPAdapter(object):
-    implements(ILDAPAdapter)
+    zope.interface.implements(interfaces.ILDAPAdapter)
 
     def __init__(self, host='localhost', port=389, useSSL=False,
                  bindDN='', bindPassword=''):
@@ -73,9 +64,9 @@
         try:
             conn.simple_bind_s(dn, password)
         except ldap.SERVER_DOWN:
-            raise ServerDown
+            raise interfaces.ServerDown
         except ldap.INVALID_CREDENTIALS:
-            raise InvalidCredentials
+            raise interfaces.InvalidCredentials
 
         return LDAPConnection(conn)
 
@@ -86,7 +77,7 @@
 
 
 class LDAPConnection(object):
-    implements(ILDAPConnection)
+    zope.interface.implements(interfaces.ILDAPConnection)
 
     def __init__(self, conn):
         self.conn = conn
@@ -105,7 +96,7 @@
         # Get current entry
         res = self.search(dn, 'base')
         if not res:
-            raise NoSuchObject(dn)
+            raise interfaces.NoSuchObject(dn)
         cur_dn, cur_entry = res[0]
 
         mod_list = []
@@ -138,7 +129,7 @@
         try:
             ldap_entries = self.conn.search_s(base, scope, filter, attrs)
         except ldap.NO_SUCH_OBJECT:
-            raise NoSuchObject(base)
+            raise interfaces.NoSuchObject(base)
         # May raise SIZELIMIT_EXCEEDED
 
         # Convert returned values from utf-8 to unicode.
@@ -153,10 +144,10 @@
         return results
 
 
-class ManageableLDAPAdapter(LDAPAdapter, Persistent, Contained):
+class ManageableLDAPAdapter(LDAPAdapter, persistent.Persistent, Contained):
     """LDAP adapter utility
     """
-    implements(IManageableLDAPAdapter)
+    zope.interface.implements(interfaces.IManageableLDAPAdapter)
 
     def _setServerURL(self, url):
         """Set the server info from an LDAP URL.
@@ -172,7 +163,7 @@
             if len(urlList) == 3:
                 port = int(urlList[2])
         else:
-            raise LDAPURIParseError(LDAP_uri_parse_error)
+            raise interfaces.LDAPURIParseError(interface.LDAP_uri_parse_error)
 
         self.host = host
         self.port = port



More information about the Zope-CVS mailing list