Пример #1
0
def advice():
    mustBeBlank = sm.State(
        "single line comment",
        lambda line: "expected a blank line, but got: {}".format(line.strip()))
    multiLine = sm.State(
        "multi line comment",
        lambda line: "expected a blank line, but got: {}".format(line.strip()))
    noComment = sm.State(
        "no comment",
        lambda line: "unexpected error at: {}".format(line.strip()))

    noComment.addTransition(mustBeBlank, isSingleLineComment)
    noComment.addTransition(multiLine, containsMultiLineComment)
    noComment.addTransition(noComment, lambda line: True)

    mustBeBlank.addTransition(noComment, isBlank)
    mustBeBlank.addTransition(multiLine, containsMultiLineComment)
    mustBeBlank.addTransition(mustBeBlank, isSingleLineComment)

    multiLine.addTransition(mustBeBlank, containsMultiLineComment)
    multiLine.addTransition(multiLine, lambda line: True)

    success, message = sm.StateMachine(noComment).run(
        lib.source(_fileName).split("\n")[::-1])
    if success:
        return a.Advice(a.AdviceLevel.GOOD, ADVICEMESSAGE)
    return a.Advice(a.AdviceLevel.BAD, ADVICEMESSAGE, message)
Пример #2
0
def advice():
    source = lib.removeComments(lib.source(_fileName))
    setIndentationStep(getIndentationStep(source))

    defaultState = sm.State(
        "default",
        lambda line: "inconsistent indentation at: {}".format(line.strip()))
    mustMatchIndentation = sm.State(
        u"must match indentation", lambda line: chr(192) +
        "inconsistent indentation at: {}".format(line.strip()))
    backslash = sm.State(
        "backslash",
        lambda line: "unexpected error occured at: {}".format(line.strip()))

    defaultState.addTransition(\
     mustMatchIndentation,\
     lambda line : endsWithDoubleDot(line) and matchesIndentationLevelOrLess(line),\
     action = lambda line : setIndentationLevel(line) and incrementIndentationLevel())
    defaultState.addTransition(\
     backslash,\
     lambda line : endsWithBackslash(line) and matchesIndentationLevelOrLess(line))
    defaultState.addTransition(\
     defaultState,\
     matchesIndentationLevelOrLess,\
     action = setIndentationLevel)

    mustMatchIndentation.addTransition(\
     mustMatchIndentation,\
     lambda line : endsWithDoubleDot(line) and matchesIndentationLevel(line),\
     action = lambda line : incrementIndentationLevel())
    mustMatchIndentation.addTransition(\
     backslash,\
     lambda line : endsWithBackslash(line) and matchesIndentationLevel(line))
    mustMatchIndentation.addTransition(\
     defaultState,\
     lambda line : matchesIndentationLevel(line),\
     action = setIndentationLevel)

    backslash.addTransition(\
     mustMatchIndentation,\
     endsWithDoubleDot,\
     action = lambda line : incrementIndentationLevel())
    backslash.addTransition(\
     backslash,\
     endsWithBackslash)
    backslash.addTransition(\
     defaultState,\
     lambda line : True)

    success, message = sm.StateMachine(defaultState).run(
        (line for line in source.split("\n") if len(line.strip()) != 0))
    if success:
        return a.Advice(a.AdviceLevel.GOOD, ADVICEMESSAGE)
    return a.Advice(a.AdviceLevel.BAD, ADVICEMESSAGE, message)
Пример #3
0
def advice():
    source = lib.source(_fileName)
    usesSpaces = False
    usesTabs = False
    for line in source.split("\n"):
        for char in line:
            if char == " ":
                usesSpaces = True
            elif char == "\t":
                usesTabs = True
            else:
                break
        if usesSpaces and usesTabs:
            return a.Advice(a.AdviceLevel.BAD, ADVICEMESSAGE)

    return a.Advice(a.AdviceLevel.GOOD, ADVICEMESSAGE)
Пример #4
0
def advice():
    source = lib.replaceStringsWithX(
        lib.replaceCommentsWithHashes(lib.source(_fileName)))

    unaryOperators = ["**", "!"]
    binaryOperators = sorted([
        "/", "//", "%", "|", "&", "=", "+=", "-=", "*=", "/=", "//=", "%=",
        "==", "!=", "<", ">", "<=", ">="
    ],
                             key=lambda x: -len(x))
    undecidedOperators = ["+", "-", "*"]
    operators = sorted(binaryOperators + unaryOperators + undecidedOperators,
                       key=lambda x: -len(x))

    operatorRegex = "[" + "|".join("'{}'".format("".join("\\" + c for c in op))
                                   for op in operators) + "]"

    UNARY_TOKEN = "`~UNARYOP"
    BINARY_TOKEN = "`~BINARYOP"
    UNDECIDED_TOKEN = "`~UNDECIDEDOP"

    operatorLines = re.findall(".*{}.*".format(operatorRegex), source)

    for line in operatorLines:
        tempLine = replaceOperatorsWith(line, binaryOperators, BINARY_TOKEN)
        tempLine = replaceOperatorsWith(tempLine, unaryOperators, UNARY_TOKEN)
        tempLine = replaceOperatorsWith(tempLine, undecidedOperators,
                                        UNDECIDED_TOKEN)

        if re.findall(".*[^ ]{}.*".format(BINARY_TOKEN),
                      tempLine) or re.findall(
                          ".*{}[^ ].*".format(BINARY_TOKEN), tempLine):
            return a.Advice(a.AdviceLevel.BAD, ADVICEMESSAGE, annotation=line)
Пример #5
0
def advice():
    #print lib.source(_fileName)
    source = lib.replaceStringsWithX(
        lib.replaceCommentsWithHashes(lib.source(_fileName)))
    #print source

    nLines = 0
    nLinesOfComments = 0
    for line in source.split("\n"):
        nLines += 1
        if "#" in line:
            nLinesOfComments += 1

    if nLinesOfComments / nLines < 0.1:
        return a.Advice(a.AdviceLevel.UNSURE, ADVICEMESSAGE)
Пример #6
0
def advice():
    try:
        root = ast.parse(lib.source(_fileName))
    except:
        return

    names = {node.id for node in ast.walk(root) if isinstance(node, ast.Name)}
    names -= set(name for name in __builtins__)
    names -= set(["x", "y", "i", "j", "k"])
    names = sorted(names)

    shortNames = [name for name in names if len(name) <= 2]

    if shortNames:
        return a.Advice(a.AdviceLevel.UNSURE,
                        ADVICEMESSAGE + ": {}".format(shortNames))
Пример #7
0
def advice():
    source = lib.source(_fileName)
    if any(len(line) > MAXLINELENGTH for line in source.split("\n")):
        return a.Advice(a.AdviceLevel.BAD, ADVICEMESSAGE)
    return a.Advice(a.AdviceLevel.GOOD, ADVICEMESSAGE)