Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

# 

# LSST Data Management System 

# Copyright 2008, 2009, 2010 LSST Corporation. 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <http://www.lsstcorp.org/LegalNotices/>. 

# 

from __future__ import print_function 

from builtins import str 

from builtins import object 

 

import os 

import re 

import sys 

 

# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 

 

 

class Color(object): 

"""Control whether strings should be coloured 

 

The usual usage is `Color(string, category)` which returns a string that 

may be printed; categories are given by the keys of Color.categories 

 

Color.colorize() may be used to set or retrieve whether the user wants 

colour; it always returns False when sys.stdout is not attached to a 

terminal. 

""" 

 

categories = dict( 

NAME="blue", 

VALUE="yellow", 

FILE="green", 

TEXT="red", 

FUNCTION_NAME="blue", 

) 

 

colors = { 

"black": 0, 

"red": 1, 

"green": 2, 

"yellow": 3, 

"blue": 4, 

"magenta": 5, 

"cyan": 6, 

"white": 7, 

} 

 

_colorize = True 

 

def __init__(self, text, category): 

"""Return a string that should display as coloured on a conformant terminal""" 

try: 

color = Color.categories[category] 

except KeyError: 

raise RuntimeError("Unknown category: %s" % category) 

 

self.rawText = str(text) 

x = color.lower().split(";") 

self.color, bold = x.pop(0), False 

75 ↛ 76line 75 didn't jump to line 76, because the condition on line 75 was never true if x: 

props = x.pop(0) 

if props in ("bold",): 

bold = True 

 

try: 

self._code = "%s" % (30 + Color.colors[self.color]) 

except KeyError: 

raise RuntimeError("Unknown colour: %s" % self.color) 

 

85 ↛ 86line 85 didn't jump to line 86, because the condition on line 85 was never true if bold: 

self._code += ";1" 

 

@staticmethod 

def colorize(val=None): 

"""Should I colour strings? With an argument, set the value 

 

The value is usually a bool, but it may be a dict which is used 

to modify Color.categories 

 

N.b. only strings written to a terminal are colourized 

""" 

 

if val is not None: 

Color._colorize = val 

 

101 ↛ 102line 101 didn't jump to line 102, because the condition on line 101 was never true if isinstance(val, dict): 

unknown = [] 

for k in val: 

if k in Color.categories: 

if val[k] in Color.colors: 

Color.categories[k] = val[k] 

else: 

print("Unknown colour %s for category %s" % (val[k], k), file=sys.stderr) 

else: 

unknown.append(k) 

 

if unknown: 

print("Unknown colourizing category: %s" % " ".join(unknown), file=sys.stderr) 

 

return Color._colorize if sys.stdout.isatty() else False 

 

def __str__(self): 

118 ↛ 121line 118 didn't jump to line 121, because the condition on line 118 was never false if not self.colorize(): 

return self.rawText 

 

base = "\033[" 

 

prefix = base + self._code + "m" 

suffix = base + "m" 

 

return prefix + self.rawText + suffix 

 

 

def _colorize(text, category): 

text = Color(text, category) 

return str(text) 

 

 

def format(config, name=None, writeSourceLine=True, prefix="", verbose=False): 

"""Format the history record for config.name""" 

 

137 ↛ 138line 137 didn't jump to line 138, because the condition on line 137 was never true if name is None: 

for i, name in enumerate(config.history.keys()): 

if i > 0: 

print() 

print(format(config, name)) 

 

outputs = [] 

for value, stack, label in config.history[name]: 

output = [] 

for frame in stack: 

147 ↛ 149line 147 didn't jump to line 149, because the condition on line 147 was never true if frame.function in ("__new__", "__set__", "__setattr__", "execfile", "wrapper") or \ 

os.path.split(frame.filename)[1] in ("argparse.py", "argumentParser.py"): 

if not verbose: 

continue 

 

line = [] 

153 ↛ 154line 153 didn't jump to line 154, because the condition on line 153 was never true if writeSourceLine: 

line.append(["%s" % ("%s:%d" % (frame.filename, frame.lineno)), "FILE", ]) 

 

line.append([frame.content, "TEXT", ]) 

if False: 

line.append([frame.function, "FUNCTION_NAME", ]) 

 

output.append(line) 

 

outputs.append([value, output]) 

# 

# Find the maximum widths of the value and file:lineNo fields 

# 

166 ↛ 167line 166 didn't jump to line 167, because the condition on line 166 was never true if writeSourceLine: 

sourceLengths = [] 

for value, output in outputs: 

sourceLengths.append(max([len(x[0][0]) for x in output])) 

sourceLength = max(sourceLengths) 

 

valueLength = len(prefix) + max([len(str(value)) for value, output in outputs]) 

# 

# actually generate the config history 

# 

msg = [] 

fullname = "%s.%s" % (config._name, name) if config._name is not None else name 

msg.append(_colorize(re.sub(r"^root\.", "", fullname), "NAME")) 

for value, output in outputs: 

line = prefix + _colorize("%-*s" % (valueLength, value), "VALUE") + " " 

for i, vt in enumerate(output): 

182 ↛ 183line 182 didn't jump to line 183, because the condition on line 182 was never true if writeSourceLine: 

vt[0][0] = "%-*s" % (sourceLength, vt[0][0]) 

 

output[i] = " ".join([_colorize(v, t) for v, t in vt]) 

 

line += ("\n%*s" % (valueLength + 1, "")).join(output) 

msg.append(line) 

 

return "\n".join(msg)