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

source_changes at macosforge.org source_changes at macosforge.org
Wed Mar 12 11:18:53 PDT 2014


Revision: 12156
          http://trac.calendarserver.org//changeset/12156
Author:   wsanchez at apple.com
Date:     2013-12-19 14:56:15 -0800 (Thu, 19 Dec 2013)
Log Message:
-----------
Pull in methods from ConstantsContainer sources.

Modified Paths:
--------------
    twext/trunk/twext/who/test/test_util.py
    twext/trunk/twext/who/util.py

Modified: twext/trunk/twext/who/test/test_util.py
===================================================================
--- twext/trunk/twext/who/test/test_util.py	2013-12-19 22:36:09 UTC (rev 12155)
+++ twext/trunk/twext/who/test/test_util.py	2013-12-19 22:56:15 UTC (rev 12156)
@@ -32,18 +32,27 @@
 
 class Tools(Names):
     hammer = NamedConstant()
+    hammer.description = u"nail pounder"
+
     screwdriver = NamedConstant()
-
-    hammer.description = u"nail pounder"
     screwdriver.description = u"screw twister"
 
 
+    @staticmethod
+    def isPounder(tool):
+        if getattr(tool, "description", "").endswith("pounder"):
+            return True
+        return False
+
+
 class MoreTools(Names):
     saw = NamedConstant()
-
     saw.description = u"z maker"
 
+    mallet = NamedConstant()
+    mallet.description = u"soft pounder"
 
+
 class Instruments(Names):
     hammer = NamedConstant()
     chisel = NamedConstant()
@@ -145,6 +154,17 @@
         )
 
 
+    def test_staticmethod(self):
+        """
+        Static methods from source containers are accessible via attributes.
+        """
+        container = ConstantsContainer((Tools, MoreTools))
+        self.assertTrue(container.isPounder(container.hammer))
+        self.assertTrue(container.isPounder(container.mallet))
+        self.assertFalse(container.isPounder(container.screwdriver))
+        self.assertFalse(container.isPounder(container.saw))
+
+
     def test_lookupByName(self):
         """
         Constants are assessible via L{ConstantsContainer.lookupByName}.

Modified: twext/trunk/twext/who/util.py
===================================================================
--- twext/trunk/twext/who/util.py	2013-12-19 22:36:09 UTC (rev 12155)
+++ twext/trunk/twext/who/util.py	2013-12-19 22:56:15 UTC (rev 12156)
@@ -40,21 +40,19 @@
     """
     def __init__(self, sources):
         self._constants = {}
+        self._methods = {}
 
         for source in sources:
             if type(source) is type:
-                if issubclass(
-                    source, (ConstantsContainer, Names, Values, Flags)
-                ):
+                if issubclass(source, CONTAINER_CLASSES):
                     self._addConstants(source.iterconstants())
+                    self._addMethods(source)
                 else:
                     raise TypeError(
                         "Unknown constants type: {0}".format(source)
                     )
 
-            elif isinstance(
-                source, (NamedConstant, ValueConstant, FlagConstant)
-            ):
+            elif isinstance(source, CONSTANT_CLASSES):
                 self._addConstants((source,))
 
             else:
@@ -79,13 +77,27 @@
             self._constants[constant.name] = constant
 
 
+    def _addMethods(self, container):
+        for name, value in container.__dict__.iteritems():
+            if type(value) is staticmethod:
+                if name in self._constants or name in self._methods:
+                    raise ValueError("Name conflict: {0}".format(name))
+
+                self._methods[name] = getattr(container, name)
+
+
     def __getattr__(self, name):
-        try:
-            return self._constants[name]
-        except KeyError:
-            raise AttributeError(name)
+        attr = self._constants.get(name, None)
+        if attr is not None:
+            return attr
 
+        attr = self._methods.get(name, None)
+        if attr is not None:
+            return attr
 
+        raise AttributeError(name)
+
+
     def iterconstants(self):
         return self._constants.itervalues()
 
@@ -129,3 +141,8 @@
         # Work around http://twistedmatrix.com/trac/ticket/6302
         # FIXME: This depends on a private attribute (flags._container)
         return (flags._container.lookupByName(name) for name in flags.names)
+
+
+
+CONTAINER_CLASSES = (ConstantsContainer, Names, Values, Flags)
+CONSTANT_CLASSES = (NamedConstant, ValueConstant, FlagConstant)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140312/3d425b04/attachment.html>


More information about the calendarserver-changes mailing list