mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-05-05 05:42:37 +02:00
refine code
This commit is contained in:
parent
31eb3fe0ee
commit
ef4323c6b4
1 changed files with 12 additions and 4 deletions
|
|
@ -13,28 +13,36 @@ from pydantic import BaseModel, model_validator
|
|||
|
||||
|
||||
class YamlModel(BaseModel):
|
||||
"""Base class for yaml model"""
|
||||
|
||||
extra_fields: Optional[Dict[str, str]] = None
|
||||
|
||||
@classmethod
|
||||
def read_yaml(cls, file_path: Path) -> Dict:
|
||||
def read_yaml(cls, file_path: Path, encoding: str = "utf-8") -> Dict:
|
||||
"""Read yaml file and return a dict"""
|
||||
if not file_path.exists():
|
||||
return {}
|
||||
with open(file_path, "r") as file:
|
||||
with open(file_path, "r", encoding=encoding) as file:
|
||||
return yaml.safe_load(file)
|
||||
|
||||
@classmethod
|
||||
def from_yaml_file(cls, file_path: Path) -> "YamlModel":
|
||||
"""Read yaml file and return a YamlModel instance"""
|
||||
return cls(**cls.read_yaml(file_path))
|
||||
|
||||
def to_yaml_file(self, file_path: Path) -> None:
|
||||
with open(file_path, "w") as file:
|
||||
def to_yaml_file(self, file_path: Path, encoding: str = "utf-8") -> None:
|
||||
"""Dump YamlModel instance to yaml file"""
|
||||
with open(file_path, "w", encoding=encoding) as file:
|
||||
yaml.dump(self.model_dump(), file)
|
||||
|
||||
|
||||
class YamlModelWithoutDefault(YamlModel):
|
||||
"""YamlModel without default values"""
|
||||
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def check_not_default_config(cls, values):
|
||||
"""Check if there is any default config in config.yaml"""
|
||||
if any(["YOUR" in v for v in values]):
|
||||
raise ValueError("Please set your config in config.yaml")
|
||||
return values
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue