irclib: Refactor hostmaskPatternEqual to expose compileHostmaskPattern.

This clarifies the code a bit.

And I want to store compiled patterns in ircdb.User in a future
commit, so they can outlive the LRU cache.
This commit is contained in:
Valentin Lorentz 2021-05-27 21:36:43 +02:00
parent 711db72ad0
commit a5cd870bd2

View File

@ -169,9 +169,9 @@ def areReceivers(s, strictRfc=True, nicklen=None, chantypes='#&!',
return all([nick(x) or chan(x) for x in s.split(',')]) return all([nick(x) or chan(x) for x in s.split(',')])
_patternCache = utils.structures.CacheDict(1000) _patternCache = utils.structures.CacheDict(1000)
def _hostmaskPatternEqual(pattern, hostmask): def compileHostmaskPattern(pattern):
try: try:
return _patternCache[pattern](hostmask) is not None return _patternCache[pattern]
except KeyError: except KeyError:
# We make our own regexps, rather than use fnmatch, because fnmatch's # We make our own regexps, rather than use fnmatch, because fnmatch's
# case-insensitivity is not IRC's case-insensitity. # case-insensitivity is not IRC's case-insensitity.
@ -194,7 +194,7 @@ def _hostmaskPatternEqual(pattern, hostmask):
fd.write('$') fd.write('$')
f = re.compile(fd.getvalue(), re.I).match f = re.compile(fd.getvalue(), re.I).match
_patternCache[pattern] = f _patternCache[pattern] = f
return f(hostmask) is not None return f
_hostmaskPatternEqualCache = utils.structures.CacheDict(1000) _hostmaskPatternEqualCache = utils.structures.CacheDict(1000)
def hostmaskPatternEqual(pattern, hostmask): def hostmaskPatternEqual(pattern, hostmask):
@ -203,9 +203,9 @@ def hostmaskPatternEqual(pattern, hostmask):
try: try:
return _hostmaskPatternEqualCache[(pattern, hostmask)] return _hostmaskPatternEqualCache[(pattern, hostmask)]
except KeyError: except KeyError:
b = _hostmaskPatternEqual(pattern, hostmask) matched = compileHostmaskPattern(pattern)(hostmask) is not None
_hostmaskPatternEqualCache[(pattern, hostmask)] = b _hostmaskPatternEqualCache[(pattern, hostmask)] = matched
return b return matched
def banmask(hostmask): def banmask(hostmask):
"""Returns a properly generic banning hostmask for a hostmask. """Returns a properly generic banning hostmask for a hostmask.