示例#1
0
    def run(self):
        """Use black to check code style."""
        # if not self.black_check:
        #    return
        msg = None
        line = 0
        col = 0

        try:
            if self.filename in self.STDIN_NAMES:
                self.filename = "stdin"
                source = stdin_utils.stdin_get_value()
            else:
                with open(self.filename, "rb") as buf:
                    source, _, _ = black.decode_bytes(buf.read())
        except Exception as e:
            source = ""
            msg = "900 Failed to load file: %s" % e

        if not source and not msg:
            # Empty file (good)
            return
        elif not self.line_length:
            msg = "998 Could not access flake8 line length setting."
        elif source:
            # Call black...
            try:
                if self._file_mode is None:
                    # Legacy version of black, 18.9b0 or older
                    new_code = black.format_file_contents(
                        source, line_length=self.line_length, fast=False)
                else:
                    # For black 19.3b0 or later
                    new_code = black.format_file_contents(source,
                                                          mode=self._file_mode,
                                                          fast=False)
            except black.NothingChanged:
                return
            except black.InvalidInput:
                msg = "901 Invalid input."
            except ValueError as e:
                msg = "997 Configuration conflict: %s" % e
            except Exception as e:
                msg = "999 Unexpected exception: %s" % e
            else:
                assert (new_code != source
                        ), "Black made changes without raising NothingChanged"
                line, col = find_diff_start(source, new_code)
                line += 1  # Strange as col seems to be zero based?
                msg = "100 Black would make changes."
        # If we don't know the line or column numbers, leaving as zero.
        yield line, col, black_prefix + msg, type(self)
    def run(self):
        """Use black to check code style."""
        msg = None
        line = 0
        col = 0

        try:
            if self.filename in self.STDIN_NAMES:
                self.filename = "stdin"
                source = stdin_utils.stdin_get_value()
            else:
                with open(self.filename, "rb") as buf:
                    source, _, _ = black.decode_bytes(buf.read())
        except Exception as e:
            source = ""
            msg = "900 Failed to load file: %s" % e

        if not source and not msg:
            # Empty file (good)
            return
        elif source:
            # Call black...
            try:
                file_mode = self._file_mode
                file_mode.is_pyi = self.filename and self.filename.endswith(
                    ".pyi")
                new_code = black.format_file_contents(source,
                                                      mode=file_mode,
                                                      fast=False)
            except black.NothingChanged:
                return
            except black.InvalidInput:
                msg = "901 Invalid input."
            except BadBlackConfig as err:
                msg = "997 Invalid TOML file: %s" % err
            except Exception as err:
                msg = "999 Unexpected exception: %s" % err
            else:
                assert (new_code != source
                        ), "Black made changes without raising NothingChanged"
                line, col = find_diff_start(source, new_code)
                line += 1  # Strange as col seems to be zero based?
                msg = "100 Black would make changes."
        # If we don't know the line or column numbers, leaving as zero.
        yield line, col, black_prefix + msg, type(self)
示例#3
0
def format_and_check(path, mode):
    try:
        with open(path, mode="rb") as f:
            content, _, _ = black.decode_bytes(f.read())

        lines = content.split("\n")

        new_content = "\n".join(format_lines(lines, mode))

        if new_content == content:
            result = "unchanged"
        else:
            print(f"would reformat {path}")
            result = "reformatted"
    except black.InvalidInput as e:
        print(f"error: cannot format {path.absolute()}: {e}")
        result = "error"

    return result
示例#4
0
def format_and_overwrite(path, mode):
    try:
        with open(path, mode="rb") as f:
            content, encoding, newline = black.decode_bytes(f.read())

        lines = content.split("\n")

        new_content = "\n".join(format_lines(lines, mode))

        if new_content == content:
            result = "unchanged"
        else:
            print(f"reformatted {path}")
            result = "reformatted"

        with open(path, "w", encoding=encoding, newline=newline) as f:
            f.write(new_content)
    except black.InvalidInput as e:
        print(f"error: cannot format {path.absolute()}: {e}")
        result = "error"

    return result