
    RiF/                         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j                  e      ZdgZd Z G d de      Zy)    N)BibDatabaseBibDataStringas_textBibDataStringExpressionSTANDARD_TYPES)BibtexExpressionBibTexParserc                 :    t        |i |}|j                  |       S N)r	   parse)dataargskwargsparsers       L/home/agent/.friday_env/lib/python3.12/site-packages/bibtexparser/bparser.pyr   r      s     4*6*F<<    c                        e Zd ZdZd fd	Z	 	 	 	 	 	 	 ddZddZddZd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Z x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.
    c                 F    |t         t        |   |       S t        |fi |S )z~
        To catch the old API structure in which creating the parser would
        immediately parse and return data.
        )superr	   __new__r   )clsr   r   	__class__s      r   r   zBibTexParser.__new__<   s.     <s3C88 &&&r   c           
      .   d| _         d| _        t               | _        || _        | j                  r| j                  j                          || _        || _        || _        || _	        d| _
        || _        ddddddddd	d
	| _        | j                          y)zs
        Creates a parser for reading BibTeX files

        :return: parser
        :rtype: `BibTexParser`
        r   Futf8keywordauthoreditorurlsubjectcrossref)	keywkeywordsauthorseditorsurlslinklinkssubjects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/   r0   r1   r2   r-   r4   s           r   __init__zBibTexParser.__init__I   s     "# &+"'M -113 + )A%
 "3 $7   *C& "  "

 	 r   c                    | xj                   dz  c_         | j                   dk(  r(| j                  st        j                  dt        d       | j                  |      }	 | j                  j                  |       | j                  r| j                  j                          | j                  S # | j                  j                  $ r.}t        j                  d|j                         |s|Y d}~td}~ww xY w)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
parse_fileParseExceptionloggererrorliner4   r,   )r7   
bibtex_strpartialbibtex_file_objexcs        r   r   zBibTexParser.parse   s     	!#!!Q&t/I/IMM i $/1>
 //
;	JJ!!/2 ))779    zz(( 	LLCSXXN	 	s   B, ,C3$C..C3c                 D    | j                  |j                         |      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
        )rI   )r   read)r7   filerI   s      r   rC   zBibTexParser.parse_file   s     zz$))+wz77r   c                 B    t                _         j                  j                   fd        j                  j                  t        j
                          j                  j                  j                   fd        j                  j                  j                   fd        j                  j                  j                   fd        j                  j                  j                   fd        j                  j                  j                   fd       y)zA
        Defines all parser expressions used internally.
        c                 6    t        j                  |d         S Nr   )r   r,   sltr7   s      r   <lambda>z0BibTexParser._init_expressions.<locals>.<lambda>   s    d//16 r   c                     j                  |j                  d      |j                  d      |j                  d            S )N	EntryTypeKeyFields)
_add_entrygetrR   s      r   rV   z0BibTexParser._init_expressions.<locals>.<lambda>   s1    DOOk"AEE%L!%%/C r   c                 ,    j                  |d         S rQ   _add_commentrR   s      r   rV   z0BibTexParser._init_expressions.<locals>.<lambda>       D--ad3 r   c                 ,    j                  |d         S rQ   r^   rR   s      r   rV   z0BibTexParser._init_expressions.<locals>.<lambda>   r`   r   c                 ,    j                  |d         S rQ   )_add_preamblerR   s      r   rV   z0BibTexParser._init_expressions.<locals>.<lambda>   s    D..qt4 r   c                 H    j                  |d   j                  |d         S )N
StringNameStringValue)_add_stringnamerR   s      r   rV   z0BibTexParser._init_expressions.<locals>.<lambda>   s&    D,,Q|_-A-A-.}-=? r   N)r   rB   set_string_name_parse_actionadd_log_functionrE   debugentryadd_parse_actionimplicit_commentexplicit_commentpreamble_decl
string_def)r7   s   `r   r6   zBibTexParser._init_expressions   s     &'
 	

//7	8
 	

##FLL1 	

))C	 	

##443	 	

##443	 	

  114	 	

..?	r   c                 $   d}t        |t              r3t        || j                  d      }t        |      dk\  rE|d   |k(  r=|dd  }n7t        |      dk\  r|d d |k(  r|dd  }|j	                  | j                        }t        j                  |      S )Ns   ﻿ignorer:   r      )r3   )
isinstancestrr3   lendecodeioStringIO)r7   rH   bytes      r   rA   zBibTexParser._bibtex_file_obj   s    j#&tT]]H5D:!#
1(='^
:!#
2A$(>'^
#**DMM*BJ{{:&&r   c                 D    |r|dk(  ry| j                   rt        |      S |S )z Clean instring before adding to dictionary

        :param val: a value
        :type val: string
        :returns: string -- value
        z{} )r2   r   )r7   vals     r   
_clean_valzBibTexParser._clean_val   s(     cTk%%3<Jr   c                 ^    |j                         }t        |t              st        |d      S |S )z Lowercase a key and return as unicode.

        :param key: a key
        :type key: string
        :returns: (unicode) string -- value
        zutf-8)lowerru   rv   r7   keys     r   
_clean_keyzBibTexParser._clean_key   s+     iik#s#sG$$Jr   c                     | j                  |      }| j                  r4|t        | j                  j	                               v r| j                  |   }|S )z Clean a bibtex field key and homogenize alternative forms.

        :param key: a key
        :type key: string
        :returns: string -- value
        )r   r1   listr5   keysr   s     r   _clean_field_keyzBibTexParser._clean_field_key  sH     ooc"!!d4==--/00mmC(
r   c                    i }| j                  |      }| j                  r|t        vrt        j	                  d|       y|D ](  }| j                  ||         || j                  |      <   * ||d<   ||d<   |j                  dd      }| j                  r|||d<   | j                  &t        j                  d       | j                  |      }| j                  j                  j                  |       y)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)r   r0   r   rE   warningr   r   r\   r4   r/   rk   r,   entriesappend)r7   
entry_typeentry_idfieldsdr   r    s          r   r[   zBibTexParser._add_entry  s     __Z0
((Z~-MNNH%' 	IC,0OOF3K,HAd##C()	I#+$55T*))h.B%AkN)LL?@""1%A!!((+r   c                     t         j                  d|j                         z          | j                  j                  j                  |       y)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)rE   rk   __repr__r,   commentsr   )r7   comments     r   r_   zBibTexParser._add_comment2  s>     	:%%'( 	)""))'2r   c                     || j                   j                  v rt        j                  d|       t        j	                  dj                  ||             | j                  |      | j                   j                  |<   y)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,   stringsrE   r   rk   formatr   )r7   
string_keystrings      r   rg   zBibTexParser._add_string=  sc     **222NNE%'.55j&IJ040G!!*-r   c                 x    t         j                  d       | j                  j                  j	                  |       y)zq
        Stores a preamble.

        :param preamble: the parsed preamble
        :type preamble: string
        z#Store preamble in list of preamblesN)rE   rk   r,   	preamblesr   )r7   preambles     r   rc   zBibTexParser._add_preambleL  s+     	:;##**84r   r   )NNTFTTF)F)__name__
__module____qualname____doc__r   r8   r   rC   r6   rA   r   r   r   r[   r_   rg   rc   __classcell__)r   s   @r   r	   r	      sg    @' !#*.#(%) $+0B!H!@8B',B	3H5r   )ry   loggingr>   bibtexparser.bibdatabaser   r   r   r   r   bibtexparser.bibtexexpressionr   	getLoggerr   rE   __all__r   objectr	    r   r   <module>r      sL    
  O O :			8	$

y56 y5r   