U
    e-                     @   sl   d dl Z d dlZd dlZd dlmZmZmZmZmZ d dl	m
Z
 eeZdgZdd ZG dd deZdS )    N)BibDatabaseBibDataStringas_textBibDataStringExpressionSTANDARD_TYPES)BibtexExpressionBibTexParserc                 O   s   t ||}|| S )N)r   parse)dataargskwargsparser r   r/mounts/lovelace/software/anaconda3/envs/qiime2-shotgun-2023.9/lib/python3.8/site-packages/bibtexparser/bparser.pyr	      s    
r	   c                       s   e Zd ZdZd fdd	Zd ddZd!d	d
Zd"ddZdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Z  ZS )#r   a  
    A parser for reading BibTeX bibliographic data files.

    Example::

        from bibtexparser.bparser import BibTexParser

        bibtex_str = ...

        parser = BibTexParser()
        parser.ignore_nonstandard_types = False
        parser.homogenize_fields = False
        parser.common_strings = False
        bib_database = bibtexparser.loads(bibtex_str, parser)

    :param customization: function or None (default)
        Customization to apply to parsed entries.
    :param ignore_nonstandard_types: bool (default True)
        If True ignores non-standard bibtex entry types.
    :param homogenize_fields: bool (default False)
        Common field name replacements (as set in alt_dict attribute).
    :param interpolate_strings: bool (default True)
        If True, replace bibtex string by their value, else uses
        BibDataString objects.
    :param common_strings: bool (default True)
        Include common string definitions (e.g. month abbreviations) to
        the bibtex file.
    :param add_missing_from_crossref: bool (default False)
        Resolve BibTeX references set in the crossref field for BibTeX entries
        and add the fields from the referenced entry to the referencing entry.
    Nc                    s(   |dkrt t| | S t|f|S dS )z~
        To catch the old API structure in which creating the parser would
        immediately parse and return data.
        N)superr   __new__r	   )clsr
   r   	__class__r   r   r   <   s    zBibTexParser.__new__TFc              
   C   st   d| _ d| _t | _|| _| jr*| j  || _|| _|| _|| _	d| _
|| _ddddddddd	d
	| _|   dS )zs
        Creates a parser for reading BibTeX files

        :return: parser
        :rtype: `BibTexParser`
        r   Futf8keywordauthoreditorurlsubjectcrossref)	ZkeywkeywordsauthorsZeditorsurlslinkZlinksZsubjectsZxrefN)_parse_call_countexpect_multiple_parser   bib_databasecommon_stringsload_common_stringscustomizationignore_nonstandard_typeshomogenize_fieldsinterpolate_stringsencodingadd_missing_from_crossrefalt_dict_init_expressions)selfr
   r%   r&   r'   r(   r#   r*   r   r   r   __init__I   s.    
zBibTexParser.__init__c              
   C   s   |  j d7  _ | j dkr.| js.tjdtdd | |}z| j| W n< | jjk
r } zt	
d|j |st|W 5 d}~X Y nX | jr| j  | jS )aF  Parse a BibTeX string into an object

        :param bibtex_str: BibTeX string
        :type: str or unicode
        :param partial: If True, print errors only on parsing failures.
            If False, an exception is raised.
        :type: boolean
        :return: bibliographic database
        :rtype: BibDatabase
              zThe parser has been called more than once. Subsequent parse calls lead to a combined BibTeX library. 
To avoid the warning, set property `parser.expect_multiple_parse` to `True`.)category
stacklevelz(Could not parse properly, starting at %sN)r    r!   warningswarnUserWarning_bibtex_file_obj_expr	parseFileParseExceptionloggererrorliner*   r"   )r-   
bibtex_strpartialZbibtex_file_objexcr   r   r   r	      s      

zBibTexParser.parsec                 C   s   | j | |dS )aF  Parse a BibTeX file into an object

        :param file: BibTeX file or file-like object
        :type: file
        :param partial: If True, print errors only on parsing failures.
            If False, an exception is raised.
        :type: boolean
        :return: bibliographic database
        :rtype: BibDatabase
        )r>   )r	   read)r-   filer>   r   r   r   
parse_file   s    zBibTexParser.parse_filec                    s   t   _ j fdd  jtj  jj fdd  jj fdd  jj	 fdd  jj
 fdd  jj fdd dS )	zA
        Defines all parser expressions used internally.
        c                    s   t  j|d S Nr   )r   r"   sltr-   r   r   <lambda>   s    z0BibTexParser._init_expressions.<locals>.<lambda>c                    s      |d|d|dS )N	EntryTypeKeyFields)
_add_entrygetrD   rH   r   r   rI      s     c                    s     |d S rC   _add_commentrD   rH   r   r   rI          c                    s     |d S rC   rO   rD   rH   r   r   rI      rQ   c                    s     |d S rC   )_add_preamblerD   rH   r   r   rI      rQ   c                    s     |d j|d S )N
StringNameStringValue)_add_stringnamerD   rH   r   r   rI      s   N)r   r7   set_string_name_parse_actionadd_log_functionr:   debugentryaddParseActionimplicit_commentexplicit_commentpreamble_decl
string_defrH   r   rH   r   r,      s(    





zBibTexParser._init_expressionsc                 C   s   d}t |trBt|| jd}t|dkrx|d |krx|dd  }n6t|dkrj|d d |krj|dd  }|j| jd}t|S )Ns   ﻿ignorer/   r      )r)   )
isinstancestrr)   lendecodeioStringIO)r-   r=   byter   r   r   r6      s    
zBibTexParser._bibtex_file_objc                 C   s&   |r|dkrdS | j rt|S |S dS )z Clean instring before adding to dictionary

        :param val: a value
        :type val: string
        :returns: string -- value
        z{} N)r(   r   )r-   valr   r   r   
_clean_val   s
    zBibTexParser._clean_valc                 C   s$   |  }t|tst|dS |S dS )z Lowercase a key and return as unicode.

        :param key: a key
        :type key: string
        :returns: (unicode) string -- value
        zutf-8N)lowerrb   rc   r-   keyr   r   r   
_clean_key   s    

zBibTexParser._clean_keyc                 C   s0   |  |}| jr,|t| j kr,| j| }|S )z Clean a bibtex field key and homogenize alternative forms.

        :param key: a key
        :type key: string
        :returns: string -- value
        )ro   r'   listr+   keysrm   r   r   r   _clean_field_key  s
    

zBibTexParser._clean_field_keyc                 C   s   i }|  |}| jr,|tkr,td| dS |D ]}| || || |< q0||d< ||d< |dd}| jr|dk	r||d< | j	dk	rt
d | 	|}| jj| dS )ap   Adds a parsed entry.
        Includes checking type and fields, cleaning, applying customizations.

        :param entry_type: the entry type
        :type entry_type: string
        :param entry_id: the entry bibid
        :type entry_id: string
        :param fields: the fields and values
        :type fields: dictionary
        :returns: string -- value
        z+Entry type %s not standard. Not considered.N	ENTRYTYPEIDr   	_crossrefz$Apply customizations and return dict)ro   r&   r   r:   warningrk   rr   rN   r*   r%   rY   r"   entriesappend)r-   
entry_typeZentry_idfieldsdrn   r   r   r   r   rM     s$    



zBibTexParser._add_entryc                 C   s$   t d|   | jj| dS )z
        Stores a comment in the list of comment.

        :param comment: the parsed comment
        :type comment: string
        z#Store comment in list of comments: N)r:   rY   __repr__r"   commentsrx   )r-   commentr   r   r   rP   2  s    zBibTexParser._add_commentc                 C   s@   || j jkrtd| td|| | || j j|< dS )z
        Stores a new string in the string dictionary.

        :param string_key: the string key
        :type string_key: string
        :param string: the string value
        :type string: string
        z(Overwriting existing string for key: %s.zStore string: {} -> {}N)r"   stringsr:   rv   rY   formatrk   )r-   Z
string_keystringr   r   r   rU   =  s    	zBibTexParser._add_stringc                 C   s   t d | jj| dS )zq
        Stores a preamble.

        :param preamble: the parsed preamble
        :type preamble: string
        z#Store preamble in list of preamblesN)r:   rY   r"   	preamblesrx   )r-   preambler   r   r   rR   L  s    
zBibTexParser._add_preamble)N)NNTFTTF)F)F)__name__
__module____qualname____doc__r   r.   r	   rB   r,   r6   rk   ro   rr   rM   rP   rU   rR   __classcell__r   r   r   r   r      s*           
D
 
!!)rf   loggingr3   Zbibtexparser.bibdatabaser   r   r   r   r   Zbibtexparser.bibtexexpressionr   	getLoggerr   r:   __all__r	   objectr   r   r   r   r   <module>	   s   
