doc: write python stub on standard output

This commit is contained in:
Sébastien Helleu 2021-08-16 22:59:07 +02:00
parent 254c1a3e8b
commit 86e3c672bb

View File

@ -24,7 +24,6 @@ This script requires Python 3.6+.
""" """
from pathlib import Path from pathlib import Path
from typing import TextIO
import re import re
@ -37,7 +36,6 @@ STUB_HEADER = """\
# #
from typing import Dict from typing import Dict
""" """
CONSTANT_RE = r"""\ CONSTANT_RE = r"""\
@ -52,10 +50,8 @@ def (?P<function>\w+)(?P<args>[^)]*)(?P<return>\) -> [^:]+:) \.\.\.\
""" """
def write_stub_constants(stub_file: TextIO) -> None: def print_stub_constants() -> None:
""" """Print constants, extracted from the Scripting guide."""
Write constants in the stub file, extracted from the Scripting guide.
"""
types = { types = {
"integer": "int", "integer": "int",
"string": "str", "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: with open(DOC_DIR / "weechat_scripting.en.adoc") as scripting_file:
scripting = scripting_file.read() scripting = scripting_file.read()
for match in constant_pattern.finditer(scripting): 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: def print_stub_functions() -> None:
""" """Print function prototypes, extracted from the Plugin API reference."""
Write function prototypes in the stub file, extracted from the
Plugin API reference.
"""
function_pattern = re.compile(FUNCTION_RE, re.DOTALL) function_pattern = re.compile(FUNCTION_RE, re.DOTALL)
with open(DOC_DIR / "weechat_plugin_api.en.adoc") as api_doc_file: with open(DOC_DIR / "weechat_plugin_api.en.adoc") as api_doc_file:
api_doc = api_doc_file.read() api_doc = api_doc_file.read()
for match in function_pattern.finditer(api_doc): for match in function_pattern.finditer(api_doc):
url = f'https://weechat.org/doc/api#_{match["function"]}' url = f'https://weechat.org/doc/api#_{match["function"]}'
stub_file.write( print(
f"""\n f"""\n
def {match["function"]}{match["args"]}{match["return"]} def {match["function"]}{match["args"]}{match["return"]}
\"""`{match["function"]} in WeeChat plugin API reference <{url}>`_\""" \"""`{match["function"]} in WeeChat plugin API reference <{url}>`_\"""
... ..."""
"""
) )
def stub_api() -> None: def stub_api() -> None:
"""Write Python stub file.""" """Write Python stub file."""
with open("weechat.pyi", "w") as stub_file: print(STUB_HEADER)
stub_file.write(STUB_HEADER) print_stub_constants()
write_stub_constants(stub_file) print_stub_functions()
write_stub_functions(stub_file)
if __name__ == "__main__": if __name__ == "__main__":