Implement hostmask_match_any, for more efficient matching of multiple hostmasks

This commit is contained in:
jesopo 2019-06-20 14:21:11 +01:00
parent 792a42be6e
commit b6e194a450

View file

@ -330,12 +330,18 @@ class BatchType(object):
t = list(set([type])&self._names) t = list(set([type])&self._names)
return t[0] if t else None return t[0] if t else None
def hostmask_match(hostmask: str, pattern: str) -> bool: def hostmask_match_many(hostmasks: typing.List[str], pattern: str) -> str:
part1_out = [] part1_out = []
for part1 in pattern.split("?"): for part1 in pattern.split("?"):
part2_out = [] part2_out = []
for part2 in part1.split("*"): for part2 in part1.split("*"):
part2_out.append(re.escape(part2)) part2_out.append(re.escape(part2))
part1_out.append(".*".join(part2_out)) part1_out.append(".*".join(part2_out))
pattern_parsed = ".".join(part1_out) pattern_re = re.compile(".".join(part1_out))
return not re.match(pattern_parsed, hostmask) == None for hostmask in hostmasks:
if pattern_re.match(hostmask):
return hostmask
return None
def hostmask_match(hostmask: str, pattern: str) -> bool:
return not hostmask_match_many([hostmask], pattern) == None