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

source_changes at macosforge.org source_changes at macosforge.org
Thu Feb 21 13:35:38 PST 2013


Revision: 10776
          http://trac.calendarserver.org//changeset/10776
Author:   wsanchez at apple.com
Date:     2013-02-21 13:35:38 -0800 (Thu, 21 Feb 2013)
Log Message:
-----------
Replace MergedConstants with ConstantsContainer

Modified Paths:
--------------
    CalendarServer/trunk/twext/who/index.py
    CalendarServer/trunk/twext/who/test/test_aggregate.py
    CalendarServer/trunk/twext/who/util.py
    CalendarServer/trunk/twext/who/xml.py

Modified: CalendarServer/trunk/twext/who/index.py
===================================================================
--- CalendarServer/trunk/twext/who/index.py	2013-02-21 18:11:19 UTC (rev 10775)
+++ CalendarServer/trunk/twext/who/index.py	2013-02-21 21:35:38 UTC (rev 10776)
@@ -24,10 +24,12 @@
     "DirectoryRecord",
 ]
 
+from itertools import chain
+
 from twisted.python.constants import Names, NamedConstant
 from twisted.internet.defer import succeed, inlineCallbacks, returnValue
 
-from twext.who.util import MergedConstants, describe, uniqueResult, iterFlags
+from twext.who.util import ConstantsContainer, describe, uniqueResult, iterFlags
 from twext.who.idirectory import FieldName as BaseFieldName
 from twext.who.expression import MatchExpression, MatchType, MatchFlags
 from twext.who.directory import DirectoryService as BaseDirectoryService
@@ -55,7 +57,7 @@
     XML directory service.
     """
 
-    fieldName = MergedConstants(BaseDirectoryService.fieldName, FieldName)
+    fieldName = ConstantsContainer(chain(BaseDirectoryService.fieldName.iterconstants(), FieldName.iterconstants()))
 
     indexedFields = (
         BaseFieldName.recordType,

Modified: CalendarServer/trunk/twext/who/test/test_aggregate.py
===================================================================
--- CalendarServer/trunk/twext/who/test/test_aggregate.py	2013-02-21 18:11:19 UTC (rev 10775)
+++ CalendarServer/trunk/twext/who/test/test_aggregate.py	2013-02-21 21:35:38 UTC (rev 10776)
@@ -26,8 +26,8 @@
 
 
 class BaseTest(object):
-    def service(self):
-        raise NotImplementedError(DirectoryService)
+    def service(self, subservices=()):
+        return DirectoryService("xyzzy", subservices)
 
     def xmlService(self, xmlData=None):
         return xmlService(self.mktemp(), xmlData)

Modified: CalendarServer/trunk/twext/who/util.py
===================================================================
--- CalendarServer/trunk/twext/who/util.py	2013-02-21 18:11:19 UTC (rev 10775)
+++ CalendarServer/trunk/twext/who/util.py	2013-02-21 21:35:38 UTC (rev 10776)
@@ -19,71 +19,43 @@
 """
 
 __all__ = [
-    "MergedConstants",
+    "ConstantsContainer",
     "uniqueResult",
     "describe",
     "iterFlags",
 ]
 
-from types import FunctionType
-
-from twisted.python.constants import NamedConstant
-
 from twext.who.idirectory import DirectoryServiceError
 
 
 
-class MergedConstants(object):
+class ConstantsContainer(object):
     """
-    Work-around for the fact that Names is apparently not subclassable
-    and doesn't provide a way to merge multiple Names classes.
+    A container for constants.
     """
-    def __init__(self, *containers):
-        seenNames = set()
-        myContainers = set()
-        for container in containers:
-            for constant in container.iterconstants():
-                if constant.name in seenNames:
-                    raise ValueError(
-                        "Multiple constants with the same name may not be merged: %s"
-                        % (constant.name,)
-                    )
-                seenNames.add(constant.name)
+    def __init__(self, constants):
+        myConstants = {}
+        for constant in constants:
+            if hasattr(self, constant.name):
+                raise ValueError("Name conflict: %r" % (constant.name,))
+            myConstants[constant.name] = constant
 
-            if isinstance(container, MergedConstants):
-                # Avoid nesting
-                myContainers |= container._containers
-            else:
-                myContainers.add(container)
+        self._constants = myConstants
 
-        self._containers = myContainers
-
     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
+        try:
+            return self.lookupByName(name)
+        except KeyError:
+            raise AttributeError(name)
 
-        raise AttributeError(name)
-
     def iterconstants(self):
-        for container in self._containers:
-            for constant in container.iterconstants():
-                yield constant
+        return self._constants.itervalues()
 
     def lookupByName(self, name):
-        for container in self._containers:
-            try:
-                return container.lookupByName(name)
-            except ValueError:
-                pass
+        return self._constants[name]
 
-        raise ValueError(name)
 
 
-
 def uniqueResult(values):
     result = None
     for value in values:

Modified: CalendarServer/trunk/twext/who/xml.py
===================================================================
--- CalendarServer/trunk/twext/who/xml.py	2013-02-21 18:11:19 UTC (rev 10775)
+++ CalendarServer/trunk/twext/who/xml.py	2013-02-21 21:35:38 UTC (rev 10776)
@@ -257,7 +257,7 @@
                 values = record.fields.get(fieldName, None)
 
                 if values is not None:
-                    if not self.fieldName.isMultiValue(fieldName):
+                    if not BaseFieldName.isMultiValue(fieldName):
                         values = (values,)
 
                     for value in values:
@@ -304,7 +304,7 @@
 
             value = fieldNode.text.encode("utf-8")
 
-            if self.fieldName.isMultiValue(fieldName):
+            if BaseFieldName.isMultiValue(fieldName):
                 values = fields.setdefault(fieldName, [])
                 values.append(value)
             else:
@@ -365,7 +365,7 @@
                     if name in fieldNames:
                         tag = fieldNames[name]
 
-                        if self.fieldName.isMultiValue(name):
+                        if BaseFieldName.isMultiValue(name):
                             values = value
                         else:
                             values = (value,)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130221/e84651a5/attachment.html>


More information about the calendarserver-changes mailing list