From 5a636abf7e98a5687811167917d0e07103599bc8 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Sat, 19 Jul 2025 10:42:15 +0300 Subject: Vendor the unmaintained telepot dependency --- telepot/filtering.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 telepot/filtering.py (limited to 'telepot/filtering.py') diff --git a/telepot/filtering.py b/telepot/filtering.py new file mode 100644 index 0000000..52ed749 --- /dev/null +++ b/telepot/filtering.py @@ -0,0 +1,34 @@ +def pick(obj, keys): + def pick1(k): + if type(obj) is dict: + return obj[k] + else: + return getattr(obj, k) + + if isinstance(keys, list): + return [pick1(k) for k in keys] + else: + return pick1(keys) + +def match(data, template): + if isinstance(template, dict) and isinstance(data, dict): + def pick_and_match(kv): + template_key, template_value = kv + if hasattr(template_key, 'search'): # regex + data_keys = list(filter(template_key.search, data.keys())) + if not data_keys: + return False + elif template_key in data: + data_keys = [template_key] + else: + return False + return any(map(lambda data_value: match(data_value, template_value), pick(data, data_keys))) + + return all(map(pick_and_match, template.items())) + elif callable(template): + return template(data) + else: + return data == template + +def match_all(msg, templates): + return all(map(lambda t: match(msg, t), templates)) -- cgit v1.3