o
    ȳg	                     @   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)annotate	booktitleeditorhowpublishedjournalkeywordsnoteorganization	publisherschoolseriestypedoiissn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.
    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/chatdoc2/venv/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titleauthorabstract)idpublished_yearr1   publicationauthorsr3   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-   r6   r.   metafieldr"   r"   r#   get_metadataA   s$   
	z BibtexparserWrapper.get_metadataN)F)__name__
__module____qualname____doc__r   model_configr   classmethodr   r   r$   strr   r+   r   boolrA   r"   r"   r"   r#   r      s"    		

r   )rE   loggingtypingr   r   r   r   pydanticr   r   r   	getLoggerrB   loggerr=   r   r"   r"   r"   r#   <module>   s    
