From 5d89684a6e389e035289bcb9b4e21d0d4a46ad86 Mon Sep 17 00:00:00 2001 From: drewcassidy Date: Sat, 3 Apr 2021 21:03:24 -0700 Subject: [PATCH] add stub generation script --- tools/stubgen.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tools/stubgen.py diff --git a/tools/stubgen.py b/tools/stubgen.py new file mode 100644 index 0000000..fdc977b --- /dev/null +++ b/tools/stubgen.py @@ -0,0 +1,44 @@ +import inspect +import os.path +import tempfile +import mypy.stubgen as sg + +package = 'quicktex' +prefix = '_' + +modules = set() + + +def find_submodules(mod): + print(mod.__name__) + modules.add(mod.__name__.split('.')[-1]) + + for element_name in dir(mod): + element = getattr(mod, element_name) + if inspect.ismodule(element): + find_submodules(element) + + +if __name__ == "__main__": + find_submodules(__import__(prefix + package)) + with tempfile.TemporaryDirectory() as out: + sg.generate_stubs(sg.parse_options(['-o', out, '-p', prefix + package])) + print(os.path.abspath(os.curdir)) + for root, dirs, files in os.walk(out): + for stub_name in files: + module_path = os.path.relpath(root, out) + + if stub_name != '__init__.pyi': + module_path = os.path.join(module_path, os.path.splitext(stub_name)[0]) + + module_name = module_path.replace(prefix, '') + + with open(os.path.join(root, stub_name), 'r') as fp: + contents = fp.read() + + for mod in modules: + new_mod = mod.replace(prefix, '') + contents.replace(mod, new_mod) + + with open(os.path.join(os.curdir, *module_name.split('.'), '__init__.pyi'), 'w') as fp: + fp.write(contents)