src/black/report.py
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | """
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Summarize Black runs to users.
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | """
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from dataclasses import dataclass
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from enum import Enum
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from pathlib import Path
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from click import style
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.output import err, out
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | class Changed(Enum):
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | NO = 0
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | CACHED = 1
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | YES = 2
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | class NothingChanged(UserWarning):
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | """Raised when reformatted code is the same as source."""
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @dataclass
---- ---- ---- ---- ---- ---- ---- 06 -- --- --- --- --- --- --- ------- ------- ------- | class Report:
---- ---- ---- ---- ---- ---- ---- 06 -- --- --- --- --- --- --- ------- ------- ------- | """Provides a reformatting counter. Can be rendered with `str(report)`."""
---- ---- ---- ---- ---- ---- ---- 06 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 06 -- --- --- --- --- --- --- ------- ------- ------- | check: bool = False
---- ---- ---- ---- ---- ---- ---- 06 -- --- --- --- --- --- --- ------- ------- ------- | diff: bool = False
---- ---- ---- ---- ---- ---- ---- 06 -- --- --- --- --- --- --- ------- ------- ------- | quiet: bool = False
---- ---- ---- ---- ---- ---- ---- 06 -- --- --- --- --- --- --- ------- ------- ------- | verbose: bool = False
---- ---- ---- ---- ---- ---- ---- 06 -- --- --- --- --- --- --- ------- ------- ------- | change_count: int = 0
---- ---- ---- ---- ---- ---- ---- 06 -- --- --- --- --- --- --- ------- ------- ------- | same_count: int = 0
---- ---- ---- ---- ---- ---- ---- 06 -- --- --- --- --- --- --- ------- ------- ------- | failure_count: int = 0
---- ---- ---- ---- ---- ---- ---- 06 -- --- --- --- --- --- --- ------- ------- ------- |
0015 0015 0014 0000 0000 0000 0001 06 08 004 011 007 013 015 020 0078.14 0184.69 0002.36 | def done(self, src: Path, changed: Changed) -> None:
0015 0015 0014 0000 0000 0000 0001 06 08 004 011 007 013 015 020 0078.14 0184.69 0002.36 | """Increment the counter for successful reformatting. Write out a message."""
0015 0015 0014 0000 0000 0000 0001 06 08 004 011 007 013 015 020 0078.14 0184.69 0002.36 | if changed is Changed.YES:
0015 0015 0014 0000 0000 0000 0001 06 08 004 011 007 013 015 020 0078.14 0184.69 0002.36 | reformatted = "would reformat" if self.check or self.diff else "reformatted"
0015 0015 0014 0000 0000 0000 0001 06 08 004 011 007 013 015 020 0078.14 0184.69 0002.36 | if self.verbose or not self.quiet:
0015 0015 0014 0000 0000 0000 0001 06 08 004 011 007 013 015 020 0078.14 0184.69 0002.36 | out(f"{reformatted} {src}")
0015 0015 0014 0000 0000 0000 0001 06 08 004 011 007 013 015 020 0078.14 0184.69 0002.36 | self.change_count += 1
0015 0015 0014 0000 0000 0000 0001 06 08 004 011 007 013 015 020 0078.14 0184.69 0002.36 | else:
0015 0015 0014 0000 0000 0000 0001 06 08 004 011 007 013 015 020 0078.14 0184.69 0002.36 | if self.verbose:
0015 0015 0014 0000 0000 0000 0001 06 08 004 011 007 013 015 020 0078.14 0184.69 0002.36 | if changed is Changed.NO:
0015 0015 0014 0000 0000 0000 0001 06 08 004 011 007 013 015 020 0078.14 0184.69 0002.36 | msg = f"{src} already well formatted, good job."
0015 0015 0014 0000 0000 0000 0001 06 08 004 011 007 013 015 020 0078.14 0184.69 0002.36 | else:
0015 0015 0014 0000 0000 0000 0001 06 08 004 011 007 013 015 020 0078.14 0184.69 0002.36 | msg = f"{src} wasn't modified on disk since last run."
0015 0015 0014 0000 0000 0000 0001 06 08 004 011 007 013 015 020 0078.14 0184.69 0002.36 | out(msg, bold=False)
0015 0015 0014 0000 0000 0000 0001 06 08 004 011 007 013 015 020 0078.14 0184.69 0002.36 | self.same_count += 1
---- ---- ---- ---- ---- ---- ---- 06 -- --- --- --- --- --- --- ------- ------- ------- |
0004 0004 0003 0000 0000 0000 0001 06 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def failed(self, src: Path, message: str) -> None:
0004 0004 0003 0000 0000 0000 0001 06 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """Increment the counter for failed reformatting. Write out a message."""
0004 0004 0003 0000 0000 0000 0001 06 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | err(f"error: cannot format {src}: {message}")
0004 0004 0003 0000 0000 0000 0001 06 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | self.failure_count += 1
---- ---- ---- ---- ---- ---- ---- 06 -- --- --- --- --- --- --- ------- ------- ------- |
0003 0003 0003 0000 0000 0000 0000 06 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def path_ignored(self, path: Path, message: str) -> None:
0003 0003 0003 0000 0000 0000 0000 06 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | if self.verbose:
0003 0003 0003 0000 0000 0000 0000 06 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | out(f"{path} ignored: {message}", bold=False)
---- ---- ---- ---- ---- ---- ---- 06 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 06 -- --- --- --- --- --- --- ------- ------- ------- | @property
0018 0008 0007 0002 0006 0003 0002 06 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def return_code(self) -> int:
0018 0008 0007 0002 0006 0003 0002 06 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """Return the exit code that the app should use.
0018 0008 0007 0002 0006 0003 0002 06 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 |
0018 0008 0007 0002 0006 0003 0002 06 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | This considers the current state of changed files and failures:
0018 0008 0007 0002 0006 0003 0002 06 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | - if there were any failures, return 123;
0018 0008 0007 0002 0006 0003 0002 06 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | - if any files were changed and --check is being used, return 1;
0018 0008 0007 0002 0006 0003 0002 06 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | - otherwise return 0.
0018 0008 0007 0002 0006 0003 0002 06 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """
0018 0008 0007 0002 0006 0003 0002 06 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | # According to http://tldp.org/LDP/abs/html/exitcodes.html starting with
0018 0008 0007 0002 0006 0003 0002 06 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | # 126 we have special return codes reserved by the shell.
0018 0008 0007 0002 0006 0003 0002 06 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | if self.failure_count:
0018 0008 0007 0002 0006 0003 0002 06 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return 123
0018 0008 0007 0002 0006 0003 0002 06 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 |
0018 0008 0007 0002 0006 0003 0002 06 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | elif self.change_count and self.check:
0018 0008 0007 0002 0006 0003 0002 06 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return 1
0018 0008 0007 0002 0006 0003 0002 06 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 |
0018 0008 0007 0002 0006 0003 0002 06 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return 0
0018 0008 0007 0002 0006 0003 0002 06 -- --- --- --- --- --- --- ------- ------- ------- |
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | def __str__(self) -> str:
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | """Render a color report of the current state.
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 |
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | Use `click.unstyle` to remove colors.
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | """
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | if self.check or self.diff:
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | reformatted = "would be reformatted"
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | unchanged = "would be left unchanged"
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | failed = "would fail to reformat"
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | else:
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | reformatted = "reformatted"
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | unchanged = "left unchanged"
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | failed = "failed to reformat"
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | report = []
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | if self.change_count:
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | s = "s" if self.change_count > 1 else ""
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | report.append(
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | style(f"{self.change_count} file{s} ", bold=True, fg="blue")
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | + style(f"{reformatted}", bold=True)
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | )
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 |
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | if self.same_count:
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | s = "s" if self.same_count > 1 else ""
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | report.append(style(f"{self.same_count} file{s} ", fg="blue") + unchanged)
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | if self.failure_count:
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | s = "s" if self.failure_count > 1 else ""
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | report.append(style(f"{self.failure_count} file{s} {failed}", fg="red"))
0028 0021 0023 0000 0003 0002 0000 06 09 003 012 007 014 015 021 0082.04 0143.58 0001.75 | return ", ".join(report) + "."