summaryrefslogtreecommitdiff
path: root/pylint/utils.py
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2015-09-01 21:41:10 +0300
committerClaudiu Popa <pcmanticore@gmail.com>2015-09-01 21:41:10 +0300
commit3deb961d3583075e48fbdcb23cc2680fecf4329d (patch)
tree58463b1f59fae9c9f34ad1c3b94402387da2e609 /pylint/utils.py
parent34a33954d7845311fc99354ef282cbfdacda0245 (diff)
downloadpylint-git-3deb961d3583075e48fbdcb23cc2680fecf4329d.tar.gz
Bring parts of logilab.common.configuration and logilab.common.optik_ext into pylint.config
The reason behind this is that we can better control the behaviour of the underlying configuration modules, such as the case for undefined options or for quickly fixing other bugs. Another side effect of this change is that it gets us closer to the moment where we will not be dependent on logilab.common anymore, which will definitely make our pytest users happy. Some parts were copied almost verbatim from logilab.common.configuration and logilab.common.optik_ext and pylint.config will definitely need a refactoring and reengineering for abstracting the configuration, so that we won't use optparse anymore, but that's subject for another patch.
Diffstat (limited to 'pylint/utils.py')
-rw-r--r--pylint/utils.py94
1 files changed, 91 insertions, 3 deletions
diff --git a/pylint/utils.py b/pylint/utils.py
index c91878391..356868b50 100644
--- a/pylint/utils.py
+++ b/pylint/utils.py
@@ -30,7 +30,6 @@ 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.configuration import rest_format_section
from logilab.common.ureports import Section
from astroid import nodes, Module
@@ -456,7 +455,7 @@ class MessagesHandlerMixIn(object):
title = '%s options' % section.capitalize()
print(title)
print('~' * len(title))
- rest_format_section(sys.stdout, None, options)
+ _rest_format_section(sys.stdout, None, options)
print("")
else:
try:
@@ -491,7 +490,7 @@ class MessagesHandlerMixIn(object):
title = 'Options'
print(title)
print('^' * len(title))
- rest_format_section(sys.stdout, None, options)
+ _rest_format_section(sys.stdout, None, options)
print("")
if msgs:
title = 'Messages'
@@ -1048,3 +1047,92 @@ def _check_csv(value):
if isinstance(value, (list, tuple)):
return value
return _splitstrip(value)
+
+
+if six.PY2:
+ def _encode(string, encoding):
+ # pylint: disable=undefined-variable
+ if isinstance(string, unicode):
+ return string.encode(encoding)
+ return str(string)
+else:
+ def _encode(string, _):
+ return str(string)
+
+def _get_encoding(encoding, stream):
+ encoding = encoding or getattr(stream, 'encoding', None)
+ if not encoding:
+ import locale
+ encoding = locale.getpreferredencoding()
+ return encoding
+
+
+def _comment(string):
+ """return string as a comment"""
+ lines = [line.strip() for line in string.splitlines()]
+ return '# ' + ('%s# ' % os.linesep).join(lines)
+
+
+def _format_option_value(optdict, value):
+ """return the user input's value from a 'compiled' value"""
+ if isinstance(value, (list, tuple)):
+ value = ','.join(value)
+ elif isinstance(value, dict):
+ value = ','.join('%s:%s' % (k, v) for k, v in value.items())
+ elif hasattr(value, 'match'): # optdict.get('type') == 'regexp'
+ # compiled regexp
+ value = value.pattern
+ elif optdict.get('type') == 'yn':
+ value = value and 'yes' or 'no'
+ elif isinstance(value, six.string_types) and value.isspace():
+ value = "'%s'" % value
+ return value
+
+
+def _ini_format_section(stream, section, options, encoding=None, doc=None):
+ """format an options section using the INI format"""
+ encoding = _get_encoding(encoding, stream)
+ if doc:
+ print(_encode(_comment(doc), encoding), file=stream)
+ print('[%s]' % section, file=stream)
+ _ini_format(stream, options, encoding)
+
+
+def _ini_format(stream, options, encoding):
+ """format options using the INI format"""
+ for optname, optdict, value in options:
+ value = _format_option_value(optdict, value)
+ help = optdict.get('help')
+ if help:
+ help = _normalize_text(help, line_len=79, indent='# ')
+ print(file=stream)
+ print(_encode(help, encoding), file=stream)
+ else:
+ print(file=stream)
+ if value is None:
+ print('#%s=' % optname, file=stream)
+ else:
+ value = _encode(value, encoding).strip()
+ print('%s=%s' % (optname, value), file=stream)
+
+format_section = _ini_format_section
+
+
+def _rest_format_section(stream, section, options, encoding=None, doc=None):
+ """format an options section using as ReST formatted output"""
+ encoding = _get_encoding(encoding, stream)
+ if section:
+ print('%s\n%s' % (section, "'"*len(section)), file=stream)
+ if doc:
+ print(_encode(_normalize_text(doc, line_len=79, indent=''), encoding), file=stream)
+ print(file=stream)
+ for optname, optdict, value in options:
+ help = optdict.get('help')
+ print(':%s:' % optname, file=stream)
+ if help:
+ help = _normalize_text(help, line_len=79, indent=' ')
+ print(_encode(help, encoding), file=stream)
+ if value:
+ value = _encode(_format_option_value(optdict, value), encoding)
+ print(file=stream)
+ print(' Default: ``%s``' % value.replace("`` ", "```` ``"), file=stream)