Nits
This commit is contained in:
parent
ee02ebe3e9
commit
041ec995e5
38
black.py
38
black.py
@ -1,5 +1,4 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import pickle
|
|
||||||
from asyncio.base_events import BaseEventLoop
|
from asyncio.base_events import BaseEventLoop
|
||||||
from concurrent.futures import Executor, ProcessPoolExecutor
|
from concurrent.futures import Executor, ProcessPoolExecutor
|
||||||
from enum import Enum, Flag
|
from enum import Enum, Flag
|
||||||
@ -10,10 +9,11 @@
|
|||||||
from multiprocessing import Manager
|
from multiprocessing import Manager
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import pickle
|
||||||
import re
|
import re
|
||||||
import tokenize
|
|
||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
|
import tokenize
|
||||||
from typing import (
|
from typing import (
|
||||||
Any,
|
Any,
|
||||||
Callable,
|
Callable,
|
||||||
@ -57,6 +57,7 @@
|
|||||||
# types
|
# types
|
||||||
FileContent = str
|
FileContent = str
|
||||||
Encoding = str
|
Encoding = str
|
||||||
|
NewLine = str
|
||||||
Depth = int
|
Depth = int
|
||||||
NodeType = int
|
NodeType = int
|
||||||
LeafID = int
|
LeafID = int
|
||||||
@ -469,7 +470,7 @@ def format_file_in_place(
|
|||||||
mode |= FileMode.PYI
|
mode |= FileMode.PYI
|
||||||
|
|
||||||
with open(src, "rb") as buf:
|
with open(src, "rb") as buf:
|
||||||
newline, encoding, src_contents = prepare_input(buf.read())
|
src_contents, encoding, newline = decode_bytes(buf.read())
|
||||||
try:
|
try:
|
||||||
dst_contents = format_file_contents(
|
dst_contents = format_file_contents(
|
||||||
src_contents, line_length=line_length, fast=fast, mode=mode
|
src_contents, line_length=line_length, fast=fast, mode=mode
|
||||||
@ -513,7 +514,7 @@ def format_stdin_to_stdout(
|
|||||||
`line_length`, `fast`, `is_pyi`, and `force_py36` arguments are passed to
|
`line_length`, `fast`, `is_pyi`, and `force_py36` arguments are passed to
|
||||||
:func:`format_file_contents`.
|
:func:`format_file_contents`.
|
||||||
"""
|
"""
|
||||||
newline, encoding, src = prepare_input(sys.stdin.buffer.read())
|
src, encoding, newline = decode_bytes(sys.stdin.buffer.read())
|
||||||
dst = src
|
dst = src
|
||||||
try:
|
try:
|
||||||
dst = format_file_contents(src, line_length=line_length, fast=fast, mode=mode)
|
dst = format_file_contents(src, line_length=line_length, fast=fast, mode=mode)
|
||||||
@ -523,26 +524,16 @@ def format_stdin_to_stdout(
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
|
f = io.TextIOWrapper(
|
||||||
|
sys.stdout.buffer, encoding=encoding, newline=newline, write_through=True
|
||||||
|
)
|
||||||
if write_back == WriteBack.YES:
|
if write_back == WriteBack.YES:
|
||||||
f = io.TextIOWrapper(
|
|
||||||
sys.stdout.buffer,
|
|
||||||
encoding=encoding,
|
|
||||||
newline=newline,
|
|
||||||
write_through=True,
|
|
||||||
)
|
|
||||||
f.write(dst)
|
f.write(dst)
|
||||||
f.detach()
|
|
||||||
elif write_back == WriteBack.DIFF:
|
elif write_back == WriteBack.DIFF:
|
||||||
src_name = "<stdin> (original)"
|
src_name = "<stdin> (original)"
|
||||||
dst_name = "<stdin> (formatted)"
|
dst_name = "<stdin> (formatted)"
|
||||||
f = io.TextIOWrapper(
|
|
||||||
sys.stdout.buffer,
|
|
||||||
encoding=encoding,
|
|
||||||
newline=newline,
|
|
||||||
write_through=True,
|
|
||||||
)
|
|
||||||
f.write(diff(src, dst, src_name, dst_name))
|
f.write(diff(src, dst, src_name, dst_name))
|
||||||
f.detach()
|
f.detach()
|
||||||
|
|
||||||
|
|
||||||
def format_file_contents(
|
def format_file_contents(
|
||||||
@ -603,17 +594,18 @@ def format_str(
|
|||||||
return dst_contents
|
return dst_contents
|
||||||
|
|
||||||
|
|
||||||
def prepare_input(src: bytes) -> Tuple[str, str, str]:
|
def decode_bytes(src: bytes) -> Tuple[FileContent, Encoding, NewLine]:
|
||||||
"""Analyze `src` and return a tuple of (newline, encoding, decoded_contents)
|
"""Return a tuple of (decoded_contents, encoding, newline).
|
||||||
|
|
||||||
Where `newline` is either CRLF or LF, and `decoded_contents` is decoded with
|
`newline` is either CRLF or LF but `decoded_contents` is decoded with
|
||||||
universal newlines (i.e. only LF).
|
universal newlines (i.e. only contains LF).
|
||||||
"""
|
"""
|
||||||
srcbuf = io.BytesIO(src)
|
srcbuf = io.BytesIO(src)
|
||||||
encoding, lines = tokenize.detect_encoding(srcbuf.readline)
|
encoding, lines = tokenize.detect_encoding(srcbuf.readline)
|
||||||
newline = "\r\n" if b"\r\n" == lines[0][-2:] else "\n"
|
newline = "\r\n" if b"\r\n" == lines[0][-2:] else "\n"
|
||||||
srcbuf.seek(0)
|
srcbuf.seek(0)
|
||||||
return newline, encoding, io.TextIOWrapper(srcbuf, encoding).read()
|
with io.TextIOWrapper(srcbuf, encoding) as tiow:
|
||||||
|
return tiow.read(), encoding, newline
|
||||||
|
|
||||||
|
|
||||||
GRAMMARS = [
|
GRAMMARS = [
|
||||||
|
@ -12,12 +12,16 @@ Assertions and checks
|
|||||||
|
|
||||||
.. autofunction:: black.assert_stable
|
.. autofunction:: black.assert_stable
|
||||||
|
|
||||||
|
.. autofunction:: black.can_omit_invisible_parens
|
||||||
|
|
||||||
.. autofunction:: black.is_empty_tuple
|
.. autofunction:: black.is_empty_tuple
|
||||||
|
|
||||||
.. autofunction:: black.is_import
|
.. autofunction:: black.is_import
|
||||||
|
|
||||||
.. autofunction:: black.is_line_short_enough
|
.. autofunction:: black.is_line_short_enough
|
||||||
|
|
||||||
|
.. autofunction:: black.is_multiline_string
|
||||||
|
|
||||||
.. autofunction:: black.is_one_tuple
|
.. autofunction:: black.is_one_tuple
|
||||||
|
|
||||||
.. autofunction:: black.is_python36
|
.. autofunction:: black.is_python36
|
||||||
@ -32,6 +36,9 @@ Assertions and checks
|
|||||||
|
|
||||||
.. autofunction:: black.is_vararg
|
.. autofunction:: black.is_vararg
|
||||||
|
|
||||||
|
.. autofunction:: black.is_yield
|
||||||
|
|
||||||
|
|
||||||
Formatting
|
Formatting
|
||||||
----------
|
----------
|
||||||
|
|
||||||
@ -57,12 +64,12 @@ File operations
|
|||||||
Parsing
|
Parsing
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
.. autofunction:: black.decode_bytes
|
||||||
|
|
||||||
.. autofunction:: black.lib2to3_parse
|
.. autofunction:: black.lib2to3_parse
|
||||||
|
|
||||||
.. autofunction:: black.lib2to3_unparse
|
.. autofunction:: black.lib2to3_unparse
|
||||||
|
|
||||||
.. autofunction:: black.prepare_input
|
|
||||||
|
|
||||||
Split functions
|
Split functions
|
||||||
---------------
|
---------------
|
||||||
|
|
||||||
@ -102,6 +109,8 @@ Utilities
|
|||||||
|
|
||||||
.. autofunction:: black.enumerate_reversed
|
.. autofunction:: black.enumerate_reversed
|
||||||
|
|
||||||
|
.. autofunction:: black.enumerate_with_length
|
||||||
|
|
||||||
.. autofunction:: black.generate_comments
|
.. autofunction:: black.generate_comments
|
||||||
|
|
||||||
.. autofunction:: black.make_comment
|
.. autofunction:: black.make_comment
|
||||||
|
Loading…
Reference in New Issue
Block a user