22 from __future__
import print_function
23 from builtins
import str
24 from builtins
import object
34 """Control whether strings should be coloured 36 The usual usage is `Color(string, category)` which returns a string that 37 may be printed; categories are given by the keys of Color.categories 39 Color.colorize() may be used to set or retrieve whether the user wants 40 colour; it always returns False when sys.stdout is not attached to a 66 """Return a string that should display as coloured on a conformant terminal""" 68 color = Color.categories[category]
70 raise RuntimeError(
"Unknown category: %s" % category)
73 x = color.lower().split(
";")
74 self.color, bold = x.pop(0),
False 77 if props
in (
"bold",):
81 self.
_code =
"%s" % (30 + Color.colors[self.color])
83 raise RuntimeError(
"Unknown colour: %s" % self.color)
90 """Should I colour strings? With an argument, set the value 92 The value is usually a bool, but it may be a dict which is used 93 to modify Color.categories 95 N.b. only strings written to a terminal are colourized 101 if isinstance(val, dict):
104 if k
in Color.categories:
105 if val[k]
in Color.colors:
106 Color.categories[k] = val[k]
108 print(
"Unknown colour %s for category %s" % (val[k], k), file=sys.stderr)
113 print(
"Unknown colourizing category: %s" %
" ".join(unknown), file=sys.stderr)
115 return Color._colorize
if sys.stdout.isatty()
else False 123 prefix = base + self.
_code +
"m" 126 return prefix + self.
rawText + suffix
129 def _colorize(text, category):
130 text =
Color(text, category)
135 def format(config, name=None, writeSourceLine=True, prefix="", verbose=False):
136 """Format the history record for config.name""" 139 for i, name
in enumerate(config.history.keys()):
142 print(
format(config, name))
145 for value, stack, label
in config.history[name]:
148 if frame.function
in (
"__new__",
"__set__",
"__setattr__",
"execfile",
"wrapper")
or \
149 os.path.split(frame.filename)[1]
in (
"argparse.py",
"argumentParser.py"):
155 line.append([
"%s" % (
"%s:%d" % (frame.filename, frame.lineno)),
"FILE", ])
157 line.append([frame.content,
"TEXT", ])
159 line.append([frame.function,
"FUNCTION_NAME", ])
163 outputs.append([value, output])
169 for value, output
in outputs:
170 sourceLengths.append(max([len(x[0][0])
for x
in output]))
171 sourceLength = max(sourceLengths)
173 valueLength = len(prefix) + max([len(str(value))
for value, output
in outputs])
178 fullname =
"%s.%s" % (config._name, name)
if config._name
is not None else name
179 msg.append(_colorize(re.sub(
r"^root\.",
"", fullname),
"NAME"))
180 for value, output
in outputs:
181 line = prefix + _colorize(
"%-*s" % (valueLength, value),
"VALUE") +
" " 182 for i, vt
in enumerate(output):
184 vt[0][0] =
"%-*s" % (sourceLength, vt[0][0])
186 output[i] =
" ".join([_colorize(v, t)
for v, t
in vt])
188 line += (
"\n%*s" % (valueLength + 1,
"")).join(output)
191 return "\n".join(msg)
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
def __init__(self, text, category)