Skip to content

Commit

Permalink
optimize doc generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Neutree committed Jul 8, 2024
1 parent 029f3f9 commit 7cb9f2b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 28 deletions.
31 changes: 3 additions & 28 deletions components/maix/gen_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
import time
try:
from .gen_api_cpp import generate_api_cpp
from .pyi_util import parse_pyi
except Exception:
from gen_api_cpp import generate_api_cpp
from pyi_util import parse_pyi

def sort_headers(headers):
# read headers_priority.txt
Expand All @@ -31,33 +33,6 @@ def sort_headers(headers):
headers = sorted(headers, key = lambda x: headers_priority.index(os.path.basename(x)) if os.path.basename(x) in headers_priority else len(headers_priority))
return headers

def parse_pyi(path):
items = {
"class": {},
"func": []
}
with open(path) as f:
lines = f.readlines()
class_item = None
for i, line in enumerate(lines):
if class_item:
if line[0] != " ":
items["class"][class_item["name"]] = class_item
class_item = None
continue
line = line.strip()
if line.startswith("def"):
class_item["func"].append(line.rsplit(":", 1)[0])

if line.startswith("def"):
items["func"].append(line.rsplit(":", 1)[0])
if line.startswith("class"):
class_item = {
"name": line.replace("class", "").replace(":", "").strip(),
"func": []
}
return items

def find_func_def(items, name):
for item in items:
# def check_bool_raise(ok: bool, msg: str = '') -> None:
Expand Down Expand Up @@ -214,7 +189,7 @@ def parse_module(pyi_path, k, v):
sidebar["items"].append(doc_maix_sidebar)
start_comment_template = '''
> You can use `{}` to access this module with MaixPy
> This module is generated from [MaixCDK](https://github.com/sipeed/MaixCDK)
> This module is generated from [MaixPy](https://github.com/sipeed/MaixPy) and [MaixCDK](https://github.com/sipeed/MaixCDK)
'''
top_api_keys = ["maix"]
Expand Down
37 changes: 37 additions & 0 deletions components/maix/pyi_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
def parse_pyi(path):
items = {
"class": {},
"func": []
}
with open(path) as f:
lines = f.readlines()
class_item = None
for i, line in enumerate(lines):
if class_item:
if line[0] != " ":
items["class"][class_item["name"]] = class_item
class_item = None
else:
line = line.strip()
if line.startswith("def"):
class_item["func"].append(line.rsplit(":", 1)[0])
continue

if line.startswith("def"):
items["func"].append(line.rsplit(":", 1)[0])
if line.startswith("class"):
class_item = {
"name": line.replace("class", "").replace(":", "").strip(),
"func": []
}
if class_item:
items["class"][class_item["name"]] = class_item
return items

if __name__ == "__main__":
import sys
items = parse_pyi(sys.argv[1])
print("Class:")
print(items["class"])
print("Func:")
print(items["func"])

0 comments on commit 7cb9f2b

Please sign in to comment.