forked from ua-parser/uap-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Uses ua-parser/uap-rust#3 - add an optional dependency on `ua-parser-rs` under the `regex` key - add a regex-based parser misc: - update the classifiers - bump required-python to 3.9 (3.8 is basically EOL) - update CI to better split up the steps - fix up the check for binary pyyaml: requirements_dev was removed in 81da21a, in May 2023, so this hasn't been working for 18 months - fix CLI script to correctly handle optional modules so it can run on pypy and graal, add regex, make tracemalloc optional as pypy doesn't support it (didn't check graal) - update tox: remove cpython 3.8, pypy 3.8 and 3.9 from tox (3.8's last supporting release was 7.3.11 in May 2023, 3.9's was 7.3.16 in April 2024), add graal Fixes ua-parser#166
- Loading branch information
Showing
9 changed files
with
196 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
__all__ = ["Resolver"] | ||
|
||
from operator import attrgetter | ||
|
||
import ua_parser_rs # type: ignore | ||
|
||
from .core import ( | ||
Device, | ||
Domain, | ||
Matchers, | ||
OS, | ||
PartialResult, | ||
UserAgent, | ||
) | ||
|
||
|
||
class Resolver: | ||
ua: ua_parser_rs.UserAgentExtractor | ||
os: ua_parser_rs.OSExtractor | ||
de: ua_parser_rs.DeviceExtractor | ||
|
||
def __init__(self, matchers: Matchers) -> None: | ||
ua, os, de = matchers | ||
self.ua = ua_parser_rs.UserAgentExtractor( | ||
map( | ||
attrgetter("regex", "family", "major", "minor", "patch", "patch_minor"), | ||
ua, | ||
) | ||
) | ||
self.os = ua_parser_rs.OSExtractor( | ||
map( | ||
attrgetter("regex", "family", "major", "minor", "patch", "patch_minor"), | ||
os, | ||
) | ||
) | ||
self.de = ua_parser_rs.DeviceExtractor( | ||
map( | ||
attrgetter("regex", "regex_flag", "family", "brand", "model"), | ||
de, | ||
) | ||
) | ||
|
||
def __call__(self, ua: str, domains: Domain, /) -> PartialResult: | ||
user_agent = os = device = None | ||
if Domain.USER_AGENT in domains: | ||
if m := self.ua.extract(ua): | ||
user_agent = UserAgent( | ||
m.family, | ||
m.major, | ||
m.minor, | ||
m.patch, | ||
m.patch_minor, | ||
) | ||
if Domain.OS in domains: | ||
if m := self.os.extract(ua): | ||
os = OS( | ||
m.family, | ||
m.major, | ||
m.minor, | ||
m.patch, | ||
m.patch_minor, | ||
) | ||
if Domain.DEVICE in domains: | ||
if m := self.de.extract(ua): | ||
device = Device( | ||
m.family, | ||
m.brand, | ||
m.model, | ||
) | ||
return PartialResult( | ||
domains=domains, | ||
string=ua, | ||
user_agent=user_agent, | ||
os=os, | ||
device=device, | ||
) |
Oops, something went wrong.