match_rtype hook selector now accepts frometypes/toetypes optional arguments to match only given subject/object entity types
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 25 Jan 2010 19:24:09 +0100
changeset 4341 2d2aa2526c56
parent 4340 cc0fe6279181
child 4342 b4e186da08f2
match_rtype hook selector now accepts frometypes/toetypes optional arguments to match only given subject/object entity types
server/hook.py
--- a/server/hook.py	Mon Jan 25 19:18:59 2010 +0100
+++ b/server/hook.py	Mon Jan 25 19:24:09 2010 +0100
@@ -141,18 +141,23 @@
     :param *expected: parameters (eg `basestring`) which are expected to be
                       found in named arguments (kwargs)
     """
-    def __init__(self, *expected):
+    def __init__(self, *expected, **more):
         self.expected = expected
-        # if len(expected) == 1:
-        #     try:
-        #         iter(expected[0])
-        #         self.expected = expected[0]
-        #     except TypeError:
-        #         pass
+        self.frometypes = more.pop('frometypes', None)
+        self.toetypes = more.pop('toetypes', None)
 
     @lltrace
     def __call__(self, cls, req, *args, **kwargs):
-        return kwargs.get('rtype') in self.expected
+        if kwargs.get('rtype') not in self.expected:
+            return 0
+        if self.frometypes is not None and \
+               req.describe(kwargs['eidfrom'])[0] not in self.frometypes:
+            return 0
+        if self.toetypes is not None and \
+               req.describe(kwargs['eidto'])[0] not in self.toetypes:
+            return 0
+        return 1
+
 
 class match_rtype_sets(match_search_state):
     """accept if parameters specified as initializer arguments are specified