U
    e&                     @   sh   d dl Zddlm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G dd deZdS )    N   )BibDataStringExpressionc                 C   s>   |   }t|dkr4|d gdd |dd D  }d|S )z?Removes leading and trailing whitespaces in all but first line.r   r   c                 S   s   g | ]}|  qS  )lstrip).0lr   r   {/mounts/lovelace/software/anaconda3/envs/qiime2-shotgun-2023.9/lib/python3.8/site-packages/bibtexparser/bibtexexpression.py
<listcomp>   s     z*_strip_after_new_lines.<locals>.<listcomp>N
)
splitlineslenjoin)slinesr   r   r   _strip_after_new_lines   s     r   c                 C   s$   t | tr| t | S t| S dS )zuRemoves leading and trailing whitespaces in all but first line.

    :param s: string or BibDataStringExpression
    N)
isinstancer   apply_on_stringsr   )r   r   r   r   strip_after_new_lines   s    

r   c                    s    fdd}  | dS )zDRegister a callback on expression parsing with the adequate message.c                    s   d  j| d S )NzFound {}: {})formatZresultsNamer   r   texprlog_funcr   r   action   s    z'add_logger_parse_action.<locals>.actionN)addParseAction)r   r   r   r   r   r   add_logger_parse_action   s    r   c                 C   s   t |dkst|d S )Nr   r   )r   AssertionErrorstring_locationtokenr   r   r   first_token'   s    r"   c                 C   s   |d r|d  dS d S )Nr   r
   )rstripr   r   r   r   remove_trailing_newlines-   s    r$   c                 C   sX   t |d dk rdS |d d dkr(dnd}|d d dkr@dnd }|d || S d S )Nr   r    {})r   )r   r    r!   startendr   r   r   remove_braces2   s
    r+   c                 C   s:   | d}| d}t|tjr(|d }| dt|fS )zO
    Looks for parsed element named 'Field'.

    :returns: (name, value).
    FieldValuer   	FieldName)getr   ppZParseResultsr   )r   r    r!   fieldvaluer   r   r   field_to_pair;   s    

r3   c                 C   s0   t d|  t d t d|  t d B S )z
    exp -> (exp)|{exp}
    r&   r(   ())r0   Suppress)expr   r   r   in_braces_or_parsM   s    r8   c                   @   sF   e Zd ZdZejZdd Zdd Zdd Zdd	 Z	d
d Z
dd ZdS )BibtexExpressionae  Gives access to pyparsing expressions.

    Attributes are pyparsing expressions for the following elements:

    * main_expression: the bibtex file
    * string_def: a string definition
    * preamble_decl: a preamble declaration
    * explicit_comment: an explicit comment
    * entry: an entry definition
    * implicit_comment: an implicit comment

    c                    s    dd  td}td}td}ttjd d}  dd  | j ttjd	}td
}t	 }|t
dt||B  d dK }|t tjddd d}td}	t
dt|	|B  d d}
|
tj tj|
|B |B ddd}| j ||B d}tdttj d}|t tdd} fdd}|| ttjd d}|t t|td | d}|t t|ttd d }|d!d  |t|td |  d" _t td t B }t|tj
t|d#d$ d% _ jt  jt d&d' }tj
t||d#d$d( _  j t t|t|td |d)  d* _!t|t| d+ _"t j! j"B  jB  jB  j B  _#d S ),Nc                 S   s   d S Nr   r   r   r   r   <lambda>g       z+BibtexExpression.__init__.<locals>.<lambda>z@stringz	@preamblez@commentz_-:Z
StringNamec                 S   s   d S r:   r   r   r   r   r   r;   q   r<   Integerz{}r&   r(   ZBracedValue)Z
ignoreExprz"{}"ZQuotedValue#)delimZStringExpressionr-   @Z	EntryType,ZKeyc                    s^   t | || }t|dk r, j| |ddt|D ]$\}}| r4 j| || ddq4|S )zParse action for validating citekeys.

            It ensures citekey is not empty and has no space.

            :args: see pyparsing documentation.
            r   zEmpty citekeys are not allowed.)locmsgz#Whitespace not allowed in citekeys.)r"   stripr   ParseException	enumerateisspace)r   r    r!   keyicselfr   r   citekeyParseAction   s       z5BibtexExpression.__init__.<locals>.citekeyParseActionz_-().+r.   =r,   Fieldsc                 S   s   dd t |dD S )Nc                 S   s   i | ]\}}||qS r   r   )r   kvr   r   r   
<dictcomp>   s      z?BibtexExpression.__init__.<locals>.<lambda>.<locals>.<dictcomp>rP   )reversedr/   r   r   r   r   r;      r<   ZEntryT)ZasStringZExplicitCommentc                 S   s   | d st dd S )Nr   zMatch must not be empty.)r0   rF   )r   r   r   r   mustNotBeEmpty   s    z1BibtexExpression.__init__.<locals>.mustNotBeEmptyZImplicitCommentZStringValueZStringDefinitionZPreambleDeclaration)$set_string_name_parse_actionr0   ZCaselessKeywordWordZ	alphanumsr   _string_name_parse_actionnumsZ
CharsNotInZForwardZoriginalTextForZ
ZeroOrMoreZsetParseActionr+   Z
nestedExprZremoveQuotesZdelimitedList_string_expr_parse_actionr6   Zalphasr"   ZSkipToGroupr3   Optionalr8   entryZLineEndLiteralZ	StringEndexplicit_commentr$   implicit_comment
string_defpreamble_declmain_expression)rM   Zstring_def_startZpreamble_startZcomment_line_startZstring_nameintegerZbraced_value_contentZbraced_valueZbrace_in_quotedZtext_in_quotedquoted_valueZstring_exprr2   Z
entry_typerI   rN   
field_namer1   
field_listZnot_an_implicit_commentrU   r   rL   r   __init__e   s    






 




zBibtexExpression.__init__c                 C   s,   | j | j| j| j| jfD ]}t|| qdS )zwAdd notice to logger on entry, comment, preamble, string definitions.

        :param log_fun: logger function
        N)r]   r`   r_   rb   ra   r   )rM   Zlog_funer   r   r   add_log_function   s    z!BibtexExpression.add_log_functionc                 C   s
   || _ dS )aK  Set the parseAction for string name expression.

        .. Note::

            For some reason pyparsing duplicates the string_name
            expression so setting its parseAction a posteriori has no effect
            in the context of a string expression. This is why this function
            should be used instead.
        NZ_string_name_parse_action_fun)rM   funr   r   r   rV     s    
z-BibtexExpression.set_string_name_parse_actionc                 C   s   |  |||S r:   rk   rM   r   r   r   r   r   r   rX     s    z*BibtexExpression._string_name_parse_actionc                 C   s
   t |S r:   )r   expression_if_neededrm   r   r   r   rZ     s    z*BibtexExpression._string_expr_parse_actionc                 C   s   | j j|ddS )NT)ZparseAll)rc   	parseFile)rM   file_objr   r   r   ro     s    zBibtexExpression.parseFileN)__name__
__module____qualname____doc__r0   rF   rh   rj   rV   rX   rZ   ro   r   r   r   r   r9   U   s    r9   )Z	pyparsingr0   bibdatabaser   r   r   r   r"   r$   r+   r3   r8   objectr9   r   r   r   r   <module>   s   	