provide negation operator for selectors, unfortunately, it's not possible to user the python keyword 'not' tls-sprint
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Wed, 18 Feb 2009 20:23:43 +0100
branchtls-sprint
changeset 827 3f08481e6e51
parent 826 51cb3d85c059
child 828 394927376a01
provide negation operator for selectors, unfortunately, it's not possible to user the python keyword 'not'
vregistry.py
--- a/vregistry.py	Wed Feb 18 19:45:28 2009 +0100
+++ b/vregistry.py	Wed Feb 18 20:23:43 2009 +0100
@@ -592,6 +592,9 @@
         return OrSelector(self, other)
     def __ror__(self, other):
         return OrSelector(other, self)
+
+    def __invert__(self):
+        return NotSelector(self)
     
     # XXX (function | function) or (function & function) not managed yet
 
@@ -685,6 +688,17 @@
                 return partscore
         return 0
 
+class NotSelector(Selector):
+    """negation selector"""
+    def __init__(self, selector):
+        self.selector = selector
+
+    def __call__(self, cls, *args, **kwargs):
+        score = self.selector(cls, *args, **kwargs)
+        return not score
+
+    def __str__(self):
+        return 'NOT(%s)' % super(NotSelector, self).__str__()
 
 # advanced selector building functions ########################################