U
    h                     @   sP   d dl Z d dlmZ d dlmZmZ d dlmZ d dlm	Z	 G dd de	Z
dS )    N)Path)IteratorUnion)Document)
BaseLoaderc                   @   s8   e Zd ZdZeeef dddZee	 dddZ
dS )	
TomlLoaderziLoad `TOML` files.

    It can load a single source file or several files in a single
    directory.
    )sourcec                 C   s   t || _dS )z:Initialize the TomlLoader with a source file or directory.N)r   r   )selfr    r
   M/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/document_loaders/toml.py__init__   s    zTomlLoader.__init__)returnc           	      c   s   ddl }| j r(| jjdkr(| jg}n$| j rDt| jd}ntd|D ]}|jdddv}|	 }z.|
|}tt|d	t|id
}|V  W n8 |jk
r } ztd| d|  W 5 d}~X Y nX W 5 Q R X qPdS )zALazily load the TOML documents from the source file or directory.r   Nz.tomlz	**/*.tomlz Invalid source path or file typerzutf-8)encodingr   )Zpage_contentmetadatazError parsing TOML file z: )tomlir   is_filesuffixis_dirlistglob
ValueErroropenreadloadsr   jsondumpsstrZTOMLDecodeErrorprint)	r	   r   files	file_pathfilecontentdatadocer
   r
   r   	lazy_load   s$    




zTomlLoader.lazy_loadN)__name__
__module____qualname____doc__r   r   r   r   r   r   r&   r
   r
   r
   r   r   
   s   r   )r   pathlibr   typingr   r   Zlangchain_core.documentsr   Z)langchain_community.document_loaders.baser   r   r
   r
   r
   r   <module>   s
   