Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/utils/termcolors.py: 23%

56 statements  

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

1""" 

2termcolors.py 

3""" 

4 

5color_names = ("black", "red", "green", "yellow", "blue", "magenta", "cyan", "white") 

6foreground = {color_names[x]: "3%s" % x for x in range(8)} 

7background = {color_names[x]: "4%s" % x for x in range(8)} 

8 

9RESET = "0" 

10opt_dict = { 

11 "bold": "1", 

12 "underscore": "4", 

13 "blink": "5", 

14 "reverse": "7", 

15 "conceal": "8", 

16} 

17 

18 

19def colorize(text="", opts=(), **kwargs): 

20 """ 

21 Return your text, enclosed in ANSI graphics codes. 

22 

23 Depends on the keyword arguments 'fg' and 'bg', and the contents of 

24 the opts tuple/list. 

25 

26 Return the RESET code if no parameters are given. 

27 

28 Valid colors: 

29 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white' 

30 

31 Valid options: 

32 'bold' 

33 'underscore' 

34 'blink' 

35 'reverse' 

36 'conceal' 

37 'noreset' - string will not be auto-terminated with the RESET code 

38 

39 Examples: 

40 colorize('hello', fg='red', bg='blue', opts=('blink',)) 

41 colorize() 

42 colorize('goodbye', opts=('underscore',)) 

43 print(colorize('first line', fg='red', opts=('noreset',))) 

44 print('this should be red too') 

45 print(colorize('and so should this')) 

46 print('this should not be red') 

47 """ 

48 code_list = [] 

49 if text == "" and len(opts) == 1 and opts[0] == "reset": 

50 return "\x1b[%sm" % RESET 

51 for k, v in kwargs.items(): 

52 if k == "fg": 

53 code_list.append(foreground[v]) 

54 elif k == "bg": 

55 code_list.append(background[v]) 

56 for o in opts: 

57 if o in opt_dict: 

58 code_list.append(opt_dict[o]) 

59 if "noreset" not in opts: 

60 text = "%s\x1b[%sm" % (text or "", RESET) 

61 return "%s%s" % (("\x1b[%sm" % ";".join(code_list)), text or "") 

62 

63 

64def make_style(opts=(), **kwargs): 

65 """ 

66 Return a function with default parameters for colorize() 

67 

68 Example: 

69 bold_red = make_style(opts=('bold',), fg='red') 

70 print(bold_red('hello')) 

71 KEYWORD = make_style(fg='yellow') 

72 COMMENT = make_style(fg='blue', opts=('bold',)) 

73 """ 

74 return lambda text: colorize(text, opts, **kwargs) 74 ↛ exitline 74 didn't run the lambda on line 74

75 

76 

77NOCOLOR_PALETTE = "nocolor" 

78DARK_PALETTE = "dark" 

79LIGHT_PALETTE = "light" 

80 

81PALETTES = { 

82 NOCOLOR_PALETTE: { 

83 "ERROR": {}, 

84 "SUCCESS": {}, 

85 "WARNING": {}, 

86 "NOTICE": {}, 

87 "SQL_FIELD": {}, 

88 "SQL_COLTYPE": {}, 

89 "SQL_KEYWORD": {}, 

90 "SQL_TABLE": {}, 

91 "HTTP_INFO": {}, 

92 "HTTP_SUCCESS": {}, 

93 "HTTP_REDIRECT": {}, 

94 "HTTP_NOT_MODIFIED": {}, 

95 "HTTP_BAD_REQUEST": {}, 

96 "HTTP_NOT_FOUND": {}, 

97 "HTTP_SERVER_ERROR": {}, 

98 "MIGRATE_HEADING": {}, 

99 "MIGRATE_LABEL": {}, 

100 }, 

101 DARK_PALETTE: { 

102 "ERROR": {"fg": "red", "opts": ("bold",)}, 

103 "SUCCESS": {"fg": "green", "opts": ("bold",)}, 

104 "WARNING": {"fg": "yellow", "opts": ("bold",)}, 

105 "NOTICE": {"fg": "red"}, 

106 "SQL_FIELD": {"fg": "green", "opts": ("bold",)}, 

107 "SQL_COLTYPE": {"fg": "green"}, 

108 "SQL_KEYWORD": {"fg": "yellow"}, 

109 "SQL_TABLE": {"opts": ("bold",)}, 

110 "HTTP_INFO": {"opts": ("bold",)}, 

111 "HTTP_SUCCESS": {}, 

112 "HTTP_REDIRECT": {"fg": "green"}, 

113 "HTTP_NOT_MODIFIED": {"fg": "cyan"}, 

114 "HTTP_BAD_REQUEST": {"fg": "red", "opts": ("bold",)}, 

115 "HTTP_NOT_FOUND": {"fg": "yellow"}, 

116 "HTTP_SERVER_ERROR": {"fg": "magenta", "opts": ("bold",)}, 

117 "MIGRATE_HEADING": {"fg": "cyan", "opts": ("bold",)}, 

118 "MIGRATE_LABEL": {"opts": ("bold",)}, 

119 }, 

120 LIGHT_PALETTE: { 

121 "ERROR": {"fg": "red", "opts": ("bold",)}, 

122 "SUCCESS": {"fg": "green", "opts": ("bold",)}, 

123 "WARNING": {"fg": "yellow", "opts": ("bold",)}, 

124 "NOTICE": {"fg": "red"}, 

125 "SQL_FIELD": {"fg": "green", "opts": ("bold",)}, 

126 "SQL_COLTYPE": {"fg": "green"}, 

127 "SQL_KEYWORD": {"fg": "blue"}, 

128 "SQL_TABLE": {"opts": ("bold",)}, 

129 "HTTP_INFO": {"opts": ("bold",)}, 

130 "HTTP_SUCCESS": {}, 

131 "HTTP_REDIRECT": {"fg": "green", "opts": ("bold",)}, 

132 "HTTP_NOT_MODIFIED": {"fg": "green"}, 

133 "HTTP_BAD_REQUEST": {"fg": "red", "opts": ("bold",)}, 

134 "HTTP_NOT_FOUND": {"fg": "red"}, 

135 "HTTP_SERVER_ERROR": {"fg": "magenta", "opts": ("bold",)}, 

136 "MIGRATE_HEADING": {"fg": "cyan", "opts": ("bold",)}, 

137 "MIGRATE_LABEL": {"opts": ("bold",)}, 

138 }, 

139} 

140DEFAULT_PALETTE = DARK_PALETTE 

141 

142 

143def parse_color_setting(config_string): 

144 """Parse a DJANGO_COLORS environment variable to produce the system palette 

145 

146 The general form of a palette definition is: 

147 

148 "palette;role=fg;role=fg/bg;role=fg,option,option;role=fg/bg,option,option" 

149 

150 where: 

151 palette is a named palette; one of 'light', 'dark', or 'nocolor'. 

152 role is a named style used by Django 

153 fg is a foreground color. 

154 bg is a background color. 

155 option is a display options. 

156 

157 Specifying a named palette is the same as manually specifying the individual 

158 definitions for each role. Any individual definitions following the palette 

159 definition will augment the base palette definition. 

160 

161 Valid roles: 

162 'error', 'success', 'warning', 'notice', 'sql_field', 'sql_coltype', 

163 'sql_keyword', 'sql_table', 'http_info', 'http_success', 

164 'http_redirect', 'http_not_modified', 'http_bad_request', 

165 'http_not_found', 'http_server_error', 'migrate_heading', 

166 'migrate_label' 

167 

168 Valid colors: 

169 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white' 

170 

171 Valid options: 

172 'bold', 'underscore', 'blink', 'reverse', 'conceal', 'noreset' 

173 """ 

174 if not config_string: 174 ↛ 178line 174 didn't jump to line 178, because the condition on line 174 was never false

175 return PALETTES[DEFAULT_PALETTE] 

176 

177 # Split the color configuration into parts 

178 parts = config_string.lower().split(";") 

179 palette = PALETTES[NOCOLOR_PALETTE].copy() 

180 for part in parts: 

181 if part in PALETTES: 

182 # A default palette has been specified 

183 palette.update(PALETTES[part]) 

184 elif "=" in part: 

185 # Process a palette defining string 

186 definition = {} 

187 

188 # Break the definition into the role, 

189 # plus the list of specific instructions. 

190 # The role must be in upper case 

191 role, instructions = part.split("=") 

192 role = role.upper() 

193 

194 styles = instructions.split(",") 

195 styles.reverse() 

196 

197 # The first instruction can contain a slash 

198 # to break apart fg/bg. 

199 colors = styles.pop().split("/") 

200 colors.reverse() 

201 fg = colors.pop() 

202 if fg in color_names: 

203 definition["fg"] = fg 

204 if colors and colors[-1] in color_names: 

205 definition["bg"] = colors[-1] 

206 

207 # All remaining instructions are options 

208 opts = tuple(s for s in styles if s in opt_dict) 

209 if opts: 

210 definition["opts"] = opts 

211 

212 # The nocolor palette has all available roles. 

213 # Use that palette as the basis for determining 

214 # if the role is valid. 

215 if role in PALETTES[NOCOLOR_PALETTE] and definition: 

216 palette[role] = definition 

217 

218 # If there are no colors specified, return the empty palette. 

219 if palette == PALETTES[NOCOLOR_PALETTE]: 

220 return None 

221 return palette