From 4e07493716642ff84edc6d4b6d489a9c9b9166c9 Mon Sep 17 00:00:00 2001 From: masklinn Date: Tue, 29 Oct 2024 17:15:17 +0100 Subject: [PATCH] Add the regex resolver at the top of the queue Not sure why I didn't do that when I merged it, but I think it's the best default, if available, which is unlikely for the reason that it requires a completely bespoke dep. --- src/ua_parser/__init__.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/ua_parser/__init__.py b/src/ua_parser/__init__.py index 85ac75a..f0340c6 100644 --- a/src/ua_parser/__init__.py +++ b/src/ua_parser/__init__.py @@ -59,9 +59,23 @@ from .loaders import load_builtins, load_lazy_builtins from .utils import IS_GRAAL -Re2Resolver: Optional[Callable[[Matchers], Resolver]] = None +_ResolverCtor = Callable[[Matchers], Resolver] +Re2Resolver: Optional[_ResolverCtor] = None if importlib.util.find_spec("re2"): from .re2 import Resolver as Re2Resolver +RegexResolver: Optional[_ResolverCtor] = None +if importlib.util.find_spec("ua_parser_rs"): + from .regex import Resolver as RegexResolver +BestAvailableResolver: _ResolverCtor = next( + filter( + None, + ( + RegexResolver, + Re2Resolver, + lambda m: CachingResolver(BasicResolver(m), Cache(200)), + ), + ) +) VERSION = (1, 0, 0) @@ -82,15 +96,7 @@ def from_matchers(cls, m: Matchers, /) -> Parser: stack. """ - if Re2Resolver is not None: - return cls(Re2Resolver(m)) - else: - return cls( - CachingResolver( - BasicResolver(m), - Cache(200), - ) - ) + return cls(BestAvailableResolver(m)) def __init__(self, resolver: Resolver) -> None: self.resolver = resolver @@ -133,7 +139,7 @@ def parse_device(self: Resolver, ua: str) -> Optional[Device]: def __getattr__(name: str) -> Parser: global parser if name == "parser": - if Re2Resolver or IS_GRAAL: + if RegexResolver or Re2Resolver or IS_GRAAL: matchers = load_lazy_builtins() else: matchers = load_builtins()