29 """Control whether strings should be coloured 31 The usual usage is `Color(string, category)` which returns a string that 32 may be printed; categories are given by the keys of Color.categories 34 Color.colorize() may be used to set or retrieve whether the user wants 35 colour; it always returns False when sys.stdout is not attached to a 61 """Return a string that should display as coloured on a conformant terminal""" 63 color = Color.categories[category]
65 raise RuntimeError(
"Unknown category: %s" % category)
68 x = color.lower().split(
";")
69 self.color, bold = x.pop(0),
False 72 if props
in (
"bold",):
76 self.
_code =
"%s" % (30 + Color.colors[self.color])
78 raise RuntimeError(
"Unknown colour: %s" % self.color)
85 """Should I colour strings? With an argument, set the value 87 The value is usually a bool, but it may be a dict which is used 88 to modify Color.categories 90 N.b. only strings written to a terminal are colourized 96 if isinstance(val, dict):
99 if k
in Color.categories:
100 if val[k]
in Color.colors:
101 Color.categories[k] = val[k]
103 print(
"Unknown colour %s for category %s" % (val[k], k), file=sys.stderr)
108 print(
"Unknown colourizing category: %s" %
" ".join(unknown), file=sys.stderr)
110 return Color._colorize
if sys.stdout.isatty()
else False 118 prefix = base + self.
_code +
"m" 121 return prefix + self.
rawText + suffix
124 def _colorize(text, category):
125 text =
Color(text, category)
129 def format(config, name=None, writeSourceLine=True, prefix="", verbose=False):
130 """Format the history record for config.name""" 133 for i, name
in enumerate(config.history.keys()):
136 print(
format(config, name))
139 for value, stack, label
in config.history[name]:
142 if frame.function
in (
"__new__",
"__set__",
"__setattr__",
"execfile",
"wrapper")
or \
143 os.path.split(frame.filename)[1]
in (
"argparse.py",
"argumentParser.py"):
149 line.append([
"%s" % (
"%s:%d" % (frame.filename, frame.lineno)),
"FILE", ])
151 line.append([frame.content,
"TEXT", ])
153 line.append([frame.function,
"FUNCTION_NAME", ])
157 outputs.append([value, output])
163 for value, output
in outputs:
164 sourceLengths.append(max([len(x[0][0])
for x
in output]))
165 sourceLength = max(sourceLengths)
167 valueLength = len(prefix) + max([len(str(value))
for value, output
in outputs])
172 fullname =
"%s.%s" % (config._name, name)
if config._name
is not None else name
173 msg.append(_colorize(re.sub(
r"^root\.",
"", fullname),
"NAME"))
174 for value, output
in outputs:
175 line = prefix + _colorize(
"%-*s" % (valueLength, value),
"VALUE") +
" " 176 for i, vt
in enumerate(output):
178 vt[0][0] =
"%-*s" % (sourceLength, vt[0][0])
180 output[i] =
" ".join([_colorize(v, t)
for v, t
in vt])
182 line += (
"\n%*s" % (valueLength + 1,
"")).join(output)
185 return "\n".join(msg)
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
def __init__(self, text, category)