Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/werkzeug/user_agent.py: 67%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

30 statements  

1from __future__ import annotations 

2 

3import typing as t 

4from typing import Any 

5 

6 

7class _UserAgent(str): 

8 """Represents a parsed user agent header value. 

9 

10 The default implementation does no parsing, only the :attr:`string` 

11 attribute is set. A subclass may parse the string to set the 

12 common attributes or expose other information. Set 

13 :attr:`werkzeug.wrappers.Request.user_agent_class` to use a 

14 subclass. 

15 

16 :param string: The header value to parse. 

17 

18 .. deprecated:: 3.2 

19 Will be removed in Werkzeug 4.0. ``Request.user_agent`` is a string and 

20 can be parsed directly if needed. 

21 

22 .. versionadded:: 2.0 

23 This replaces the previous ``useragents`` module, but does not 

24 provide a built-in parser. 

25 """ 

26 

27 platform: str | None = None 

28 """The OS name, if it could be parsed from the string.""" 

29 

30 browser: str | None = None 

31 """The browser name, if it could be parsed from the string.""" 

32 

33 version: str | None = None 

34 """The browser version, if it could be parsed from the string.""" 

35 

36 language: str | None = None 

37 """The browser language, if it could be parsed from the string.""" 

38 

39 @property 

40 def string(self) -> str: 

41 """The original header value.""" 

42 return str(self) 

43 

44 def to_header(self) -> str: 

45 """Convert to a header value.""" 

46 return str(self) 

47 

48 def __getattribute__(self, name: str, /) -> Any: 

49 if hasattr(str, name): 

50 return super().__getattribute__(name) 

51 

52 import warnings 

53 

54 warnings.warn( 

55 "The 'UserAgent' class is deprecated and will be removed in" 

56 " Werkzeug 4.0. 'Request.user_agent' is a string and can be" 

57 " parsed directly if needed.", 

58 DeprecationWarning, 

59 stacklevel=2, 

60 ) 

61 return object.__getattribute__(self, name) 

62 

63 

64if not t.TYPE_CHECKING: 

65 

66 def __getattr__(name: str) -> t.Any: 

67 if name == "UserAgent": 

68 import warnings 

69 

70 warnings.warn( 

71 "The 'UserAgent' class is deprecated and will be removed in" 

72 " Werkzeug 4.0. 'Request.user_agent' is a string and can be" 

73 " parsed directly if needed.", 

74 DeprecationWarning, 

75 stacklevel=2, 

76 ) 

77 return _UserAgent 

78 

79 raise AttributeError(name)