summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Bruhin <me@the-compiler.org>2015-07-26 10:52:05 +0200
committerFlorian Bruhin <me@the-compiler.org>2015-07-26 10:52:05 +0200
commit4f67ba0b84ad47ed47559b6a2c9e9edfa002ef57 (patch)
treea36a776d65de3d99c8b73e669340d29c656eb4ad
parent7fe9ab5b254ecf606601f9c3c9034e6118f5d66e (diff)
downloadpylint-4f67ba0b84ad47ed47559b6a2c9e9edfa002ef57.tar.gz
Get rid of logilab.common.optik_ext and logilab.common.textutils.
-rw-r--r--pylint/lint.py16
-rw-r--r--pylint/reporters/text.py88
-rw-r--r--pylint/test/unittest_lint.py9
-rw-r--r--pylint/utils.py61
4 files changed, 155 insertions, 19 deletions
diff --git a/pylint/lint.py b/pylint/lint.py
index 6cf32a9..570edf3 100644
--- a/pylint/lint.py
+++ b/pylint/lint.py
@@ -43,9 +43,7 @@ import astroid
from astroid.__pkginfo__ import version as astroid_version
from astroid import modutils
from logilab.common import configuration
-from logilab.common import optik_ext
from logilab.common import interface
-from logilab.common import textutils
from logilab.common import ureports
import six
@@ -528,7 +526,7 @@ class PyLinter(configuration.OptionsManagerMixIn,
warnings.warn('%s is deprecated, replace it by %s' % (optname,
optname.split('-')[0]),
DeprecationWarning)
- value = optik_ext.check_csv(None, optname, value)
+ value = utils.check_csv(None, optname, value)
if isinstance(value, (list, tuple)):
for _id in value:
meth(_id, ignore_unknown=True)
@@ -668,7 +666,7 @@ class PyLinter(configuration.OptionsManagerMixIn,
# found a "(dis|en)able-msg" pragma deprecated suppresssion
self.add_message('deprecated-pragma', line=start[0],
args=(opt, opt.replace('-msg', '')))
- for msgid in textutils.splitstrip(value):
+ for msgid in utils.splitstrip(value):
# Add the line where a control pragma was encountered.
if opt in control_pragmas:
self._pragma_lineno[msgid] = start[0]
@@ -1282,11 +1280,11 @@ group are mutually exclusive.'),
# run init hook, if present, before loading plugins
if config_parser.has_option('MASTER', 'init-hook'):
cb_init_hook('init-hook',
- textutils.unquote(config_parser.get('MASTER',
- 'init-hook')))
+ utils.unquote(config_parser.get('MASTER',
+ 'init-hook')))
# is there some additional plugins in the file configuration, in
if config_parser.has_option('MASTER', 'load-plugins'):
- plugins = textutils.splitstrip(
+ plugins = utils.splitstrip(
config_parser.get('MASTER', 'load-plugins'))
linter.load_plugin_modules(plugins)
# now we can load file config and command line, plugins (which can
@@ -1344,7 +1342,7 @@ group are mutually exclusive.'),
def cb_add_plugins(self, name, value):
"""callback for option preprocessing (i.e. before option parsing)"""
- self._plugins.extend(textutils.splitstrip(value))
+ self._plugins.extend(utils.splitstrip(value))
def cb_error_mode(self, *args, **kwargs):
"""error mode:
@@ -1369,7 +1367,7 @@ group are mutually exclusive.'),
def cb_help_message(self, option, optname, value, parser):
"""optik callback for printing some help about a particular message"""
- self.linter.msgs_store.help_message(textutils.splitstrip(value))
+ self.linter.msgs_store.help_message(utils.splitstrip(value))
sys.exit(0)
def cb_full_documentation(self, option, optname, value, parser):
diff --git a/pylint/reporters/text.py b/pylint/reporters/text.py
index 7b76674..b66af3c 100644
--- a/pylint/reporters/text.py
+++ b/pylint/reporters/text.py
@@ -23,14 +23,100 @@ import sys
import six
from logilab.common.ureports import TextWriter
-from logilab.common.textutils import colorize_ansi
from pylint.interfaces import IReporter
from pylint.reporters import BaseReporter
+from pylint import utils
TITLE_UNDERLINES = ['', '=', '-', '.']
+ANSI_PREFIX = '\033['
+ANSI_END = 'm'
+ANSI_RESET = '\033[0m'
+ANSI_STYLES = {
+ 'reset': "0",
+ 'bold': "1",
+ 'italic': "3",
+ 'underline': "4",
+ 'blink': "5",
+ 'inverse': "7",
+ 'strike': "9",
+}
+ANSI_COLORS = {
+ 'reset': "0",
+ 'black': "30",
+ 'red': "31",
+ 'green': "32",
+ 'yellow': "33",
+ 'blue': "34",
+ 'magenta': "35",
+ 'cyan': "36",
+ 'white': "37",
+}
+
+def _get_ansi_code(color=None, style=None):
+ """return ansi escape code corresponding to color and style
+
+ :type color: str or None
+ :param color:
+ the color name (see `ANSI_COLORS` for available values)
+ or the color number when 256 colors are available
+
+ :type style: str or None
+ :param style:
+ style string (see `ANSI_COLORS` for available values). To get
+ several style effects at the same time, use a coma as separator.
+
+ :raise KeyError: if an unexistent color or style identifier is given
+
+ :rtype: str
+ :return: the built escape code
+ """
+ ansi_code = []
+ if style:
+ style_attrs = utils.splitstrip(style)
+ for effect in style_attrs:
+ ansi_code.append(ANSI_STYLES[effect])
+ if color:
+ if color.isdigit():
+ ansi_code.extend(['38', '5'])
+ ansi_code.append(color)
+ else:
+ ansi_code.append(ANSI_COLORS[color])
+ if ansi_code:
+ return ANSI_PREFIX + ';'.join(ansi_code) + ANSI_END
+ return ''
+
+def colorize_ansi(msg, color=None, style=None):
+ """colorize message by wrapping it with ansi escape codes
+
+ :type msg: str or unicode
+ :param msg: the message string to colorize
+
+ :type color: str or None
+ :param color:
+ the color identifier (see `ANSI_COLORS` for available values)
+
+ :type style: str or None
+ :param style:
+ style string (see `ANSI_COLORS` for available values). To get
+ several style effects at the same time, use a coma as separator.
+
+ :raise KeyError: if an unexistent color or style identifier is given
+
+ :rtype: str or unicode
+ :return: the ansi escaped string
+ """
+ # If both color and style are not defined, then leave the text as is
+ if color is None and style is None:
+ return msg
+ escape_code = _get_ansi_code(color, style)
+ # If invalid (or unknown) color, don't wrap msg with ansi codes
+ if escape_code:
+ return '%s%s%s' % (escape_code, msg, ANSI_RESET)
+ return msg
+
class TextReporter(BaseReporter):
"""reports messages and layouts in plain text"""
diff --git a/pylint/test/unittest_lint.py b/pylint/test/unittest_lint.py
index bb137d4..f756702 100644
--- a/pylint/test/unittest_lint.py
+++ b/pylint/test/unittest_lint.py
@@ -622,14 +622,7 @@ class MessagesStoreTC(unittest.TestCase):
self.store.register_messages(Checker())
def _compare_messages(self, desc, msg, checkerref=False):
- # replace \r\n with \n, because
- # logilab.common.textutils.normalize_text
- # uses os.linesep, which will
- # not properly compare with triple
- # quoted multilines used in these tests
- self.assertMultiLineEqual(
- desc,
- msg.format_help(checkerref=checkerref).replace('\r\n', '\n'))
+ self.assertMultiLineEqual(desc, msg.format_help(checkerref=checkerref))
def test_check_message_id(self):
self.assertIsInstance(self.store.check_message_id('W1234'),
diff --git a/pylint/utils.py b/pylint/utils.py
index d2c9ed8..c573409 100644
--- a/pylint/utils.py
+++ b/pylint/utils.py
@@ -24,13 +24,13 @@ import re
import sys
import tokenize
import warnings
+import textwrap
from os.path import dirname, basename, splitext, exists, isdir, join, normpath
import six
from six.moves import zip # pylint: disable=redefined-builtin
from logilab.common.interface import implements
-from logilab.common.textutils import normalize_text
from logilab.common.configuration import rest_format_section
from logilab.common.ureports import Section
@@ -941,3 +941,62 @@ def deprecated_option(shortname=None, opt_type=None, help_msg=None):
if shortname:
option['shortname'] = shortname
return option
+
+
+def splitstrip(string, sep=','):
+ """return a list of stripped string by splitting the string given as
+ argument on `sep` (',' by default). Empty string are discarded.
+
+ >>> splitstrip('a, b, c , 4,,')
+ ['a', 'b', 'c', '4']
+ >>> splitstrip('a')
+ ['a']
+ >>>
+
+ :type string: str or unicode
+ :param string: a csv line
+
+ :type sep: str or unicode
+ :param sep: field separator, default to the comma (',')
+
+ :rtype: str or unicode
+ :return: the unquoted string (or the input string if it wasn't quoted)
+ """
+ return [word.strip() for word in string.split(sep) if word.strip()]
+
+
+def unquote(string):
+ """remove optional quotes (simple or double) from the string
+
+ :type string: str or unicode
+ :param string: an optionally quoted string
+
+ :rtype: str or unicode
+ :return: the unquoted string (or the input string if it wasn't quoted)
+ """
+ if not string:
+ return string
+ if string[0] in '"\'':
+ string = string[1:]
+ if string[-1] in '"\'':
+ string = string[:-1]
+ return string
+
+
+def normalize_text(text, line_len=80, indent=''):
+ """Wrap the text on the given line length."""
+ return '\n'.join(textwrap.wrap(text, width=line_len, initial_indent=indent,
+ subsequent_indent=indent))
+
+
+def check_csv(option, opt, value):
+ """check a csv value by trying to split it
+ return the list of separated values
+ """
+ if isinstance(value, (list, tuple)):
+ return value
+ try:
+ return splitstrip(value)
+ except ValueError:
+ raise OptionValueError(
+ "option %s: invalid csv value: %r" % (opt, value))