o
    Zh	                     @   s^   d Z ddlZddlmZmZmZmZ ddlmZm	Z	m
Z
 eeZg dZG dd deZdS )zUtil that calls bibtexparser.    N)AnyDictListMapping)	BaseModel
ConfigDictmodel_validator)Zannotate	booktitleZeditorZhowpublishedjournalkeywordsZnoteZorganizationZ	publisherschoolZseriestypedoiZissnZisbnc                	   @   s   e Zd ZdZeddZeddedede	fdd	Z
d
edeeee	f  fddZ	ddeee	f dedeee	f f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.
    Zforbid)extrabefore)modevaluesreturnc                 C   s&   zddl }W |S  ty   tdw )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   [/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/utilities/bibtex.pyvalidate_environment+   s   
z(BibtexparserWrapper.validate_environmentpathc                 C   sB   ddl }t|}||j}W d   |S 1 sw   Y  |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_entries9   s   

z'BibtexparserWrapper.load_bibtex_entriesFentry
load_extrac                 C   s   | dp	| d}d|v r|d }nd|v rd|d  }nd}| d| d| d	|| d
| d|d}|rHtD ]	}| |||< q>dd | D S )z!Get metadata for the given entry.r
   r	   urlr   zhttps://doi.org/NIDyeartitleZauthorabstract)idZpublished_yearr'   publicationZauthorsr(   r$   c                 S   s   i | ]\}}|d ur||qS )Nr   ).0kvr   r   r   
<dictcomp>X   s    z4BibtexparserWrapper.get_metadata.<locals>.<dictcomp>)getOPTIONAL_FIELDSitems)r   r"   r#   r*   r$   metafieldr   r   r   get_metadataA   s$   
	z BibtexparserWrapper.get_metadataN)F)__name__
__module____qualname____doc__r   Zmodel_configr   classmethodr   r   r   strr   r!   r   boolr4   r   r   r   r   r      s"    		

r   )r8   loggingtypingr   r   r   r   Zpydanticr   r   r   	getLoggerr5   loggerr0   r   r   r   r   r   <module>   s    
