23 __all__ = (
'Color',
'format')
31 """A controller that determines whether strings should be colored. 36 Text content to print to a terminal. 38 Semantic category of the ``text``. See `categories` for possible values. 43 Raised when the ``category`` is not a key of ``Color.categories``. 47 The usual usage is ``Color(string, category)`` which returns a string that 48 may be printed; categories are given by the keys of `Color.categories`. 50 `Color.colorize` may be used to set or retrieve whether the user wants 51 color. It always returns `False` when `sys.stdout` is not attached to a 62 """Mapping of semantic labels to color names (`dict`). 66 The default categories are: 85 """Mapping of color names to terminal color codes (`dict`). 92 color = Color.categories[category]
94 raise RuntimeError(
"Unknown category: %s" % category)
97 x = color.lower().split(
";")
98 self.color, bold = x.pop(0),
False 101 if props
in (
"bold",):
105 self.
_code =
"%s" % (30 + Color.colors[self.color])
107 raise RuntimeError(
"Unknown colour: %s" % self.color)
114 """Get or set whether the string should be colorized. 118 val : `bool` or `dict`, optional 119 The value is usually a bool, but it may be a dict which is used 120 to modify Color.categories 124 shouldColorize : `bool` 125 If `True`, the string should be colorized. A string **will not** be 126 colorized if standard output or standard error are not attached to 127 a terminal or if the ``val`` argument was `False`. 129 Only strings written to a terminal are colorized. 133 Color._colorize = val
135 if isinstance(val, dict):
138 if k
in Color.categories:
139 if val[k]
in Color.colors:
140 Color.categories[k] = val[k]
142 print(
"Unknown colour %s for category %s" % (val[k], k), file=sys.stderr)
147 print(
"Unknown colourizing category: %s" %
" ".join(unknown), file=sys.stderr)
149 return Color._colorize
if sys.stdout.isatty()
else False 157 prefix = base + self.
_code +
"m" 160 return prefix + self.
rawText + suffix
163 def _colorize(text, category):
164 text =
Color(text, category)
168 def format(config, name=None, writeSourceLine=True, prefix="", verbose=False):
169 """Format the history record for a configuration, or a specific 174 config : `lsst.pex.config.Config` 175 A configuration instance. 176 name : `str`, optional 177 The name of a configuration field to specifically format the history 178 for. Otherwise the history of all configuration fields is printed. 179 writeSourceLine : `bool`, optional 180 If `True`, prefix each printout line with the code filename and line 181 number where the configuration event occurred. Default is `True`. 182 prefix : `str`, optional 183 A prefix for to add to each printout line. This prefix occurs first, 184 even before any source line. The default is an empty string. 185 verbose : `bool`, optional 190 for i, name
in enumerate(config.history.keys()):
193 print(
format(config, name))
196 for value, stack, label
in config.history[name]:
199 if frame.function
in (
"__new__",
"__set__",
"__setattr__",
"execfile",
"wrapper")
or \
200 os.path.split(frame.filename)[1]
in (
"argparse.py",
"argumentParser.py"):
206 line.append([
"%s" % (
"%s:%d" % (frame.filename, frame.lineno)),
"FILE", ])
208 line.append([frame.content,
"TEXT", ])
210 line.append([frame.function,
"FUNCTION_NAME", ])
214 outputs.append([value, output])
219 for value, output
in outputs:
220 sourceLengths.append(
max([len(x[0][0])
for x
in output]))
221 sourceLength =
max(sourceLengths)
223 valueLength = len(prefix) +
max([len(
str(value))
for value, output
in outputs])
227 fullname =
"%s.%s" % (config._name, name)
if config._name
is not None else name
228 msg.append(_colorize(re.sub(
r"^root\.",
"", fullname),
"NAME"))
229 for value, output
in outputs:
230 line = prefix + _colorize(
"%-*s" % (valueLength, value),
"VALUE") +
" " 231 for i, vt
in enumerate(output):
233 vt[0][0] =
"%-*s" % (sourceLength, vt[0][0])
235 output[i] =
" ".join([_colorize(v, t)
for v, t
in vt])
237 line += (
"\n%*s" % (valueLength + 1,
"")).join(output)
240 return "\n".join(msg)
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
def __init__(self, text, category)