예제 #1
0
    def parse(self, errors, explicit_ignore):
        """Parse the errors returned from the PyFlakes library
        """

        error_list = []
        if errors is None:
            return error_list

        errors.sort(key=linter.cmp_to_key(lambda a, b: a.lineno < b.lineno))
        for error in errors:
            error_level = 'W' if not hasattr(error, 'level') else error.level
            message = error.message.capitalize()

            error_data = {
                'underline_range': False,
                'level': error_level,
                'lineno': error.lineno,
                'message': message,
                'raw_error': str(error)
            }
            if hasattr(error, 'offset'):
                error_data['offset'] = error.offset
            elif hasattr(error, 'col'):
                error_data['offset'] = error.col

            if (isinstance(error, (linter.OffsetError))):
                error_data['underline_range'] = True
                error_list.append(error_data)
            elif (isinstance(error, (pyflakes.messages.RedefinedWhileUnused,
                                     pyflakes.messages.UndefinedName,
                                     pyflakes.messages.UndefinedExport,
                                     pyflakes.messages.UndefinedLocal,
                                     pyflakes.messages.UnusedVariable))
                  and error.__class__.__name__ not in explicit_ignore):

                error_data['len'] = len(error.message_args[0])
                error_data['regex'] = (
                    r'((and|or|not|if|elif|while|in)\s+|[+\-*^%%<>=\(\{{])*\s'
                    '*(?P<underline>[\w\.]*{0}[\w]*)'.format(
                        re.escape(error.message_args[0])))
                error_list.append(error_data)
            elif isinstance(error, pyflakes.messages.ImportShadowedByLoopVar):
                regex = 'for\s+(?P<underline>[\w]*{0}[\w*])'.format(
                    re.escape(error.message_args[0]))
                error_data['regex'] = regex
                error_list.append(error_data)
            elif (isinstance(error, (pyflakes.messages.UnusedImport,
                                     pyflakes.messages.ImportStarUsed))
                  and error.__class__.__name__ not in explicit_ignore):
                if isinstance(error, pyflakes.messages.ImportStarUsed):
                    word = '*'
                else:
                    word = error.message_args[0]

                linematch = '(from\s+[\w_\.]+\s+)?import\s+(?P<match>[^#;]+)'
                r = '(^|\s+|,\s*|as\s+)(?P<underline>[\w]*{0}[\w]*)'.format(
                    re.escape(word))
                error_data['regex'] = r
                error_data['linematch'] = linematch
                error_list.append(error_data)
            elif (isinstance(error, pyflakes.messages.DuplicateArgument)
                  and error.__class__.__name__ not in explicit_ignore):
                regex = 'def [\w_]+\(.*?(?P<underline>[\w]*{0}[\w]*)'.format(
                    re.escape(error.message_args[0]))
                error_data['regex'] = regex
                error_list.append(error_data)
            elif isinstance(error, pyflakes.messages.LateFutureImport):
                pass
            elif isinstance(error, linter.PythonError):
                print(error)
            else:
                print('Ooops, we missed an error type for pyflakes',
                      type(error))

        return error_list
    def parse(self, errors, explicit_ignore):
        """Parse the errors returned from the PyFlakes library
        """

        error_list = []
        if errors is None:
            return error_list

        errors.sort(key=linter.cmp_to_key(lambda a, b: a.lineno < b.lineno))
        for error in errors:
            error_level = 'W' if not hasattr(error, 'level') else error.level
            message = error.message.capitalize()

            error_data = {
                'underline_range': False,
                'level': error_level,
                'lineno': error.lineno,
                'message': message,
                'raw_error': str(error)
            }
            if hasattr(error, 'offset'):
                error_data['offset'] = error.offset
            elif hasattr(error, 'col'):
                error_data['offset'] = error.col

            if (isinstance(error, (linter.OffsetError))):
                error_data['underline_range'] = True
                error_list.append(error_data)
            elif (isinstance(
                error, (
                    pyflakes.messages.RedefinedWhileUnused,
                    pyflakes.messages.UndefinedName,
                    pyflakes.messages.UndefinedExport,
                    pyflakes.messages.UndefinedLocal,
                    pyflakes.messages.Redefined,
                    pyflakes.messages.UnusedVariable)) and
                    error.__class__.__name__ not in explicit_ignore):

                error_data['len'] = len(error.message_args[0])
                error_data['regex'] = (
                    r'((and|or|not|if|elif|while|in)\s+|[+\-*^%%<>=\(\{{])*\s'
                    '*(?P<underline>[\w\.]*{0}[\w]*)'.format(re.escape(
                        error.message_args[0]
                    ))
                )
                error_list.append(error_data)
            elif isinstance(error, pyflakes.messages.ImportShadowedByLoopVar):
                regex = 'for\s+(?P<underline>[\w]*{0}[\w*])'.format(
                    re.escape(error.message_args[0])
                )
                error_data['regex'] = regex
                error_list.append(error_data)
            elif (isinstance(
                error, (
                    pyflakes.messages.UnusedImport,
                    pyflakes.messages.ImportStarUsed)) and
                    error.__class__.__name__ not in explicit_ignore):
                if isinstance(error, pyflakes.messages.ImportStarUsed):
                    word = '*'
                else:
                    word = error.message_args[0]

                linematch = '(from\s+[\w_\.]+\s+)?import\s+(?P<match>[^#;]+)'
                r = '(^|\s+|,\s*|as\s+)(?P<underline>[\w]*{0}[\w]*)'.format(
                    re.escape(word)
                )
                error_data['regex'] = r
                error_data['linematch'] = linematch
                error_list.append(error_data)
            elif (isinstance(error, pyflakes.messages.DuplicateArgument) and
                    error.__class__.__name__ not in explicit_ignore):
                regex = 'def [\w_]+\(.*?(?P<underline>[\w]*{0}[\w]*)'.format(
                    re.escape(error.message_args[0])
                )
                error_data['regex'] = regex
                error_list.append(error_data)
            elif isinstance(error, pyflakes.messages.LateFutureImport):
                pass
            elif isinstance(error, linter.PythonError):
                print(error)
            else:
                print(
                    'Ooops, we missed an error type for pyflakes', type(error)
                )

        return error_list