1from __future__ import annotations
2
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 the
9 common attributes or expose other information. Set
10 :attr:`werkzeug.wrappers.Request.user_agent_class` to use a
11 subclass.
12
13 :param string: The header value to parse.
14
15 .. versionadded:: 2.0
16 This replaces the previous ``useragents`` module, but does not
17 provide a built-in parser.
18 """
19
20 platform: str | None = None
21 """The OS name, if it could be parsed from the string."""
22
23 browser: str | None = None
24 """The browser name, if it could be parsed from the string."""
25
26 version: str | None = None
27 """The browser version, if it could be parsed from the string."""
28
29 language: str | None = None
30 """The browser language, if it could be parsed from the string."""
31
32 def __init__(self, string: str) -> None:
33 self.string: str = string
34 """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.string
41
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.string