5
5
import os
6
6
import sys
7
7
import tempfile
8
-
9
- import black
8
+ import subprocess
10
9
11
10
12
11
def to_snake_case (name ):
@@ -48,7 +47,7 @@ def print(*args, **kwargs):
48
47
"""Report something (will be printed and added to a file."""
49
48
# __builtins__.print(*args, **kwargs)
50
49
if args and not args [0 ].lstrip ().startswith ("#" ):
51
- args = ("*" ,) + args
50
+ args = ("*" , * args )
52
51
for f in _file_objects_to_print_to :
53
52
__builtins__ ["print" ](* args , file = f , flush = True , ** kwargs )
54
53
@@ -103,14 +102,35 @@ def remove_c_comments(code):
103
102
return new_code
104
103
105
104
106
- def blacken (src , singleline = False ):
107
- """Format the given src string using black. If singleline is True,
108
- all function signatures become single-line, so they can be parsed
109
- and updated.
105
+ class FormatError (Exception ):
106
+ pass
107
+
108
+
109
+ def format_code (src , singleline = False ):
110
+ """Format the given src string. If singleline is True, all function
111
+ signatures become single-line, so they can be parsed and updated.
110
112
"""
111
- # Normal black
112
- mode = black .FileMode (line_length = 999 if singleline else 88 )
113
- result = black .format_str (src , mode = mode )
113
+
114
+ # Use Ruff to format the line. Ruff does not yet have a Python API, so we use its CLI.
115
+ tempfilename = os .path .join (tempfile .gettempdir (), "wgpupy_codegen_format.py" )
116
+ with open (tempfilename , "wb" ) as fp :
117
+ fp .write (src .encode ())
118
+ line_length = 320 if singleline else 88
119
+ cmd = [
120
+ sys .executable ,
121
+ "-m" ,
122
+ "ruff" ,
123
+ "format" ,
124
+ "--line-length" ,
125
+ str (line_length ),
126
+ tempfilename ,
127
+ ]
128
+ p = subprocess .run (cmd , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
129
+ if p .returncode :
130
+ raise FormatError (p .stdout .decode (errors = "ignore" ))
131
+ with open (tempfilename , "rb" ) as fp :
132
+ result = fp .read ().decode ()
133
+ os .remove (tempfilename )
114
134
115
135
# Make defs single-line. You'd think that setting the line length
116
136
# to a very high number would do the trick, but it does not.
@@ -175,7 +195,7 @@ def _init(self, code):
175
195
self ._diffs = {}
176
196
self ._classes = {}
177
197
if code :
178
- self .lines = blacken (code , True ).splitlines () # inf line length
198
+ self .lines = format_code (code , True ).splitlines () # inf line length
179
199
180
200
def remove_line (self , i ):
181
201
"""Remove the line at the given position. There must not have been
@@ -221,8 +241,8 @@ def dumps(self, format=True):
221
241
text = "\n " .join (lines )
222
242
if format :
223
243
try :
224
- text = blacken (text )
225
- except black . InvalidInput as err : # pragma: no cover
244
+ text = format_code (text )
245
+ except FormatError as err : # pragma: no cover
226
246
# If you get this error, it really helps to load the code
227
247
# in an IDE to see where the error is. Let's help with that ...
228
248
filename = os .path .join (tempfile .gettempdir (), "wgpu_patcher_fail.py" )
@@ -233,8 +253,8 @@ def dumps(self, format=True):
233
253
raise RuntimeError (
234
254
f"It appears that the patcher has generated invalid Python:"
235
255
f"\n \n { err } \n \n "
236
- f'Wrote the generated (but unblackened ) code to:\n \n "{ filename } "'
237
- )
256
+ f'Wrote the generated (but unformatted ) code to:\n \n "{ filename } "'
257
+ ) from None
238
258
239
259
return text
240
260
0 commit comments