From 86e3c672bb45b26a76ce1039984802525fbfb475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Mon, 16 Aug 2021 22:59:07 +0200 Subject: [PATCH] doc: write python stub on standard output --- doc/python_stub.py | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/doc/python_stub.py b/doc/python_stub.py index 9e4be9ed8..4faa99287 100755 --- a/doc/python_stub.py +++ b/doc/python_stub.py @@ -24,7 +24,6 @@ This script requires Python 3.6+. """ from pathlib import Path -from typing import TextIO import re @@ -37,7 +36,6 @@ STUB_HEADER = """\ # from typing import Dict - """ CONSTANT_RE = r"""\ @@ -52,10 +50,8 @@ def (?P\w+)(?P[^)]*)(?P\) -> [^:]+:) \.\.\.\ """ -def write_stub_constants(stub_file: TextIO) -> None: - """ - Write constants in the stub file, extracted from the Scripting guide. - """ +def print_stub_constants() -> None: + """Print constants, extracted from the Scripting guide.""" types = { "integer": "int", "string": "str", @@ -64,34 +60,29 @@ def write_stub_constants(stub_file: TextIO) -> None: with open(DOC_DIR / "weechat_scripting.en.adoc") as scripting_file: scripting = scripting_file.read() for match in constant_pattern.finditer(scripting): - stub_file.write(f'{match["constant"]}: {types[match["type"]]}\n') + print(f'{match["constant"]}: {types[match["type"]]}') -def write_stub_functions(stub_file: TextIO) -> None: - """ - Write function prototypes in the stub file, extracted from the - Plugin API reference. - """ +def print_stub_functions() -> None: + """Print function prototypes, extracted from the Plugin API reference.""" function_pattern = re.compile(FUNCTION_RE, re.DOTALL) with open(DOC_DIR / "weechat_plugin_api.en.adoc") as api_doc_file: api_doc = api_doc_file.read() for match in function_pattern.finditer(api_doc): url = f'https://weechat.org/doc/api#_{match["function"]}' - stub_file.write( + print( f"""\n def {match["function"]}{match["args"]}{match["return"]} \"""`{match["function"]} in WeeChat plugin API reference <{url}>`_\""" - ... -""" + ...""" ) def stub_api() -> None: """Write Python stub file.""" - with open("weechat.pyi", "w") as stub_file: - stub_file.write(STUB_HEADER) - write_stub_constants(stub_file) - write_stub_functions(stub_file) + print(STUB_HEADER) + print_stub_constants() + print_stub_functions() if __name__ == "__main__":