34
34
import argparse
35
35
import difflib
36
36
import fnmatch
37
- import io
38
37
import multiprocessing
39
38
import os
40
39
import signal
@@ -87,20 +86,20 @@ def list_files(files, recursive=False, extensions=None, exclude=None):
87
86
def make_diff (file , original , reformatted ):
88
87
return list (
89
88
difflib .unified_diff (
90
- original , reformatted , fromfile = "{ }\t (original)". format ( file ) , tofile = "{ }\t (reformatted)". format ( file ) , n = 3
89
+ original , reformatted , fromfile = f" { file } \t (original)" , tofile = f" { file } \t (reformatted)" , n = 3
91
90
)
92
91
)
93
92
94
93
95
94
class DiffError (Exception ):
96
95
def __init__ (self , message , errs = None ):
97
- super (DiffError , self ).__init__ (message )
96
+ super ().__init__ (message )
98
97
self .errs = errs or []
99
98
100
99
101
100
class UnexpectedError (Exception ):
102
101
def __init__ (self , message , exc = None ):
103
- super (UnexpectedError , self ).__init__ (message )
102
+ super ().__init__ (message )
104
103
self .formatted_traceback = traceback .format_exc ()
105
104
self .exc = exc
106
105
@@ -112,14 +111,14 @@ def run_clang_format_diff_wrapper(args, file):
112
111
except DiffError :
113
112
raise
114
113
except Exception as e :
115
- raise UnexpectedError ("{ }: {}: {}" . format ( file , e .__class__ .__name__ , e ) , e )
114
+ raise UnexpectedError (f" { file } : { e .__class__ .__name__ } : { e } " , e )
116
115
117
116
118
117
def run_clang_format_diff (args , file ):
119
118
try :
120
- with io . open (file , "r" , encoding = "utf-8" ) as f :
119
+ with open (file , encoding = "utf-8" ) as f :
121
120
original = f .readlines ()
122
- except IOError as exc :
121
+ except OSError as exc :
123
122
raise DiffError (str (exc ))
124
123
invocation = [args .clang_format_executable , file ]
125
124
@@ -145,7 +144,7 @@ def run_clang_format_diff(args, file):
145
144
invocation , stdout = subprocess .PIPE , stderr = subprocess .PIPE , universal_newlines = True , encoding = "utf-8"
146
145
)
147
146
except OSError as exc :
148
- raise DiffError ("Command '{}' failed to start: {}" . format ( subprocess . list2cmdline ( invocation ), exc ) )
147
+ raise DiffError (f "Command '{ subprocess . list2cmdline ( invocation ) } ' failed to start: { exc } " )
149
148
proc_stdout = proc .stdout
150
149
proc_stderr = proc .stderr
151
150
@@ -203,7 +202,7 @@ def print_trouble(prog, message, use_colors):
203
202
error_text = "error:"
204
203
if use_colors :
205
204
error_text = bold_red (error_text )
206
- print ("{ }: {} {}" . format ( prog , error_text , message ) , file = sys .stderr )
205
+ print (f" { prog } : { error_text } { message } " , file = sys .stderr )
207
206
208
207
209
208
def main ():
@@ -216,7 +215,7 @@ def main():
216
215
)
217
216
parser .add_argument (
218
217
"--extensions" ,
219
- help = "comma separated list of file extensions (default: {})" . format ( DEFAULT_EXTENSIONS ) ,
218
+ help = f "comma separated list of file extensions (default: { DEFAULT_EXTENSIONS } )" ,
220
219
default = DEFAULT_EXTENSIONS ,
221
220
)
222
221
parser .add_argument ("-r" , "--recursive" , action = "store_true" , help = "run recursively over directories" )
@@ -227,7 +226,7 @@ def main():
227
226
metavar = "N" ,
228
227
type = int ,
229
228
default = 0 ,
230
- help = "run N clang-format jobs in parallel" " (default number of cpus + 1)" ,
229
+ help = "run N clang-format jobs in parallel (default number of cpus + 1)" ,
231
230
)
232
231
parser .add_argument (
233
232
"--color" , default = "auto" , choices = ["auto" , "always" , "never" ], help = "show colored diff (default: auto)"
@@ -238,7 +237,7 @@ def main():
238
237
metavar = "PATTERN" ,
239
238
action = "append" ,
240
239
default = [],
241
- help = "exclude paths matching the given glob-like pattern(s)" " from recursive search" ,
240
+ help = "exclude paths matching the given glob-like pattern(s) from recursive search" ,
242
241
)
243
242
244
243
args = parser .parse_args ()
@@ -263,7 +262,7 @@ def main():
263
262
colored_stdout = sys .stdout .isatty ()
264
263
colored_stderr = sys .stderr .isatty ()
265
264
266
- version_invocation = [args .clang_format_executable , str ( "--version" ) ]
265
+ version_invocation = [args .clang_format_executable , "--version" ]
267
266
try :
268
267
subprocess .check_call (version_invocation , stdout = DEVNULL )
269
268
except subprocess .CalledProcessError as e :
@@ -272,7 +271,7 @@ def main():
272
271
except OSError as e :
273
272
print_trouble (
274
273
parser .prog ,
275
- "Command '{}' failed to start: {}" . format ( subprocess . list2cmdline ( version_invocation ), e ) ,
274
+ f "Command '{ subprocess . list2cmdline ( version_invocation ) } ' failed to start: { e } " ,
276
275
use_colors = colored_stderr ,
277
276
)
278
277
return ExitStatus .TROUBLE
0 commit comments