codekingpro/portable-devtools
114k
1from __future__ import annotations2 3 4class UserAgent:5 """Represents a parsed user agent header value.6 7 The default implementation does no parsing, only the :attr:`string`8 attribute is set. A subclass may parse the string to set the9 common attributes or expose other information. Set10 :attr:`werkzeug.wrappers.Request.user_agent_class` to use a11 subclass.12 13 :param string: The header value to parse.14 15 .. versionadded:: 2.016 This replaces the previous ``useragents`` module, but does not17 provide a built-in parser.18 """19 20 platform: str | None = None21 """The OS name, if it could be parsed from the string."""22 23 browser: str | None = None24 """The browser name, if it could be parsed from the string."""25 26 version: str | None = None27 """The browser version, if it could be parsed from the string."""28 29 language: str | None = None30 """The browser language, if it could be parsed from the string."""31 32 def __init__(self, string: str) -> None:33 self.string: str = string34 """The original header value."""35 36 def __repr__(self) -> str:37 return f"<{type(self).__name__} {self.browser}/{self.version}>"38 39 def __str__(self) -> str:40 return self.string41 42 def __bool__(self) -> bool:43 return bool(self.browser)44 45 def to_header(self) -> str:46 """Convert to a header value."""47 return self.string48 