U
    h	                     @   st   d Z ddlZddlmZmZmZmZ ddlmZm	Z	 e
eZddddd	d
dddddddddgZG dd deZdS )zUtil that calls bibtexparser.    N)AnyDictListMapping)	BaseModelroot_validatorZannotate	booktitleZeditorZhowpublishedjournalkeywordsZnoteZorganizationZ	publisherZschoolZseriestypedoiZissnZisbnc                   @   sx   e Zd ZdZG dd dZeddeedddZee	eee
f  d	d
dZdeee
f eeee
f dddZdS )BibtexparserWrappera   Wrapper around bibtexparser.

    To use, you should have the ``bibtexparser`` python package installed.
    https://bibtexparser.readthedocs.io/en/master/

    This wrapper will use bibtexparser to load a collection of references from
    a bibtex file and fetch document summaries.
    c                   @   s   e Zd ZdZdS )zBibtexparserWrapper.ConfigZforbidN)__name__
__module____qualname__extra r   r   H/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/utilities/bibtex.pyConfig'   s   r   T)pre)valuesreturnc                 C   s.   zddl }W n tk
r(   tdY nX |S )z7Validate that the python package exists in environment.r   Nz`Could not import bibtexparser python package. Please install it with `pip install bibtexparser`.)bibtexparserImportError)clsr   r   r   r   r   validate_environment*   s    
z(BibtexparserWrapper.validate_environment)pathr   c              	   C   s,   ddl }t|}||j}W 5 Q R X |S )z;Load bibtex entries from the bibtex file at the given path.r   N)r   openloadentries)selfr   r   filer   r   r   r   load_bibtex_entries7   s    
z'BibtexparserWrapper.load_bibtex_entriesF)entry
load_extrar   c                 C   s   | dp| d}d|kr&|d }nd|kr>d|d  }nd}| d| d| d	|| d
| d|d}|rtD ]}| |||< q|dd | D S )z!Get metadata for the given entry.r	   r   urlr   zhttps://doi.org/NZIDyeartitleauthorabstract)idZpublished_yearr'   publicationZauthorsr)   r%   c                 S   s   i | ]\}}|d k	r||qS )Nr   ).0kvr   r   r   
<dictcomp>V   s       z4BibtexparserWrapper.get_metadata.<locals>.<dictcomp>)getOPTIONAL_FIELDSitems)r    r#   r$   r+   r%   metafieldr   r   r   get_metadata?   s$    
	z BibtexparserWrapper.get_metadataN)F)r   r   r   __doc__r   r   r   r   strr   r   r"   r   boolr5   r   r   r   r   r      s   		 
 
r   )r6   loggingtypingr   r   r   r   Zlangchain_core.pydantic_v1r   r   	getLoggerr   loggerr1   r   r   r   r   r   <module>   s*   
