[CalendarServer-changes] [408] CalendarServer/branches/users/wsanchez/provisioning/twistedcaldav/ directory

source_changes at macosforge.org source_changes at macosforge.org
Thu Nov 9 14:20:18 PST 2006


Revision: 408
          http://trac.macosforge.org/projects/calendarserver/changeset/408
Author:   wsanchez at apple.com
Date:     2006-11-09 14:20:18 -0800 (Thu, 09 Nov 2006)

Log Message:
-----------
Add some directory exception classes.
Add __repr__ implementations to a couple of classes.
Add directory attribute to OpenDirectoryService so that OpenDirectoryRecord can use it.
Change directory init arg in DirectoryRecord to service.

Modified Paths:
--------------
    CalendarServer/branches/users/wsanchez/provisioning/twistedcaldav/directory/appleopendirectory.py
    CalendarServer/branches/users/wsanchez/provisioning/twistedcaldav/directory/directory.py

Modified: CalendarServer/branches/users/wsanchez/provisioning/twistedcaldav/directory/appleopendirectory.py
===================================================================
--- CalendarServer/branches/users/wsanchez/provisioning/twistedcaldav/directory/appleopendirectory.py	2006-11-09 22:17:23 UTC (rev 407)
+++ CalendarServer/branches/users/wsanchez/provisioning/twistedcaldav/directory/appleopendirectory.py	2006-11-09 22:20:18 UTC (rev 408)
@@ -23,6 +23,7 @@
 __all__ = [
     "OpenDirectoryService",
     "OpenDirectoryRecord",
+    "OpenDirectoryInitError",
 ]
 
 import opendirectory
@@ -31,17 +32,22 @@
 from twisted.cred.credentials import UsernamePassword
 
 from twistedcaldav.directory.directory import DirectoryService, DirectoryRecord
+from twistedcaldav.directory.directory import DirectoryError, UnknownRecordTypeError, UnknownRecordError
 
 class OpenDirectoryService(DirectoryService):
     """
     Open Directory implementation of L{IDirectoryService}.
     """
+    def __repr__(self):
+        return "<%s %s>" % (self.__class__.__name__, self.node)
+
     def __init__(self, node="/Search"):
         directory = opendirectory.odInit(node)
         if directory is None:
-            raise ValueError("Failed to open Open Directory Node: %s" % (node,))
+            raise OpenDirectoryInitError("Failed to open Open Directory Node: %s" % (node,))
 
-        self._directory = directory
+        self.directory = directory
+        self.node = node
 
     def recordTypes(self):
         return ("user", "group", "resource")
@@ -54,9 +60,9 @@
         elif recordType == "resource":
             listRecords = opendirectory.listResources
         else:
-            raise AssertionError("Unknown Open Directory record type: %s" % (recordType,))
+            raise UnknownRecordTypeError("Unknown Open Directory record type: %s" % (recordType,))
 
-        for shortName, guid, lastModified, principalURI in opendirectory.listUsers(self._directory):
+        for shortName, guid, lastModified, principalURI in opendirectory.listUsers(self.directory):
             if guid:
                 yield OpenDirectoryRecord(
                     directory = self,
@@ -68,19 +74,19 @@
 
     def recordWithShortName(self, recordType, shortName):
         if recordType == "user":
-            result = opendirectory.listUsersWithAttributes(self._directory, [shortName])
+            result = opendirectory.listUsersWithAttributes(self.directory, [shortName])
             if result is None or shortName not in result:
                 return None
             result = result[shortName]
         elif recordType == "group":
-            result = opendirectory.groupAttributes(self._directory, shortName)
+            result = opendirectory.groupAttributes(self.directory, shortName)
         elif recordType == "resource":
-            result = opendirectory.resourceAttributes(self._directory, shortName)
+            result = opendirectory.resourceAttributes(self.directory, shortName)
         else:
-            raise ValueError("Unknown record type: %s" % (recordType,))
+            raise UnknownRecordError("Unknown record type: %s" % (recordType,))
 
         return OpenDirectoryRecord(
-            directory = self,
+            service = self,
             recordType = recordType,
             guid = result[dsattributes.attrGUID],
             shortName = shortName,
@@ -93,6 +99,11 @@
     """
     def verifyCredentials(self, credentials):
         if isinstance(credentials, UsernamePassword):
-            return opendirectory.authenticateUser(self.directory, self.shortName, credentials.password)
+            return opendirectory.authenticateUser(self.service.directory, self.shortName, credentials.password)
 
-        return False
+        return super(OpenDirectoryInitError, self).verifyCredentials(credentials)
+
+class OpenDirectoryInitError(DirectoryError):
+    """
+    OpenDirectory initialization error.
+    """

Modified: CalendarServer/branches/users/wsanchez/provisioning/twistedcaldav/directory/directory.py
===================================================================
--- CalendarServer/branches/users/wsanchez/provisioning/twistedcaldav/directory/directory.py	2006-11-09 22:17:23 UTC (rev 407)
+++ CalendarServer/branches/users/wsanchez/provisioning/twistedcaldav/directory/directory.py	2006-11-09 22:20:18 UTC (rev 408)
@@ -21,7 +21,11 @@
 """
 
 __all__ = [
+    "DirectoryService",
     "DirectoryRecord",
+    "DirectoryError",
+    "UnknownRecordError",
+    "UnknownRecordTypeError",
 ]
 
 from zope.interface import implements
@@ -34,12 +38,30 @@
 class DirectoryRecord(object):
     implements(IDirectoryRecord)
 
-    def __init__(self, directory, recordType, guid, shortName, fullName=None):
-        self.directory  = directory
+    def __repr__(self):
+        return "<%s[%s@%s] %s(%s) %r>" % (self.__class__.__name__, self.recordType, self.service, self.guid, self.shortName, self.fullName)
+
+    def __init__(self, service, recordType, guid, shortName, fullName=None):
+        self.service    = service
         self.recordType = recordType
         self.guid       = guid
         self.shortName  = shortName
         self.fullName   = fullName
 
-    def authenticate(credentials):
+    def verifyCredentials(credentials):
         return False
+
+class DirectoryError(RuntimeError):
+    """
+    Generic directory error.
+    """
+
+class UnknownRecordTypeError(DirectoryError):
+    """
+    Unknown directory record type.
+    """
+
+class UnknownRecordError(DirectoryError):
+    """
+    Unknown directory record.
+    """

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20061109/05a7a04b/attachment.html


More information about the calendarserver-changes mailing list