From 1a12f6e369ece25fc3b5d9e802465d3d88f5869b Mon Sep 17 00:00:00 2001 From: Andrew Hamilton Date: Thu, 18 Feb 2016 19:37:31 +0000 Subject: [PATCH] Coding style. Removed the Code widget which was almost the same as the Text widget. --- TODO | 1 + fill3.py | 42 --------------- fill3_test.py | 16 ------ golden-files/results/bcpp-hello_cpp | 3 +- golden-files/results/contents-hi3_py | 3 +- golden-files/results/perltidy-perl_pl | 3 +- golden-files/results/python_gut-hi3_py | 4 +- golden-files/results/python_gut-hi_py | 4 +- .../results/uncrustify-closure-util_java | 2 +- golden-files/results/uncrustify-hello_c | 3 +- golden-files/results/uncrustify-hello_cpp | 3 +- golden-files/results/uncrustify-hello_h | 3 +- tools.py | 53 ++++++++++++------- 13 files changed, 46 insertions(+), 94 deletions(-) diff --git a/TODO b/TODO index 71579a8..0a127f2 100644 --- a/TODO +++ b/TODO @@ -10,6 +10,7 @@ Todo (tool related) - Report on python doctests. (also coverage of) - Cache tools._python_version. - Add bandit tool for python. +- Recognize python test files like: test_*.py Done diff --git a/fill3.py b/fill3.py index 496495c..b367e03 100644 --- a/fill3.py +++ b/fill3.py @@ -341,48 +341,6 @@ class Table: for y, row in enumerate(appearances)]) -def _parse_rgb(hex_rgb): - if hex_rgb.startswith("#"): - hex_rgb = hex_rgb[1:] - return tuple(eval("0x"+hex_rgb[index:index+2]) for index in [0, 2, 4]) - - -def _char_style_for_token_type(token_type, pygment_style): - token_style = pygment_style.style_for_token(token_type) - fg_color = (None if token_style["color"] is None - else _parse_rgb(token_style["color"])) - bg_color = (None if token_style["bgcolor"] is None - else _parse_rgb(token_style["bgcolor"])) - return termstr.CharStyle(fg_color, bg_color, token_style["bold"], - token_style["italic"], token_style["underline"]) - - -def _pygments_to_termstr(tokens, pygment_style): - return termstr.TermStr("").join( - termstr.TermStr(text, _char_style_for_token_type( - token_type, pygment_style)) - for token_type, text in tokens) - - -class Code: - - def __init__(self, tokens, style): - code = _pygments_to_termstr(tokens, style).split("\n") - max_width = max(len(line) for line in code) - height = len(code) - # bg_color = _parse_rgb(style.background_color) - # bg_style = termstr.CharStyle(1, bg_color) - # pad_char = termstr.TermStr(" ", bg_style) - pad_char = " " - self.code = appearance_resize(code, (max_width, height), pad_char) - - def appearance_min(self): - return self.code - - def appearance(self, dimensions): - return appearance_resize(self.appearance_min(), dimensions) - - class Border: THIN = ["─", "─", "│", "│", "┌", "└", "┘", "┐"] diff --git a/fill3_test.py b/fill3_test.py index cf67e7b..228f761 100755 --- a/fill3_test.py +++ b/fill3_test.py @@ -8,10 +8,6 @@ import unittest import fill3 as fill -# import pygments.lexers -# import pygments.styles.default -# import pygments.styles.emacs - class WidgetTests(unittest.TestCase): @@ -68,18 +64,6 @@ class WidgetTests(unittest.TestCase): "│abcdef│\n" "└──────┘") - # def test_pygments_widget(self): - # text = "print('hello world')" - # tokens = pygments.lex(text, pygments.lexers.PythonLexer()) - # code = fill.Code(tokens, pygments.styles.default.DefaultStyle) - # self.assert_string(code.appearance_min(), - # "\x1b[1m\x1b[38;2;0;128;0m\x1b[48;2;248;248;248m" - # "print\x1b(B\x1b[m\x1b[315m\x1b[48;2;248;248;248m" - # "(\x1b(B\x1b[m\x1b[38;2;186;33;33m\x1b[48;2;248;248;248m" - # "'hello world'\x1b(B\x1b[m\x1b[315m\x1b[48;2;248;248;248m" - # ")\x1b(B\x1b[m\x1b[315m\x1b[40m\n\x1b(B\x1b[m\x1b[31m" - # "\x1b[48;2;248;248;248m \x1b[0m") - def test_placeholder_widget(self): placeholder = fill.Placeholder(self.TEXT_A) self.assert_string(placeholder.appearance_min(), "A") diff --git a/golden-files/results/bcpp-hello_cpp b/golden-files/results/bcpp-hello_cpp index d8e111a..1820b2b 100644 --- a/golden-files/results/bcpp-hello_cpp +++ b/golden-files/results/bcpp-hello_cpp @@ -5,5 +5,4 @@ int main() {  cout << "Hello World" << endl; -} -  \ No newline at end of file +}  \ No newline at end of file diff --git a/golden-files/results/contents-hi3_py b/golden-files/results/contents-hi3_py index 6038b9d..5c0ad63 100644 --- a/golden-files/results/contents-hi3_py +++ b/golden-files/results/contents-hi3_py @@ -1,3 +1,2 @@ def hi(): - print("hi") -  \ No newline at end of file + print("hi") \ No newline at end of file diff --git a/golden-files/results/perltidy-perl_pl b/golden-files/results/perltidy-perl_pl index bac2a31..b0eddbf 100644 --- a/golden-files/results/perltidy-perl_pl +++ b/golden-files/results/perltidy-perl_pl @@ -1,3 +1,2 @@ #!/usr/bin/perl -print "Hello, world!\n"; -  \ No newline at end of file +print "Hello, world!\n"; \ No newline at end of file diff --git a/golden-files/results/python_gut-hi3_py b/golden-files/results/python_gut-hi3_py index 9ede04f..3c1602a 100644 --- a/golden-files/results/python_gut-hi3_py +++ b/golden-files/results/python_gut-hi3_py @@ -1,2 +1,2 @@ -def hi(): -  \ No newline at end of file +def hi(): + \ No newline at end of file diff --git a/golden-files/results/python_gut-hi_py b/golden-files/results/python_gut-hi_py index 9ede04f..3c1602a 100644 --- a/golden-files/results/python_gut-hi_py +++ b/golden-files/results/python_gut-hi_py @@ -1,2 +1,2 @@ -def hi(): -  \ No newline at end of file +def hi(): + \ No newline at end of file diff --git a/golden-files/results/uncrustify-closure-util_java b/golden-files/results/uncrustify-closure-util_java index 9941620..e4693e4 100644 --- a/golden-files/results/uncrustify-closure-util_java +++ b/golden-files/results/uncrustify-closure-util_java @@ -1,2 +1,2 @@ - +  \ No newline at end of file diff --git a/golden-files/results/uncrustify-hello_c b/golden-files/results/uncrustify-hello_c index 7a28916..a72156b 100644 --- a/golden-files/results/uncrustify-hello_c +++ b/golden-files/results/uncrustify-hello_c @@ -4,5 +4,4 @@ {  printf("Hello World\n");  return 0; -} -  \ No newline at end of file +}  \ No newline at end of file diff --git a/golden-files/results/uncrustify-hello_cpp b/golden-files/results/uncrustify-hello_cpp index d8e111a..1820b2b 100644 --- a/golden-files/results/uncrustify-hello_cpp +++ b/golden-files/results/uncrustify-hello_cpp @@ -5,5 +5,4 @@ int main() {  cout << "Hello World" << endl; -} -  \ No newline at end of file +}  \ No newline at end of file diff --git a/golden-files/results/uncrustify-hello_h b/golden-files/results/uncrustify-hello_h index 3bbd8f4..c70b8f0 100644 --- a/golden-files/results/uncrustify-hello_h +++ b/golden-files/results/uncrustify-hello_h @@ -3,5 +3,4 @@ void hello(); -#endif -  \ No newline at end of file +#endif  \ No newline at end of file diff --git a/tools.py b/tools.py index ffb09ea..be5c3ea 100644 --- a/tools.py +++ b/tools.py @@ -123,11 +123,30 @@ def _run_command(command, status_text=Status.ok): return status, fill3.Text(_fix_input(output)) -def _syntax_highlight_code(text, path): +def _syntax_highlight(text, lexer, style, pad_char=" "): + def _parse_rgb(hex_rgb): + if hex_rgb.startswith("#"): + hex_rgb = hex_rgb[1:] + return tuple(eval("0x"+hex_rgb[index:index+2]) for index in [0, 2, 4]) + def _char_style_for_token_type(token_type): + token_style = style.style_for_token(token_type) + fg_color = (None if token_style["color"] is None + else _parse_rgb(token_style["color"])) + bg_color = (None if token_style["bgcolor"] is None + else _parse_rgb(token_style["bgcolor"])) + return termstr.CharStyle(fg_color, bg_color, token_style["bold"], + token_style["italic"], + token_style["underline"]) + text = fill3.join("", + [termstr.TermStr(text, _char_style_for_token_type(token_type)) + for token_type, text in pygments.lex(text, lexer)]) + return fill3.Text(text, pad_char=pad_char) + + +def _syntax_highlight_using_path(text, path): lexer = pygments.lexers.get_lexer_for_filename(path, text) - tokens = pygments.lex(text, lexer) native_style = pygments.styles.get_style_by_name("native") - return fill3.Code(tokens, native_style) + return _syntax_highlight(text, lexer, native_style) def pygments_(path): @@ -138,7 +157,8 @@ def pygments_(path): return Status.not_applicable, fill3.Text("Not unicode") else: try: - source_widget = _syntax_highlight_code(_fix_input(text), path) + source_widget = _syntax_highlight_using_path(_fix_input(text), + path) except pygments.util.ClassNotFound: return Status.normal, fill3.Text(text) return Status.normal, source_widget @@ -279,11 +299,10 @@ def python_unittests(path): python_version = _python_version(path) cmd = [path] if _has_shebang_line(path) else [python_version, path] stdout, stderr, returncode = _do_command(cmd, timeout=TIMEOUT) - markup = pygments.lex(stderr, _python_console_lexer) status = Status.ok if returncode == 0 else Status.problem native_style = pygments.styles.get_style_by_name("native") - code = fill3.Code(markup, native_style) - return status, code + return status, _syntax_highlight(stderr, _python_console_lexer, + native_style) else: return Status.not_applicable, fill3.Text("No tests.") python_unittests.dependencies = {"python", "python3"} @@ -359,7 +378,7 @@ pylint.dependencies = {"pylint", "pylint3"} def python_gut(path): with open(path) as module_file: output = gut.gut_module(module_file.read()) - source_widget = _syntax_highlight_code(_fix_input(output), path) + source_widget = _syntax_highlight_using_path(_fix_input(output), path) return Status.normal, source_widget python_gut.dependencies = set() @@ -396,7 +415,7 @@ python_mccabe.dependencies = {"python-mccabe", "python3-mccabe"} def python_tidy(path): # Deps: found on internet? stdout, *rest = _do_command(["python", "python-tidy.py", path]) - return Status.normal, _syntax_highlight_code(stdout, path) + return Status.normal, _syntax_highlight_using_path(stdout, path) def disassemble_pyc(path): @@ -428,7 +447,7 @@ perldoc.dependencies = {"perl-doc"} def perltidy(path): stdout, *rest = _do_command(["perltidy", "-st", path]) - return Status.normal, _syntax_highlight_code(stdout, path) + return Status.normal, _syntax_highlight_using_path(stdout, path) perltidy.dependencies = {"perltidy"} @@ -526,8 +545,7 @@ html2text.dependencies = {"html2text"} def bcpp(path): stdout, stderr, returncode = _do_command(["bcpp", "-fi", path]) status = Status.normal if returncode == 0 else Status.problem - source_widget = _syntax_highlight_code(stdout, path) - return status, source_widget + return status, _syntax_highlight_using_path(stdout, path) bcpp.dependencies = {"bcpp"} @@ -540,8 +558,7 @@ def uncrustify(path): stdout, stderr, returncode = _do_command( ["uncrustify", "-c", config_path, "-f", path]) status = Status.normal if returncode == 0 else Status.problem - source_widget = _syntax_highlight_code(stdout, path) - return status, source_widget + return status, _syntax_highlight_using_path(stdout, path) uncrustify.dependencies = {"uncrustify"} @@ -744,11 +761,9 @@ def run_tool_no_error(path, tool): except subprocess.TimeoutExpired: status, result = Status.timed_out, fill3.Text("Timed out") except: - # Maybe use code.InteractiveInterpreter.showtraceback() ? - tokens = pygments.lex(traceback.format_exc(), - _get_python_traceback_lexer()) - native_style = pygments.styles.get_style_by_name("native") - status, result = Status.error, fill3.Code(tokens, native_style) + status, result = Status.error, _syntax_highlight( + traceback.format_exc(), _get_python_traceback_lexer(), + pygments.styles.get_style_by_name("native")) return status, result