]> git.madduck.net Git - etc/vim.git/blob - src/black/report.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Treat raw strings like other docstrings (#3947)
[etc/vim.git] / src / black / report.py
1 """
2 Summarize Black runs to users.
3 """
4
5 from dataclasses import dataclass
6 from enum import Enum
7 from pathlib import Path
8
9 from click import style
10
11 from black.output import err, out
12
13
14 class Changed(Enum):
15     NO = 0
16     CACHED = 1
17     YES = 2
18
19
20 class NothingChanged(UserWarning):
21     """Raised when reformatted code is the same as source."""
22
23
24 @dataclass
25 class Report:
26     """Provides a reformatting counter. Can be rendered with `str(report)`."""
27
28     check: bool = False
29     diff: bool = False
30     quiet: bool = False
31     verbose: bool = False
32     change_count: int = 0
33     same_count: int = 0
34     failure_count: int = 0
35
36     def done(self, src: Path, changed: Changed) -> None:
37         """Increment the counter for successful reformatting. Write out a message."""
38         if changed is Changed.YES:
39             reformatted = "would reformat" if self.check or self.diff else "reformatted"
40             if self.verbose or not self.quiet:
41                 out(f"{reformatted} {src}")
42             self.change_count += 1
43         else:
44             if self.verbose:
45                 if changed is Changed.NO:
46                     msg = f"{src} already well formatted, good job."
47                 else:
48                     msg = f"{src} wasn't modified on disk since last run."
49                 out(msg, bold=False)
50             self.same_count += 1
51
52     def failed(self, src: Path, message: str) -> None:
53         """Increment the counter for failed reformatting. Write out a message."""
54         err(f"error: cannot format {src}: {message}")
55         self.failure_count += 1
56
57     def path_ignored(self, path: Path, message: str) -> None:
58         if self.verbose:
59             out(f"{path} ignored: {message}", bold=False)
60
61     @property
62     def return_code(self) -> int:
63         """Return the exit code that the app should use.
64
65         This considers the current state of changed files and failures:
66         - if there were any failures, return 123;
67         - if any files were changed and --check is being used, return 1;
68         - otherwise return 0.
69         """
70         # According to http://tldp.org/LDP/abs/html/exitcodes.html starting with
71         # 126 we have special return codes reserved by the shell.
72         if self.failure_count:
73             return 123
74
75         elif self.change_count and self.check:
76             return 1
77
78         return 0
79
80     def __str__(self) -> str:
81         """Render a color report of the current state.
82
83         Use `click.unstyle` to remove colors.
84         """
85         if self.check or self.diff:
86             reformatted = "would be reformatted"
87             unchanged = "would be left unchanged"
88             failed = "would fail to reformat"
89         else:
90             reformatted = "reformatted"
91             unchanged = "left unchanged"
92             failed = "failed to reformat"
93         report = []
94         if self.change_count:
95             s = "s" if self.change_count > 1 else ""
96             report.append(
97                 style(f"{self.change_count} file{s} ", bold=True, fg="blue")
98                 + style(f"{reformatted}", bold=True)
99             )
100
101         if self.same_count:
102             s = "s" if self.same_count > 1 else ""
103             report.append(style(f"{self.same_count} file{s} ", fg="blue") + unchanged)
104         if self.failure_count:
105             s = "s" if self.failure_count > 1 else ""
106             report.append(style(f"{self.failure_count} file{s} {failed}", fg="red"))
107         return ", ".join(report) + "."