refactor: py/js entry func name (#31)

This commit is contained in:
sigoden
2024-06-07 16:10:05 +08:00
committed by GitHub
parent 739a832d87
commit e1d895cc9a
9 changed files with 32 additions and 24 deletions
+11 -7
View File
@@ -7,13 +7,14 @@ import re
import sys
from collections import OrderedDict
TOOL_ENTRY_FUNC = "run"
def main():
def main(is_tool = True):
scriptfile = sys.argv[1]
with open(scriptfile, "r", encoding="utf-8") as f:
contents = f.read()
functions = extract_functions(contents)
functions = extract_functions(contents, is_tool)
declarations = []
for function in functions:
func_name, docstring, func_args = function
@@ -22,15 +23,16 @@ def main():
build_declaration(func_name, description, params, func_args)
)
name = os.path.splitext(os.path.basename(scriptfile))[0]
if declarations:
declarations = declarations[0:1]
declarations[0]["name"] = name
if is_tool:
name = os.path.splitext(os.path.basename(scriptfile))[0]
if declarations:
declarations = declarations[0:1]
declarations[0]["name"] = name
print(json.dumps(declarations, indent=2))
def extract_functions(contents: str):
def extract_functions(contents: str, is_tool: bool):
tree = ast.parse(contents)
output = []
for node in ast.walk(tree):
@@ -39,6 +41,8 @@ def extract_functions(contents: str):
func_name = node.name
if func_name.startswith("_"):
continue
if is_tool and func_name != TOOL_ENTRY_FUNC:
continue
docstring = ast.get_docstring(node) or ""
func_args = OrderedDict()
for arg in node.args.args: