def test_jack_and_jill(self): p = Predicate("name is 'Jack'\r\nand friend is 'Jill'") assert p.is_valid() assert p.evaluate({"name": "Jack", "friend": "Jill"}) res, ctx = p.analyze({"name": "Jack", "friend": "Jill"}) assert res assert p.description() == """AND operator at line: 2, col 1
def test_error(self): p = Predicate("foo is\nbar !! fun") assert not p.is_valid() assert p.errors()['errors'] == [ 'Failed to parse characters !! at line 2, col 5', 'Syntax error with fun at line 2, col 8', ]
def test_invalid_ast(self): p = Predicate("server matches '(unbal'") assert not p.is_valid() errs = p.errors() assert 'Compilation failed for' in errs["errors"][0] assert 'unbalanced parenthesis' == errs["regex"]["(unbal"] or\ errs["regex"]["(unbal"].startswith("missing ), unterminated subpattern")
def test_samples(): p = os.path.dirname(os.path.abspath(__file__)) fh = open(os.path.join(p, "preds.txt")) for line, pred in enumerate(fh): pred = pred.strip() obj = Predicate(pred) if not obj.is_valid(): print "Invalid Predicate!" print "Line: ", line print "Predicate: ", pred info = obj.errors() print "Errors: ", "\n".join(info["errors"]) for k, v in info["regex"].iteritems(): print "\t%s : %s" % (k, repr(v)) assert False res, ctx = obj.analyze(DOC) if not pred.endswith("true") and not pred.endswith("false"): print "Line: ", line print "Unknown result!" print "Predicate: ", pred assert False if (pred.endswith("true") and not res) or (pred.endswith("false") and res): print "Line: ", line print "Predicate: ", pred print "Failures: ", "\n".join(ctx.failed) print "Literals: " for k, v in ctx.literals.iteritems(): print "\t%s : %s" % (k, repr(v)) assert False
def test_samples(): p = os.path.dirname(os.path.abspath(__file__)) fh = open(os.path.join(p, "preds.txt")) for line, pred in enumerate(fh): pred = pred.strip() obj = Predicate(pred) if not obj.is_valid(): print "Invalid Predicate!" print "Line: ", line print "Predicate: ", pred info = obj.errors() print "Errors: ", "\n".join(info["errors"]) for k, v in info["regex"].iteritems(): print "\t%s : %s" % (k, repr(v)) assert False res, info = obj.analyze(DOC) if not pred.endswith("true") and not pred.endswith("false"): print "Line: ", line print "Unknown result!" print "Predicate: ", pred assert False if (pred.endswith("true") and not res) or (pred.endswith("false") and res): print "Line: ", line print "Predicate: ", pred print "Failures: ", "\n".join(info["failed"]) print "Literals: " for k, v in info["literals"].iteritems(): print "\t%s : %s" % (k, repr(v)) assert False
def main(): import sys if len(sys.argv) == 2: p = Predicate(sys.argv[1]) if not p.is_valid(): for error in p.errors()['errors']: print(error) return else: p = None rows = [] for id, size, date, sender, msg, dest in mailq(): colsObject = { "cols": [{ "value": id }, { "value": size }, { "value": date }, { "value": sender }, { "value": dest }] } rows.append(colsObject) #doc = list(id, size, date, sender, msg, dest) if p == None or p.evaluate(doc): rowsObject = {"rows": rows} hrowsColsObject = { "cols": [{ "value": "ID" }, { "value": "Size" }, { "value": "Date" }, { "value": "Sender" }, { "value": "Recipient" }] } hrowsColsArray = [] hrowsColsArray.append(hrowsColsObject) hrowsObject = {"hrows": hrowsColsArray} returnString = json.dumps(hrowsObject)[1:-1] + ", " + json.dumps( rowsObject)[1:-1] print(returnString)
class TriggerCondition(object): """Represent a trigger condition""" def __init__(self, predicate): from pypred import Predicate self.predicate = Predicate(predicate) if not self.predicate.is_valid(): raise ValueError('Predicate is invalid: ' + str(predicate)) def satisfied_by(self, execution_context): """test the predicate against the execution context""" return self.predicate.evaluate(execution_context)
def test_jack_and_jill(self): p = Predicate("name is 'Jack' and friend is 'Jill'") assert p.is_valid() assert p.evaluate({"name": "Jack", "friend": "Jill"}) res, ctx = p.analyze({"name": "Jack", "friend": "Jill"}) assert res assert ( p.description() == """AND operator at line: 1, col 15 IS comparison at line: 1, col 5 Literal name at line: 1, col 0 Literal 'Jack' at line: 1, col 8 IS comparison at line: 1, col 26 Literal friend at line: 1, col 19 Literal 'Jill' at line: 1, col 29 """ )
def check_condition(self, tracker, condition): if condition is None: return True if isinstance(condition, str): slots = dict() for slotName, slotValue in tracker.slots.items(): slots[slotName] = slotValue.value predicate = Predicate(condition) if predicate.is_valid() is not True: return False result = predicate.evaluate(slots) return result props = condition.get("properties", {}) children = condition.get("children1", {}).values() if condition.get("type") == "rule": return self.check_atomic_condition(tracker, **props) conjunction_operator = any if props.get("conjunction") == "OR" else all polarity = (lambda p: not p) if props.get("not") else (lambda p: p) return polarity( conjunction_operator( self.check_condition(tracker, child) for child in children))
def test_invalid_token(self): p = Predicate("name is !! and true") assert not p.is_valid() assert "Failed to parse characters !!" in p.errors()["errors"][0]
def test_invalid_parse(self): p = Predicate("true true") assert not p.is_valid() assert 'Syntax error with true' in p.errors()["errors"][0]
def test_invalid_token(self): p = Predicate("name is !! and true") assert not p.is_valid() assert 'Failed to parse characters !!' in p.errors()["errors"][0]
def test_invalid_end(self): p = Predicate("name is 'Jack' and ") assert not p.is_valid() assert 'Unexpected end of predicate!' in p.errors()["errors"]
def test_invalid_ast(self): p = Predicate("server matches '(unbal'") assert not p.is_valid() errs = p.errors() assert 'Compilation failed for' in errs["errors"][0] assert 'unbalanced parenthesis' == errs["regex"]["(unbal"]
def test_invalid_ast(self): p = Predicate("server matches '(unbal'") assert not p.is_valid() errs = p.errors() assert "Compilation failed for" in errs["errors"][0] assert "unbalanced parenthesis" == errs["regex"]["(unbal"]
def test_invalid_parse(self): p = Predicate("true true") assert not p.is_valid() assert "Syntax error with true" in p.errors()["errors"][0]
def test_invalid_end(self): p = Predicate("name is 'Jack' and ") assert not p.is_valid() assert "Unexpected end of predicate!" in p.errors()["errors"]