@@ -37,7 +37,7 @@ def needs_space(l: str, r: str) -> bool:
37
37
def untokenize (tokens : Tuple [str , ...]) -> str :
38
38
line = []
39
39
for i in range (len (tokens ) - 1 ):
40
- if tokens [i : i + 2 ] == ("void" , ")" ):
40
+ if tokens [i : i + 2 ] == ("void" , ")" ):
41
41
break
42
42
line .append (tokens [i ])
43
43
if needs_space (tokens [i ], tokens [i + 1 ]):
@@ -74,9 +74,8 @@ def parse_params(tokens: Tuple[str, ...]) -> List[Tuple[List[str], str]]:
74
74
return params
75
75
76
76
77
- def finalize_handler (
78
- type_prefix : str , handlers : List [str ], event : str , params : List [str ]
79
- ) -> None :
77
+ def finalize_handler (type_prefix : str , handlers : List [str ], event : str ,
78
+ params : List [str ]) -> None :
80
79
# handlers[-1] += " noexcept"
81
80
handlers [- 1 ] += ":"
82
81
self = ""
@@ -134,9 +133,8 @@ def handle_macro(tokens: Sequence[str], state: List[str]) -> bool:
134
133
return False
135
134
136
135
137
- def handle_types (
138
- tokens : Sequence [str ], state : List [str ], extern : List [str ], const_prefix : str
139
- ) -> bool :
136
+ def handle_types (tokens : Sequence [str ], state : List [str ], extern : List [str ],
137
+ const_prefix : str ) -> bool :
140
138
# struct definitions (members are ignored)
141
139
if tokens [0 ] == "struct" and tokens [- 1 ] == "{" :
142
140
state .append ("struct" )
@@ -148,7 +146,8 @@ def handle_types(
148
146
extern .append (f" ctypedef struct { tokens [2 ]} " )
149
147
150
148
# enums
151
- if (tokens [:2 ] == ("typedef" , "enum" ) or tokens [0 ] == "enum" ) and tokens [- 1 ] == "{" :
149
+ if (tokens [:2 ] == ("typedef" , "enum" )
150
+ or tokens [0 ] == "enum" ) and tokens [- 1 ] == "{" :
152
151
enum_name = tokens [- 2 ]
153
152
if enum_name != "Tox_Log_Level" :
154
153
extern .append ("" )
@@ -163,41 +162,38 @@ def handle_types(
163
162
164
163
165
164
def handle_functions (
166
- tokens : Tuple [str , ...],
167
- state : List [str ],
168
- extern : List [str ],
169
- fun_prefix : str ,
170
- type_prefix : str ,
171
- event : str ,
172
- params : List [str ],
173
- handlers : List [str ],
174
- install_handlers : List [str ],
165
+ tokens : Tuple [str , ...],
166
+ state : List [str ],
167
+ extern : List [str ],
168
+ fun_prefix : str ,
169
+ type_prefix : str ,
170
+ event : str ,
171
+ params : List [str ],
172
+ handlers : List [str ],
173
+ install_handlers : List [str ],
175
174
) -> str :
176
175
# functions and callbacks
177
- if (
178
- "(" in tokens
179
- and tokens [0 ].isidentifier ()
180
- and token_before ("(" , tokens ).startswith (fun_prefix )
181
- and tokens [0 ] != "typedef"
182
- ):
176
+ if ("(" in tokens and tokens [0 ].isidentifier ()
177
+ and token_before ("(" , tokens ).startswith (fun_prefix )
178
+ and tokens [0 ] != "typedef" ):
183
179
extern .append (f" cdef { untokenize_fun (tokens )} " )
184
180
if ";" not in tokens :
185
181
state .append ("fun" )
186
182
return event
187
183
if tokens [:2 ] == ("typedef" , "void" ):
188
184
extern .append (f" c{ untokenize_fun (tokens )} " )
189
185
190
- event = tokens [2 ][len (fun_prefix ) : - 3 ]
186
+ event = tokens [2 ][len (fun_prefix ): - 3 ]
191
187
params .clear ()
192
188
params .extend (tokens [3 :])
193
189
194
190
# TODO(iphydf): Handle this better (by checking whether we have a callback install
195
191
# function for this event).
196
192
if event != "log" :
197
- handlers .append (f"cdef void handle_{ untokenize_fun ((event ,) + tokens [3 :])} " )
193
+ handlers .append (
194
+ f"cdef void handle_{ untokenize_fun ((event ,) + tokens [3 :])} " )
198
195
install_handlers .append (
199
- f" { fun_prefix } callback_{ event } (ptr, handle_{ event } )"
200
- )
196
+ f" { fun_prefix } callback_{ event } (ptr, handle_{ event } )" )
201
197
if ";" not in tokens :
202
198
state .append ("callback" )
203
199
else :
@@ -222,7 +218,8 @@ def handle_functions(
222
218
return event
223
219
224
220
225
- def gen_cython (lines : Sequence [str ], fun_prefix : str , extern_line : str ) -> List [str ]:
221
+ def gen_cython (lines : Sequence [str ], fun_prefix : str ,
222
+ extern_line : str ) -> List [str ]:
226
223
const_prefix = fun_prefix .upper ()
227
224
type_prefix = fun_prefix .capitalize ()
228
225
@@ -282,7 +279,8 @@ def gen_cython(lines: Sequence[str], fun_prefix: str, extern_line: str) -> List[
282
279
)
283
280
284
281
if install_handlers :
285
- install_handlers = ["cdef void install_handlers(Tox *ptr):" ] + install_handlers
282
+ install_handlers = ["cdef void install_handlers(Tox *ptr):"
283
+ ] + install_handlers
286
284
return extern + ["" ] + handlers + ["" ] + install_handlers
287
285
288
286
@@ -300,23 +298,18 @@ def main() -> None:
300
298
with open (src , "r" , encoding = "utf-8" ) as src_fh :
301
299
for line in src_fh .readlines ():
302
300
if line .startswith (cdef_extern_prefix ) and line .endswith (
303
- cdef_extern_suffix
304
- ):
301
+ cdef_extern_suffix ):
305
302
api_file = line .removeprefix (cdef_extern_prefix ).removesuffix (
306
- cdef_extern_suffix
307
- )
303
+ cdef_extern_suffix )
308
304
api = os .path .join (api_base , api_file )
309
305
extern_line = line .removesuffix (" pass\n " )
310
306
with open (api , "r" , encoding = "utf-8" ) as api_fh :
311
- print (
312
- "\n " .join (
313
- gen_cython (
314
- api_fh .readlines (),
315
- fun_prefix = get_fun_prefix (api ),
316
- extern_line = extern_line ,
317
- )
318
- )
319
- )
307
+ print ("\n " .join (
308
+ gen_cython (
309
+ api_fh .readlines (),
310
+ fun_prefix = get_fun_prefix (api ),
311
+ extern_line = extern_line ,
312
+ )))
320
313
else :
321
314
print (line .rstrip ())
322
315
0 commit comments