o
    Ö|ãaj#  ã                   @   s  d dl Z d dlZ d dlZd dlZz;d dlmZmZmZmZm	Z	m
Z
mZmZmZ d dlmZ d dlmZmZmZ eeeeef  geeeef  f ZW n eyZ   dZ	dd„ ZY nw e	rid dlmZ d d	lmZ d
ddœdd„Zddœdd„ZG dd„ de jj
e ƒZ	ddd„ZdS )é    N)	ÚOptionalÚUnionÚIterableÚCallableÚTYPE_CHECKINGÚIteratorÚTypeÚcastÚList)ÚT)ÚTEÚRÚTokenOrElementFc                 C   s   |S ©N© )ÚtÚvr   r   ú</usr/lib/python3/dist-packages/debian/_deb822_repro/_util.pyÚ<lambda>   s    r   ©ÚDeb822Element)ÚDeb822Tokené   )Úend_marker_afterÚoutput_functionc                C   s  ddl m} d}t| |ƒr| g} ddt| ƒfg}d}|du r!tj}|rŒ|d \}}}	|	D ]7}
|d7 }|du r<dt|ƒ }t|
|ƒr[| ||
jj	t|
 
¡ ƒf¡ |||
jj	 ƒ d} n-||t|
ƒ ƒ q,| ¡  d}|durˆ|| |krˆ|rˆ|du r€dt|ƒ }||d | ƒ |s#dS dS )	a~  Debugging aid, which can dump a Deb822Element or a list of tokens/elements

    :param ast_tree: Either a Deb822Element or an iterable Deb822Token/Deb822Element entries
      (both types may be mixed in the same iterable, which enable it to dump the
      ast tree at different stages of parse_deb822_file method)
    :param end_marker_after: The dump will add "end of element" markers if a
      given element spans at least this many tokens/elements. Can be disabled
      with by passing None as value. Use 0 for unconditionally marking all
      elements (note that tokens never get an "end of element" marker as they
      are not an elements).
    :param output_function: Callable that receives a single str argument and is responsible
      for "displaying" that line. The callable may be invoked multiple times (one per line
      of output).  Defaults to logging.info if omitted.

    r   r   NÚ éÿÿÿÿé   z  z# <-- END OF )Údebian._deb822_repro.parsingr   Ú
isinstanceÚiterÚloggingÚinfoÚlenÚappendÚ	__class__Ú__name__Ú
iter_partsÚstrÚpop)Úast_treer   r   r   ÚprefixÚstackÚ
current_noÚstart_noÚnameÚcurrent_iterÚcurrentr   r   r   Ú	print_ast   s6   

ír2   )Úconstructorc                   s*   |du r
t d|ƒ‰ n|‰ ‡ ‡fdd„}|S )z¹Combines runs of one type into another type

    This is primarily useful for transforming tokens (e.g, Comment tokens) into
    the relevant element (such as the Comment element).
    NzCallable[[List[TE]], R]c                 3   s\   � g }| D ]}t |ˆƒr| |¡ q|rˆ t|ƒƒV  | ¡  |V  q|r,ˆ |ƒV  d S d S r   )r   r$   ÚlistÚclear)Útoken_streamÚtokensÚtoken©Ú_constructorÚsource_classr   r   Ú_implb   s   €

ÿz'combine_into_replacement.<locals>._impl)r	   )r;   Úreplacement_classr3   r<   r   r9   r   Úcombine_into_replacementQ   s
   r>   c                   @   sn   e Zd Zdee ddfdd„Zdd„ Zdd	„ Zd
d„ Zdd„ Z		ddd„Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )ÚBufferingIteratorÚstreamÚreturnNc                 C   s   t |ƒ| _t ¡ | _d| _d S )NF)r    Ú_streamÚcollectionsÚdequeÚ_bufferÚ_expired)Úselfr@   r   r   r   Ú__init__w   s   


zBufferingIterator.__init__c                 C   s$   | j r| j  ¡ S | jrt‚t| jƒS r   )rE   ÚpopleftrF   ÚStopIterationÚnextrB   ©rG   r   r   r   Ú__next__|   s
   

zBufferingIterator.__next__c                 c   sP   � | j }|s|  d¡r&|d }||ƒr| ¡  |V  ndS |s|  d¡sdS dS )zVVariant of itertools.takewhile except it does not discard the first non-matching tokenr   r   N)rE   Ú_fill_bufferrI   )rG   Ú	predicateÚbufferr   r   r   r   Ú	takewhile„   s   €úzBufferingIterator.takewhilec                 C   s^   |   |¡ | j}t|ƒ|krt|ƒ}| ¡  |S g }|r-|r-| | ¡ ¡ |d8 }|r-|s|S ©Nr   )rN   rE   r#   r4   r5   r$   rI   )rG   ÚcountrP   Úretr   r   r   Úconsume_many�   s   
üþzBufferingIterator.consume_manyc                 C   s
   t | jƒS r   )r4   rE   rL   r   r   r   Úpeek_bufferž   ó   
zBufferingIterator.peek_bufferc                 C   sx   | j }d}|d u s||k r:|t|ƒkr"|  |d ¡ |t|ƒkr"d S || }||ƒr.|d S |d7 }|d u s||k sd S )Nr   r   r   )rE   r#   rN   )rG   rO   ÚlimitrP   Úir   r   r   r   Ú	peek_find¢   s   ø	zBufferingIterator.peek_findc                 C   s\   | j s)t| jƒ|k r)z| j t| jƒ¡ W n ty!   d| _ Y nw t| jƒ|k s
t| jƒS )NT)rF   r#   rE   r$   rK   rB   rJ   Úbool)rG   Únumberr   r   r   rN   ´   s   þý
zBufferingIterator._fill_bufferc                 C   s
   |   d¡S rR   )Úpeek_atrL   r   r   r   Úpeek¿   rW   zBufferingIterator.peekc                 C   s"   |   |¡ | jr| j|d  S d S rR   )rN   rE   )rG   Útokens_aheadr   r   r   r]   Ã   s   
zBufferingIterator.peek_atc                 C   sb   |   |¡ | j}t|ƒ|krt|ƒ}|S |r-g }|D ]}| |¡ |d8 }|s* |S q|S g }|S rR   )rN   rE   r#   r4   r$   )rG   r\   rP   rT   r   r   r   r   Ú	peek_manyÈ   s"   

÷
üÿzBufferingIterator.peek_manyr   )r&   Ú
__module__Ú__qualname__r   r   rH   rM   rQ   rU   rV   rZ   rN   r^   r]   r`   r   r   r   r   r?   u   s    
þr?   c              	   c   sÌ   � |du r	t | ƒ}d}|D ]0}z	td|ƒ ¡ }W n ty-   td|ƒ}|t |jƒ7 }Y nw |D ]	}|t |jƒ7 }q0|V  q||krd||k rUt d¡j||| d�}t|ƒ‚t d¡j||| d�}t|ƒ‚dS )	zOFlatten a parser's output into tokens and verify it covers the entire line/textNr   r   r   z¯            Value parser did not fully cover the entire line with tokens (
            missing range {covered}..{content_len}).  Occurred when parsing "{content}"
            )ÚcoveredÚcontent_lenÚlinezî                    Value parser emitted tokens for more text than was present?  Should have
                     emitted {content_len} characters, got {covered}. Occurred when parsing
                     "{content}"
                    )rc   rd   Úcontent)	r#   r	   Úiter_tokensÚAttributeErrorÚtextÚtextwrapÚdedentÚformatÚ
ValueError)rf   r@   rd   rc   Útoken_or_elementr7   r8   Úmsgr   r   r   Úlen_check_iteratorÚ   s2   €
þýüôrp   r   )rC   Úcollections.abcr!   rj   Útypingr   r   r   r   r   r   r   r	   r
   Údebian._utilr   Údebian._deb822_repro.typesr   r   r   Ú_combine_parts_ret_typeÚImportErrorr   r   Údebian._deb822_repro.tokensr   r2   r>   Úabcr?   rp   r   r   r   r   Ú<module>   s8    ,ÿÿþý8ý$gþ