move color code to colorize.py

Summary:public
small refactor

Reviewed By: jeremydubreil

Differential Revision: D3023939

fb-gh-sync-id: 53de286
shipit-source-id: 53de286
master
Jules Villard 9 years ago committed by Facebook Github Bot 8
parent 7c464c5bac
commit b03304e0d8

@ -21,7 +21,7 @@ import shutil
import subprocess import subprocess
import sys import sys
from inferlib import config, issues, source, utils from inferlib import colorize, config, issues, source, utils
HTML_REPORT_DIR = 'report.html' HTML_REPORT_DIR = 'report.html'
TRACES_REPORT_DIR = 'traces' TRACES_REPORT_DIR = 'traces'
@ -93,9 +93,9 @@ class Tracer(object):
if not self.args.no_source: if not self.args.no_source:
self.indenter.indent_push(node[issues.JSON_INDEX_TRACE_LEVEL]) self.indenter.indent_push(node[issues.JSON_INDEX_TRACE_LEVEL])
mode = source.TERMINAL_FORMATTER mode = colorize.TERMINAL_FORMATTER
if self.args.html: if self.args.html:
mode = source.PLAIN_FORMATTER mode = colorize.PLAIN_FORMATTER
self.indenter.add(source.build_source_context(fname, self.indenter.add(source.build_source_context(fname,
mode, mode,
report_line)) report_line))

@ -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)

@ -21,7 +21,7 @@ import sys
import tempfile import tempfile
import xml.etree.ElementTree as ET 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 # 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 = [] text_errors_list = []
error_types_count = {} error_types_count = {}
for report in reports: for report in reports:
@ -185,7 +185,7 @@ def _text_of_report_list(reports, formatter=source.TERMINAL_FORMATTER):
if formatter is not None: if formatter is not None:
source_context = source.build_source_context( source_context = source.build_source_context(
filename, filename,
source.TERMINAL_FORMATTER, colorize.TERMINAL_FORMATTER,
line, line,
) )
indenter = source.Indenter() \ indenter = source.Indenter() \

@ -11,25 +11,13 @@ from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
import codecs 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 BASE_INDENT = 2
# how many lines of context around each report # how many lines of context around each report
SOURCE_CONTEXT = 2 SOURCE_CONTEXT = 2
# syntax highlighting modes
PLAIN_FORMATTER = 0
TERMINAL_FORMATTER = 1
class Indenter(str): class Indenter(str):
def __init__(self): def __init__(self):
super(Indenter, self).__init__() super(Indenter, self).__init__()
@ -88,17 +76,4 @@ def build_source_context(source_name, mode, report_line):
caret = '> ' caret = '> '
s += '%s. %s%s' % (num, caret, line) s += '%s. %s%s' % (num, caret, line)
line_number += 1 line_number += 1
return _syntax_highlighting(source_name, mode, s) return colorize.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)

Loading…
Cancel
Save