def is_pep8_available():
"""
Checks if pep8 is availalbe.
:returns: **True** if we can use pep8 and **False** otherwise
"""
try:
import pep8
if not hasattr(pep8, 'BaseReport'):
raise ImportError()
return True
except ImportError:
return False
python类BaseReport()的实例源码
def is_pep8_available():
"""
Checks if pep8 is availalbe.
:returns: **True** if we can use pep8 and **False** otherwise
"""
try:
import pep8
if not hasattr(pep8, 'BaseReport'):
raise ImportError()
return True
except ImportError:
return False
def is_pep8_available():
"""
Checks if pep8 is availalbe.
:returns: **True** if we can use pep8 and **False** otherwise
"""
try:
import pep8
if not hasattr(pep8, 'BaseReport'):
raise ImportError()
return True
except ImportError:
return False
def _execute_pep8(pep8_options, source):
"""Execute pep8 via python method calls."""
class QuietReport(pep8.BaseReport):
"""Version of checker that does not print."""
def __init__(self, options):
super(QuietReport, self).__init__(options)
self.__full_error_results = []
def error(self, line_number, offset, text, _):
"""Collect errors."""
code = super(QuietReport, self).error(line_number, offset, text, _)
if code:
self.__full_error_results.append(
{'id': code,
'line': line_number,
'column': offset + 1,
'info': text})
def full_error_results(self):
"""Return error results in detail.
Results are in the form of a list of dictionaries. Each
dictionary contains 'id', 'line', 'column', and 'info'.
"""
return self.__full_error_results
checker = pep8.Checker('', lines=source,
reporter=QuietReport, **pep8_options)
checker.check_all()
return checker.report.full_error_results()
def _pep8_annotations(text, ignore=None, max_line_length=None):
import pep8
class _Pep8AnnotationReport(pep8.BaseReport):
def __init__(self, options):
super().__init__(options)
self.annotations = []
def error(self, line_number, offset, text, check):
# If super doesn't return code, this one is ignored
if not super().error(line_number, offset, text, check):
return
annotation = _AnalyzerAnnotation(self.line_offset + line_number, text, _Source.pep8, Style.warning)
self.annotations.append(annotation)
# pep8 requires you to include \n at the end of lines
lines = text.splitlines(True)
style_guide = pep8.StyleGuide(reporter=_Pep8AnnotationReport, )
options = style_guide.options
if ignore:
options.ignore = tuple(ignore)
else:
options.ignore = tuple()
if max_line_length:
options.max_line_length = max_line_length
checker = pep8.Checker(None, lines, options, None)
checker.check_all()
return checker.report.annotations
#
# pyflakes
#
def run(self, settings, **options):
output = open(os.path.join(options[CI.OUTPUT_DIR], Reports.PEP8_REPORT), 'w')
class JenkinsReport(pep8.BaseReport):
def error(self, line_number, offset, text, check):
code = super(JenkinsReport, self).error(line_number, offset, text, check)
if code:
source_line = self.line_offset + line_number
output.write('%s:%s:%s: %s\n' % (self.filename, source_line, offset + 1, text))
pep8_options = {}
config_file = self.get_config_path(settings, options)
if config_file is not None:
pep8_options[Pep8.CONFIG_FILE] = config_file
pep8_options[Pep8.MAX_LINE_LENGTH] = options[Pep8.PEP8_MAX_LINE_LENGTH]
pep8style = pep8.StyleGuide(
parse_argv=False,
reporter=JenkinsReport,
**pep8_options)
pep8style.options.report.start()
for location in getattr(settings, Settings.PROJECT_APPS, []):
pep8style.input_dir(os.path.relpath(location))
pep8style.options.report.stop()
output.close()
def _execute_pep8(pep8_options, source):
"""Execute pep8 via python method calls."""
class QuietReport(pep8.BaseReport):
"""Version of checker that does not print."""
def __init__(self, options):
super(QuietReport, self).__init__(options)
self.__full_error_results = []
def error(self, line_number, offset, text, check):
"""Collect errors."""
code = super(QuietReport, self).error(line_number,
offset,
text,
check)
if code:
self.__full_error_results.append(
{'id': code,
'line': line_number,
'column': offset + 1,
'info': text})
def full_error_results(self):
"""Return error results in detail.
Results are in the form of a list of dictionaries. Each
dictionary contains 'id', 'line', 'column', and 'info'.
"""
return self.__full_error_results
checker = pep8.Checker('', lines=source,
reporter=QuietReport, **pep8_options)
checker.check_all()
return checker.report.full_error_results()