MCPcopy
hub / github.com/psf/black / format_stdin_to_stdout

Function format_stdin_to_stdout

src/black/__init__.py:1023–1072  ·  view source on GitHub ↗

Format file on stdin. Return True if changed. If content is None, it's read from sys.stdin. If `write_back` is YES, write reformatted code back to stdout. If it is DIFF, write a diff to stdout. The `mode` argument is passed to :func:`format_file_contents`.

(
    fast: bool,
    *,
    content: str | None = None,
    write_back: WriteBack = WriteBack.NO,
    mode: Mode,
    lines: Collection[tuple[int, int]] = (),
)

Source from the content-addressed store, hash-verified

1021
1022
1023def format_stdin_to_stdout(
1024 fast: bool,
1025 *,
1026 content: str | None = None,
1027 write_back: WriteBack = WriteBack.NO,
1028 mode: Mode,
1029 lines: Collection[tuple[int, int]] = (),
1030) -> bool:
1031 """Format file on stdin. Return True if changed.
1032
1033 If content is None, it's read from sys.stdin.
1034
1035 If `write_back` is YES, write reformatted code back to stdout. If it is DIFF,
1036 write a diff to stdout. The `mode` argument is passed to
1037 :func:`format_file_contents`.
1038 """
1039 then = datetime.now(timezone.utc)
1040
1041 if content is None:
1042 src, encoding, newline = decode_bytes(sys.stdin.buffer.read(), mode)
1043 else:
1044 src, encoding, newline = content, "utf-8", "\n"
1045
1046 dst = src
1047 try:
1048 dst = format_file_contents(src, fast=fast, mode=mode, lines=lines)
1049 return True
1050
1051 except NothingChanged:
1052 return False
1053
1054 finally:
1055 f = io.TextIOWrapper(
1056 sys.stdout.buffer, encoding=encoding, newline=newline, write_through=True
1057 )
1058 if write_back == WriteBack.YES:
1059 # Make sure there's a newline after the content
1060 if dst and dst[-1] != "\n" and dst[-1] != "\r":
1061 dst += newline
1062 f.write(dst)
1063 elif write_back in (WriteBack.DIFF, WriteBack.COLOR_DIFF):
1064 now = datetime.now(timezone.utc)
1065 src_name = f"STDIN\t{then}"
1066 dst_name = f"STDOUT\t{now}"
1067 d = diff(src, dst, src_name, dst_name)
1068 if write_back == WriteBack.COLOR_DIFF:
1069 d = color_diff(d)
1070 f = wrap_stream_for_windows(f)
1071 f.write(d)
1072 f.detach()
1073
1074
1075def check_stability_and_equivalence(

Callers 2

reformat_codeFunction · 0.85
reformat_oneFunction · 0.85

Calls 7

diffFunction · 0.90
color_diffFunction · 0.90
wrap_stream_for_windowsFunction · 0.90
decode_bytesFunction · 0.85
format_file_contentsFunction · 0.85
readMethod · 0.80
writeMethod · 0.80

Tested by

no test coverage detected