Merge branch 'feature/explit_io' into 'mgx_ops'

feat: Implemenet of RFC236 #3

See merge request pub/MetaGPT!110
This commit is contained in:
林义章 2024-06-05 06:47:30 +00:00
commit fccbc9d9da
34 changed files with 1060 additions and 432 deletions

View file

@ -667,6 +667,8 @@ def role_raise_decorator(func):
@handle_exception
async def aread(filename: str | Path, encoding="utf-8") -> str:
"""Read file asynchronously."""
if not filename or not Path(filename).exists():
return ""
try:
async with aiofiles.open(str(filename), mode="r", encoding=encoding) as reader:
content = await reader.read()
@ -899,3 +901,51 @@ async def init_python_folder(workdir: str | Path):
return
async with aiofiles.open(init_filename, "a"):
os.utime(init_filename, None)
def get_markdown_code_block_type(filename: str) -> str:
if not filename:
return ""
ext = Path(filename).suffix
types = {
".py": "python",
".js": "javascript",
".java": "java",
".cpp": "cpp",
".c": "c",
".html": "html",
".css": "css",
".xml": "xml",
".json": "json",
".yaml": "yaml",
".md": "markdown",
".sql": "sql",
".rb": "ruby",
".php": "php",
".sh": "bash",
".swift": "swift",
".go": "go",
".rs": "rust",
".pl": "perl",
".asm": "assembly",
".r": "r",
".scss": "scss",
".sass": "sass",
".lua": "lua",
".ts": "typescript",
".tsx": "tsx",
".jsx": "jsx",
".yml": "yaml",
".ini": "ini",
".toml": "toml",
".svg": "xml", # SVG can often be treated as XML
# Add more file extensions and corresponding code block types as needed
}
return types.get(ext, "")
def to_markdown_code_block(val: str, type_: str = "") -> str:
if not val:
return val or ""
val = val.replace("```", "\\`\\`\\`")
return f"\n```{type_}\n{val}\n```\n"

View file

@ -72,7 +72,6 @@ class File:
class MemoryFileSystem(_MemoryFileSystem):
@classmethod
def _strip_protocol(cls, path):
return super()._strip_protocol(str(path))

View file

@ -156,6 +156,8 @@ class GitRepository:
:param local_path: The local path to check.
:return: True if the directory is a Git repository, False otherwise.
"""
if not local_path:
return False
git_dir = Path(local_path) / ".git"
if git_dir.exists() and is_git_dir(git_dir):
return True

View file

@ -4,11 +4,10 @@ from __future__ import annotations
from typing import Generator, Optional
from urllib.parse import urljoin, urlparse
import htmlmin
from bs4 import BeautifulSoup
from pydantic import BaseModel, PrivateAttr
import htmlmin
class WebPage(BaseModel):
inner_text: str

View file

@ -140,10 +140,11 @@ class ProjectRepo(FileRepository):
return bool(code_files)
def with_src_path(self, path: str | Path) -> ProjectRepo:
try:
self._srcs_path = Path(path).relative_to(self.workdir)
except ValueError:
self._srcs_path = Path(path)
path = Path(path)
if path.is_relative_to(self.workdir):
self._srcs_path = path.relative_to(self.workdir)
else:
self._srcs_path = path
return self
@property