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

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

# 

# 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/>. 

# 

 

__all__ = ('Color', 'format') 

 

import os 

import re 

import sys 

 

 

class Color: 

"""A controller that determines whether strings should be colored. 

 

Parameters 

---------- 

text : `str` 

Text content to print to a terminal. 

category : `str` 

Semantic category of the ``text``. See `categories` for possible values. 

 

Raises 

------ 

RuntimeError 

Raised when the ``category`` is not a key of ``Color.categories``. 

 

Notes 

----- 

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 

color. 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", 

) 

"""Mapping of semantic labels to color names (`dict`). 

 

Notes 

----- 

The default categories are: 

 

- ``'NAME'`` 

- ``'VALUE'`` 

- ``'FILE'`` 

- ``'TEXT'`` 

- ``'FUNCTION_NAME'`` 

""" 

 

colors = { 

"black": 0, 

"red": 1, 

"green": 2, 

"yellow": 3, 

"blue": 4, 

"magenta": 5, 

"cyan": 6, 

"white": 7, 

} 

"""Mapping of color names to terminal color codes (`dict`). 

""" 

 

_colorize = True 

 

def __init__(self, text, category): 

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 

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) 

 

if bold: 

self._code += ";1" 

 

@staticmethod 

def colorize(val=None): 

"""Get or set whether the string should be colorized. 

 

Parameters 

---------- 

val : `bool` or `dict`, optional 

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

to modify Color.categories 

 

Returns 

------- 

shouldColorize : `bool` 

If `True`, the string should be colorized. A string **will not** be 

colorized if standard output or standard error are not attached to 

a terminal or if the ``val`` argument was `False`. 

 

Only strings written to a terminal are colorized. 

""" 

 

if val is not None: 

Color._colorize = val 

 

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): 

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 a configuration, or a specific 

configuration field. 

 

Parameters 

---------- 

config : `lsst.pex.config.Config` 

A configuration instance. 

name : `str`, optional 

The name of a configuration field to specifically format the history 

for. Otherwise the history of all configuration fields is printed. 

writeSourceLine : `bool`, optional 

If `True`, prefix each printout line with the code filename and line 

number where the configuration event occurred. Default is `True`. 

prefix : `str`, optional 

A prefix for to add to each printout line. This prefix occurs first, 

even before any source line. The default is an empty string. 

verbose : `bool`, optional 

Default is `False`. 

""" 

 

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: 

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 = [] 

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. 

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]) 

 

# Generate the config history content. 

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): 

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)