Exemplo n.º 1
0
    def load_file(self):
        if self.filename in ("stdin", "-", None):
            self.filename = "stdin"
            self.lines = stdin_utils.stdin_get_value().splitlines(True)
        else:
            self.lines = pycodestyle.readlines(self.filename)

        if not self.tree:
            self.tree = ast.parse("".join(self.lines))
Exemplo n.º 2
0
    def run(self):
        tree = self.tree

        if self.filename == 'stdin':
            lines = stdin_utils.stdin_get_value()
            tree = ast.parse(lines)

        for statement in ast.walk(tree):
            for child in ast.iter_child_nodes(statement):
                child.__flake8_builtins_parent = statement

        function_nodes = [ast.FunctionDef]
        if getattr(ast, 'AsyncFunctionDef', None):
            function_nodes.append(ast.AsyncFunctionDef)
        function_nodes = tuple(function_nodes)

        for_nodes = [ast.For]
        if getattr(ast, 'AsyncFor', None):
            for_nodes.append(ast.AsyncFor)
        for_nodes = tuple(for_nodes)

        with_nodes = [ast.With]
        if getattr(ast, 'AsyncWith', None):
            with_nodes.append(ast.AsyncWith)
        with_nodes = tuple(with_nodes)

        for statement in ast.walk(tree):
            value = None
            if isinstance(statement, ast.Assign):
                value = self.check_assignment(statement)

            elif isinstance(statement, function_nodes):
                value = self.check_function_definition(statement)

            elif isinstance(statement, for_nodes):
                value = self.check_for_loop(statement)

            elif isinstance(statement, with_nodes):
                value = self.check_with(statement)

            elif isinstance(statement, ast.excepthandler):
                value = self.check_exception(statement)

            elif isinstance(statement, ast.ListComp):
                value = self.check_list_comprehension(statement)

            elif isinstance(statement, (ast.Import, ast.ImportFrom)):
                value = self.check_import(statement)

            elif isinstance(statement, ast.ClassDef):
                value = self.check_class(statement)

            if value:
                for line, offset, msg, rtype in value:
                    yield line, offset, msg, rtype
    def run(self):
        if self.filename == 'stdin':
            lines = stdin_utils.stdin_get_value().splitlines(True)
        else:
            with open(self.filename) as f:
                lines = f.readlines()

        for lineno, line in enumerate(lines, start=1):
            found = HASATTR_RE.search(line)
            if found:
                yield lineno, line.find('hasattr'), self.message, type(self)
    def run(self):
        ''' 返回iterater of (line_number, offset, text, check) '''
        tree = self.tree

        if self.filename == 'stdin':
            lines = stdin_utils.stdin_get_value()
            tree = ast.parse(lines)

        visitor = CallVisitor(self)
        visitor.visit(self.tree)
        return iter(visitor.violations)
    def run(self):
        """check the code"""
        tree = self.tree

        if self.filename == "stdin":
            lines = stdin_utils.stdin_get_value()
            tree = ast.parse(lines)

        visitor = CheckVisitor()
        visitor.visit(tree)
        for error in visitor.errors:
            yield error
Exemplo n.º 6
0
def get_tokens(filename):
    if filename == 'stdin':
        file_contents = stdin_utils.stdin_get_value().splitlines(True)
    else:
        file_contents = pycodestyle.readlines(filename)
    file_contents_iter = iter(file_contents)

    def file_contents_next():
        return next(file_contents_iter)

    for t in tokenize.generate_tokens(file_contents_next):
        yield Token(t)
Exemplo n.º 7
0
def get_tokens(filename):
    if filename == 'stdin':
        file_contents = stdin_utils.stdin_get_value().splitlines(True)
    else:
        file_contents = pycodestyle.readlines(filename)
    file_contents_iter = iter(file_contents)

    def file_contents_next():
        return next(file_contents_iter)

    for t in tokenize.generate_tokens(file_contents_next):
        yield Token(t)
Exemplo n.º 8
0
    def run(self):
        if self.filename == 'stdin':
            lines = stdin_utils.stdin_get_value().splitlines(True)
        else:
            with open(self.filename, 'rb') as f:
                lines = [line.decode('utf-8') for line in f.readlines()]

        for lineno, line in enumerate(lines, start=1):
            for newer_version, old_alias in self.flat_checks:
                position = line.find(old_alias)
                if position != -1:
                    msg = self.message.format(old_alias, newer_version)
                    yield lineno, position, msg, type(self)
Exemplo n.º 9
0
 def read_headers(self):
     if self.filename in ('stdin', '-', None):
         try:
             # flake8 >= v3.0
             from flake8.engine import pep8 as stdin_utils
         except ImportError:
             from flake8 import utils as stdin_utils
         return stdin_utils.stdin_get_value().splitlines(True)[:2]
     else:
         try:
             import pycodestyle
         except ImportError:
             import pep8 as pycodestyle
         return pycodestyle.readlines(self.filename)[:2]
Exemplo n.º 10
0
    def _load_source(self):
        """Loads the file in a way that auto-detects source encoding and deals
        with broken terminal encodings for stdin.

        Stolen from flake8_import_order because it's good.
        """

        if self.filename in ("stdin", "-", None):
            self.filename = "stdin"
            self.lines = stdin_utils.stdin_get_value().splitlines(True)
        else:
            self.lines = pycodestyle.readlines(self.filename)
        if not self.tree:
            self.tree = ast.parse("".join(self.lines))
Exemplo n.º 11
0
    def run(self):
        filename = self.filename

        if filename == 'stdin':
            lines = stdin_utils.stdin_get_value().splitlines(True)
        else:
            lines = pycodestyle.readlines(filename)

        for idx, line in enumerate(lines):
            line = line.strip()
            if line and line in ['},', '),', '],']:
                yield (
                    idx,
                    len(line),
                    "My Word!",
                    "C811",
                )
Exemplo n.º 12
0
    def run(self):
        tree = self.tree

        if self.filename == 'stdin':
            lines = stdin_utils.stdin_get_value()
            tree = ast.parse(lines)

        for statement in ast.walk(tree):
            for child in ast.iter_child_nodes(statement):
                child.__flake8_builtins_parent = statement

            value = None
            if isinstance(statement, ast.Assign):
                value = self.check_assignment(statement)

            elif isinstance(statement, ast.FunctionDef):
                value = self.check_function_definition(statement)

            if value:
                for line, offset, msg, rtype in value:
                    yield line, offset, msg, rtype
    def run(self):
        ''' 返回iterater of (line_number, offset, text, check) '''
        tree = self.tree

        if self.filename == 'stdin':
            lines = stdin_utils.stdin_get_value()
            tree = ast.parse(lines)

        function_nodes = [ast.FunctionDef]
        if getattr(ast, 'AsyncFunctionDef', None):
            function_nodes.append(ast.AsyncFunctionDef)
        function_nodes = tuple(function_nodes)

        for statement in ast.walk(tree):
            value = None

            if isinstance(statement, function_nodes):
                value = self.check_function_definition(statement)

            if value:
                for line, offset, msg, rtype in value:
                    yield line, offset, msg, rtype
Exemplo n.º 14
0
def get_lines(filename):
    if filename in ('stdin', '-', None):
        return stdin_utils.stdin_get_value().splitlines(True)
    else:
        return pep8.readlines(filename)