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 Fixes ua-parser#166
- Loading branch information
Showing
9 changed files
with
169 additions
and
67 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, | ||
) |
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
Oops, something went wrong.