[CalendarServer-changes] [10645] CalendarServer/trunk/twext/who

source_changes at macosforge.org source_changes at macosforge.org
Mon Feb 4 15:15:41 PST 2013


Revision: 10645
          http://trac.calendarserver.org//changeset/10645
Author:   wsanchez at apple.com
Date:     2013-02-04 15:15:40 -0800 (Mon, 04 Feb 2013)
Log Message:
-----------
Add MergedConstants class for combining Names classes.
Get DirectoryRecord.members() working for XML.

Modified Paths:
--------------
    CalendarServer/trunk/twext/who/directory.py
    CalendarServer/trunk/twext/who/idirectory.py
    CalendarServer/trunk/twext/who/test/test_xml.py
    CalendarServer/trunk/twext/who/xml.py

Modified: CalendarServer/trunk/twext/who/directory.py
===================================================================
--- CalendarServer/trunk/twext/who/directory.py	2013-02-04 20:49:49 UTC (rev 10644)
+++ CalendarServer/trunk/twext/who/directory.py	2013-02-04 23:15:40 UTC (rev 10645)
@@ -23,9 +23,12 @@
     "DirectoryRecord",
 ]
 
+from types import FunctionType
+
 from zope.interface import implements
 
 from twisted.python.util import FancyEqMixin
+from twisted.python.constants import NamedConstant
 from twisted.internet.defer import inlineCallbacks, returnValue
 from twisted.internet.defer import succeed, fail
 
@@ -38,6 +41,40 @@
 
 
 
+class MergedConstants(object):
+    """
+    Work-around for the fact that Names is apparently not subclassable
+    and doesn't provide a way to merge multiple Names classes.
+    """
+    def __init__(self, *containers):
+        self._containers = containers
+
+    def __getattr__(self, name):
+        for container in self._containers:
+            attr = getattr(container, name, None)
+            if attr is not None:
+                # Named constant or static method
+                if isinstance(attr, (NamedConstant, FunctionType)):
+                    return attr
+
+        raise AttributeError(name)
+
+    def iterconstants(self):
+        for container in self._containers:
+            for constant in container.iterconstants():
+                yield constant
+
+    def lookupByName(self, name):
+        for container in self._containers:
+            try:
+                return container.lookupByName(name)
+            except ValueError:
+                pass
+
+        raise ValueError(name)
+
+
+
 class DirectoryService(FancyEqMixin, object):
     implements(IDirectoryService)
 
@@ -45,8 +82,8 @@
         "realmName",
     )
 
-    RecordTypeClass = RecordType
-    FieldNameClass  = FieldName
+    RecordTypeClass = MergedConstants(RecordType)
+    FieldNameClass  = MergedConstants(FieldName)
 
 
     def __init__(self, realmName):

Modified: CalendarServer/trunk/twext/who/idirectory.py
===================================================================
--- CalendarServer/trunk/twext/who/idirectory.py	2013-02-04 20:49:49 UTC (rev 10644)
+++ CalendarServer/trunk/twext/who/idirectory.py	2013-02-04 23:15:40 UTC (rev 10645)
@@ -99,8 +99,8 @@
     fullNames.multiValue      = True
     emailAddresses.multiValue = True
 
-    @classmethod
-    def isMultiValue(cls, name):
+    @staticmethod
+    def isMultiValue(name):
         return getattr(name, "multiValue", False)
 
 class MatchType(Names, _DescriptionMixIn):

Modified: CalendarServer/trunk/twext/who/test/test_xml.py
===================================================================
--- CalendarServer/trunk/twext/who/test/test_xml.py	2013-02-04 20:49:49 UTC (rev 10644)
+++ CalendarServer/trunk/twext/who/test/test_xml.py	2013-02-04 23:15:40 UTC (rev 10645)
@@ -259,5 +259,3 @@
                 "__dre__",
             ))
         )
-
-    test_members.todo = "No worky."

Modified: CalendarServer/trunk/twext/who/xml.py
===================================================================
--- CalendarServer/trunk/twext/who/xml.py	2013-02-04 20:49:49 UTC (rev 10644)
+++ CalendarServer/trunk/twext/who/xml.py	2013-02-04 23:15:40 UTC (rev 10645)
@@ -28,7 +28,7 @@
 from xml.etree.ElementTree import parse as parseXML
 from xml.etree.ElementTree import ParseError as XMLParseError
 
-from twisted.python.constants import Values, ValueConstant, NamedConstant
+from twisted.python.constants import Names, NamedConstant, Values, ValueConstant
 from twisted.internet.defer import succeed, inlineCallbacks, returnValue
 
 from twext.who.idirectory import DirectoryServiceError
@@ -37,6 +37,8 @@
 from twext.who.idirectory import DirectoryQueryMatchExpression
 from twext.who.directory import DirectoryService as BaseDirectoryService
 from twext.who.directory import DirectoryRecord as BaseDirectoryRecord
+from twext.who.directory import MergedConstants
+from twext.who.idirectory import _DescriptionMixIn # FIXME
 
 
 
@@ -44,7 +46,7 @@
 # Data type extentions
 ##
 
-class FieldName (BaseFieldName):
+class FieldName(Names, _DescriptionMixIn):
     memberUIDs = NamedConstant()
     memberUIDs.description = "member UIDs"
     memberUIDs.multiValue = True
@@ -63,22 +65,22 @@
     # Field names
     #
     uid = ValueConstant("uid")
-    uid.fieldName = FieldName.uid
+    uid.fieldName = BaseFieldName.uid
 
     guid = ValueConstant("guid")
-    guid.fieldName = FieldName.guid
+    guid.fieldName = BaseFieldName.guid
 
     shortName = ValueConstant("short-name")
-    shortName.fieldName = FieldName.shortNames
+    shortName.fieldName = BaseFieldName.shortNames
 
     fullName = ValueConstant("full-name")
-    fullName.fieldName = FieldName.fullNames
+    fullName.fieldName = BaseFieldName.fullNames
 
     emailAddress = ValueConstant("email")
-    emailAddress.fieldName = FieldName.emailAddresses
+    emailAddress.fieldName = BaseFieldName.emailAddresses
 
     password = ValueConstant("password")
-    password.fieldName = FieldName.password
+    password.fieldName = BaseFieldName.password
 
     memberUID = ValueConstant("member-uid")
     memberUID.fieldName = FieldName.memberUIDs
@@ -118,18 +120,18 @@
     XML directory service.
     """
 
-    FieldNameClass  = FieldName
+    FieldNameClass = MergedConstants(BaseFieldName, FieldName)
 
     ElementClass   = Element
     AttributeClass = Attribute
     ValueClass     = Value
 
     indexedFields = (
-        FieldName.recordType,
-        FieldName.uid,
-        FieldName.guid,
-        FieldName.shortNames,
-        FieldName.emailAddresses,
+        BaseFieldName.recordType,
+        BaseFieldName.uid,
+        BaseFieldName.guid,
+        BaseFieldName.shortNames,
+        BaseFieldName.emailAddresses,
     )
 
 
@@ -230,7 +232,7 @@
                 recordType = self.RecordTypeClass.user
 
             fields = {}
-            fields[FieldName.recordType] = recordType
+            fields[self.FieldNameClass.recordType] = recordType
 
             for fieldNode in recordNode.getchildren():
                 try:
@@ -312,8 +314,9 @@
     """
     XML directory record
     """
+    @inlineCallbacks
     def members(self):
-        return succeed((
-            self.service.recordForUID(uid)
-            for uid in getattr(self, "memberUIDs", ())
-        ))
+        uids = set()
+        for uid in getattr(self, "memberUIDs", ()):
+            uids.add((yield self.service.recordWithUID(uid)).uid)
+        returnValue(uids)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130204/1680fa2a/attachment-0001.html>


More information about the calendarserver-changes mailing list