Skip to content

Commit

Permalink
Restyled by yapf
Browse files Browse the repository at this point in the history
  • Loading branch information
restyled-commits committed Sep 7, 2023
1 parent 0a4a055 commit aa8cd07
Showing 1 changed file with 31 additions and 36 deletions.
67 changes: 31 additions & 36 deletions tools/gen_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def needs_space(l: str, r: str) -> bool:
def untokenize(tokens: Tuple[str, ...]) -> str:
line = []
for i in range(len(tokens) - 1):
if tokens[i : i + 2] == ("void", ")"):
if tokens[i:i + 2] == ("void", ")"):
break
line.append(tokens[i])
if needs_space(tokens[i], tokens[i + 1]):
Expand Down Expand Up @@ -74,9 +74,8 @@ def parse_params(tokens: Tuple[str, ...]) -> List[Tuple[List[str], str]]:
return params


def finalize_handler(
type_prefix: str, handlers: List[str], event: str, params: List[str]
) -> None:
def finalize_handler(type_prefix: str, handlers: List[str], event: str,
params: List[str]) -> None:
handlers[-1] += ":"
self = ""
array = ""
Expand Down Expand Up @@ -133,9 +132,8 @@ def handle_macro(tokens: Sequence[str], state: List[str]) -> bool:
return False


def handle_types(
tokens: Sequence[str], state: List[str], extern: List[str], const_prefix: str
) -> bool:
def handle_types(tokens: Sequence[str], state: List[str], extern: List[str],
const_prefix: str) -> bool:
# struct definitions (members are ignored)
if tokens[0] == "struct" and tokens[-1] == "{":
state.append("struct")
Expand All @@ -147,7 +145,8 @@ def handle_types(
extern.append(f" ctypedef struct {tokens[2]}")

# enums
if (tokens[:2] == ("typedef", "enum") or tokens[0] == "enum") and tokens[-1] == "{":
if (tokens[:2] == ("typedef", "enum")
or tokens[0] == "enum") and tokens[-1] == "{":
extern.append("")
extern.append(f" cpdef enum {tokens[-2]}:")
state.append("enum")
Expand All @@ -158,41 +157,38 @@ def handle_types(


def handle_functions(
tokens: Tuple[str, ...],
state: List[str],
extern: List[str],
fun_prefix: str,
type_prefix: str,
event: str,
params: List[str],
handlers: List[str],
install_handlers: List[str],
tokens: Tuple[str, ...],
state: List[str],
extern: List[str],
fun_prefix: str,
type_prefix: str,
event: str,
params: List[str],
handlers: List[str],
install_handlers: List[str],
) -> str:
# functions and callbacks
if (
"(" in tokens
and tokens[0].isidentifier()
and token_before("(", tokens).startswith(fun_prefix)
and tokens[0] != "typedef"
):
if ("(" in tokens and tokens[0].isidentifier()
and token_before("(", tokens).startswith(fun_prefix)
and tokens[0] != "typedef"):
extern.append(f" cdef {untokenize_fun(tokens)}")
if ";" not in tokens:
state.append("fun")
return event
if tokens[:2] == ("typedef", "void"):
extern.append(f" c{untokenize_fun(tokens)}")

event = tokens[2][len(fun_prefix) : -3]
event = tokens[2][len(fun_prefix):-3]
params.clear()
params.extend(tokens[3:])

# TODO(iphydf): Handle this better (by checking whether we have a callback install
# function for this event).
if event != "log":
handlers.append(f"cdef void handle_{untokenize_fun((event,) + tokens[3:])}")
handlers.append(
f"cdef void handle_{untokenize_fun((event,) + tokens[3:])}")
install_handlers.append(
f" {fun_prefix}callback_{event}(ptr, handle_{event})"
)
f" {fun_prefix}callback_{event}(ptr, handle_{event})")
if ";" not in tokens:
state.append("callback")
else:
Expand Down Expand Up @@ -277,7 +273,8 @@ def gen_cython(lines: Sequence[str], fun_prefix: str) -> List[str]:
)

if install_handlers:
install_handlers = ["cdef void install_handlers(Tox *ptr):"] + install_handlers
install_handlers = ["cdef void install_handlers(Tox *ptr):"
] + install_handlers
return extern + [""] + handlers + [""] + install_handlers


Expand All @@ -289,14 +286,12 @@ def main() -> None:
for line in src_fh.readlines():
if line.startswith("cdef extern from"):
with open(api, "r", encoding="utf-8") as api_fh:
print(
"\n".join(
gen_cython(
api_fh.readlines(),
fun_prefix=os.path.split(api)[-1].split(".")[0] + "_",
)
)
)
print("\n".join(
gen_cython(
api_fh.readlines(),
fun_prefix=os.path.split(api)[-1].split(".")[0] +
"_",
)))
else:
print(line.rstrip())

Expand Down

0 comments on commit aa8cd07

Please sign in to comment.