Skip to content

Commit

Permalink
optimize doc generate
Browse files Browse the repository at this point in the history
  • Loading branch information
Neutree committed Jul 18, 2024
1 parent 3204e02 commit 66d1a53
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
4 changes: 2 additions & 2 deletions projects/app_detector/main/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ int _main(int argc, char *argv[])
img->draw_string(r.x, r.y, detector.labels[r.class_id], maix::image::Color::from_rgb(255, 0, 0));
}
img->draw_image(0, 0, *ret_img);
snprintf(tmp_chars, sizeof(tmp_chars), "All: %ldms, cam: %ldms\ndetect: %ldms, show: %ldms", t_all, t2 - t, t3 - t2, t_show);
img->draw_string(ret_img->width() + 2, 2, tmp_chars, image::COLOR_RED);
snprintf(tmp_chars, sizeof(tmp_chars), "All: %ldms, fps: %ld\ncam: %ldms, detect: %ldms, show: %ldms", t_all, 1000 / t_all, t2 - t, t3 - t2, t_show);
img->draw_string(2, img->height() - 40, tmp_chars, image::COLOR_RED);
disp.show(*img);
t_show = time::ticks_ms() - t3;
t_all = time::ticks_ms() - t;
Expand Down
28 changes: 13 additions & 15 deletions tools/doc_tool/gen_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def parse_api_item_info(item):
raise Exception("parse_api_info key {} value error: {} => {}".format(api, item, kv_str))
return api, info

def parse_api(code, apis, sdks = ["maixpy", "maixcdk"], header_name = None):
def parse_api(code, apis, sdks = ["maixpy", "maixcdk"], header_name = None, module_name = "maix"):

# if not header_name.endswith("api_example.hpp"):
# return apis, "", False, []
Expand Down Expand Up @@ -492,7 +492,7 @@ def parse_item_code_def(item):
apis_default = {
"type": "top_module",
"members": {
"maix": {
module_name: {
"type": "module",
"doc": {"breif": "MaixPy C/C++ API from MaixCDK"},
"members":{
Expand Down Expand Up @@ -544,12 +544,12 @@ def parse_item_code_def(item):
# parse api name
names = api.split(".")
# to limit only have maix module
if names[0] != "maix":
return None, "API name must start with maix, api: {} ".format(api), False, []
if names[0] != module_name:
return None, "API name must start with {}, api: {} ".format(module_name, api), False, []
for i in range(len(names)):
key = ".".join(names[:i+1])
if key not in items:
if key != "maix":
if key != module_name:
print("-- Auto add module {}, < {} >".format(key, header_name))
items[key] = {"type": "module", "doc": {"brief": "{} module".format(key)}, "members": {}, "auto_add": True}
final_keys.append(key)
Expand Down Expand Up @@ -649,14 +649,11 @@ def get_parent_node(root, api):

return apis, "", len(final_keys) > 0, keys

def parse_api_from_header(header_path, api_tree = {}, for_sdk = "maixpy"):
def parse_api_from_header(header_path, api_tree = {}, sdks = ["maixpy"], module_name = "maix"):
with open(header_path, "r", encoding="utf-8") as f:
code = f.read()
sdks = ["maixpy"]
if for_sdk == "maixcdk":
sdks.append("maixcdk")
try:
api_tree, msg, updated, keys = parse_api(code, api_tree, sdks, os.path.basename(header_path))
api_tree, msg, updated, keys = parse_api(code, api_tree, sdks, os.path.basename(header_path), module_name)
if api_tree is None:
raise Exception("parse_api_from_header {} error: {}".format(header_path, msg))
except Exception as e:
Expand All @@ -670,6 +667,7 @@ def parse_api_from_header(header_path, api_tree = {}, for_sdk = "maixpy"):
parser.add_argument('--vars', type=str, default="", help="CMake global variables file, a json file in build/config dir")
parser.add_argument('--doc', type=str, default="", help="API documentation output file")
parser.add_argument("--sdk_path", type=str, default="", help="MaixCDK SDK path")
parser.add_argument("--module_name", type=str, default="maix", help="module name")
args = parser.parse_args()

t = time.time()
Expand Down Expand Up @@ -701,7 +699,7 @@ def parse_api_from_header(header_path, api_tree = {}, for_sdk = "maixpy"):
rm = []
all_keys = {}
for header in headers:
api_tree, updated, keys = parse_api_from_header(header, api_tree, for_sdk = "maixcdk")
api_tree, updated, keys = parse_api_from_header(header, api_tree, sdks = ["maixpy", "maixcdk"], module_name=args.module_name)
if not updated:
rm.append(header)
for h, ks in all_keys.items():
Expand Down Expand Up @@ -733,7 +731,7 @@ def parse_api_from_header(header_path, api_tree = {}, for_sdk = "maixpy"):
json.dump(api_tree, f, indent=4)

doc_maix_sidebar = {
"label": "maix",
"label": args.module_name,
"collapsed": False,
"items": [
# {
Expand All @@ -749,8 +747,8 @@ def parse_api_from_header(header_path, api_tree = {}, for_sdk = "maixpy"):
>
> For MaixCDK developer: DO NOT edit this doc file manually, this doc is auto generated!
\n'''
top_api_keys = ["maix"]
module_members = api_tree["members"]["maix"]["members"]
top_api_keys = [args.module_name]
module_members = api_tree["members"][args.module_name]["members"]
def gen_modules_doc(module_members, parents):
sidebar_items = []
for m, v in module_members.items():
Expand Down Expand Up @@ -794,7 +792,7 @@ def gen_modules_doc(module_members, parents):
readme += "| --- | --- |\n"
for m, v in module_members.items():
# add link to module api doc
readme += "|[maix::{}](./maix/{}.md) | {} |\n".format(m, m, v["doc"]["brief"].replace("\n", "<br>")
readme += "|[{}::{}](./{}/{}.md) | {} |\n".format(args.module_name, m, m, args.module_name, v["doc"]["brief"].replace("\n", "<br>")
)
with open(readme_path, "w", encoding="utf-8") as f:
f.write(readme)
Expand Down
10 changes: 5 additions & 5 deletions tools/doc_tool/gen_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def gen_md_doc_from_comment(doc_comment):
continue
content += '### {}\n\n'.format(key.replace("_", "\_"))
content += item["doc"].get("brief", "") + "\n\n"
content += '| item | doc |\n'
content += '| item | describe |\n'
content += '| --- | --- |\n'
content += gen_md_doc_from_comment(item["doc"])
content += '| **values** | '
Expand All @@ -327,7 +327,7 @@ def gen_md_doc_from_comment(doc_comment):
continue
content += '### {}\n\n'.format(key.replace("_", "\_"))
content += item["doc"].get("brief", "") + "\n\n"
content += '| item | doc |\n'
content += '| item | description |\n'
content += '| --- | --- |\n'
content += gen_md_doc_from_comment(item["doc"])
content += '| **value** | **{}** |\n'.format(item["value"].replace('\n', '<br>')) if item["value"] else ""
Expand All @@ -345,7 +345,7 @@ def gen_func_info(key, item, overload_count = -1):
content += f'```python\n{item["py_def"]}\n```\n'
content += item["doc"].get("brief", "") + "\n\n"
if have_doc_kv(item["doc"]):
content += '| item | doc |\n'
content += '| item | description |\n'
content += '| --- | --- |\n'
content += gen_md_doc_from_comment(item["doc"])
content += '\n> C++ defination code:\n> ```cpp\n{}\n> ```\n'.format(multilines_add_prefix(item["def"], "> "))
Expand All @@ -368,7 +368,7 @@ def gen_func_info(key, item, overload_count = -1):
content += '### {}\n\n'.format(key.replace("_", "\_"))
content += item["doc"].get("brief", "") + "\n\n"
if have_doc_kv(item["doc"]):
content += '| item | doc |\n'
content += '| item | description |\n'
content += '| --- | --- |\n'
content += gen_md_doc_from_comment(item["doc"])
content += '\n> C++ defination code:\n> ```cpp\n{}\n> ```\n'.format(multilines_add_prefix(item["def"], "> "))
Expand All @@ -383,7 +383,7 @@ def gen_class_func_info(key, item, overload_count = -1):
if "py_def" in item:
content += f'```python\n{item["py_def"]}\n```\n'
content += item["doc"].get("brief", "") + "\n\n"
content += '| item | doc |\n'
content += '| item | description |\n'
content += '| --- | --- |\n'
content += '| **type** | {} |\n'.format(item["type"])
content += gen_md_doc_from_comment(item["doc"])
Expand Down
2 changes: 1 addition & 1 deletion tools/doc_tool/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
curr_dir = os.path.abspath(os.path.dirname(__file__))

header_path = os.path.join(curr_dir, "..", "..", "components", "basic", "include", "maix_api_example.hpp")
res = parse_api_from_header(header_path, api_tree = {}, for_sdk="maixcdk")
res = parse_api_from_header(header_path, api_tree = {}, for_sdk=["maixpy", "maixcdk"])
import json, os

with open("test.json", "w") as f:
Expand Down

0 comments on commit 66d1a53

Please sign in to comment.