Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def run_linter(self, settings, code, filename):
"""Check the code to find errors
"""
errors = []
if settings.get("pep8", True):
check_params = {
'ignore': settings.get('pep8_ignore', []),
'max_line_length': settings.get(
'pep8_max_line_length', pep8.MAX_LINE_LENGTH
)
}
errors.extend(self.pep8_check(
code, filename, settings.get('pep8_rcfile'), **check_params
))
pyflakes_ignore = settings.get('pyflakes_ignore', None)
pyflakes_disabled = settings.get('pyflakes_disabled', False)
explicit_ignore = settings.get('pyflakes_explicit_ignore', [])
if not pyflakes_disabled and not settings.get('use_pylint'):
errors.extend(self.pyflakes_check(code, filename, pyflakes_ignore))
return self.parse_errors(errors, explicit_ignore)
def add_arguments(self, parser):
parser.add_argument("--pep8-exclude",
dest="pep8-exclude",
help="exclude files or directories which match these "
"comma separated patterns (default: %s)" %
(pep8.DEFAULT_EXCLUDE + ",south_migrations"))
parser.add_argument("--pep8-select", dest="pep8-select",
help="select errors and warnings (e.g. E,W6)")
parser.add_argument("--pep8-ignore", dest="pep8-ignore",
help="skip errors and warnings (e.g. E4,W)"),
parser.add_argument("--pep8-max-line-length",
dest="pep8-max-line-length", type=int,
help="set maximum allowed line length (default: %d)" % pep8.MAX_LINE_LENGTH)
parser.add_argument("--pep8-rcfile", dest="pep8-rcfile",
help="PEP8 configuration file")
def output(self):
stdout, sys.stdout = sys.stdout, StringIO.StringIO()
try:
import pep8
pep8.MAX_LINE_LENGTH = self.max_line_length or 79
pep8style = pep8.StyleGuide(parse_argv=False, config_file=True)
options = pep8style.options
ignored = self.get_ignored_ids()
if ignored is not None:
options.ignore.append(ignored)
if options.doctest or options.testsuite:
from testsuite.support import run_tests
report = run_tests(pep8style)
else:
report = pep8style.input_file(self.path)
if options.testsuite and not options.quiet:
report.print_results()
return sys.stdout.getvalue().strip()
dest='flake8-max-complexity',
type=int,
help='McCabe complexity treshold')
parser.add_argument("--pep8-exclude",
dest="pep8-exclude",
help="exclude files or directories which match these "
"comma separated patterns (default: %s)" %
(pep8.DEFAULT_EXCLUDE + ",south_migrations"))
parser.add_argument("--pep8-select", dest="pep8-select",
help="select errors and warnings (e.g. E,W6)")
parser.add_argument("--pep8-ignore", dest="pep8-ignore",
help="skip errors and warnings (e.g. E4,W)")
parser.add_argument("--pep8-max-line-length",
dest="pep8-max-line-length", type=int,
help="set maximum allowed line length (default: %d)" %
pep8.MAX_LINE_LENGTH)
parser.add_argument("--pep8-rcfile", dest="pep8-rcfile",
help="PEP8 configuration file")
pep8_options = {}
config_file = self.get_config_path(options)
if config_file is not None:
pep8_options['config_file'] = config_file
set_option(pep8_options, 'exclude', options['pep8-exclude'], config_file,
default=pep8.DEFAULT_EXCLUDE + ",south_migrations", split=',')
set_option(pep8_options, 'select', options['pep8-select'], config_file, split=',')
set_option(pep8_options, 'ignore', options['pep8-ignore'], config_file, split=',')
set_option(pep8_options, 'max_line_length', options['pep8-max-line-length'], config_file,
default=pep8.MAX_LINE_LENGTH)
set_option(pep8_options, 'max_complexity', options['flake8-max-complexity'], config_file,
default=-1)
old_stdout, flake8_output = sys.stdout, StringIO()
sys.stdout = flake8_output
pep8style = get_style_guide(
parse_argv=False,
jobs='1',
**pep8_options)
try:
for location in apps_locations:
pep8style.input_file(os.path.relpath(location))
finally:
def run_pep8(request_data):
"""
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,
output.write('%s:%s:%s: %s\n' % (instance.filename, sourceline, offset + 1, text))
pep8_options = {}
config_file = self.get_config_path(options)
if config_file is not None:
pep8_options['config_file'] = config_file
set_option(pep8_options, 'exclude', options['pep8-exclude'], config_file,
default=pep8.DEFAULT_EXCLUDE + ",south_migrations", split=',')
set_option(pep8_options, 'select', options['pep8-select'], config_file, split=',')
set_option(pep8_options, 'ignore', options['pep8-ignore'], config_file, split=',')
set_option(pep8_options, 'max_line_length', options['pep8-max-line-length'], config_file,
default=pep8.MAX_LINE_LENGTH)
pep8style = pep8.StyleGuide(
parse_argv=False,
reporter=JenkinsReport,
**pep8_options)
pep8style.options.report.start()
for location in apps_locations:
pep8style.input_dir(os.path.relpath(location))
pep8style.options.report.stop()
output.close()
def add_arguments(cls, parser):
parser.add_argument("--pep8-exclude",
dest="pep8-exclude",
default=pep8.DEFAULT_EXCLUDE + ",migrations",
help="exclude files or directories which match these "
"comma separated patterns (default: %s)" %
pep8.DEFAULT_EXCLUDE)
parser.add_argument("--pep8-select", dest="pep8-select",
help="select errors and warnings (e.g. E,W6)")
parser.add_argument("--pep8-ignore", dest="pep8-ignore",
help="skip errors and warnings (e.g. E4,W)")
parser.add_argument("--pep8-max-line-length",
dest="pep8-max-line-length", type=int,
help="set maximum allowed line length (default: %d)" %
pep8.MAX_LINE_LENGTH)
parser.add_argument("--pep8-rcfile", dest="pep8-rcfile",
help="PEP8 configuration file")
def _validate_pep8(fd, options={}):
import pep8
# if user doesn't define a new value use the pep8 default
max_line_length = options.get('max_line_length', pep8.MAX_LINE_LENGTH)
pep8style = pep8.StyleGuide(max_line_length=max_line_length)
check = pep8style.input_file(fd.name)
return check == 0