Annotate `matcher.py` (#1532)

This commit is contained in:
Marcelo Trylesinski 2022-06-03 08:43:36 +02:00 committed by GitHub
parent c4829754db
commit 1a7b9c490b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 11 deletions

View File

@ -4,10 +4,13 @@ from __future__ import annotations
from fnmatch import fnmatch from fnmatch import fnmatch
from re import match as rematch from re import match as rematch
from typing import Callable, Dict, Optional, cast
from .utils.compat import entrypoints from .utils.compat import entrypoints
from .utils.encoding import bytes_to_str from .utils.encoding import bytes_to_str
MatcherFunction = Callable[[str, str], bool]
class MatcherNotInstalled(Exception): class MatcherNotInstalled(Exception):
"""Matcher not installed/found.""" """Matcher not installed/found."""
@ -19,15 +22,15 @@ class MatcherRegistry:
MatcherNotInstalled = MatcherNotInstalled MatcherNotInstalled = MatcherNotInstalled
matcher_pattern_first = ["pcre", ] matcher_pattern_first = ["pcre", ]
def __init__(self): def __init__(self) -> None:
self._matchers = {} self._matchers: Dict[str, MatcherFunction] = {}
self._default_matcher = None self._default_matcher: Optional[MatcherFunction] = None
def register(self, name, matcher): def register(self, name: str, matcher: MatcherFunction) -> None:
"""Add matcher by name to the registry.""" """Add matcher by name to the registry."""
self._matchers[name] = matcher self._matchers[name] = matcher
def unregister(self, name): def unregister(self, name: str) -> None:
"""Remove matcher by name from the registry.""" """Remove matcher by name from the registry."""
try: try:
self._matchers.pop(name) self._matchers.pop(name)
@ -36,7 +39,7 @@ class MatcherRegistry:
f'No matcher installed for {name}' f'No matcher installed for {name}'
) )
def _set_default_matcher(self, name): def _set_default_matcher(self, name: str) -> None:
"""Set the default matching method. """Set the default matching method.
:param name: The name of the registered matching method. :param name: The name of the registered matching method.
@ -53,7 +56,13 @@ class MatcherRegistry:
f'No matcher installed for {name}' f'No matcher installed for {name}'
) )
def match(self, data, pattern, matcher=None, matcher_kwargs=None): def match(
self,
data: bytes,
pattern: bytes,
matcher: Optional[str] = None,
matcher_kwargs: Optional[Dict[str, str]] = None
) -> bool:
"""Call the matcher.""" """Call the matcher."""
if matcher and not self._matchers.get(matcher): if matcher and not self._matchers.get(matcher):
raise self.MatcherNotInstalled( raise self.MatcherNotInstalled(
@ -99,7 +108,7 @@ match = registry.match
.. function:: register(name, matcher): .. function:: register(name, matcher):
Register a new matching method. Register a new matching method.
:param name: A convience name for the mathing method. :param name: A convenient name for the mathing method.
:param matcher: A method that will be passed data and pattern. :param matcher: A method that will be passed data and pattern.
""" """
register = registry.register register = registry.register
@ -113,14 +122,14 @@ register = registry.register
unregister = registry.unregister unregister = registry.unregister
def register_glob(): def register_glob() -> None:
"""Register glob into default registry.""" """Register glob into default registry."""
registry.register('glob', fnmatch) registry.register('glob', fnmatch)
def register_pcre(): def register_pcre() -> None:
"""Register pcre into default registry.""" """Register pcre into default registry."""
registry.register('pcre', rematch) registry.register('pcre', cast(MatcherFunction, rematch))
# Register the base matching methods. # Register the base matching methods.

View File

@ -41,6 +41,7 @@ files =
kombu/transport/__init__.py, kombu/transport/__init__.py,
kombu/transport/virtual/__init__.py, kombu/transport/virtual/__init__.py,
kombu/utils/__init__.py, kombu/utils/__init__.py,
kombu/matcher.py,
kombu/asynchronous/semaphore.py kombu/asynchronous/semaphore.py