예제 #1
0
    def from_filename(cls, filename):
        if filename != 'stdin':
            source = read_file_using_source_encoding(filename)
        elif six.PY2:  # ✘py3
            # On python 2, reading from stdin gives you bytes, which must
            # be decoded.
            source = decode_bytes_using_source_encoding(
                pycodestyle.stdin_get_value())
        else:  # ✘py2
            # On python 3, reading from stdin gives you text.
            source = pycodestyle.stdin_get_value()

        return cls.from_text(source, filename=filename)
예제 #2
0
 def source(self):
     if self._source is None:
         if self.filename != 'stdin':
             self._source = read_file_using_source_encoding(self.filename)
         elif six.PY2:  # ✘py33 ✘py34 ✘py35
             # On python 2, reading from stdin gives you bytes, which must
             # be decoded.
             self._source = decode_string_using_source_encoding(
                 pycodestyle.stdin_get_value())
         else:  # ✘py27
             # On python 3, reading from stdin gives you text.
             self._source = pycodestyle.stdin_get_value()
     return self._source
예제 #3
0
 def source(self):
     if self._source is None:
         if self.filename != 'stdin':
             self._source = read_file_using_source_encoding(self.filename)
         elif six.PY2:  # ✘py33 ✘py34 ✘py35
             # On python 2, reading from stdin gives you bytes, which must
             # be decoded.
             self._source = decode_string_using_source_encoding(
                 pycodestyle.stdin_get_value())
         else:  # ✘py27
             # On python 3, reading from stdin gives you text.
             self._source = pycodestyle.stdin_get_value()
     return self._source
예제 #4
0
def _process_file(filename):
    if filename == 'stdin':
        code = pycodestyle.stdin_get_value()
    else:
        with open(filename, 'rt') as f:
            code = f.read()
    return _process_code(code)
예제 #5
0
 def load_source(self):
     """Load the source for the specified file."""
     if self.filename in self.STDIN_NAMES:
         self.filename = 'stdin'
         self.source = pycodestyle.stdin_get_value()
     else:
         with pep257.tokenize_open(self.filename) as fd:
             self.source = fd.read()
예제 #6
0
    def load_file(self):
        if self.filename in ('stdin', '-', None):
            self.filename = 'stdin'
            self.lines = pycodestyle.stdin_get_value().splitlines(True)
        else:
            self.lines = pycodestyle.readlines(self.filename)

        if not self.tree:
            self.tree = ast.parse(''.join(self.lines))
예제 #7
0
    def load_file(self):
        if self.filename in ("stdin", "-", None):
            self.filename = "stdin"
            self.lines = pycodestyle.stdin_get_value().splitlines(True)
        else:
            self.lines = pycodestyle.readlines(self.filename)

        if not self.tree:
            self.tree = ast.parse("".join(self.lines))
예제 #8
0
    def read_headers(self):
        try:
            # flake8 >= v3.0
            import pycodestyle as pep8
        except ImportError:
            import pep8

        if self.filename in ('stdin', '-', None):
            return pep8.stdin_get_value().splitlines(True)[:2]
        else:
            return pep8.readlines(self.filename)[:2]
예제 #9
0
 def _load_source(self):
     """Load the source for the specified file."""
     if self.filename == "stdin":
         # atom, emacs
         self.source = pycodestyle.stdin_get_value()
         with open("tempbanditpythonfile.py", "w+") as f:
             f.write(self.source)
         self.filename = "tempbanditpythonfile.py"
     else:
         # vim, vscode
         with open(self.filename) as f:
             self.source = f.read()
예제 #10
0
 def run(self):
     if self.config_file and not self.search_isort_config():
         yield 0, 0, self.no_config_msg, type(self)
     else:
         with OutputCapture():
             if self.filename == 'stdin':
                 sort_result = SortImports(
                     file_contents=stdin_get_value(),
                     check=True,
                 )
             else:
                 sort_result = SortImports(self.filename, check=True)
         if sort_result.incorrectly_sorted:
             yield 0, 0, self.isort_error_msg, type(self)
예제 #11
0
 def run(self):
     if self.config_file and not self.search_isort_config():
         yield 0, 0, self.no_config_msg, type(self)
     else:
         with OutputCapture():
             if self.filename == 'stdin':
                 sort_result = SortImports(
                     file_contents=stdin_get_value(),
                     check=True,
                 )
             else:
                 sort_result = SortImports(self.filename, check=True)
         if sort_result.incorrectly_sorted:
             yield 0, 0, self.isort_error_msg, type(self)
예제 #12
0
    def load_file(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 = pycodestyle.stdin_get_value().splitlines(True)
        else:
            self.lines = pycodestyle.readlines(self.filename)

        if not self.tree:
            self.tree = ast.parse("".join(self.lines))
예제 #13
0
    def run(self):
        if self.config_file and not self.search_isort_config():
            yield 0, 0, self.no_config_msg, type(self)
        else:
            with OutputCapture():
                if self.filename == 'stdin':
                    sort_result = SortImports(
                        file_contents=pycodestyle.stdin_get_value(),
                        check=True,
                    )
                else:
                    sort_result = SortImports(self.filename, check=True)

            for line_num, message in self.sortimports_linenum_msg(sort_result):
                yield line_num, 0, message, type(self)
예제 #14
0
    def run(self):
        if self.filename is 'stdin':
            lines = pycodestyle.stdin_get_value().splitlines()
            tree = ast.parse(lines)
        elif self.tree:
            tree = self.tree
        else:
            with open(self.filename) as f:
                tree = ast.parse(f.read())

        for stmt in ast.walk(tree):
            if isinstance(stmt, ast.BinOp) and \
                    isinstance(stmt.op, ast.Mod) and \
                    isinstance(stmt.left, ast.Str):
                yield stmt.lineno, stmt.col_offset, self.message, type(self)
예제 #15
0
    def handle(self, error):  # type: (Violation) -> None
        """Handle an error reported by Flake8.

        This defaults to calling :meth:`format`, :meth:`show_source`, and
        then :meth:`write`. This version implements the pattern-based ignore
        behavior from `spack flake8` as a native flake8 plugin.

        :param error:
            This will be an instance of
            :class:`~flake8.style_guide.Violation`.
        :type error:
            flake8.style_guide.Violation
        """

        # print(error.code)
        # print(error.physical_line)
        # get list of patterns for this error code
        pats = self.spack_errors.get(error.code, None)
        # if any pattern matches, skip line
        if pats is not None and any(
            (pat.search(error.physical_line) for pat in pats)):
            return

        # Special F811 handling
        # Prior to Python 3.8, `noqa: F811` needed to be placed on the `@when`
        # line
        # Starting with Python 3.8, it must be placed on the `def` line
        # https://gitlab.com/pycqa/flake8/issues/583
        # we can only determine if F811 should be ignored given the previous
        # line, so get the previous line and check it
        if (self.spack_errors.get("F811", False) and error.code == "F811"
                and error.line_number > 1):
            if self.file_lines is None:
                if self.filename in {"stdin", "-", "(none)", None}:
                    self.file_lines = pycodestyle.stdin_get_value().splitlines(
                        True)
                else:
                    self.file_lines = pycodestyle.readlines(self.filename)
            for pat in self.spack_errors["F811"]:
                if pat.search(self.file_lines[error.line_number - 2]):
                    return

        self.error_seen = True
        line = self.format(error)
        source = self.show_source(error)
        self.write(line, source)
예제 #16
0
    def run(self):
        if self.filename == 'stdin':
            lines = pycodestyle.stdin_get_value()
            tree = ast.parse(lines)
        elif self.tree:
            tree = self.tree
        else:
            with open(self.filename) as f:
                tree = ast.parse(f.read())

        for stmt in ast.walk(tree):
            if isinstance(stmt, ast.BinOp) and \
                    isinstance(stmt.op, ast.Mod):
                if isinstance(stmt.left, ast.Num) or \
                        isinstance(stmt.right, ast.Num):
                    continue

                if isinstance(stmt.left, (ast.Str, ast.Name)):
                    yield (
                        stmt.lineno,
                        stmt.col_offset,
                        self.message,
                        type(self),
                    )
예제 #17
0
def get_lines(filename):
    if filename in ("stdin", "-", None):
        return pep8.stdin_get_value().splitlines(True)
    else:
        return pep8.readlines(filename)
예제 #18
0
def get_lines(filename):
    if filename in ('stdin', '-', None):
        return pep8.stdin_get_value().splitlines(True)
    else:
        return pep8.readlines(filename)
예제 #19
0
 def get_file_contents(self):
     if self.filename in ('stdin', '-', None):
         return pycodestyle.stdin_get_value().splitlines(True)
     else:
         return pycodestyle.readlines(self.filename)
def get_lines(filename):
    if filename in ("stdin", "-", None):
        return pycodestyle.stdin_get_value().splitlines(True)
    else:
        return pycodestyle.readlines(filename)
예제 #21
0
 def load_file(self):
     if self.filename in ('stdin', '-', None):
         self.filename = 'stdin'
         self.lines = pycodestyle.stdin_get_value().splitlines(True)
     else:
         self.lines = pycodestyle.readlines(self.filename)