Esempio n. 1
0
def get_code_complexity(code, min=7, filename='stdin'):
    complex = []
    try:
        ast = parse(code)
    except (AttributeError, SyntaxError):
        e = sys.exc_info()[1]
        sys.stderr.write("Unable to parse %s: %s\n" % (filename, e))
        return 0

    visitor = PathGraphingAstVisitor()
    visitor.preorder(ast, visitor)
    for graph in visitor.graphs.values():
        if graph is None:
            # ?
            continue
        if graph.complexity() >= min:
            graph.filename = filename
            if not skip_warning(graph):
                msg = '%s:%d:1: %s %r is too complex (%d)' % (
                    filename,
                    graph.lineno,
                    WARNING_CODE,
                    graph.entity,
                    graph.complexity(),
                )
                complex.append(msg)

    if len(complex) == 0:
        return 0

    print('\n'.join(complex))
    return len(complex)
Esempio n. 2
0
def get_code_complexity(code, min=7, filename='stdin'):
    complex = []
    try:
        ast = parse(code)
    except (AttributeError, SyntaxError):
        e = sys.exc_info()[1]
        sys.stderr.write("Unable to parse %s: %s\n" % (filename, e))
        return 0

    visitor = PathGraphingAstVisitor()
    visitor.preorder(ast, visitor)
    for graph in visitor.graphs.values():
        if graph is None:
            # ?
            continue
        if graph.complexity() >= min:
            graph.filename = filename
            if not skip_warning(graph):
                msg = '%s:%d:1: %s %r is too complex (%d)' % (
                    filename,
                    graph.lineno,
                    WARNING_CODE,
                    graph.entity,
                    graph.complexity(),
                )
                complex.append(msg)

    if len(complex) == 0:
        return 0

    print('\n'.join(complex))
    return len(complex)
Esempio n. 3
0
def check(codeString, ignore, filename='stdin'):
    """
    Check the Python source given by C{codeString} for flakes.

    @param codeString: The Python source to check.
    @type codeString: C{str}

    @param filename: The name of the file the source came from, used to report
        errors.
    @type filename: C{str}

    @return: The number of warnings emitted.
    @rtype: C{int}
    """
    # First, compile into an AST and handle syntax errors.
    try:
        tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
    except SyntaxError:
        value = sys.exc_info()[1]
        msg = value.args[0]

        (lineno, offset, text) = value.lineno, value.offset, value.text

        # If there's an encoding problem with the file, the text is None.
        if text is None:
            # Avoid using msg, since for the only known case, it contains a
            # bogus message that claims the encoding the file declared was
            # unknown.
            sys.stderr.write("%s: problem decoding source\n" % (filename))
        else:
            line = text.splitlines()[-1]

            if offset is not None:
                offset = offset - (len(text) - len(line))

            sys.stderr.write('%s:%d: %s\n' % (filename, lineno, msg))
            sys.stderr.write(line + '\n')

            if offset is not None:
                sys.stderr.write(" " * offset + "^\n")

        return 1
    else:
        # Okay, it's syntactically valid.  Now check it.
        w = Checker(tree, filename)
        sorting = [(msg.lineno, msg) for msg in w.messages]
        sorting.sort()
        w.messages = [msg for index, msg in sorting]
        valid_warnings = 0

        for warning in w.messages:
            if skip_warning(warning, ignore):
                continue
            print(warning)
            valid_warnings += 1

        return valid_warnings
Esempio n. 4
0
def check(codeString, filename='(code)'):
    """
    Check the Python source given by C{codeString} for flakes.

    @param codeString: The Python source to check.
    @type codeString: C{str}

    @param filename: The name of the file the source came from, used to report
        errors.
    @type filename: C{str}

    @return: The number of warnings emitted.
    @rtype: C{int}
    """
    # First, compile into an AST and handle syntax errors.
    try:
        tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
    except SyntaxError:
        value = sys.exc_info()[1]
        msg = value.args[0]

        (lineno, offset, text) = value.lineno, value.offset, value.text

        # If there's an encoding problem with the file, the text is None.
        if text is None:
            # Avoid using msg, since for the only known case, it contains a
            # bogus message that claims the encoding the file declared was
            # unknown.
            sys.stderr.write("%s: problem decoding source\n" % (filename))
        else:
            line = text.splitlines()[-1]

            if offset is not None:
                offset = offset - (len(text) - len(line))

            sys.stderr.write('%s:%d: %s\n' % (filename, lineno, msg))
            sys.stderr.write(line + '\n')

            if offset is not None:
                sys.stderr.write(" " * offset + "^\n")

        return 1
    else:
        # Okay, it's syntactically valid.  Now check it.
        w = Checker(tree, filename)
        sorting = [(msg.lineno, msg) for msg in w.messages]
        sorting.sort()
        w.messages = [msg for index, msg in sorting]
        valid_warnings = 0

        for warning in w.messages:
            if skip_warning(warning):
                continue
            print(warning)
            valid_warnings += 1

        return valid_warnings
Esempio n. 5
0
            # Avoid using msg, since for the only known case, it contains a
            # bogus message that claims the encoding the file declared was
            # unknown.
            print >> sys.stderr, "%s: problem decoding source" % (filename, )
        else:
            line = text.splitlines()[-1]

            if offset is not None:
                offset = offset - (len(text) - len(line))

            print >> sys.stderr, '%s:%d: %s' % (filename, lineno, msg)
            print >> sys.stderr, line

            if offset is not None:
                print >> sys.stderr, " " * offset, "^"

        return 1
    else:
        # Okay, it's syntactically valid.  Now check it.
        w = Checker(tree, filename)
        w.messages.sort(lambda a, b: cmp(a.lineno, b.lineno))
        valid_warnings = 0

        for warning in w.messages:
            if skip_warning(warning):
                continue
            print warning
            valid_warnings += 1

        return valid_warnings