[CalendarServer-changes] [12551] CalendarServer/trunk/txdav/dps

source_changes at macosforge.org source_changes at macosforge.org
Wed Mar 12 11:25:08 PDT 2014


Revision: 12551
          http://trac.calendarserver.org//changeset/12551
Author:   sagen at apple.com
Date:     2014-02-04 10:14:29 -0800 (Tue, 04 Feb 2014)
Log Message:
-----------
Implements more directory service methods

Modified Paths:
--------------
    CalendarServer/trunk/txdav/dps/client.py
    CalendarServer/trunk/txdav/dps/protocol.py
    CalendarServer/trunk/txdav/dps/service.py

Modified: CalendarServer/trunk/txdav/dps/client.py
===================================================================
--- CalendarServer/trunk/txdav/dps/client.py	2014-02-04 17:46:43 UTC (rev 12550)
+++ CalendarServer/trunk/txdav/dps/client.py	2014-02-04 18:14:29 UTC (rev 12551)
@@ -20,20 +20,20 @@
 from twisted.internet import reactor
 from twisted.internet.defer import inlineCallbacks
 
-import sys
-
 log = Logger()
 
 
 @inlineCallbacks
 def makeEvenBetterRequest():
-    shortName = sys.argv[1]
-
     ds = DirectoryService(None)
-    record = (yield ds.recordWithShortName(RecordType.user, shortName))
-    print("A: {r}".format(r=record))
-    record = (yield ds.recordWithShortName(RecordType.user, shortName))
-    print("B: {r}".format(r=record))
+    record = (yield ds.recordWithShortName(RecordType.user, "sagen"))
+    print("short name: {r}".format(r=record))
+    record = (yield ds.recordWithUID("__dre__"))
+    print("uid: {r}".format(r=record))
+    record = (yield ds.recordWithGUID("A3B1158F-0564-4F5B-81E4-A89EA5FF81B0"))
+    print("guid: {r}".format(r=record))
+    records = (yield ds.recordsWithRecordType(RecordType.user))
+    print("recordType: {r}".format(r=records))
 
 
 def succeeded(result):
@@ -45,6 +45,7 @@
     print("boo: {f}".format(f=failure))
     reactor.stop()
 
+
 if __name__ == '__main__':
     d = makeEvenBetterRequest()
     d.addCallbacks(succeeded, failed)

Modified: CalendarServer/trunk/txdav/dps/protocol.py
===================================================================
--- CalendarServer/trunk/txdav/dps/protocol.py	2014-02-04 17:46:43 UTC (rev 12550)
+++ CalendarServer/trunk/txdav/dps/protocol.py	2014-02-04 18:14:29 UTC (rev 12551)
@@ -18,7 +18,7 @@
 from twisted.protocols import amp
 from twisted.internet.defer import inlineCallbacks, returnValue
 from twext.python.log import Logger
-
+import uuid
 import cPickle as pickle
 
 log = Logger()
@@ -35,6 +35,61 @@
     ]
 
 
+class RecordWithUIDCommand(amp.Command):
+    arguments = [
+        ('uid', amp.String()),
+    ]
+    response = [
+        ('fields', amp.String()),
+    ]
+
+
+class RecordWithGUIDCommand(amp.Command):
+    arguments = [
+        ('guid', amp.String()),
+    ]
+    response = [
+        ('fields', amp.String()),
+    ]
+
+
+class RecordsWithRecordTypeCommand(amp.Command):
+    arguments = [
+        ('recordType', amp.String()),
+    ]
+    response = [
+        ('fieldsList', amp.String()),
+    ]
+
+
+class RecordsWithEmailAddressCommand(amp.Command):
+    arguments = [
+        ('emailAddress', amp.String()),
+    ]
+    response = [
+        ('fieldsList', amp.String()),
+    ]
+
+
+class UpdateRecordsCommand(amp.Command):
+    arguments = [
+        ('fieldsList', amp.String()),
+        ('create', amp.Boolean(optional=True)),
+    ]
+    response = [
+        ('success', amp.Boolean()),
+    ]
+
+
+class RemoveRecordsCommand(amp.Command):
+    arguments = [
+        ('uids', amp.ListOf(amp.String())),
+    ]
+    response = [
+        ('success', amp.Boolean()),
+    ]
+
+
 class DirectoryProxyAMPProtocol(amp.AMP):
     """
     Server side of directory proxy
@@ -47,6 +102,20 @@
         self._directory = directory
 
 
+    def recordToDict(self, record):
+        """
+        This to be replaced by something awesome
+        """
+        fields = {}
+        if record is not None:
+            for field, value in record.fields.iteritems():
+                # print("%s: %s" % (field.name, value))
+                valueType = self._directory.fieldName.valueType(field)
+                if valueType is unicode:
+                    fields[field.name] = value
+        return fields
+
+
     @RecordWithShortNameCommand.responder
     @inlineCallbacks
     def recordWithShortName(self, recordType, shortName):
@@ -56,16 +125,55 @@
         record = (yield self._directory.recordWithShortName(
             RecordType.lookupByName(recordType), shortName)
         )
-        fields = {}
-        for field, value in record.fields.iteritems():
-            # print("%s: %s" % (field.name, value))
-            valueType = self._directory.fieldName.valueType(field)
-            # TODO: handle other value types like NamedConstants
-            if valueType is unicode:
-                fields[field.name] = value
+        fields = self.recordToDict(record)
+        response = {
+            "fields": pickle.dumps(fields),
+        }
+        log.debug("Responding with: {response}", response=response)
+        returnValue(response)
 
+
+    @RecordWithUIDCommand.responder
+    @inlineCallbacks
+    def recordWithUID(self, uid):
+        uid = uid.decode("utf-8")
+        log.debug("RecordWithUID: {u}", u=uid)
+        record = (yield self._directory.recordWithUID(uid))
+        fields = self.recordToDict(record)
         response = {
             "fields": pickle.dumps(fields),
         }
         log.debug("Responding with: {response}", response=response)
         returnValue(response)
+
+
+    @RecordWithGUIDCommand.responder
+    @inlineCallbacks
+    def recordWithGUID(self, guid):
+        guid = uuid.UUID(guid)
+        log.debug("RecordWithGUID: {g}", g=guid)
+        record = (yield self._directory.recordWithGUID(guid))
+        fields = self.recordToDict(record)
+        response = {
+            "fields": pickle.dumps(fields),
+        }
+        log.debug("Responding with: {response}", response=response)
+        returnValue(response)
+
+
+    @RecordsWithRecordTypeCommand.responder
+    @inlineCallbacks
+    def recordsWithRecordType(self, recordType):
+        recordType = recordType.decode("utf-8")
+        log.debug("RecordsWithRecordType: {r}", r=recordType)
+        records = (yield self._directory.recordsWithRecordType(
+            RecordType.lookupByName(recordType))
+        )
+        fieldsList = []
+        for record in records:
+            fieldsList.append(self.recordToDict(record))
+        response = {
+            "fieldsList": pickle.dumps(fieldsList),
+        }
+        log.debug("Responding with: {response}", response=response)
+        returnValue(response)

Modified: CalendarServer/trunk/txdav/dps/service.py
===================================================================
--- CalendarServer/trunk/txdav/dps/service.py	2014-02-04 17:46:43 UTC (rev 12550)
+++ CalendarServer/trunk/txdav/dps/service.py	2014-02-04 18:14:29 UTC (rev 12551)
@@ -35,7 +35,10 @@
 from twext.python.log import Logger
 from twisted.python.filepath import FilePath
 
-from .protocol import DirectoryProxyAMPProtocol, RecordWithShortNameCommand
+from .protocol import (
+    DirectoryProxyAMPProtocol, RecordWithShortNameCommand, RecordWithUIDCommand,
+    RecordWithGUIDCommand, RecordsWithRecordTypeCommand
+)
 
 from twisted.internet import reactor
 from twisted.internet.defer import inlineCallbacks, returnValue
@@ -57,6 +60,36 @@
     )
 
 
+    def _dictToRecord(self, serializedFields):
+        """
+        This to be replaced by something awesome
+        """
+        if not serializedFields:
+            return None
+
+        fields = {}
+        for fieldName, value in serializedFields.iteritems():
+            field = self.fieldName.lookupByName(fieldName)
+            fields[field] = value
+        fields[self.fieldName.recordType] = self.recordType.user
+        return DirectoryRecord(self, fields)
+
+
+    def _processSingleRecord(self, result):
+        serializedFields = pickle.loads(result['fields'])
+        return self._dictToRecord(serializedFields)
+
+
+    def _processMultipleRecords(self, result):
+        serializedFieldsList = pickle.loads(result['fieldsList'])
+        results = []
+        for serializedFields in serializedFieldsList:
+            record = self._dictToRecord(serializedFields)
+            if record is not None:
+                results.append(record)
+        return results
+
+
     @inlineCallbacks
     def _getConnection(self):
         # path = config.DirectoryProxy.SocketPath
@@ -72,26 +105,61 @@
 
     def recordWithShortName(self, recordType, shortName):
 
-        def deserialize(result):
-            rawFields = pickle.loads(result['fields'])
-            fields = {}
-            for fieldName, value in rawFields.iteritems():
-                field = self.fieldName.lookupByName(fieldName)
-                fields[field] = value
-            fields[self.fieldName.recordType] = recordType
-            return DirectoryRecord(self, fields)
-
-        def call(ampProto):
+        def _call(ampProto):
             return ampProto.callRemote(
                 RecordWithShortNameCommand,
                 recordType=recordType.description.encode("utf-8"),
                 shortName=shortName.encode("utf-8")
             )
 
-        return self._getConnection().addCallback(call).addCallback(deserialize)
+        d = self._getConnection()
+        d.addCallback(_call)
+        d.addCallback(self._processSingleRecord)
+        return d
 
 
+    def recordWithUID(self, uid):
 
+        def _call(ampProto):
+            return ampProto.callRemote(
+                RecordWithUIDCommand,
+                uid=uid.encode("utf-8")
+            )
+
+        d = self._getConnection()
+        d.addCallback(_call)
+        d.addCallback(self._processSingleRecord)
+        return d
+
+
+    def recordWithGUID(self, guid):
+
+        def _call(ampProto):
+            return ampProto.callRemote(
+                RecordWithGUIDCommand,
+                guid=guid.encode("utf-8")
+            )
+
+        d = self._getConnection()
+        d.addCallback(_call)
+        d.addCallback(self._processSingleRecord)
+        return d
+
+
+    def recordsWithRecordType(self, recordType):
+
+        def _call(ampProto):
+            return ampProto.callRemote(
+                RecordsWithRecordTypeCommand,
+                recordType=recordType.description.encode("utf-8")
+            )
+
+        d = self._getConnection()
+        d.addCallback(_call)
+        d.addCallback(self._processMultipleRecords)
+        return d
+
+
 class DirectoryRecord(BaseDirectoryRecord):
     pass
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140312/eb2decb9/attachment.html>


More information about the calendarserver-changes mailing list