Esempio n. 1
0
    def run(path, code=None, params=None, **meta):
        """MCCabe code checking.

        :return list: List of errors.
        """
        tree = compile(code, path, "exec", ast.PyCF_ONLY_AST)

        McCabeChecker.max_complexity = int(params.get('complexity', 10))
        return [
            {'lnum': lineno, 'offset': offset, 'text': text, 'type': McCabeChecker._code}
            for lineno, offset, text, _ in McCabeChecker(tree, path).run()
        ]
Esempio n. 2
0
def mccabe(filename):
    with open(filename, "rU") as mod:
        code = mod.read()
        try:
            tree = compile(code, filename, "exec", _ast.PyCF_ONLY_AST)
        except Exception:
            return []

    complx = []
    McCabeChecker.max_complexity = MccabeOptions.complexity
    for lineno, offset, text, check in McCabeChecker(tree, filename).run():
        complx.append(dict(col=offset, lnum=lineno, text=text))

    return complx
Esempio n. 3
0
    def process_module(self, node):
        """
        Check complexity for a module.
        """
        code = node.file_stream.read()

        try:
            tree = compile(code, node.file, "exec", ast.PyCF_ONLY_AST)
        except SyntaxError:
            # Pylint would have already failed
            return

        McCabeChecker.max_complexity = self.max_complexity
        results = McCabeChecker(tree, node.file).run()
        for lineno, _, text, _ in results:
            text = text[5:]
            self.add_message('C0901', line=lineno, args=text)
Esempio n. 4
0
def get_code_complexity(code, threshold=7, filename='stdin'):
    """
    This is a monkey-patch for flake8.pyflakes.check.
    Return array of errors instead of print them into STDERR.
    """
    try:
        tree = compile(code, filename, "exec", ast.PyCF_ONLY_AST)
    except SyntaxError:
        # return [(value.lineno, value.offset, value.args[0])]
        # be silent when error, or else syntax errors are reported twice
        return []

    complexity = []
    McCabeChecker.max_complexity = threshold
    for lineno, offset, text, check in McCabeChecker(tree, filename).run():
        complexity.append((lineno, offset, text))
    return complexity
Esempio n. 5
0
    def run_check(self, ctx: RunContext):
        """Run Mccabe code checker."""
        params = ctx.get_params("mccabe")
        options = ctx.options
        if options:
            params.setdefault("max-complexity", options.max_complexity)

        McCabeChecker.max_complexity = int(params.get("max-complexity", 10))
        McCabeChecker._error_tmpl = "%r is too complex (%d)"
        number = McCabeChecker._code
        for lineno, offset, text, _ in McCabeChecker(ctx.ast,
                                                     ctx.filename).run():
            ctx.push(
                col=offset + 1,
                lnum=lineno,
                number=number,
                text=text,
                type="C",
                source="mccabe",
            )
Esempio n. 6
0
    def run(path, code=None, params=None, **meta):
        """MCCabe code checking.

        :return list: List of errors.
        """
        try:
            tree = compile(code, path, "exec", ast.PyCF_ONLY_AST)
        except SyntaxError as exc:
            return [{
                'lnum': exc.lineno,
                'text': 'Invalid syntax: %s' % exc.text.strip()
            }]

        McCabeChecker.max_complexity = int(params.get('complexity', 10))
        return [{
            'lnum': lineno,
            'offset': offset,
            'text': text,
            'type': McCabeChecker._code
        } for lineno, offset, text, _ in McCabeChecker(tree, path).run()]