Author: Armin Rigo <ar...@tunes.org> Branch: remove-py-log Changeset: r82981:6066499ee520 Date: 2016-03-11 17:17 +0100 http://bitbucket.org/pypy/pypy/changeset/6066499ee520/
Log: Support for unknown names diff --git a/rpython/tool/ansi_print.py b/rpython/tool/ansi_print.py --- a/rpython/tool/ansi_print.py +++ b/rpython/tool/ansi_print.py @@ -12,27 +12,29 @@ wrote_dot = False # global shared state -class Logger(object): +def _make_method(subname, colors): + # + def logger_method(self, text): + global wrote_dot + text = "[%s%s] %s" % (self.name, subname, text) + if isatty(): + col = colors + else: + col = () + if wrote_dot: + text = '\n' + text + ansi_print(text, col) + wrote_dot = False + # + return logger_method + + +class AnsiLogger(object): def __init__(self, name): self.name = name - def _make_method(subname, colors): - # - def logger_method(self, text): - global wrote_dot - text = "[%s%s] %s" % (self.name, subname, text) - if isatty(): - col = colors - else: - col = () - if wrote_dot: - text = '\n' + text - ansi_print(text, col) - wrote_dot = False - # - return logger_method - + # these methods write "[name:method] text" to the terminal, with color codes red = _make_method('', (31,)) bold = _make_method('', (1,)) WARNING = _make_method(':WARNING', (31,)) @@ -41,11 +43,25 @@ Error = _make_method(':Error', (1, 31)) info = _make_method(':info', (35,)) stub = _make_method(':stub', (34,)) + + # directly calling the logger writes "[name] text" with no particular color __call__ = _make_method('', ()) + # calling unknown method names writes "[name:method] text" without color + def __getattr__(self, name): + if name[0].isalpha(): + method = _make_method(':' + name, ()) + setattr(self.__class__, name, method) + return getattr(self, name) + raise AttributeError(name) + def dot(self): + """Output a mandelbrot dot to the terminal.""" global wrote_dot if not wrote_dot: mandelbrot_driver.reset() wrote_dot = True mandelbrot_driver.dot() + + def debug(self, info): + """For messages that are dropped. Can be monkeypatched in tests.""" diff --git a/rpython/tool/test/test_ansi_print.py b/rpython/tool/test/test_ansi_print.py --- a/rpython/tool/test/test_ansi_print.py +++ b/rpython/tool/test/test_ansi_print.py @@ -8,6 +8,7 @@ self.tty = tty self.output = [] def __enter__(self, *args): + ansi_print.wrote_dot = False self.monkey.setattr(ansi_print, 'ansi_print', self._print) self.monkey.setattr(ansi_print, 'isatty', self._isatty) self.monkey.setattr(ansi_mandelbrot, 'ansi_print', self._print) @@ -24,25 +25,25 @@ def test_simple(): - log = ansi_print.Logger('test') + log = ansi_print.AnsiLogger('test') with FakeOutput() as output: log('Hello') assert output == [('[test] Hello\n', ())] def test_bold(): - log = ansi_print.Logger('test') + log = ansi_print.AnsiLogger('test') with FakeOutput() as output: log.bold('Hello') assert output == [('[test] Hello\n', (1,))] def test_not_a_tty(): - log = ansi_print.Logger('test') + log = ansi_print.AnsiLogger('test') with FakeOutput(tty=False) as output: log.bold('Hello') assert output == [('[test] Hello\n', ())] def test_dot_1(): - log = ansi_print.Logger('test') + log = ansi_print.AnsiLogger('test') with FakeOutput() as output: log.dot() assert len(output) == 1 @@ -50,7 +51,7 @@ # output[0][1] is some ansi color code from mandelbort_driver def test_dot_mixing_with_regular_lines(): - log = ansi_print.Logger('test') + log = ansi_print.AnsiLogger('test') with FakeOutput() as output: log.dot() log.dot() @@ -63,3 +64,13 @@ assert output[2] == ('\n[test:WARNING] oops\n', (31,)) assert output[3] == ('[test:WARNING] maybe?\n', (31,)) assert len(output[4][0]) == 1 # single character + +def test_unknown_method_names(): + log = ansi_print.AnsiLogger('test') + with FakeOutput() as output: + log.foo('Hello') + log.foo('World') + log.BAR('!') + assert output == [('[test:foo] Hello\n', ()), + ('[test:foo] World\n', ()), + ('[test:BAR] !\n', ())] _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit