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 11, 2023
1 parent 2761466 commit 39274e0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 43 deletions.
3 changes: 2 additions & 1 deletion test/core_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def test_set_status_message(self):

tox.status_message = b"x" * core.MAX_STATUS_MESSAGE_LENGTH
with self.assertRaises(error.ApiException):
tox.status_message = b"x" * (core.MAX_STATUS_MESSAGE_LENGTH + 1)
tox.status_message = b"x" * (core.MAX_STATUS_MESSAGE_LENGTH +
1)

def test_set_status(self):
with core.Core() as tox:
Expand Down
77 changes: 35 additions & 42 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] += " noexcept"
handlers[-1] += ":"
self = ""
Expand Down Expand Up @@ -134,9 +133,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 @@ -148,7 +146,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] == "{":
enum_name = tokens[-2]
if enum_name != "Tox_Log_Level":
extern.append("")
Expand All @@ -163,41 +162,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 All @@ -222,7 +218,8 @@ def handle_functions(
return event


def gen_cython(lines: Sequence[str], fun_prefix: str, extern_line: str) -> List[str]:
def gen_cython(lines: Sequence[str], fun_prefix: str,
extern_line: str) -> List[str]:
const_prefix = fun_prefix.upper()
type_prefix = fun_prefix.capitalize()

Expand Down Expand Up @@ -282,7 +279,8 @@ def gen_cython(lines: Sequence[str], fun_prefix: str, extern_line: str) -> List[
)

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 @@ -300,23 +298,18 @@ def main() -> None:
with open(src, "r", encoding="utf-8") as src_fh:
for line in src_fh.readlines():
if line.startswith(cdef_extern_prefix) and line.endswith(
cdef_extern_suffix
):
cdef_extern_suffix):
api_file = line.removeprefix(cdef_extern_prefix).removesuffix(
cdef_extern_suffix
)
cdef_extern_suffix)
api = os.path.join(api_base, api_file)
extern_line = line.removesuffix(" pass\n")
with open(api, "r", encoding="utf-8") as api_fh:
print(
"\n".join(
gen_cython(
api_fh.readlines(),
fun_prefix=get_fun_prefix(api),
extern_line=extern_line,
)
)
)
print("\n".join(
gen_cython(
api_fh.readlines(),
fun_prefix=get_fun_prefix(api),
extern_line=extern_line,
)))
else:
print(line.rstrip())

Expand Down

0 comments on commit 39274e0

Please sign in to comment.