diff --git a/test/core_test.py b/test/core_test.py index 0b453b7..dc3d648 100644 --- a/test/core_test.py +++ b/test/core_test.py @@ -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: diff --git a/tools/gen_api.py b/tools/gen_api.py index 2dce4b0..893ae4b 100755 --- a/tools/gen_api.py +++ b/tools/gen_api.py @@ -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]): @@ -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 = "" @@ -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") @@ -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("") @@ -163,23 +162,20 @@ 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") @@ -187,17 +183,17 @@ def handle_functions( 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: @@ -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() @@ -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 @@ -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())