[docs]classTomlLoader(BaseLoader):"""Load `TOML` files. It can load a single source file or several files in a single directory. """
[docs]def__init__(self,source:Union[str,Path]):"""Initialize the TomlLoader with a source file or directory."""self.source=Path(source)
[docs]deflazy_load(self)->Iterator[Document]:"""Lazily load the TOML documents from the source file or directory."""importtomliifself.source.is_file()andself.source.suffix==".toml":files=[self.source]elifself.source.is_dir():files=list(self.source.glob("**/*.toml"))else:raiseValueError("Invalid source path or file type")forfile_pathinfiles:withfile_path.open("r",encoding="utf-8")asfile:content=file.read()try:data=tomli.loads(content)doc=Document(page_content=json.dumps(data),metadata={"source":str(file_path)},)yielddocexcepttomli.TOMLDecodeErrorase:print(f"Error parsing TOML file {file_path}: {e}")# noqa: T201