Skip to content

Commit aa8cd07

Browse files
Restyled by yapf
1 parent 0a4a055 commit aa8cd07

File tree

1 file changed

+31
-36
lines changed

1 file changed

+31
-36
lines changed

tools/gen_api.py

+31-36
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def needs_space(l: str, r: str) -> bool:
3737
def untokenize(tokens: Tuple[str, ...]) -> str:
3838
line = []
3939
for i in range(len(tokens) - 1):
40-
if tokens[i : i + 2] == ("void", ")"):
40+
if tokens[i:i + 2] == ("void", ")"):
4141
break
4242
line.append(tokens[i])
4343
if needs_space(tokens[i], tokens[i + 1]):
@@ -74,9 +74,8 @@ def parse_params(tokens: Tuple[str, ...]) -> List[Tuple[List[str], str]]:
7474
return params
7575

7676

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:
8079
handlers[-1] += ":"
8180
self = ""
8281
array = ""
@@ -133,9 +132,8 @@ def handle_macro(tokens: Sequence[str], state: List[str]) -> bool:
133132
return False
134133

135134

136-
def handle_types(
137-
tokens: Sequence[str], state: List[str], extern: List[str], const_prefix: str
138-
) -> bool:
135+
def handle_types(tokens: Sequence[str], state: List[str], extern: List[str],
136+
const_prefix: str) -> bool:
139137
# struct definitions (members are ignored)
140138
if tokens[0] == "struct" and tokens[-1] == "{":
141139
state.append("struct")
@@ -147,7 +145,8 @@ def handle_types(
147145
extern.append(f" ctypedef struct {tokens[2]}")
148146

149147
# enums
150-
if (tokens[:2] == ("typedef", "enum") or tokens[0] == "enum") and tokens[-1] == "{":
148+
if (tokens[:2] == ("typedef", "enum")
149+
or tokens[0] == "enum") and tokens[-1] == "{":
151150
extern.append("")
152151
extern.append(f" cpdef enum {tokens[-2]}:")
153152
state.append("enum")
@@ -158,41 +157,38 @@ def handle_types(
158157

159158

160159
def handle_functions(
161-
tokens: Tuple[str, ...],
162-
state: List[str],
163-
extern: List[str],
164-
fun_prefix: str,
165-
type_prefix: str,
166-
event: str,
167-
params: List[str],
168-
handlers: List[str],
169-
install_handlers: List[str],
160+
tokens: Tuple[str, ...],
161+
state: List[str],
162+
extern: List[str],
163+
fun_prefix: str,
164+
type_prefix: str,
165+
event: str,
166+
params: List[str],
167+
handlers: List[str],
168+
install_handlers: List[str],
170169
) -> str:
171170
# functions and callbacks
172-
if (
173-
"(" in tokens
174-
and tokens[0].isidentifier()
175-
and token_before("(", tokens).startswith(fun_prefix)
176-
and tokens[0] != "typedef"
177-
):
171+
if ("(" in tokens and tokens[0].isidentifier()
172+
and token_before("(", tokens).startswith(fun_prefix)
173+
and tokens[0] != "typedef"):
178174
extern.append(f" cdef {untokenize_fun(tokens)}")
179175
if ";" not in tokens:
180176
state.append("fun")
181177
return event
182178
if tokens[:2] == ("typedef", "void"):
183179
extern.append(f" c{untokenize_fun(tokens)}")
184180

185-
event = tokens[2][len(fun_prefix) : -3]
181+
event = tokens[2][len(fun_prefix):-3]
186182
params.clear()
187183
params.extend(tokens[3:])
188184

189185
# TODO(iphydf): Handle this better (by checking whether we have a callback install
190186
# function for this event).
191187
if event != "log":
192-
handlers.append(f"cdef void handle_{untokenize_fun((event,) + tokens[3:])}")
188+
handlers.append(
189+
f"cdef void handle_{untokenize_fun((event,) + tokens[3:])}")
193190
install_handlers.append(
194-
f" {fun_prefix}callback_{event}(ptr, handle_{event})"
195-
)
191+
f" {fun_prefix}callback_{event}(ptr, handle_{event})")
196192
if ";" not in tokens:
197193
state.append("callback")
198194
else:
@@ -277,7 +273,8 @@ def gen_cython(lines: Sequence[str], fun_prefix: str) -> List[str]:
277273
)
278274

279275
if install_handlers:
280-
install_handlers = ["cdef void install_handlers(Tox *ptr):"] + install_handlers
276+
install_handlers = ["cdef void install_handlers(Tox *ptr):"
277+
] + install_handlers
281278
return extern + [""] + handlers + [""] + install_handlers
282279

283280

@@ -289,14 +286,12 @@ def main() -> None:
289286
for line in src_fh.readlines():
290287
if line.startswith("cdef extern from"):
291288
with open(api, "r", encoding="utf-8") as api_fh:
292-
print(
293-
"\n".join(
294-
gen_cython(
295-
api_fh.readlines(),
296-
fun_prefix=os.path.split(api)[-1].split(".")[0] + "_",
297-
)
298-
)
299-
)
289+
print("\n".join(
290+
gen_cython(
291+
api_fh.readlines(),
292+
fun_prefix=os.path.split(api)[-1].split(".")[0] +
293+
"_",
294+
)))
300295
else:
301296
print(line.rstrip())
302297

0 commit comments

Comments
 (0)