feat(filter): add some set theory filters

This commit is contained in:
Jan Christian Grünhage 2023-12-12 10:49:55 +01:00
parent df5783c489
commit ee40fd92f7
No known key found for this signature in database
GPG key ID: EEC1170CE56FA2ED

37
plugins/filter/base.py Normal file
View file

@ -0,0 +1,37 @@
import collections
import re
from ansible.errors import AnsibleFilterError
def consensus(pipe_in: dict[str], set: bool = False, msg=""):
apply = FilterModule.xset if set else id
[head, *tail] = map(apply, pipe_in.values())
if not all(head == x for x in tail):
raise AnsibleFilterError(msg or "No consensus", pipe_in)
return head
def split2multidict(pipe_in, sep=None):
result = collections.defaultdict(list)
for key, val in map(lambda line: line.split(sep, 1), pipe_in):
result[key].append(val)
return result
def regex_replace(pipe_in, pattern, repl, count=0, require_n=0):
result, actual_n = re.subn(pattern, repl, pipe_in, count=count)
if require_n not in {0, actual_n}:
raise AnsibleFilterError("unexpected substitution count")
return result
class FilterModule(object):
xset = set
def filters(self):
return {
"consensus": consensus,
"split2multidict": split2multidict,
"regex_replace": regex_replace,
}