From 249e99805aeb894941b77c5556bdb202118b066d Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Sat, 1 Sep 2018 00:25:51 +0100 Subject: [PATCH] Implement optional x in y matching for attributes in the monitor system --- commands/mon.py | 5 +++++ modules/monitor.py | 24 +++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/commands/mon.py b/commands/mon.py index 9b9ed4c..5231abe 100644 --- a/commands/mon.py +++ b/commands/mon.py @@ -20,6 +20,9 @@ class Mon: group2.add_argument("-p", "--append", action="store_true", dest="doAppend", help="Append entries to lists instead of replacing") group2.add_argument("-r", "--remove", action="store_true", dest="doRemove", help="Remove entries in lists instead of replacing") + self.parser.add_argument("--inside", action="store_true", dest="inside", help="Use x in y logic for matching instead of comparing exact values") + self.parser.add_argument("--outside", action="store_false", dest="inside", help="Don't use x in y logic for matching instead of comparing exact values") + self.parser.add_argument("--type", nargs="*", metavar="type", dest="specType", help="Specify type of spec matching. Available types: join, part, quit, msg, topic, mode, nick, kick, notice, action, who") self.parser.add_argument("--free", nargs="*", metavar="query", dest="free", help="Use freeform matching") self.parser.add_argument("--exact", nargs="*", metavar="query", dest="exact", help="Use exact matching") @@ -218,6 +221,8 @@ class Mon: cast["real"] = [] for i in obj.real: cast["real"].append(" ".join(i)) + if not obj.inside == None: + cast["inside"] = obj.inside return cast def subtractCast(self, source, patch, info): diff --git a/modules/monitor.py b/modules/monitor.py index 48b01c3..7552648 100644 --- a/modules/monitor.py +++ b/modules/monitor.py @@ -19,12 +19,34 @@ def testNetTarget(name, target): if not called: return False +def contained_in(x, y): + if x is None or y is None: + return False + elif x == y: + return True + else: + return y in x + +def is_in(k, vs, A): + return any(contained_in(A.get(k), vp) for vp in vs) + +def matches(A, B): + return all(is_in(k, vs, A) for (k, vs) in B.items()) + def magicFunction(A, B): + isInside = False if "send" in B.keys(): del B["send"] if "sources" in B.keys(): del B["sources"] - return all(A[k] in B[k] for k in set(A) & set(B)) and set(B) <= set(A) + if "inside" in B.keys(): + if B["inside"] == True: + isInside = True + del B["inside"] + if isInside: + return matches(A, B) + else: + return all(A[k] in B[k] for k in set(A) & set(B)) and set(B) <= set(A) def event(name, target, cast): monitorGroups = testNetTarget(name, target)