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 typing import TextIO
import re
@ -37,7 +36,6 @@ STUB_HEADER = """\
#
from typing import Dict
"""
CONSTANT_RE = r"""\
@ -52,10 +50,8 @@ def (?P<function>\w+)(?P<args>[^)]*)(?P<return>\) -> [^:]+:) \.\.\.\
"""
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__":