Skip to content

Commit

Permalink
Add the regex resolver at the top of the queue
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
masklinn committed Oct 29, 2024
1 parent de1c9c8 commit 4e07493
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/ua_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 4e07493

Please sign in to comment.