mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-05 14:55:18 +02:00
添加代码文件输出保存,创建项目文件夹,使用项目文件夹隔离
完整代码保存前,可考虑拼接全量代码再输出
This commit is contained in:
parent
091a9e888f
commit
0231cfdcc7
2 changed files with 70 additions and 0 deletions
40
metagpt/utils/save_code.py
Normal file
40
metagpt/utils/save_code.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# @Date : 12/12/2023 4:14 PM
|
||||
# @Author : stellahong (stellahong@fuzhi.ai)
|
||||
# @Desc :
|
||||
import os
|
||||
import json
|
||||
|
||||
from metagpt.const import DATA_PATH
|
||||
|
||||
def save_code_file(name: str, code_context: str, file_format: str = "py") -> None:
|
||||
"""
|
||||
Save code files to a specified path.
|
||||
|
||||
Args:
|
||||
- name (str): The name of the folder to save the files.
|
||||
- code_context (str): The code content.
|
||||
- file_format (str, optional): The file format, supports 'py' (Python file) and 'json' (JSON file). Default is 'py'.
|
||||
|
||||
Returns:
|
||||
- None
|
||||
"""
|
||||
# Create the folder path if it doesn't exist
|
||||
os.makedirs(name=DATA_PATH / "output" / f"{name}", exist_ok=True)
|
||||
|
||||
# Choose to save as a Python file or a JSON file based on the file format
|
||||
file_path = DATA_PATH / "output" / f"{name}/code.{file_format}"
|
||||
if file_format == "py":
|
||||
with open(file_path, "w", encoding="utf-8") as fp:
|
||||
fp.write(code_context + "\n\n")
|
||||
elif file_format == "json":
|
||||
# Parse the code content as JSON and save
|
||||
data = {"code": code_context}
|
||||
with open(file_path, "w", encoding="utf-8") as fp:
|
||||
json.dump(data, fp, indent=2)
|
||||
else:
|
||||
raise ValueError("Unsupported file format. Please choose 'py' or 'json'.")
|
||||
|
||||
|
||||
|
||||
|
||||
30
tests/metagpt/utils/test_save_code.py
Normal file
30
tests/metagpt/utils/test_save_code.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# @Date : 12/12/2023 4:17 PM
|
||||
# @Author : stellahong (stellahong@fuzhi.ai)
|
||||
# @Desc :
|
||||
import os
|
||||
import json
|
||||
|
||||
from metagpt.utils.save_code import save_code_file, DATA_PATH
|
||||
|
||||
|
||||
def test_save_code_file_python():
|
||||
save_code_file("example", "print('Hello, World!')")
|
||||
file_path = DATA_PATH / "output" / "example" / "code.py"
|
||||
assert os.path.exists(file_path), f"File does not exist: {file_path}"
|
||||
|
||||
|
||||
def test_save_code_file_python():
|
||||
save_code_file("example", "print('Hello, World!')")
|
||||
file_path = DATA_PATH / "output" / "example" / "code.py"
|
||||
with open(file_path, "r", encoding="utf-8") as fp:
|
||||
content = fp.read()
|
||||
assert "print('Hello, World!')" in content, "File content does not match"
|
||||
|
||||
def test_save_code_file_json():
|
||||
save_code_file("example_json", "print('Hello, JSON!')", file_format="json")
|
||||
file_path = DATA_PATH / "output" / "example_json" / "code.json"
|
||||
with open(file_path, "r", encoding="utf-8") as fp:
|
||||
data = json.load(fp)
|
||||
assert "code" in data, "JSON key 'code' is missing"
|
||||
assert data["code"] == "print('Hello, JSON!')", "JSON content does not match"
|
||||
Loading…
Add table
Add a link
Reference in a new issue