22 __all__ = (
'Color',
'format')
30 """A controller that determines whether strings should be colored. 35 Text content to print to a terminal. 37 Semantic category of the ``text``. See `categories` for possible values. 42 Raised when the ``category`` is not a key of ``Color.categories``. 46 The usual usage is ``Color(string, category)`` which returns a string that 47 may be printed; categories are given by the keys of `Color.categories`. 49 `Color.colorize` may be used to set or retrieve whether the user wants 50 color. It always returns `False` when `sys.stdout` is not attached to a 61 """Mapping of semantic labels to color names (`dict`). 65 The default categories are: 84 """Mapping of color names to terminal color codes (`dict`). 91 color = Color.categories[category]
93 raise RuntimeError(
"Unknown category: %s" % category)
96 x = color.lower().split(
";")
97 self.color, bold = x.pop(0),
False 100 if props
in (
"bold",):
104 self.
_code =
"%s" % (30 + Color.colors[self.color])
106 raise RuntimeError(
"Unknown colour: %s" % self.color)
113 """Get or set whether the string should be colorized. 117 val : `bool` or `dict`, optional 118 The value is usually a bool, but it may be a dict which is used 119 to modify Color.categories 123 shouldColorize : `bool` 124 If `True`, the string should be colorized. A string **will not** be 125 colorized if standard output or standard error are not attached to 126 a terminal or if the ``val`` argument was `False`. 128 Only strings written to a terminal are colorized. 132 Color._colorize = val
134 if isinstance(val, dict):
137 if k
in Color.categories:
138 if val[k]
in Color.colors:
139 Color.categories[k] = val[k]
141 print(
"Unknown colour %s for category %s" % (val[k], k), file=sys.stderr)
146 print(
"Unknown colourizing category: %s" %
" ".join(unknown), file=sys.stderr)
148 return Color._colorize
if sys.stdout.isatty()
else False 156 prefix = base + self.
_code +
"m" 159 return prefix + self.
rawText + suffix
162 def _colorize(text, category):
163 text =
Color(text, category)
167 def format(config, name=None, writeSourceLine=True, prefix="", verbose=False):
168 """Format the history record for a configuration, or a specific 173 config : `lsst.pex.config.Config` 174 A configuration instance. 175 name : `str`, optional 176 The name of a configuration field to specifically format the history 177 for. Otherwise the history of all configuration fields is printed. 178 writeSourceLine : `bool`, optional 179 If `True`, prefix each printout line with the code filename and line 180 number where the configuration event occurred. Default is `True`. 181 prefix : `str`, optional 182 A prefix for to add to each printout line. This prefix occurs first, 183 even before any source line. The default is an empty string. 184 verbose : `bool`, optional 189 for i, name
in enumerate(config.history.keys()):
192 print(
format(config, name))
195 for value, stack, label
in config.history[name]:
198 if frame.function
in (
"__new__",
"__set__",
"__setattr__",
"execfile",
"wrapper")
or \
199 os.path.split(frame.filename)[1]
in (
"argparse.py",
"argumentParser.py"):
205 line.append([
"%s" % (
"%s:%d" % (frame.filename, frame.lineno)),
"FILE", ])
207 line.append([frame.content,
"TEXT", ])
209 line.append([frame.function,
"FUNCTION_NAME", ])
213 outputs.append([value, output])
218 for value, output
in outputs:
219 sourceLengths.append(max([len(x[0][0])
for x
in output]))
220 sourceLength = max(sourceLengths)
222 valueLength = len(prefix) + max([len(str(value))
for value, output
in outputs])
226 fullname =
"%s.%s" % (config._name, name)
if config._name
is not None else name
227 msg.append(_colorize(re.sub(
r"^root\.",
"", fullname),
"NAME"))
228 for value, output
in outputs:
229 line = prefix + _colorize(
"%-*s" % (valueLength, value),
"VALUE") +
" " 230 for i, vt
in enumerate(output):
232 vt[0][0] =
"%-*s" % (sourceLength, vt[0][0])
234 output[i] =
" ".join([_colorize(v, t)
for v, t
in vt])
236 line += (
"\n%*s" % (valueLength + 1,
"")).join(output)
239 return "\n".join(msg)
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
def __init__(self, text, category)