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

source_changes at macosforge.org source_changes at macosforge.org
Wed Mar 12 11:21:07 PDT 2014


Revision: 12130
          http://trac.calendarserver.org//changeset/12130
Author:   wsanchez at apple.com
Date:     2013-12-18 14:28:51 -0800 (Wed, 18 Dec 2013)
Log Message:
-----------
Make ConstantsContainer a bit more user-friendly.

Modified Paths:
--------------
    twext/trunk/twext/who/aggregate.py
    twext/trunk/twext/who/directory.py
    twext/trunk/twext/who/idirectory.py
    twext/trunk/twext/who/index.py
    twext/trunk/twext/who/opendirectory/service.py
    twext/trunk/twext/who/test/test_util.py
    twext/trunk/twext/who/util.py

Modified: twext/trunk/twext/who/aggregate.py
===================================================================
--- twext/trunk/twext/who/aggregate.py	2013-12-18 19:29:44 UTC (rev 12129)
+++ twext/trunk/twext/who/aggregate.py	2013-12-18 22:28:51 UTC (rev 12130)
@@ -71,10 +71,10 @@
     @property
     def recordType(self):
         if not hasattr(self, "_recordType"):
-            self._recordType = ConstantsContainer(chain(*tuple(
+            self._recordType = ConstantsContainer(tuple(
                 s.recordTypes()
                 for s in self.services
-            )))
+            ))
         return self._recordType
 
 

Modified: twext/trunk/twext/who/directory.py
===================================================================
--- twext/trunk/twext/who/directory.py	2013-12-18 19:29:44 UTC (rev 12129)
+++ twext/trunk/twext/who/directory.py	2013-12-18 22:28:51 UTC (rev 12130)
@@ -87,7 +87,7 @@
     """
 
     recordType = RecordType
-    fieldName  = FieldName
+    fieldName = FieldName
 
     normalizedFields = {
         FieldName.emailAddresses: lambda e: bytes(e).lower(),

Modified: twext/trunk/twext/who/idirectory.py
===================================================================
--- twext/trunk/twext/who/idirectory.py	2013-12-18 19:29:44 UTC (rev 12129)
+++ twext/trunk/twext/who/idirectory.py	2013-12-18 22:28:51 UTC (rev 12130)
@@ -119,7 +119,7 @@
     """
     Constants for common directory record field names.
 
-    Fields as assciated with either a single value or an iterable of values.
+    Fields as associated with either a single value or an iterable of values.
 
     @cvar uid: The primary unique identifier for a directory record.
         The associated value must be a L{unicode}.

Modified: twext/trunk/twext/who/index.py
===================================================================
--- twext/trunk/twext/who/index.py	2013-12-18 19:29:44 UTC (rev 12129)
+++ twext/trunk/twext/who/index.py	2013-12-18 22:28:51 UTC (rev 12130)
@@ -24,8 +24,6 @@
     "DirectoryRecord",
 ]
 
-from itertools import chain
-
 from twisted.python.constants import Names, NamedConstant
 from twisted.internet.defer import succeed, inlineCallbacks, returnValue
 
@@ -142,9 +140,8 @@
         which are indexed.
     """
 
-    fieldName = ConstantsContainer(chain(
-        BaseDirectoryService.fieldName.iterconstants(),
-        FieldName.iterconstants()
+    fieldName = ConstantsContainer((
+        BaseDirectoryService.fieldName, FieldName
     ))
 
     indexedFields = (

Modified: twext/trunk/twext/who/opendirectory/service.py
===================================================================
--- twext/trunk/twext/who/opendirectory/service.py	2013-12-18 19:29:44 UTC (rev 12129)
+++ twext/trunk/twext/who/opendirectory/service.py	2013-12-18 22:28:51 UTC (rev 12130)
@@ -194,6 +194,7 @@
     compound = ValueConstant(0x210B)
     compound.matchType = MatchType.compound
 
+
     @classmethod
     def fromMatchType(cls, matchType):
         if not hasattr(cls, "_matchTypeByMatchType"):

Modified: twext/trunk/twext/who/test/test_util.py
===================================================================
--- twext/trunk/twext/who/test/test_util.py	2013-12-18 19:29:44 UTC (rev 12129)
+++ twext/trunk/twext/who/test/test_util.py	2013-12-18 22:28:51 UTC (rev 12130)
@@ -18,6 +18,8 @@
 Directory service utility tests.
 """
 
+from itertools import chain
+
 from twisted.trial import unittest
 from twisted.python.constants import Names, NamedConstant
 from twisted.python.constants import Flags, FlagConstant
@@ -36,7 +38,12 @@
     screwdriver.description = u"screw twister"
 
 
+class MoreTools(Names):
+    saw = NamedConstant()
 
+    saw.description = u"z maker"
+
+
 class Instruments(Names):
     hammer = NamedConstant()
     chisel = NamedConstant()
@@ -57,14 +64,66 @@
 
 
 class ConstantsContainerTest(unittest.TestCase):
-    def test_conflict(self):
-        constants = set((Tools.hammer, Instruments.hammer))
-        self.assertRaises(ValueError, ConstantsContainer, constants)
+    """
+    Tests for L{ConstantsContainer}.
+    """
 
+    def test_constants_from_constants(self):
+        """
+        Initialize a container from some constants.
+        """
+        constants = set((Tools.hammer, Tools.screwdriver, Instruments.chisel))
+        self.assertEquals(
+            set(ConstantsContainer(constants).iterconstants()),
+            constants,
+        )
 
+
+    def test_constants_from_containers(self):
+        """
+        Initialize a container from other containers.
+        """
+        self.assertEquals(
+            set(ConstantsContainer((Tools, MoreTools)).iterconstants()),
+            set(chain(Tools.iterconstants(), MoreTools.iterconstants())),
+        )
+
+
+    def test_constants_from_iterables(self):
+        """
+        Initialize a container from iterables of constants.
+        """
+        self.assertEquals(
+            set(
+                ConstantsContainer((
+                    Tools.iterconstants(), MoreTools.iterconstants()
+                )).iterconstants()
+            ),
+            set(chain(Tools.iterconstants(), MoreTools.iterconstants())),
+        )
+
+
+    def test_conflictingClasses(self):
+        """
+        A container can't contain two constants with the same name.
+        """
+        self.assertRaises(TypeError, ConstantsContainer, (Tools, Switches))
+
+
+    def test_conflictingNames(self):
+        """
+        A container can't contain two constants with the same name.
+        """
+        self.assertRaises(ValueError, ConstantsContainer, (Tools, Instruments))
+
+
     def test_attrs(self):
-        constants = set((Tools.hammer, Tools.screwdriver, Instruments.chisel))
-        container = ConstantsContainer(constants)
+        """
+        Constants are assessible via attributes.
+        """
+        container = ConstantsContainer((
+            Tools.hammer, Tools.screwdriver, Instruments.chisel
+        ))
 
         self.assertEquals(container.hammer, Tools.hammer)
         self.assertEquals(container.screwdriver, Tools.screwdriver)
@@ -73,6 +132,10 @@
 
 
     def test_iterconstants(self):
+        """
+        L{ConstantsContainer.iterconstants}C{()} produces the contained
+        constants.
+        """
         constants = set((Tools.hammer, Tools.screwdriver, Instruments.chisel))
         container = ConstantsContainer(constants)
 
@@ -81,7 +144,11 @@
             constants,
         )
 
+
     def test_lookupByName(self):
+        """
+        Constants are assessible via L{ConstantsContainer.lookupByName}.
+        """
         constants = set((
             Instruments.hammer,
             Tools.screwdriver,
@@ -110,14 +177,20 @@
 
 
 class UtilTest(unittest.TestCase):
+    """
+    Miscellaneous tests.
+    """
+
     def test_uniqueResult(self):
         self.assertEquals(1, uniqueResult((1,)))
         self.assertRaises(DirectoryServiceError, uniqueResult, (1, 2, 3))
 
+
     def test_describe(self):
         self.assertEquals(u"nail pounder", describe(Tools.hammer))
         self.assertEquals(u"hammer", describe(Instruments.hammer))
 
+
     def test_describeFlags(self):
         self.assertEquals(u"blue", describe(Switches.b))
         self.assertEquals(u"red|green", describe(Switches.r | Switches.g))

Modified: twext/trunk/twext/who/util.py
===================================================================
--- twext/trunk/twext/who/util.py	2013-12-18 19:29:44 UTC (rev 12129)
+++ twext/trunk/twext/who/util.py	2013-12-18 22:28:51 UTC (rev 12130)
@@ -26,7 +26,8 @@
     "iterFlags",
 ]
 
-from twisted.python.constants import FlagConstant
+from twisted.python.constants import Names, Values, Flags
+from twisted.python.constants import NamedConstant, ValueConstant, FlagConstant
 
 from twext.who.idirectory import DirectoryServiceError
 
@@ -36,14 +37,45 @@
     """
     A container for constants.
     """
-    def __init__(self, constants):
-        myConstants = {}
+    def __init__(self, sources):
+        self._constants = {}
+
+        for source in sources:
+            if type(source) is type:
+                if issubclass(
+                    source, (ConstantsContainer, Names, Values, Flags)
+                ):
+                    self._addConstants(source.iterconstants())
+                else:
+                    raise TypeError(
+                        "Unknown constants type: {0}".format(source)
+                    )
+
+            elif isinstance(
+                source, (NamedConstant, ValueConstant, FlagConstant)
+            ):
+                self._addConstants((source,))
+
+            else:
+                self._addConstants(source)
+
+
+    def _addConstants(self, constants):
         for constant in constants:
-            if constant.name in myConstants:
+            if hasattr(self, "_constantsClass"):
+                if constant.__class__ != self._constantsClass:
+                    raise TypeError(
+                        "Can't mix constants classes in the "
+                        "same constants container: {0} != {1}"
+                        .format(constant.__class__, self._constantsClass)
+                    )
+            else:
+                self._constantsClass = constant.__class__
+
+            if constant.name in self._constants:
                 raise ValueError("Name conflict: {0}".format(constant.name))
-            myConstants[constant.name] = constant
 
-        self._constants = myConstants
+            self._constants[constant.name] = constant
 
 
     def __getattr__(self, name):
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140312/a420fc60/attachment.html>


More information about the calendarserver-changes mailing list