[selectors] new is_in_state select to avoid common error when writing state based selector stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 26 Mar 2010 15:54:01 +0100
branchstable
changeset 5044 1d9bef4a2b0c
parent 5043 fe52dd3936cf
child 5045 c5c1fd2bdfa4
[selectors] new is_in_state select to avoid common error when writing state based selector
selectors.py
--- a/selectors.py	Fri Mar 26 15:53:07 2010 +0100
+++ b/selectors.py	Fri Mar 26 15:54:01 2010 +0100
@@ -1078,6 +1078,24 @@
             return 1
         return 0
 
+class is_in_state(score_entity):
+    """return 1 if entity is in one of the states given as argument list
+
+    you should use this instead of your own score_entity x: x.state == 'bla'
+    selector to avoid some gotchas:
+
+    * possible views gives a fake entity with no state
+    * you must use the latest tr info, not entity.state for repository side
+      checking of the current state
+    """
+    def __init__(self, *states):
+        def score(entity, states=set(states)):
+            try:
+                return entity.latest_trinfo().new_state.name in states
+            except AttributeError:
+                return None
+        super(is_in_state, self).__init__(score)
+
 
 ## deprecated stuff ############################################################