diff --git a/README.md b/README.md index 0d5af375474cdd99034b4a72f784d7c7d3293f72..722feb0878f61cc5261ee7bcfe7df93f9f8b8e6d 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,7 @@ Make sure you have mypy installed. Navigate to where your library.pyd/so is loca ```terminal stubgen -m library -o . ``` +After creating the .pyi file run the generate_pydocs.py file according to the instructions in that file. # How to use the library with PyCharm diff --git a/generate_pydocs.py b/generate_pydocs.py new file mode 100644 index 0000000000000000000000000000000000000000..881018916222c94e38e9e24bfcc5a84915a5f378 --- /dev/null +++ b/generate_pydocs.py @@ -0,0 +1,79 @@ +""" +This file inserts the docs of each class and method into the pyi file for autocompletetion. +Before running make sure you have created the pyi according to the README. +You might need to change the pyi_path. +Then simply run this file. +After running all the docs should have been inserted but YOU'RE NOT DONE YET. +You need to fix the intedention at the two bottom function of the pyi file. +Now you're done and can enjoy working autocompletion of this library. +""" + +import sys +import inspect +import re + +sys.path.append('build/python-api-src') +import library + +def update_pyi_with_docstrings(pyi_path): + """ + Update the .pyi file with docstrings from the corresponding library. + + Args: + pyi_path (str): The path to the .pyi file. + + Returns: + None + """ + with open(pyi_path, 'r') as file: + lines = file.readlines() + + new_lines = [] + known_def = {} #To keep track of docs to class methods + + for i in range(len(lines)): + line = lines[i] + if line.strip().startswith('class '): + new_lines.append(line) + if not line.strip().endswith("ID:"): + class_name = re.split(r'[\(: ]', line)[1] + docstring = inspect.getdoc(getattr(library, class_name)) + new_lines.append(f' """{docstring}"""\n') + for func in dir(getattr(library, class_name)): + if func.startswith("__"): + continue + func_docs = inspect.getdoc(getattr(getattr(library, class_name), func)) + known_def[func] = func_docs + + elif line.strip().startswith('def '): + new_lines.append(line[:-4]) + func_name = re.split(r'[\(: ]', line.strip())[1] + if func_name.startswith("__"): + new_lines.append(f' {line[-4:]}') + continue + elif func_name in known_def: + new_lines.append(f'\n """{known_def[func_name]}"""\n') + new_lines.append(f' {line[-4:]}') + continue + else: + try: + func_doc = inspect.getdoc(getattr(library, func_name)) + new_lines.append(f'\n """{func_doc}"""\n') + new_lines.append(f' {line[-4:]}') + continue + except AttributeError: + new_lines.append(f'\n """not found"""\n') + new_lines.append(f' {line[-4:]}') + continue + else: + new_lines.append(line) + + + # Write the updated content back to the .pyi file + with open(pyi_path, 'w') as file: + file.writelines(new_lines) + + +# Path to the generated .pyi file +pyi_file_path = 'build/python-api-src/library.pyi' +update_pyi_with_docstrings(pyi_file_path)