diff --git a/infer/lib/python/inferTraceBugs b/infer/lib/python/inferTraceBugs index 075828cd9..5e9ba4bda 100755 --- a/infer/lib/python/inferTraceBugs +++ b/infer/lib/python/inferTraceBugs @@ -21,7 +21,7 @@ import shutil import subprocess import sys -from inferlib import config, issues, source, utils +from inferlib import colorize, config, issues, source, utils HTML_REPORT_DIR = 'report.html' TRACES_REPORT_DIR = 'traces' @@ -93,9 +93,9 @@ class Tracer(object): if not self.args.no_source: self.indenter.indent_push(node[issues.JSON_INDEX_TRACE_LEVEL]) - mode = source.TERMINAL_FORMATTER + mode = colorize.TERMINAL_FORMATTER if self.args.html: - mode = source.PLAIN_FORMATTER + mode = colorize.PLAIN_FORMATTER self.indenter.add(source.build_source_context(fname, mode, report_line)) diff --git a/infer/lib/python/inferlib/colorize.py b/infer/lib/python/inferlib/colorize.py new file mode 100644 index 000000000..a79856c7c --- /dev/null +++ b/infer/lib/python/inferlib/colorize.py @@ -0,0 +1,37 @@ +# Copyright (c) 2013 - present Facebook, Inc. +# All rights reserved. +# +# This source code is licensed under the BSD style license found in the +# LICENSE file in the root directory of this source tree. An additional grant +# of patent rights can be found in the PATENTS file in the same directory. + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from __future__ import unicode_literals + +import sys + +try: + import pygments + import pygments.formatters + import pygments.lexers +except ImportError: + pygments = None + +# syntax highlighting modes +PLAIN_FORMATTER = 0 +TERMINAL_FORMATTER = 1 + + +def syntax_highlighting(source_name, mode, s): + if pygments is None or mode == PLAIN_FORMATTER: + return s + + lexer = pygments.lexers.get_lexer_for_filename(source_name) + formatter = None + if mode == TERMINAL_FORMATTER: + if not sys.stdout.isatty(): + return s + formatter = pygments.formatters.TerminalFormatter() + return pygments.highlight(s, lexer, formatter) diff --git a/infer/lib/python/inferlib/issues.py b/infer/lib/python/inferlib/issues.py index 44601bf53..0dd932412 100644 --- a/infer/lib/python/inferlib/issues.py +++ b/infer/lib/python/inferlib/issues.py @@ -21,7 +21,7 @@ import sys import tempfile import xml.etree.ElementTree as ET -from . import config, source, utils +from . import config, colorize, source, utils # Increase the limit of the CSV parser to sys.maxlimit @@ -174,7 +174,7 @@ def text_of_report(report): ) -def _text_of_report_list(reports, formatter=source.TERMINAL_FORMATTER): +def _text_of_report_list(reports, formatter=colorize.TERMINAL_FORMATTER): text_errors_list = [] error_types_count = {} for report in reports: @@ -185,7 +185,7 @@ def _text_of_report_list(reports, formatter=source.TERMINAL_FORMATTER): if formatter is not None: source_context = source.build_source_context( filename, - source.TERMINAL_FORMATTER, + colorize.TERMINAL_FORMATTER, line, ) indenter = source.Indenter() \ diff --git a/infer/lib/python/inferlib/source.py b/infer/lib/python/inferlib/source.py index 8f2ea27aa..d97cf0eeb 100644 --- a/infer/lib/python/inferlib/source.py +++ b/infer/lib/python/inferlib/source.py @@ -11,25 +11,13 @@ from __future__ import print_function from __future__ import unicode_literals import codecs -try: - import pygments - import pygments.formatters - import pygments.lexers -except ImportError: - pygments = None -import sys -from . import config, utils +from . import config, colorize, utils BASE_INDENT = 2 # how many lines of context around each report SOURCE_CONTEXT = 2 -# syntax highlighting modes -PLAIN_FORMATTER = 0 -TERMINAL_FORMATTER = 1 - - class Indenter(str): def __init__(self): super(Indenter, self).__init__() @@ -88,17 +76,4 @@ def build_source_context(source_name, mode, report_line): caret = '> ' s += '%s. %s%s' % (num, caret, line) line_number += 1 - return _syntax_highlighting(source_name, mode, s) - - -def _syntax_highlighting(source_name, mode, s): - if pygments is None or mode == PLAIN_FORMATTER: - return s - - lexer = pygments.lexers.get_lexer_for_filename(source_name) - formatter = None - if mode == TERMINAL_FORMATTER: - if not sys.stdout.isatty(): - return s - formatter = pygments.formatters.TerminalFormatter() - return pygments.highlight(s, lexer, formatter) + return colorize.syntax_highlighting(source_name, mode, s)