LeadKit/project-scripts/gen_api_doc.py

74 lines
1.5 KiB
Python

import sys
import os
from os.path import isfile, join
from typing import Optional
SRCROOT = os.getenv("SRCROOT")
DOCROOT = SRCROOT + "/docs"
API_PATH = DOCROOT + "/api_doc.md"
# TODO: add url to documentation
def main():
module_name = sys.argv[1]
rows = create_rows(module_name)
if (rows):
append_row(rows)
def create_rows(module_name: str) -> list[str]:
path = DOCROOT + f"/{module_name.lower()}"
files = [path + f"/{f}" for f in os.listdir(path) if isfile(join(path, f))]
info = []
for f in files:
type, name = get_element_info(f)
if type and name:
row = create_row(type, name, module_name)
info.append(row)
return info
def append_row(rows: list[str]):
with open(API_PATH, "a") as f:
for row in rows:
f.write(row)
def get_element_info(file_path: str) -> tuple[Optional[str], Optional[str]]:
line = _get_last_line(file_path)
if not _validate(line):
return None, None
words = line.split()
if len(words) < 3:
print("‼️ Pass arguments like this: `#### ElementType ElementName`!")
return None, None
return words[1], words[2]
def create_row(element_type: str, element_name: str, module_name: str) -> str:
return f"\n| {element_type} | {module_name} | {element_name} | | | |"
def _validate(line: str) -> bool:
return "####" in line
def _get_last_line(path: str) -> str:
with open(path, "r") as f:
return f.readlines()[-1]
if __name__ == "__main__":
main()