Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/black/report.py: 40%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

63 statements  

1""" 

2Summarize Black runs to users. 

3""" 

4 

5from dataclasses import dataclass 

6from enum import Enum 

7from pathlib import Path 

8 

9from black.output import err, out, style_output 

10 

11 

12class Changed(Enum): 

13 NO = 0 

14 CACHED = 1 

15 YES = 2 

16 

17 

18class NothingChanged(UserWarning): 

19 """Raised when reformatted code is the same as source.""" 

20 

21 

22@dataclass 

23class Report: 

24 """Provides a reformatting counter. Can be rendered with `str(report)`.""" 

25 

26 check: bool = False 

27 diff: bool = False 

28 quiet: bool = False 

29 verbose: bool = False 

30 change_count: int = 0 

31 same_count: int = 0 

32 failure_count: int = 0 

33 

34 def done(self, src: Path, changed: Changed) -> None: 

35 """Increment the counter for successful reformatting. Write out a message.""" 

36 if changed is Changed.YES: 

37 reformatted = "would reformat" if self.check or self.diff else "reformatted" 

38 if self.verbose or not self.quiet: 

39 out(f"{reformatted} {src}") 

40 self.change_count += 1 

41 else: 

42 if self.verbose: 

43 if changed is Changed.NO: 

44 msg = f"{src} already well formatted, good job." 

45 else: 

46 msg = f"{src} wasn't modified on disk since last run." 

47 out(msg, bold=False) 

48 self.same_count += 1 

49 

50 def failed(self, src: Path, message: str) -> None: 

51 """Increment the counter for failed reformatting. Write out a message.""" 

52 err(f"error: cannot format {src}: {message}") 

53 self.failure_count += 1 

54 

55 def path_ignored(self, path: Path, message: str) -> None: 

56 if self.verbose: 

57 out(f"{path} ignored: {message}", bold=False) 

58 

59 @property 

60 def return_code(self) -> int: 

61 """Return the exit code that the app should use. 

62 

63 This considers the current state of changed files and failures: 

64 - if there were any failures, return 123; 

65 - if any files were changed and --check is being used, return 1; 

66 - otherwise return 0. 

67 """ 

68 # According to http://tldp.org/LDP/abs/html/exitcodes.html starting with 

69 # 126 we have special return codes reserved by the shell. 

70 if self.failure_count: 

71 return 123 

72 

73 elif self.change_count and self.check: 

74 return 1 

75 

76 return 0 

77 

78 def __str__(self) -> str: 

79 """Render a color report of the current state. 

80 

81 Use `click.unstyle` to remove colors. 

82 """ 

83 if self.check or self.diff: 

84 reformatted = "would be reformatted" 

85 unchanged = "would be left unchanged" 

86 failed = "would fail to reformat" 

87 else: 

88 reformatted = "reformatted" 

89 unchanged = "left unchanged" 

90 failed = "failed to reformat" 

91 report = [] 

92 if self.change_count: 

93 s = "s" if self.change_count > 1 else "" 

94 report.append( 

95 style_output(f"{self.change_count} file{s} ", bold=True, fg="blue") 

96 + style_output(f"{reformatted}", bold=True) 

97 ) 

98 

99 if self.same_count: 

100 s = "s" if self.same_count > 1 else "" 

101 report.append( 

102 style_output(f"{self.same_count} file{s} ", fg="blue") + unchanged 

103 ) 

104 if self.failure_count: 

105 s = "s" if self.failure_count > 1 else "" 

106 report.append( 

107 style_output(f"{self.failure_count} file{s} {failed}", fg="red") 

108 ) 

109 return ", ".join(report) + "."