From 6d95cd5dd37821e78f5061238e8958907efeddb7 Mon Sep 17 00:00:00 2001 From: Doug Blank Date: Fri, 6 May 2016 10:04:24 -0400 Subject: [PATCH] Where: work with methods, ala Rules --- gramps/gen/db/test/test_where.py | 8 ++++++++ gramps/gen/db/where.py | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/gramps/gen/db/test/test_where.py b/gramps/gen/db/test/test_where.py index c23b4ee63..0ef4d0d62 100644 --- a/gramps/gen/db/test/test_where.py +++ b/gramps/gen/db/test/test_where.py @@ -19,6 +19,7 @@ # from gramps.gen.db.where import eval_where +from gramps.gen.lib import Person import unittest ########## @@ -40,6 +41,9 @@ class Thing(object): def where(self): return lambda person: person.gramps_id == self.list[1] + def apply(self, db, person): + return person.gender == Person.MALE + class ClosureTest(unittest.TestCase): def check(self, test): result = eval_where(test[0]) @@ -98,5 +102,9 @@ class ClosureTest(unittest.TestCase): (lambda person: LIKE(person.gramps_id, "I000%"), ["gramps_id", "LIKE", "I000%"])) + def test_11(self): + self.check( + [Thing().apply, ["gender", "==", 1]]) + if __name__ == "__main__": unittest.main() diff --git a/gramps/gen/db/where.py b/gramps/gen/db/where.py index e9641143b..e5d8fa4a9 100644 --- a/gramps/gen/db/where.py +++ b/gramps/gen/db/where.py @@ -115,6 +115,13 @@ class ParseFilter(Visitor): self.parameter = self.visit(node.args)[0] return self.visit(node.body) + def visitFunctionDef(self, node): + self.parameter = self.visit(node.args)[2] # ['self', 'db', 'person'] + return self.visit(node.body)[0] + + def visitReturn(self, node): + return self.visit(node.value) + def visitarguments(self, node): return [self.visit(arg) for arg in node.args]