[trivial] Simplify mode
and write_back
calculation in main()
This commit is contained in:
parent
1aa14c5db0
commit
e7b312fb43
40
black.py
40
black.py
@ -119,6 +119,13 @@ class WriteBack(Enum):
|
|||||||
YES = 1
|
YES = 1
|
||||||
DIFF = 2
|
DIFF = 2
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_configuration(cls, *, check: bool, diff: bool) -> "WriteBack":
|
||||||
|
if check and not diff:
|
||||||
|
return cls.NO
|
||||||
|
|
||||||
|
return cls.DIFF if diff else cls.YES
|
||||||
|
|
||||||
|
|
||||||
class Changed(Enum):
|
class Changed(Enum):
|
||||||
NO = 0
|
NO = 0
|
||||||
@ -132,6 +139,19 @@ class FileMode(Flag):
|
|||||||
PYI = 2
|
PYI = 2
|
||||||
NO_STRING_NORMALIZATION = 4
|
NO_STRING_NORMALIZATION = 4
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_configuration(
|
||||||
|
cls, *, py36: bool, pyi: bool, skip_string_normalization: bool
|
||||||
|
) -> "FileMode":
|
||||||
|
mode = cls.AUTO_DETECT
|
||||||
|
if py36:
|
||||||
|
mode |= cls.PYTHON36
|
||||||
|
if pyi:
|
||||||
|
mode |= cls.PYI
|
||||||
|
if skip_string_normalization:
|
||||||
|
mode |= cls.NO_STRING_NORMALIZATION
|
||||||
|
return mode
|
||||||
|
|
||||||
|
|
||||||
@click.command()
|
@click.command()
|
||||||
@click.option(
|
@click.option(
|
||||||
@ -242,6 +262,11 @@ def main(
|
|||||||
src: List[str],
|
src: List[str],
|
||||||
) -> None:
|
) -> None:
|
||||||
"""The uncompromising code formatter."""
|
"""The uncompromising code formatter."""
|
||||||
|
write_back = WriteBack.from_configuration(check=check, diff=diff)
|
||||||
|
mode = FileMode.from_configuration(
|
||||||
|
py36=py36, pyi=pyi, skip_string_normalization=skip_string_normalization
|
||||||
|
)
|
||||||
|
report = Report(check=check, quiet=quiet)
|
||||||
sources: List[Path] = []
|
sources: List[Path] = []
|
||||||
try:
|
try:
|
||||||
include_regex = re.compile(include)
|
include_regex = re.compile(include)
|
||||||
@ -265,21 +290,6 @@ def main(
|
|||||||
sources.append(p)
|
sources.append(p)
|
||||||
else:
|
else:
|
||||||
err(f"invalid path: {s}")
|
err(f"invalid path: {s}")
|
||||||
|
|
||||||
if check and not diff:
|
|
||||||
write_back = WriteBack.NO
|
|
||||||
elif diff:
|
|
||||||
write_back = WriteBack.DIFF
|
|
||||||
else:
|
|
||||||
write_back = WriteBack.YES
|
|
||||||
mode = FileMode.AUTO_DETECT
|
|
||||||
if py36:
|
|
||||||
mode |= FileMode.PYTHON36
|
|
||||||
if pyi:
|
|
||||||
mode |= FileMode.PYI
|
|
||||||
if skip_string_normalization:
|
|
||||||
mode |= FileMode.NO_STRING_NORMALIZATION
|
|
||||||
report = Report(check=check, quiet=quiet)
|
|
||||||
if len(sources) == 0:
|
if len(sources) == 0:
|
||||||
out("No paths given. Nothing to do 😴")
|
out("No paths given. Nothing to do 😴")
|
||||||
ctx.exit(0)
|
ctx.exit(0)
|
||||||
|
Loading…
Reference in New Issue
Block a user