Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def pep8_error_count(path):
# process_options initializes some data structures and MUST be called before each Checker().check_all()
pep8.process_options(['pep8', '--ignore=E202,E221,E222,E241,E301,E302,E401,E501,E701,W391,W601,W602', '--show-source', 'dummy_path'])
error_count = pep8.Checker(path).check_all()
return error_count
def testPEP8Compliance(self):
# Ensure PEP8 is installed
try:
import pep8
except ImportError:
self.fail(msg="PEP8 not installed.")
# Check the CAN driver
basedriver = os.path.abspath('pycan/basedriver.py')
pep8_checker = pep8.Checker(basedriver)
violation_count = pep8_checker.check_all()
error_message = "PEP8 violations found: %d" % (violation_count)
self.assertTrue(violation_count == 0, msg = error_message)
# Check the file for errors with PEP8
sys.argv = [path]
pep8.process_options([path])
python_utils_conf = kate.configuration.root.get('python_utils', {})
ignore_pep8_errors = python_utils_conf.get(_IGNORE_PEP8_ERRORS, DEFAULT_IGNORE_PEP8_ERRORS)
if ignore_pep8_errors:
ignore_pep8_errors = ignore_pep8_errors.split(",")
else:
ignore_pep8_errors = []
if pep8.__version__ in OLD_PEP8_VERSIONS:
checker = StoreErrorsChecker(path)
pep8.options.ignore = ignore_pep8_errors
checker.check_all()
errors = checker.get_errors()
else:
checker = pep8.Checker(path, reporter=KateReport, ignore=ignore_pep8_errors)
checker.check_all()
errors = checker.report.get_errors()
if len(errors) == 0:
showOk(i18n('Pep8 Ok'))
return
errors_to_show = []
# Paint errors found
for error in errors:
errors_to_show.append({
"line": error[0],
"column": error[1] + 1,
"message": error[3],
})
showErrors(i18n('Pep8 Errors:'),
errors_to_show,
mark_key,
_ignore = ignore + pep8.DEFAULT_IGNORE.split(',')
params['ignore'] = _ignore
else:
params['config_file'] = os.path.expanduser(rcfile)
options = pep8.StyleGuide(**params).options
if not rcfile:
options.max_line_length = max_line_length
good_lines = [l + '\n' for l in _lines]
good_lines[-1] = good_lines[-1].rstrip('\n')
if not good_lines[-1]:
good_lines = good_lines[:-1]
pep8.Checker(filename, good_lines, options=options).check_all()
return messages
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 input_file(filename):
"""
Run all checks on a Python source file.
"""
if excluded(filename):
return {}
if options.verbose:
message('checking ' + filename)
files_counter_before = options.counters.get('files', 0)
if options.testsuite: # Keep showing errors for multiple tests
options.counters = {}
options.counters['files'] = files_counter_before + 1
errors = Checker(filename).check_all()
if options.testsuite: # Check if the expected error was found
basename = os.path.basename(filename)
code = basename[:4]
count = options.counters.get(code, 0)
if count == 0 and 'not' not in basename:
message("%s: error %s not found" % (filename, code))
def check_text(text, temp_dir, logger=None):
"""
check text for pep8 requirements
"""
#prepare code
code_file, code_filename = tempfile.mkstemp(dir=temp_dir)
with open(code_filename, 'w') as code_file:
code_file.write(text.encode('utf8'))
#initialize pep8 checker
pep8style = pep8.StyleGuide(parse_argv=False, config_file=False)
options = pep8style.options
#redirect print and get result
temp_outfile = StringIO.StringIO()
sys.stdout = temp_outfile
checker = pep8.Checker(code_filename, options=options)
checker.check_all()
sys.stdout = sys.__stdout__
result = temp_outfile.buflist[:]
#clear all
temp_outfile.close()
code_file.close()
os.remove(code_filename)
fullResultList = pep8parser(result)
fullResultList.sort(key=lambda x: (int(x['line']), int(x["place"])))
if logger:
logger.debug(result)
return fullResultList
parser.remove_option(opt)
except ValueError:
pass
parser.add_option('--exit-zero', action='store_true',
help="exit with code 0 even if there are errors")
for parser_hook in parser_hooks:
parser_hook(parser)
parser.add_option('--install-hook', default=False, action='store_true',
help='Install the appropriate hook for this '
'repository.', dest='install_hook')
return parser, options_hooks
class StyleGuide(pep8.StyleGuide):
# Backward compatibility pep8 <= 1.4.2
checker_class = pep8.Checker
def input_file(self, filename, lines=None, expected=None, line_offset=0):
"""Run all checks on a Python source file."""
if self.options.verbose:
print('checking %s' % filename)
fchecker = self.checker_class(
filename, lines=lines, options=self.options)
# Any "# flake8: noqa" line?
if any(_flake8_noqa(line) for line in fchecker.lines):
return 0
return fchecker.check_all(expected=expected, line_offset=line_offset)
def get_style_guide(**kwargs):
"""Parse the options and configure the checker. This returns a sub-class
of ``pep8.StyleGuide``."""