示例#1
0
    def find_pep8_errors(cls, filename=None, lines=None):
        try:
            sys.stdout = cStringIO.StringIO()
            config = {}

            # Ignore long lines on test files, as the test names can get long
            # when following our test naming standards.
            if cls._is_test(filename):
                config['ignore'] = ['E501']

            checker = pycodestyle.Checker(filename=filename,
                                          lines=lines,
                                          **config)
            checker.check_all()
            output = sys.stdout.getvalue()
        finally:
            sys.stdout = sys.__stdout__

        errors = []
        for line in output.split('\n'):
            parts = line.split(' ', 2)
            if len(parts) == 3:
                location, error, desc = parts
                line_no = location.split(':')[1]
                errors.append('%s ln:%s %s' % (error, line_no, desc))
        return errors
示例#2
0
def pyls_lint(config, workspace, document):
    # Read config from all over the place
    config_files = config.find_parents(document.path, CONFIG_FILES)
    pycodestyle_conf = pyls_config.build_config('pycodestyle', config_files)
    pep8_conf = pyls_config.build_config('pep8', config_files)

    conf_to_use = pycodestyle_conf if pycodestyle_conf else pep8_conf

    conf = {k.replace("-", "_"): v for k, v in conf_to_use.items()}
    log.debug("Got pycodestyle config: %s", conf)

    # Grab the pycodestyle parser and set the defaults based on the config we found
    parser = pycodestyle.get_parser()
    parser.set_defaults(**conf)
    opts, _args = parser.parse_args([])
    styleguide = pycodestyle.StyleGuide(vars(opts))

    c = pycodestyle.Checker(filename=document.uri,
                            lines=document.lines,
                            options=styleguide.options,
                            report=PyCodeStyleDiagnosticReport(
                                styleguide.options))
    c.check_all()
    diagnostics = c.report.diagnostics
    return diagnostics
示例#3
0
    def test_pycodestyle(self):

        # NOTE(jecarey): Add tests marked as off_by_default to enable testing
        turn_on = set(['H106'])
        if self.options.select:
            turn_on.update(self.options.select)
        self.options.select = tuple(turn_on)
        self.options.ignore = ('N530', )

        report = pycodestyle.BaseReport(self.options)
        checker = pycodestyle.Checker(filename=self.filename,
                                      lines=self.lines,
                                      options=self.options,
                                      report=report)
        checker.check_all()
        self.addDetail('doctest', content.text_content(self.raw))
        if self.code == 'Okay':
            self.assertThat(
                len(report.counters),
                matchers.Not(
                    matchers.GreaterThan(len(self.options.benchmark_keys))),
                "incorrectly found %s" % ', '.join([
                    key for key in report.counters
                    if key not in self.options.benchmark_keys
                ]))
        else:
            self.addDetail(
                'reason',
                content.text_content("Failed to trigger rule %s" % self.code))
            self.assertIn(self.code, report.counters)
示例#4
0
def pylsp_lint(workspace, document):
    config = workspace._config
    settings = config.plugin_settings('pycodestyle',
                                      document_path=document.path)
    log.debug("Got pycodestyle settings: %s", settings)

    opts = {
        'exclude': settings.get('exclude'),
        'filename': settings.get('filename'),
        'hang_closing': settings.get('hangClosing'),
        'ignore': settings.get('ignore'),
        'max_line_length': settings.get('maxLineLength'),
        'indent_size': settings.get('indentSize'),
        'select': settings.get('select'),
    }
    kwargs = {k: v for k, v in opts.items() if v}
    styleguide = pycodestyle.StyleGuide(kwargs)

    c = pycodestyle.Checker(filename=document.uri,
                            lines=document.lines,
                            options=styleguide.options,
                            report=PyCodeStyleDiagnosticReport(
                                styleguide.options))
    c.check_all()
    diagnostics = c.report.diagnostics

    return diagnostics
示例#5
0
    def find_pep8_errors(cls, filename=None, lines=None):
        try:
            sys.stdout = cStringIO.StringIO()
            config = {
                'ignore': [
                    # W503/W504 - breaking before/after binary operators is agreed
                    # to not be a concern and was changed to be ignored by default.
                    # However we overwrite the ignore list here, so add it back in.
                    # See: https://github.com/PyCQA/pycodestyle/issues/498
                    'W503',
                    'W504',
                ]
            }

            # Ignore long lines on test files, as the test names can get long
            # when following our test naming standards.
            if cls._is_test(filename):
                config['ignore'].append('E501')

            checker = pycodestyle.Checker(filename=filename,
                                          lines=lines,
                                          **config)
            checker.check_all()
            output = sys.stdout.getvalue()
        finally:
            sys.stdout = sys.__stdout__

        errors = []
        for line in output.split('\n'):
            parts = line.split(' ', 2)
            if len(parts) == 3:
                location, error, desc = parts
                line_no = location.split(':')[1]
                errors.append('%s ln:%s %s' % (error, line_no, desc))
        return errors
示例#6
0
def getfunc(file, context):
    if not file:
        return []
    classes = []
    defs = []

    report = []
    failures = []
    linenumber = []
    character = []
    codestyleopts = pycodestyle.StyleGuide(ignore=ignores['pep8'],
                                           max_line_length=120,
                                           format='pylint').options
    report = StringReport(codestyleopts)
    failures = []
    checker = pycodestyle.Checker(file, options=codestyleopts, report=report)
    checker.check_all()
    report = checker.report
    if report.get_file_results() > 0:
        failures.extend(report.get_failures())
        failures.append('')

    for l in failures:
        print(l)
        if l.find(':') == 1:
            start = 'py:'
            end = ': '
            linenumber = l[l.find(start) + len(start):l.rfind(end)]
            end = '] '
            error = l[l.rfind(end) + 2:]
        character = l.find('^')
        if error and character > -1:
            classes.append([error.title(), linenumber, character])
    return classes
示例#7
0
 def test_local_check(self):
     flake8_style = engine.get_style_guide(parse_argv=False, ignore='F')
     report = pycodestyle.BaseReport(flake8_style.options)
     line = ["#this-is-the-test-phrase"]
     checker = pycodestyle.Checker(lines=line, options=flake8_style.options,
                                   report=report)
     checker.check_all()
     self.assertIn("L100", report.counters)
示例#8
0
    def run_check(self, code):
        pycodestyle.register_check(checks.check_oslo_namespace_imports)

        lines = textwrap.dedent(code).strip().splitlines(True)

        checker = pycodestyle.Checker(lines=lines)
        checker.check_all()
        checker.report._deferred_print.sort()
        return checker.report._deferred_print
示例#9
0
    def run_check(self, code, checker, filename=None):
        pycodestyle.register_check(checker)
        lines = textwrap.dedent(code).strip().splitlines(True)
        checker = pycodestyle.Checker(filename=filename, lines=lines)
        with mock.patch('pycodestyle.StandardReport.get_file_results'):
            checker.check_all()
        checker.report._deferred_print.sort()

        return checker.report._deferred_print
示例#10
0
def check_file_codestyle(path, ignore=None, max_line_length=100):
    ignore = ['E402', 'E731', 'W503', 'W504'] if ignore is None else ignore
    checker = pycodestyle.Checker(
        filename=str(path),
        ignore=ignore,
        max_line_length=max_line_length,
        show_source=True,
    )
    assert not checker.check_all(), "bad codestyle"
示例#11
0
    def _run_check(self, code, checker, filename=None):
        pycodestyle.register_check(checker)

        lines = textwrap.dedent(code).strip().splitlines(True)

        checker = pycodestyle.Checker(filename=filename, lines=lines)
        checker.check_all()
        checker.report._deferred_print.sort()
        return checker.report._deferred_print
示例#12
0
    def check_file(self, path):
        """
        Check one regular file with pylint for py syntax errors.

        :param path: Path to a regular file.
        :return: False, if pylint found syntax problems, True, if pylint didn't
                 find problems, or path is not a python module or script.
        """
        checker = PathChecker(path=path,
                              args=self.args,
                              label='Style',
                              logger=self.log)
        if not checker.check_attributes('text', 'python', 'not_empty'):
            return True

        try:
            opt_obj = pycodestyle.StyleGuide().options
            ignore_list = self.ignored_errors.split(',') + list(opt_obj.ignore)
            opt_obj.ignore = tuple(set(ignore_list))
            # pylint: disable=E1123
            runner = pycodestyle.Checker(filename=path, options=opt_obj)
            status = runner.check_all()
        except Exception:
            self.log.error('Unexpected exception while checking %s', path)
            exc_info = sys.exc_info()
            stacktrace.log_exc_info(exc_info, 'inspektor.style')
            status = 1

        if status != 0:
            self.failed_paths.append(path)
            fix_status = ''
            if AUTOPEP8_CAPABLE:
                if self.args.fix:
                    self.log.info('Trying to fix errors with autopep8')
                    try:
                        process.run(
                            'autopep8 --in-place --max-line-length=%s --ignore %s %s'
                            % (self.args.max_line_length, self.ignored_errors,
                               path))
                        fix_status = 'FIX OK'
                    except Exception:
                        self.log.error('Unable to fix errors')
                        exc_info = sys.exc_info()
                        stacktrace.log_exc_info(exc_info, 'inspektor.style')
                        fix_status = 'FIX NOT OK'
            else:
                self.log.error('Python library autopep8 not installed. '
                               'Please install it if you want to use --fix')
                fix_status = 'FIX NOT OK'
            checker.log_status(status='FAIL', extra=fix_status)
        else:
            checker.log_status(status='PASS')

        return status == 0
示例#13
0
def pycodestyle_check(filename):
    "run pycodestyle, return #errors and error string"

    pycodestyle_res = io.StringIO()

    sys.stdout = pycodestyle_res
    pycodestyle_errors = pycodestyle.Checker(filename).check_all()
    sys.stdout = sys.__stdout__

    res = pycodestyle_res.getvalue()

    return pycodestyle_errors, res
    def run_check(self, code):
        pycodestyle.register_check(self.get_checker())

        lines = textwrap.dedent(code).strip().splitlines(True)

        # Load all keystone hacking checks, they are of the form Kddd,
        # where ddd can from range from 000-999
        guide = pycodestyle.StyleGuide(select='K')
        checker = pycodestyle.Checker(lines=lines, options=guide.options)
        checker.check_all()
        checker.report._deferred_print.sort()
        return checker.report._deferred_print
示例#15
0
def check_code_for_debugger_statements(code):
    """Process code using pycodestyle Checker and return all errors."""
    from tempfile import NamedTemporaryFile

    test_file = NamedTemporaryFile(delete=False)
    test_file.write(code.encode())
    test_file.flush()
    report = CaptureReport(options=_debugger_test_style)
    lines = [line + '\n' for line in code.split('\n')]
    checker = pycodestyle.Checker(filename=test_file.name, lines=lines, options=_debugger_test_style, report=report)

    checker.check_all()
    return report._results
示例#16
0
    def _run_check(self, code, checker, filename=None):
        pycodestyle.register_check(checker)

        lines = textwrap.dedent(code).strip().splitlines(True)

        checker = pycodestyle.Checker(filename=filename, lines=lines)
        # NOTE(sdague): the standard reporter has printing to stdout
        # as a normal part of check_all, which bleeds through to the
        # test output stream in an unhelpful way. This blocks that printing.
        with mock.patch('pycodestyle.StandardReport.get_file_results'):
            checker.check_all()
        checker.report._deferred_print.sort()
        return checker.report._deferred_print
示例#17
0
 def runtest(self):
     # http://pycodestyle.pycqa.org/en/latest/api.html#pycodestyle.Checker
     # http://pycodestyle.pycqa.org/en/latest/advanced.html
     checker = pycodestyle.Checker(filename=str(self.fspath),
                                   options=self.options)
     file_errors, out, err = py.io.StdCapture.call(checker.check_all)
     if file_errors > 0:
         raise PyCodeStyleError(out)
     elif hasattr(self.config, 'cache'):
         # update cache
         # http://pythonhosted.org/pytest-cache/api.html
         cache = self.config.cache.get(self.CACHE_KEY, {})
         cache[str(self.fspath)] = self.fspath.mtime()
         self.config.cache.set(self.CACHE_KEY, cache)
    def process_module(self, node):
        style_checker = pycodestyle.Checker(node.stream().name)

        # catch the output of check_all() in pycodestyle
        with io.StringIO() as buf, redirect_stdout(buf):
            style_checker.check_all()
            output = buf.getvalue()

        # Handle the case of multiple error messages
        lst = output.split('\n')

        for line in lst:
            if line != '':
                line = line.split(':')
                self.add_message('pep8-errors', line=line[1], args=line[3])
示例#19
0
def lambda_handler(event, context):
    code = event.get('code', '')

    with open(TEMPFILE.name, 'w') as f:
        f.write(f'{code}\n')  # need to add final newline
        f.seek(0)  # rewind file

        out = StringIO()
        with redirect_stdout(out):
            pep = pycodestyle.Checker(f.name, show_source=True)
            pep.check_all()

        return {
            'statusCode': 200,  # = ok, lambda raises exceptions early
            'body': out.getvalue()
        }
示例#20
0
 def test_conformance(self):
     """Test that we conform to PEP-8."""
     style = pycodestyle.StyleGuide(quiet=True)
     path_f = []
     for d, dirs, files in os.walk('./src'):
         for f in files:
             if f.split('.')[1] == 'py' \
                     and not f.split('.')[0][0].isdigit() \
                     and f.split('.')[0] != 'base_settings' \
                     and f.split('.')[0] != 'production_settings' \
                     and f.split('.')[0] != 'local_settings':
                 path = os.path.join(d, f)
                 st = pycodestyle.Checker(filename=path, show_source=True)
                 print(st.check_all())
                 path_f.append(path)
     result = style.check_files(path_f)
     self.assertEqual(result.total_errors, 0,
                      "Found code style errors (warnings).")
示例#21
0
    def check(self, lines):
        from webkitpy.thirdparty.autoinstalled import pycodestyle

        def handler(line_number, offset, text, check):
            # Text is of the form 'E### <description of error>'
            code = text[:4]
            message = text[5:]
            category = "pycodestyle/" + code
            self._handle_style_error(
                line_number=line_number,
                category=category,
                confidence=5,
                message=message,
            )

        checker = pycodestyle.Checker(self._file_path)
        checker.report_error = handler
        checker.check_all()
    def _run_check(self, code, checker, filename=None):
        # We are patching pycodestyle (pep8) so that only the check under test
        # is actually installed.
        mock_checks = {'physical_line': {}, 'logical_line': {}, 'tree': {}}
        with mock.patch('pycodestyle._checks', mock_checks):
            pycodestyle.register_check(checker)

            lines = textwrap.dedent(code).strip().splitlines(True)

            checker = pycodestyle.Checker(filename=filename, lines=lines)
            # NOTE(sdague): the standard reporter has printing to stdout
            # as a normal part of check_all, which bleeds through to the
            # test output stream in an unhelpful way. This blocks that
            # printing.
            with mock.patch('pycodestyle.StandardReport.get_file_results'):
                checker.check_all()
            checker.report._deferred_print.sort()
            return checker.report._deferred_print
示例#23
0
def pyls_lint(config, document):
    # Read config from all over the place
    config_files = config.find_parents(document.path, CONFIG_FILES)
    if pycodestyle.USER_CONFIG:
        config_files = [pycodestyle.USER_CONFIG] + config_files
    pycodestyle_conf = pyls_config.build_config('pycodestyle', config_files)
    pep8_conf = pyls_config.build_config('pep8', config_files)

    conf_to_use = pycodestyle_conf if pycodestyle_conf else pep8_conf

    conf = {k.replace("-", "_"): v for k, v in conf_to_use.items()}

    # Grab the pycodestyle parser and set the defaults based on the config we found
    parser = pycodestyle.get_parser()
    parser.set_defaults(**conf)

    # Override with any options set in the language server config
    argv = []
    ls_conf = config.plugin_settings('pycodestyle')
    if ls_conf.get('exclude') is not None:
        argv.extend(['--exclude', ','.join(ls_conf['exclude'])])
    if ls_conf.get('filename') is not None:
        argv.extend(['--filename', ','.join(ls_conf['filename'])])
    if ls_conf.get('select') is not None:
        argv.extend(['--select', ','.join(ls_conf['select'])])
    if ls_conf.get('ignore') is not None:
        argv.extend(['--ignore', ','.join(ls_conf['ignore'])])
    if ls_conf.get('maxLineLength') is not None:
        argv.extend(['--max-line-length', str(ls_conf['maxLineLength'])])
    if ls_conf.get('hangClosing'):
        argv.extend(['--hang-closing'])

    opts, _args = parser.parse_args(argv)
    log.debug("Got pycodestyle config: %s", opts)
    styleguide = pycodestyle.StyleGuide(vars(opts))

    c = pycodestyle.Checker(filename=document.uri,
                            lines=document.lines,
                            options=styleguide.options,
                            report=PyCodeStyleDiagnosticReport(
                                styleguide.options))
    c.check_all()
    diagnostics = c.report.diagnostics
    return diagnostics
示例#24
0
 def runtest(self):
     # http://pycodestyle.pycqa.org/en/latest/api.html#pycodestyle.Checker
     # http://pycodestyle.pycqa.org/en/latest/advanced.html
     checker = pycodestyle.Checker(
                     filename=str(self.fspath),
                     max_line_length=int(self.config.getini('codestyle_max_line_length')),
                     select=self.config.getini('codestyle_select'),
                     ignore=self.config.getini('codestyle_ignore'),
                     show_source=self.config.getini('codestyle_show_source')
                 )
     file_errors, out, err = py.io.StdCapture.call(checker.check_all)
     if file_errors > 0:
         raise CodeStyleError(out)
     else:
         # update cache
         # http://pythonhosted.org/pytest-cache/api.html
         cache = self.config.cache.get(self.CACHE_KEY, {})
         cache[str(self.fspath)] = self.fspath.mtime()
         self.config.cache.set(self.CACHE_KEY, cache)
示例#25
0
    def check_file(self, path):
        """
        Check one regular file with pylint for py syntax errors.

        :param path: Path to a regular file.
        :return: False, if pylint found syntax problems, True, if pylint didn't
                 find problems, or path is not a python module or script.
        """
        inspector = PathInspector(path=path, args=self.args)
        if inspector.is_toignore():
            return True
        if not inspector.is_python():
            return True
        try:
            opt_obj = pycodestyle.StyleGuide().options
            ignore_list = self.args.disable.split(',') + list(opt_obj.ignore)
            opt_obj.ignore = tuple(set(ignore_list))
            # pylint: disable=E1123
            runner = pycodestyle.Checker(filename=path, options=opt_obj)
            status = runner.check_all()
        except Exception:
            log.error('Unexpected exception while checking %s', path)
            exc_info = sys.exc_info()
            stacktrace.log_exc_info(exc_info, 'inspektor.style')
            status = 1

        if status != 0:
            log.error('Style check fail: %s', path)
            self.failed_paths.append(path)
            if AUTOPEP8_CAPABLE:
                if self.args.fix:
                    log.info('Trying to fix errors with autopep8')
                    try:
                        process.run(
                            'autopep8 --in-place --max-line-length=%s --ignore %s %s'
                            % (self.args.max_line_length, self.args.disable,
                               path))
                    except Exception:
                        log.error('Unable to fix errors')
                        exc_info = sys.exc_info()
                        stacktrace.log_exc_info(exc_info, 'inspektor.style')

        return status == 0
示例#26
0
    def test_pep8(self):
        pep8opts = pycodestyle.StyleGuide(
            ignore=ignores['pep8'],
            max_line_length=100,
            format='pylint'
        ).options

        report = StringReport(pep8opts)
        failures = []
        for file in _get_files():
            checker = pycodestyle.Checker(file, options=pep8opts, report=report)
            checker.check_all()
            report = checker.report
            if report.get_file_results() > 0:
                failures.extend(report.get_failures())
                failures.append('')

        if failures:
            self.fail('{} PEP 8 Failure(s):\n'.format(report.total_errors) + '\n'.join(failures))
示例#27
0
    def _check_pycodestyle(self, lines):
        # Initialize pycodestyle.options, which is necessary for
        # Checker.check_all() to execute.
        pycodestyle.process_options(arglist=[self._file_path])

        pycodestyle_checker = pycodestyle.Checker(self._file_path)

        def _pycodestyle_handle_error(line_number, offset, text, check):
            # FIXME: Incorporate the character offset into the error output.
            #        This will require updating the error handler __call__
            #        signature to include an optional "offset" parameter.
            pycodestyle_code = text[:4]
            pycodestyle_message = text[5:]

            category = "pep8/" + pycodestyle_code

            self._handle_style_error(line_number, category, 5, pycodestyle_message)

        pycodestyle_checker.report_error = _pycodestyle_handle_error
        pycodestyle_errors = pycodestyle_checker.check_all()
示例#28
0
    def process_module(self, aStream):
        """process a module."""
        self.oChecker = pycodestyle.Checker(None)
        # Handle recent pylint changes
        aLines = []
        if hasattr(aStream, 'file_stream'):
            aStream = aStream.file_stream
            # Only needed for older pylints
            aStream.seek(0)
            aLines = list(aStream)
        elif hasattr(aStream, 'stream'):
            with aStream.stream() as aData:
                for _iLineNo, sLine in enumerate(aData):
                    aLines.append(sLine)
        else:
            aLines = list(aStream)

        self.oChecker.lines = aLines
        self.oChecker.report_error = self._transform_error
        self.oChecker.check_all()
示例#29
0
 def check(self, name, content):
     lines = content.splitlines(True)
     # workaround - gedit always adds \n to the last line on save
     if content.endswith('\n'):
         lines.append('')
     else:
         lines[-1] = lines[-1] + '\n'
     # issue #6 - skip noqa
     lines = [
         line if not (
             line.strip().endswith('# noqa')
             or line.strip().endswith('# flake8: noqa')
         ) else '#'
         for line in lines
     ]
     old_stderr, sys.stderr = sys.stderr, StringIO()
     old_stdout, sys.stdout = sys.stdout, StringIO()
     try:
         pycodestyle.Checker(
             name,
             lines=lines,
             options=self.options
         ).check_all()
     except:
         pass
     finally:
         sys.stderr = old_stderr
         sys.stdout, result = old_stdout, sys.stdout
     result.seek(0)
     errors = [
         rePycodestyle.match(line)
         for line in result.readlines()
         if line
     ]
     errors = [e for e in errors if e]
     for match in sorted(errors, key=lambda x: x.group(2)):
         lineno = int(match.group(2))
         text = match.group(5)
         col = int(match.group(3) or -1)
         err_type = match.group(4)
         yield Message(STYLE, err_type, lineno, text, col=col)
示例#30
0
    def __format_check(self, file):
        """
           Check format using pycodestyle for python source codes
        """
        # only check python file
        if file.language() == "python":
            for m in GPS.Message.list(category="Pep8"):
                if m.get_file() == file:
                    m.remove()

            # is buffer opened yet
            buf = GPS.EditorBuffer.get(file=file, open=False)
            if buf is None:
                return

            # parse text in buffer and catches stdout
            s = buf.get_chars()
            pref = GPS.Preference("Src-Editor-Strip-Trailing-Blanks")
            if (pref.get() != "Never"):
                source = [i.rstrip(" ") + "\n" for i in s.splitlines()]
            else:
                source = [i + "\n" for i in s.splitlines()]

            with Catch_Stdout() as output:
                m = pycodestyle.Checker(filename=None,
                                        lines=source,
                                        report=False)
                m.check_all()

            for i in output:
                a = i.split(":")
                m = GPS.Message(category="Pep8",
                                file=GPS.EditorBuffer.get(file=file,
                                                          open=False).file(),
                                line=int(a[1]),
                                column=int(a[2]),
                                text=a[3],
                                show_in_locations=False)

                m.set_action("", "gps-emblem-build-warning", m.get_text())
                m.set_style(colorschemes.STYLE_WARNING, 1)