feat: js/py generate declarations from comments (#30)
This commit is contained in:
+22
-20
@@ -5,6 +5,7 @@ import json
|
||||
import sys
|
||||
import importlib.util
|
||||
|
||||
|
||||
def parse_argv():
|
||||
func_name = sys.argv[0]
|
||||
func_data = None
|
||||
@@ -21,6 +22,7 @@ def parse_argv():
|
||||
|
||||
return func_name, func_data
|
||||
|
||||
|
||||
def load_func(func_name):
|
||||
func_file_name = f"{func_name}.py"
|
||||
func_path = os.path.join(os.environ["LLM_FUNCTIONS_DIR"], f"tools/{func_file_name}")
|
||||
@@ -33,20 +35,24 @@ def load_func(func_name):
|
||||
print(f"Invalid function: {func_file_name}")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def load_env(file_path):
|
||||
try:
|
||||
with open(file_path, 'r') as f:
|
||||
with open(file_path, "r") as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if line.startswith('#') or line == '':
|
||||
if line.startswith("#") or line == "":
|
||||
continue
|
||||
|
||||
key, *value = line.split('=')
|
||||
os.environ[key.strip()] = '='.join(value).strip()
|
||||
key, *value = line.split("=")
|
||||
os.environ[key.strip()] = "=".join(value).strip()
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
os.environ["LLM_FUNCTIONS_DIR"] = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
||||
|
||||
os.environ["LLM_FUNCTIONS_DIR"] = os.path.abspath(
|
||||
os.path.join(os.path.dirname(__file__), "..")
|
||||
)
|
||||
|
||||
load_env(os.path.join(os.environ["LLM_FUNCTIONS_DIR"], ".env"))
|
||||
|
||||
@@ -54,20 +60,16 @@ func_name, func_data = parse_argv()
|
||||
|
||||
os.environ["LLM_FUNCTION_NAME"] = func_name
|
||||
|
||||
if os.getenv("LLM_FUNCTION_ACTION") == "declarate":
|
||||
module = load_func(func_name)
|
||||
print(json.dumps(module.declarate(), indent=2))
|
||||
else:
|
||||
if not func_data:
|
||||
print("No json data")
|
||||
sys.exit(1)
|
||||
if not func_data:
|
||||
print("No json data")
|
||||
sys.exit(1)
|
||||
|
||||
args = None
|
||||
try:
|
||||
args = json.loads(func_data)
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
print("Invalid json data")
|
||||
sys.exit(1)
|
||||
args = None
|
||||
try:
|
||||
args = json.loads(func_data)
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
print("Invalid json data")
|
||||
sys.exit(1)
|
||||
|
||||
module = load_func(func_name)
|
||||
module.execute(args)
|
||||
module = load_func(func_name)
|
||||
module.main(**args)
|
||||
|
||||
Reference in New Issue
Block a user