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 read_lines_from_stdin(self): # type: () -> List[str] """Read the lines from standard in.""" return utils.stdin_get_value().splitlines(True)