introduce MultiSelector base class tls-sprint
authorsylvain.thenault@logilab.fr
Tue, 17 Feb 2009 21:50:24 +0100
branchtls-sprint
changeset 698 7dfd03e9e810
parent 697 06807984e610
child 699 cc149f4def1e
introduce MultiSelector base class
vregistry.py
--- a/vregistry.py	Tue Feb 17 21:46:46 2009 +0100
+++ b/vregistry.py	Tue Feb 17 21:50:24 2009 +0100
@@ -587,6 +587,36 @@
     This class is only here to give access to binary operators, the
     selector logic itself should be implemented in the __call__ method
     """
+
+    def search_selector(self, selector):
+        """search for the given selector or selector instance in the selectors
+        tree. Return it of None if not found
+        """
+        if self is selector:
+            return self
+        if isinstance(selector, type) and instance(self, selector):
+            return self
+        return None
+    
+    def __and__(self, other):
+        return AndSelector(self, other)
+    def __rand__(self, other):
+        return AndSelector(other, self)
+
+    def __or__(self, other):
+        return OrSelector(self, other)
+    def __ror__(self, other):
+        return OrSelector(other, self)
+    
+    # XXX (function | function) or (function & function) not managed yet
+
+    def __call__(self, cls, *args, **kwargs):
+        return NotImplementedError("selector %s must implement its logic "
+                                   "in its __call__ method" % self.__class__)
+
+class MultiSelector(Selector):
+    """base class for compound selector classes
+    """
     def __init__(self, *selectors):
         self.selectors = self.merge_selectors(selectors)
 
@@ -609,10 +639,6 @@
         """search for the given selector or selector instance in the selectors
         tree. Return it of None if not found
         """
-        if self is selector:
-            return self
-        if isinstance(selector, type) and instance(self, selector):
-            return self
         for childselector in self.selectors:
             try:
                 if childselector.use_selector(selector):
@@ -621,22 +647,6 @@
                 if childselector is selector:
                     return childselector
         return None
-    
-    def __and__(self, other):
-        return AndSelector(self, other)
-    def __rand__(self, other):
-        return AndSelector(other, self)
-
-    def __or__(self, other):
-        return OrSelector(self, other)
-    def __ror__(self, other):
-        return OrSelector(other, self)
-    
-    # XXX (function | function) or (function & function) not managed yet
-
-    def __call__(self, cls, *args, **kwargs):
-        return NotImplementedError("selector %s must implement its logic "
-                                   "in its __call__ method" % self.__class__)
 
     
 def objectify_selector(selector_func):
@@ -652,7 +662,7 @@
                 {'__call__': lambda self, *args: selector_func(*args)})
 
 
-class AndSelector(Selector):
+class AndSelector(MultiSelector):
     """and-chained selectors (formerly known as chainall)"""
     def __call__(self, cls, *args, **kwargs):
         score = 0
@@ -664,7 +674,7 @@
         return score
 
 
-class OrSelector(Selector):
+class OrSelector(MultiSelector):
     """or-chained selectors (formerly known as chainfirst)"""
     def __call__(self, cls, *args, **kwargs):
         for selector in self.selectors: