Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/core/management/color.py: 66%

45 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1""" 

2Sets up the terminal color scheme. 

3""" 

4 

5import functools 

6import os 

7import sys 

8 

9from django.utils import termcolors 

10 

11try: 

12 import colorama 

13 

14 colorama.init() 

15except (ImportError, OSError): 

16 HAS_COLORAMA = False 

17else: 

18 HAS_COLORAMA = True 

19 

20 

21def supports_color(): 

22 """ 

23 Return True if the running system's terminal supports color, 

24 and False otherwise. 

25 """ 

26 

27 def vt_codes_enabled_in_windows_registry(): 

28 """ 

29 Check the Windows Registry to see if VT code handling has been enabled 

30 by default, see https://superuser.com/a/1300251/447564. 

31 """ 

32 try: 

33 # winreg is only available on Windows. 

34 import winreg 

35 except ImportError: 

36 return False 

37 else: 

38 reg_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Console") 

39 try: 

40 reg_key_value, _ = winreg.QueryValueEx(reg_key, "VirtualTerminalLevel") 

41 except FileNotFoundError: 

42 return False 

43 else: 

44 return reg_key_value == 1 

45 

46 # isatty is not always implemented, #6223. 

47 is_a_tty = hasattr(sys.stdout, "isatty") and sys.stdout.isatty() 

48 

49 return is_a_tty and ( 

50 sys.platform != "win32" 

51 or HAS_COLORAMA 

52 or "ANSICON" in os.environ 

53 or 

54 # Windows Terminal supports VT codes. 

55 "WT_SESSION" in os.environ 

56 or 

57 # Microsoft Visual Studio Code's built-in terminal supports colors. 

58 os.environ.get("TERM_PROGRAM") == "vscode" 

59 or vt_codes_enabled_in_windows_registry() 

60 ) 

61 

62 

63class Style: 

64 pass 

65 

66 

67def make_style(config_string=""): 

68 """ 

69 Create a Style object from the given config_string. 

70 

71 If config_string is empty django.utils.termcolors.DEFAULT_PALETTE is used. 

72 """ 

73 

74 style = Style() 

75 

76 color_settings = termcolors.parse_color_setting(config_string) 

77 

78 # The nocolor palette has all available roles. 

79 # Use that palette as the basis for populating 

80 # the palette as defined in the environment. 

81 for role in termcolors.PALETTES[termcolors.NOCOLOR_PALETTE]: 

82 if color_settings: 82 ↛ 87line 82 didn't jump to line 87, because the condition on line 82 was never false

83 format = color_settings.get(role, {}) 

84 style_func = termcolors.make_style(**format) 

85 else: 

86 

87 def style_func(x): 

88 return x 

89 

90 setattr(style, role, style_func) 

91 

92 # For backwards compatibility, 

93 # set style for ERROR_OUTPUT == ERROR 

94 style.ERROR_OUTPUT = style.ERROR 

95 

96 return style 

97 

98 

99@functools.lru_cache(maxsize=None) 

100def no_style(): 

101 """ 

102 Return a Style object with no color scheme. 

103 """ 

104 return make_style("nocolor") 

105 

106 

107def color_style(force_color=False): 

108 """ 

109 Return a Style object from the Django color scheme. 

110 """ 

111 if not force_color and not supports_color(): 111 ↛ 112line 111 didn't jump to line 112, because the condition on line 111 was never true

112 return no_style() 

113 return make_style(os.environ.get("DJANGO_COLORS", ""))