Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
try:
import pep8
has_pep8 = True
except ImportError:
if options.with_pep8:
sys.stderr.write('# Could not find pep8 library.')
sys.exit(1)
if has_pep8:
guide_main = pep8.StyleGuide(
ignore=[],
paths=['bonzo/'],
exclude=[],
max_line_length=80,
)
guide_tests = pep8.StyleGuide(
ignore=['E221'],
paths=['tests/'],
max_line_length=80,
)
for guide in (guide_main, guide_tests):
report = guide.check_files()
if report.total_errors:
sys.exit(1)
suite = make_suite('', tuple(extra_args), options.force_all)
runner = TextTestRunner(verbosity=options.verbosity - options.quietness + 1)
result = runner.run(suite)
sys.exit(not result.wasSuccessful())
def test_pep8(self):
basepath = os.getcwd()
print 'Running PEP-8 checks on path', basepath
path_list = [basepath + '/dssg', basepath + '/test']
pep8style = pep8.StyleGuide(paths=path_list, ignore=['E128', 'E501'])
report = pep8style.check_files()
if report.total_errors:
print report.total_errors
self.assertEqual(
report.total_errors, 0, 'Codebase does not pass PEP-8')
def test_pep8_conformance(self):
cur_dir_path = os.path.dirname(__file__)
root_path = os.path.join(cur_dir_path, os.pardir)
config_path = os.path.join(cur_dir_path, 'pep8_config')
pep8_style = pep8.StyleGuide(
paths=[root_path],
config_file=config_path)
result = pep8_style.check_files()
self.assertEqual(
result.total_errors, 0,
'Found {:d} code style errors/warnings in {}!'.format(
result.total_errors, root_path))
def test_pep8(self):
pep8style = pep8.StyleGuide([['statistics', True],
['show-sources', True],
['repeat', True],
['ignore', "E501"],
['paths', [os.path.dirname(
os.path.abspath(__file__))]]],
parse_argv=False)
report = pep8style.check_files()
assert report.total_errors == 0
def run_check(self, path):
"""Common method to run the pep8 test."""
pep8style = pep8.StyleGuide()
result = pep8style.check_files(paths=[path])
if result.total_errors != 0:
self.assertEqual(
result.total_errors, 0,
"Found code style errors (and warnings).")
def test_pep8(self):
self.pep8style = pep8.StyleGuide(show_source=True)
files = ('after_hours.py', 'after_hours_unittests.py')
self.pep8style.check_files(files)
self.assertEqual(self.pep8style.options.report.total_errors, 0)
for filename, codes in exception_lists:
exceptions[filename] = codes
def get_exceptions(f):
try:
ignore = exceptions[f]
except KeyError:
ignore = None
return ignore
if args.update:
print "Starting update of %d files" % len(manifest)
bad = []
for f in manifest:
checker = pep8.StyleGuide(quiet=True, ignore=get_exceptions(f))
results = checker.check_files([f])
if results.total_errors:
bad.append(f)
print "%s: %s" % (results.total_errors and "FAIL" or "PASS", f)
with file(blacklist_filename, "w") as fh:
print >>fh, """\
# cpep8.blacklist: The list of files that do not meet PEP8 standards.
# DO NOT ADD NEW FILES!! Instead, fix the code to be compliant.
# Over time, this list should shrink and (eventually) be eliminated."""
print >>fh, "\n".join(sorted(bad))
if args.scan:
with file(manifest_filename, "w") as fh:
print >>fh, "\n".join(sorted(manifest))
sys.exit(0)
Worker that run the pep8 tool on the current editor text.
:returns a list of tuples (msg, msg_type, line_number)
"""
import pep8
from pyqode.python.backend.pep8utils import CustomChecker
WARNING = 1
code = request_data['code']
path = request_data['path']
max_line_length = request_data['max_line_length']
ignore_rules = request_data['ignore_rules']
ignore_rules += ['W291', 'W292', 'W293', 'W391']
pep8.MAX_LINE_LENGTH = max_line_length
# setup our custom style guide with our custom checker which returns a list
# of strings instread of spitting the results at stdout
pep8style = pep8.StyleGuide(parse_argv=False, config_file='',
checker_class=CustomChecker)
try:
results = pep8style.input_file(path, lines=code.splitlines(True))
except Exception:
_logger().exception('Failed to run PEP8 analysis with data=%r'
% request_data)
return []
else:
messages = []
for line_number, offset, code, text, doc in results:
if code in ignore_rules:
continue
messages.append(('[PEP8] %s: %s' % (code, text), WARNING,
line_number - 1))
return messages
def check_pep8(files):
print(">>> Running pep8...")
sg = pep8.StyleGuide(parse_argv=False, config_file=False)
sg.options.repeat = True
sg.options.show_pep8 = True
report = sg.check_files(files)
if report.total_errors:
raise Exception("ERROR: pep8 failed on some source files")