o
    |a	                    @   sv  d dl Zd dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZmZmZ d dl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mZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& d d	l'm(Z(m)Z)m*Z* z[d d
l+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z; d dlm<Z< d dlm=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZD e2ee)e ge>f ZEe2eFge,e/de>f  f ZGed ZHW n eIy   e;sdd Z8dd Z9Y nw G dd de4e? ZJG dd de4e>e=f ejKd ZLG dd de4e< ZMG dd deMe< e4e<e>f ZNdd ZOG dd deNeLe>e=f e>f ZPdd  ZQd!d" ZRd#d$ ZSd%d& ZTG d'd( d(ZUG d)d* d*eUZVG d+d, d,eUZWG d-d. d.eUZXG d/d0 d0eUZYG d1d2 d2eUZZG d3d deUZ[d4d5 Z\	6d[d7d8Z]d9d: Z^G d;d< d<e4e< ejj:e@e<f Z_G d=d> d>e_eF ejj`e@eFf eZaG d?d@ d@e_e< eZbG dAdB dBebe< ZcG dCdD dDebeF eaZdG dEdF dFeUeaeZeG dGdH dHeeZfG dIdJ dJeeZgG dKdL dLeUZhe(e#eVZie(eeZZje(eWeXZke(e[eeeejldMZmdNdO ZnePe&eQeYedPd enZoePe%eSeYe e enZpePe%eTeYe e enZqdQdR ZrdSdT ZsdUdV Ztd6d6dWdXdYZuevdZkr9d dlwZwewx  dS dS )\    N)ABC)TracebackType)ReferenceType)FormatterContentToken%one_value_per_line_trailing_separatorformat_field)resolve_ref
LinkedListLinkedListNode
OrderedSet_strIdefault_field_sort_key)AmbiguousDeb822FieldKeyError)Deb822TokenDeb822ValueToken'Deb822SemanticallySignificantWhiteSpaceDeb822SpaceSeparatorTokenDeb822CommentTokenDeb822WhitespaceTokenDeb822ValueContinuationTokenDeb822NewlineAfterValueTokenDeb822CommaTokenDeb822FieldNameTokenDeb822FieldSeparatorTokenDeb822ErrorTokentokenize_deb822_filecomma_split_tokenizerwhitespace_split_tokenizer)combine_into_replacementBufferingIteratorlen_check_iterator)IterableIteratorListUnionDictOptionalCallableAnyGenericTypeTupleIOcastoverloadMappingTYPE_CHECKING)T)STVETEParagraphKeyTokenOrElement
CommentishParagraphKeyBaseFormatterCallbackr   Deb822KeyValuePairElementc                 C   s   |S N )tvr<   r<   >/usr/lib/python3/dist-packages/debian/_deb822_repro/parsing.py<lambda>.       r@   c                 C      d S r;   r<   )fr<   r<   r?   r@   /   rA   c                   @   sF   e Zd ZdZdZdd Zdd Zedd Zej	d	d Zd
d Z
dS )ValueReferenceaf  Reference to a value inside a Deb822 paragraph

    This is useful for cases where want to modify values "in-place" or maybe
    conditionally remove a value after looking at it.

    ValueReferences can be invalidated by various changes or actions performed
    to the underlying provider of the value reference.  As an example, sorting
    a list of values will generally invalidate all ValueReferences related to
    that list.

    The ValueReference will raise validity issues where it detects them but most
    of the time it will not notice.  As a means to this end,  the ValueReference
    will *not* keep a strong reference to the underlying value.  This enables it
    to detect when the container goes out of scope.  However, keep in mind that
    the timeliness of garbage collection is implementation defined (e.g., pypy
    does not use ref-counting).
    )_node_render_value_factory_removal_handler_mutation_notifierc                 C   s(   t || _|| _|| _|| _|| _d S r;   )weakrefrefrE   rF   rG   rH   rI   )selfnoderendervalue_factoryremoval_handlermutation_notifierr<   r<   r?   __init__H   s
   
zValueReference.__init__c                 C   s.   | j d u r	td|   }|d u rtd|S )Nz(Cannot use ValueReference after remove()z-ValueReference is invalid (garbage collected))rE   RuntimeError)rL   rM   r<   r<   r?   _resolve_nodeU   s   
zValueReference._resolve_nodec                 C   s   |  |  jS )z Resolve the reference into a str)rF   rT   valuerL   r<   r<   r?   rU   `   s   zValueReference.valuec                 C   s*   |  ||  _| jdur|   dS dS )a  Update the reference value

        Updating the value via this method will *not* invalidate the reference (or other
        references to the same container).

        This can raise an exception of the new value does not follow the requirements
        for the referenced values.  As an example, values in whitespace separated
        lists cannot contain spaces and would trigger an exception.
        N)rG   rT   rU   rI   rL   	new_valuer<   r<   r?   rU   f   s   
c                 C   s   |  td|   d| _dS )zRemove the underlying value

        This will invalidate the ValueReference (and any other ValueReferences pointing
        to that exact value).  The validity of other ValueReferences to that container
        remains unaffected.
        zLinkedListNode[TokenOrElement]N)rH   r-   rT   rE   rV   r<   r<   r?   removev   s   
zValueReference.removeN)__name__
__module____qualname____doc__	__slots__rR   rT   propertyrU   setterrY   r<   r<   r<   r?   rD   2   s    

rD   c                       s  e Zd Zdd Zdd Zdd Z fddZe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d Zdd Zdd Zd d! Zed"d# Zd$d% Zd&d' Zd(d) Zd*d+ Z	,d?d-d.Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d,d8d9d:Zd7d;d<d=Z  Z S )@Deb822ParsedTokenListc           	      C   s   || _ t|| _|| _|| _|| _|| _t||| _|| _	d| _
t| _d| _d | _| js.J | jj}|d urBt|trD| j  d S d S d S )NTF)_kvpair_elementr	   _token_list_vtype_stype_str2value_parser_default_separator_factory_parser_to_value_factoryrG   rF   $_format_preserve_original_formattingr   
_formatter_changed._Deb822ParsedTokenList__continuation_line_chartail
isinstancer   pop)	rL   kvpair_elementinterpreted_value_elementvtypestypestr2value_parserdefault_separator_factoryrN   
last_tokenr<   r<   r?   rR      s"   


zDeb822ParsedTokenList.__init__c                 #         fdd j D E d H  d S )Nc                 3   s    | ]}  |V  qd S r;   )rF   .0r>   rV   r<   r?   	<genexpr>       z1Deb822ParsedTokenList.__iter__.<locals>.<genexpr>)value_partsrV   r<   rV   r?   __iter__   s   zDeb822ParsedTokenList.__iter__c                 C   s   t t| d d uS r;   )nextiterrV   r<   r<   r?   __bool__   s   zDeb822ParsedTokenList.__bool__c                    s&   |d u r| j r|   t |||S r;   )rk   _update_fieldsuper__exit__)rL   exc_typeexc_valexc_tb	__class__r<   r?   r      s   zDeb822ParsedTokenList.__exit__c                 #   rw   )Nc                 3   s     | ]}t | jr|V  qd S r;   )rn   rd   rx   rV   r<   r?   rz      s    z4Deb822ParsedTokenList.value_parts.<locals>.<genexpr>rc   rV   r<   rV   r?   r|      s   z!Deb822ParsedTokenList.value_partsc                 C   
   d| _ d S NT)rk   rV   r<   r<   r?   _mark_changed      
z#Deb822ParsedTokenList._mark_changedc                 #   s$     fdd j  D E dH  dS )a  Iterate over all values in the list (as ValueReferences)

        This is useful for doing inplace modification of the values or even
        streaming removal of field values.  It is in general also more
        efficient when more than one value is updated or removed.
        c                 3   s<    | ]}t |j jrttd | j j j jV  qdS )zLinkedListNode[VE]N)	rn   rU   rd   rD   r-   rF   rG   _remove_noder   )ry   nrV   r<   r?   rz      s    z>Deb822ParsedTokenList.iter_value_references.<locals>.<genexpr>N)rc   
iter_nodesrV   r<   rV   r?   iter_value_references   s   
z+Deb822ParsedTokenList.iter_value_referencesTc                 C   sR   |   }|jr	d}d| _|   | j| |r%|js'| jtd d S d S d S NFT )rg   is_whitespacerk   ,_append_continuation_line_token_if_necessaryrc   appendr   )rL   space_after_separatorseparator_tokenr<   r<   r?   append_separator   s   
z&Deb822ParsedTokenList.append_separatorc                 C   sR   | j }| j D ]}t|j|r$| |j|kr$| ||_d| _ dS qtd)zReplace the first instance of a value with another

        This method will *not* affect the validity of ValueReferences.
        Tz!list.replace(x, y): x not in listN)	rd   rc   r   rn   rU   rF   rG   rk   
ValueError)rL   
orig_valuerX   rr   rM   r<   r<   r?   replace   s   zDeb822ParsedTokenList.replacec                 C   sL   | j }| j D ]}t|j|r| |j|kr|} nqtd| |S )zRemove the first instance of a value

        Removal will invalidate ValueReferences to the value being removed.
        ValueReferences to other values will be unaffected.
        zlist.remove(x): x not in list)rd   rc   r   rn   rU   rF   r   r   )rL   rU   rr   rM   node_to_remover<   r<   r?   rY      s   
zDeb822ParsedTokenList.removec                 C   s0  | j }d| _d }d }d}d}|jddD ]}|j}t|tr$|jr$d}qt||r-|} nq|jddD ]}	|	j}
t|
trD|
jrDd}q4t|
|rM|	} nq4|d u r]|d u r]| j	  d S |d urf|sfd}n|d uro|sod}n|d u}|r{|}|j
}n|j}|}|d u r|| j_|d u r|| j_t|| d S )NTFskip_current)rd   rk   iter_previousrU   rn   r   
is_comment	iter_nextrc   clear	next_nodeprevious_node	head_node	tail_noder
   
link_nodes)rL   r   rr   first_value_on_lhsfirst_value_on_rhscomment_before_previous_valuecomment_before_next_value	past_node
past_tokenfuture_nodefuture_tokendelete_lhs_of_nodefirst_remain_lhsfirst_remain_rhsr<   r<   r?   r     sT   ;


z"Deb822ParsedTokenList._remove_nodec                 C   s   |  |}| | d S r;   )rG   append_value)rL   rU   vtr<   r<   r?   r   x  s   
zDeb822ParsedTokenList.appendc                 C   s   | j }|r+d}| j}| j}t|D ]}t||rd} nt||r# nq|r*|   n| j td |   d| _	|| d S r   )
rc   re   rd   reversedrn   r   r   r   r   rk   )rL   r   r|   needs_separatorrs   rr   r=   r<   r<   r?   r   }  s&   

z"Deb822ParsedTokenList.append_valuec                 C   s   | j j}|d uo| dS N
)rc   rm   convert_to_textendswithrL   rm   r<   r<   r?   _previous_is_newline  s   z*Deb822ParsedTokenList._previous_is_newlinec                 C   s"   |   rtd| jt  d S )Nz9Cannot add a newline after a token that ends on a newline)r   r   rc   r   r   rV   r<   r<   r?   append_newline  s   z$Deb822ParsedTokenList.append_newlinec                 C   sB   | j j}|d u s| ds|   tt|}| j | d S r   )rc   rm   r   r   r   r   _format_commentr   )rL   comment_textrm   comment_tokenr<   r<   r?   append_comment  s
   z$Deb822ParsedTokenList.append_commentc                 C   s<   | j }|d u rd}| jD ]}t|tr|j} nq|| _ |S Nr   )rl   rc   rn   r   text)rL   chartokenr<   r<   r?   _continuation_line_char  s   

z-Deb822ParsedTokenList._continuation_line_charc                 C   s<   | j j}|d ur| dr| j t| j d S d S d S r   )rc   rm   r   r   r   r   r   r   r<   r<   r?   r     s   zBDeb822ParsedTokenList._append_continuation_line_token_if_necessaryc                 C   s   |    d| _d S r   )_enable_reformattingrk   rV   r<   r<   r?   reformat_when_finished  s   
z,Deb822ParsedTokenList.reformat_when_finishedc                 C   r   )NFri   rV   r<   r<   r?   r     r   z*Deb822ParsedTokenList._enable_reformattingc                 C   r   r   r   rV   r<   r<   r?   no_reformatting_when_finished  r   z3Deb822ParsedTokenList.no_reformatting_when_finishedFc                 C   s   || _ d| _|rd| _dS dS )al  Use a custom formatter when formatting the value

        :param formatter: A formatter (see debian._deb822_repro.formatter.format_field
          for details)
        :param force_reformat: If True, always reformat the field even if there are
          no (other) changes performed.  By default, fields are only reformatted if
          they are changed.
        FTN)rj   ri   rk   )rL   	formatterforce_reformatr<   r<   r?   value_formatter  s
   
z%Deb822ParsedTokenList.value_formatterc                 c   s2    | j D ]}t|tr| E d H  q|V  qd S r;   )rc   rn   Deb822Elementiter_tokens)rL   ter<   r<   r?   _iter_content_as_tokens  s   

z-Deb822ParsedTokenList._iter_content_as_tokensc                    sH   |   }| j| j | j fdd}t| j| jjt	|j
| S )Nc                  3   st    d} D ]2}t |tr&|jrt|jV  qt | r%|j} t| V  qt |s-J | } t| V  qd S )N )	rn   r   r   r   r   r   r   r   value_token)r   r   rs   
token_listrr   r<   r?   _token_iter  s   

zNDeb822ParsedTokenList._generate_reformatted_field_content.<locals>._token_iter)rg   rd   re   rc   r   rj   rb   
field_namer   r   r   )rL   r   r   r<   r   r?   #_generate_reformatted_field_content  s   
z9Deb822ParsedTokenList._generate_reformatted_field_contentc                 C      d dd |  D S )Nr   c                 s       | ]}|j V  qd S r;   r   ry   r=   r<   r<   r?   rz          z@Deb822ParsedTokenList._generate_field_content.<locals>.<genexpr>)joinr   rV   r<   r<   r?   _generate_field_content     z-Deb822ParsedTokenList._generate_field_contentc                 C   s  | j }|j}| j}|j}|  D ]
}|js|js nqtd|d us%J t|t	r1|jr1td|
 ds<|   | jrK|  }d||f}n|  }|jdd}tt|}	|	 }
|
rgtd| tt|	}t|tstJ ||}|d usJ |j|_d| _d S )	Nz>Field must have content (i.e. non-whitespace and non-comments)z Fields must not end on a commentr   :Tkeepends$Syntax error in new field value for F)rb   r   rc   rm   r   r   r   r   rn   r   r   r   r   ri   r   r   r   
splitlinesparse_deb822_filer   find_first_error_elementr~   'Deb822NoDuplicateFieldsParagraphElementget_kvpair_elementvalue_elementrk   )rL   rp   r   r   rm   r=   
value_textr   new_contentdeb822_fileerror_token	paragraphnew_kvpair_elementr<   r<   r?   r     s:   

z#Deb822ParsedTokenList._update_fieldNkeyreversec                   sf  d}| j }| j fdd}g }| j D ]=}|j}t|tr)|jr)|du r(|}qt||rRg }	|durI|jddD ]}
|
|u rB n|		|
j q:|	||	f d}q|j
||d d| _| j  d}|  j}|D ]E\}}	|rd}|	rfdd	|	D }	|   n"|stfd
d|	D s| jdd |	r|   n| j	td | j|	 | | qkdS )a^  Sort the elements (abstract values) in this list.

        This method will sort the logical values of the list. It will
        attempt to preserve comments associated with a given value where
        possible.  Whether space and separators are preserved depends on
        the contents of the field as well as the formatting settings.

        Sorting (without reformatting) is likely to leave you with "awkward"
        whitespace. Therefore, you almost always want to apply reformatting
        such as the reformat_when_finished() method.

        Sorting will invalidate all ValueReferences.
        Nc                    s    r | d S | d   S Nr   r   )xr   r<   r?   key_funcB  s   z5Deb822ParsedTokenList.sort_elements.<locals>.key_funcFr   r   Tc                    s   g | ]	}t | s|qS r<   rn   ry   r   rs   r<   r?   
<listcomp>i  s    z7Deb822ParsedTokenList.sort_elements.<locals>.<listcomp>c                 3   s    | ]}t | V  qd S r;   r   r   r   r<   r?   rz   n  r{   z6Deb822ParsedTokenList.sort_elements.<locals>.<genexpr>)r   r   )rd   re   rc   r   rU   rn   r   r   r   r   sortrk   r   rg   r   r   anyr   r   extendr   )rL   r   r   comment_start_noderr   r   partsrM   rU   comments	keep_nodefirst_valueseparator_is_spacer<   )r   rs   r?   sort_elements,  sR   



z#Deb822ParsedTokenList.sort_elementsr   c                   s4    dur| j  fdd|d< | jdi | dS )a\  Sort the values (rendered as str) in this list.

        This method will sort the logical values of the list. It will
        attempt to preserve comments associated with a given value where
        possible.  Whether space and separators are preserved depends on
        the contents of the field as well as the formatting settings.

        Sorting (without reformatting) is likely to leave you with "awkward"
        whitespace. Therefore, you almost always want to apply reformatting
        such as the reformat_when_finished() method.

        Sorting will invalidate all ValueReferences.
        Nc                    s    | S r;   r<   )r   r   rN   r<   r?   r@     s    z,Deb822ParsedTokenList.sort.<locals>.<lambda>r   r<   )rF   r  )rL   r   kwargsr<   r  r?   r   |  s   zDeb822ParsedTokenList.sortTF)!rZ   r[   r\   rR   r}   r   r   r_   r|   r   r   r   r   rY   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   __classcell__r<   r<   r   r?   ra      sF    #


t

+Rra   zDeb822ParsedTokenList[VE, ST]c                   @   s   e Zd Z	dddZdS )InterpretationTc                 C      t r;   NotImplementedError)rL   rp   discard_comments_on_readr<   r<   r?   	interpret     zInterpretation.interpretNr
  )rZ   r[   r\   r  r<   r<   r<   r?   r    s    r  c                       sL   e Zd Z fddZ	dddZdd Zdd	 Zd
d Z	dddZ  Z	S )!GenericContentBasedInterpretationc                    s   t    || _|| _d S r;   )r   rR   
_tokenizer_value_parser)rL   	tokenizervalue_parserr   r<   r?   rR     s   

z*GenericContentBasedInterpretation.__init__Tc                 C   r  r;   r  rL   rp   r   r  r<   r<   r?   _high_level_interpretation     z<GenericContentBasedInterpretation._high_level_interpretationc                 c   s4    | j }|D ]}t|tr|||V  q|V  qd S r;   )r  rn   r   )rL   buffered_iteratorr  r   r<   r<   r?   _parse_stream  s   
z/GenericContentBasedInterpretation._parse_streamc                 c   s     |j  }| |E d H  d S r;   )r   r   
_parse_str)rL   kvpaircontentr<   r<   r?   _parse_kvpair  s   
z/GenericContentBasedInterpretation._parse_kvpairc                 c   s@    t |}tt|| ||d}t|| ||dE d H  d S )N)content_len)lenr   r    r  r  )rL   r   r"  biterr<   r<   r?   r    s   z,GenericContentBasedInterpretation._parse_strc                 C   s$   g }| | | | j|||dS N)r  )r   r!  r  )rL   rp   r  r   r<   r<   r?   r    s   z+GenericContentBasedInterpretation.interpretr
  )
rZ   r[   r\   rR   r  r  r!  r  r  r  r<   r<   r   r?   r    s    
r  c                    s    fdd}|S )Nc                    s   | dkrt dt | }t|d }t|d }|d us$J d|  d |d ur5tdj| d}t |t|sVt|trI|jsE|j	rIt dd}t |j| j
|d	t| t| ksoJ d
jt| t| d|S )Nr   zThe empty string is not a valuez.Bad parser - it returned None (or no TE) for ""z            The input "{v}" should have been exactly one element, but the parser provided at
             least two.  This can happen with unnecessary leading/trailing whitespace
             or including commas the value for a comma list.
            r>   z<The input "{v}" is whitespace or a comment: Expected a valuezaThe input "{v}" should have produced a element of type {vtype_name}, but instead it produced {t1})r>   
vtype_namet1zSBad tokenizer - the token did not cover the input text exactly ({t1_len} != {v_len})t1_lenv_len)r   r   r~   textwrapdedentformatrn   r   r   r   rZ   r#  r   )r>   
token_iterr)  t2msgparserrr   r<   r?   rG     s.   


z0_parser_to_value_factory.<locals>._value_factoryr<   )r3  rr   rG   r<   r2  r?   rh     s   rh   c                       s(   e Zd Z fddZ	dddZ  ZS )ListInterpretationc                    s*   t  || || _|| _|| _|| _d S r;   )r   rR   rd   re   rg   _render_factory)rL   r  r  rr   rs   ru   render_factoryr   r<   r?   rR     s
   	
zListInterpretation.__init__Tc              
   C   s"   t ||| j| j| j| j| |S r;   )ra   rd   re   r  rg   r5  r  r<   r<   r?   r    s   z-ListInterpretation._high_level_interpretationr
  )rZ   r[   r\   rR   r  r  r<   r<   r   r?   r4    s    r4  c                 C   s
   t | gS r;   )Deb822ParsedValueElement)r   _r<   r<   r?   _parse_whitespace_list_value$  r   r9  c                 C   s
   t | tS r;   )rn   r   r'  r<   r<   r?   _is_comma_token)     
r:  c                 C   s   | t}| g}|d ur|||d  n||  |r4t|d ts4|  |r4t|d tr'|t	|d  t
|S )N   )	peek_findr:  r   	peek_manypeek_bufferrn   r   ro   consume_manyr#  r7  )r   r  comma_offsetr|   r<   r<   r?   _parse_comma_list_value/  s   
rC  c           	      C   sf  | g}d}|d ur| t}|d ur|d g}|||d  d}t|d }|dkrf|| } t| tr^| jdr]||	| t|d trS|d jds[J dt
| d}n|d8 }|dks-|rm	 t|S ||	| t|d ts~J n,| }t|}|| |rt|d ts|  |d8 }|rt|d tr|	| |d us	t|S )Nr=  r<  Fr   >zGot: T)r>  r:  r   r?  r#  rn   r   r   r   rA  strr   r@  ro   r7  )	r   r  r|   rB  peeked_elementscomma_was_separatoriremaining_partconsume_elementsr<   r<   r?   _parse_uploaders_list_valueA  sL   





&rK  c                   @   st   e Zd ZdZdZdd Zdd Zdd Zd	d
 ZddddZ	e
dd Zejdd Zdd Zdd Zdd ZdS )r   z1Composite elements (consists of 1 or more tokens))_parent_element__weakref__c                 C   s
   d | _ d S r;   )rL  rV   r<   r<   r?   rR   v  r   zDeb822Element.__init__c                 C   r  r;   r  rV   r<   r<   r?   
iter_partsz     zDeb822Element.iter_partsc                 c   s$    |   D ]
}t||r|V  qd S r;   )rN  rn   rL   only_element_or_token_typepartr<   r<   r?   iter_parts_of_type~  s   
z Deb822Element.iter_parts_of_typec                 c   sB    |   D ]}|jd usJ t|tr| E d H  q|V  qd S r;   )rN  rL  rn   r   r   rL   rR  r<   r<   r?   r     s   
zDeb822Element.iter_tokensNrQ  c                c   sN    |   D ]}|d u st||rtd|V  t|tr$|j|dE d H  qd S )Nr4   rU  )rN  rn   r-   r   iter_recurserP  r<   r<   r?   rV    s   
zDeb822Element.iter_recursec                 C   
   t | jS r;   )r   rL  rV   r<   r<   r?   parent_element  r;  zDeb822Element.parent_elementc                 C   s"   |d urt || _d S d | _d S r;   )rJ   rK   rL  )rL   
new_parentr<   r<   r?   rX    s   "c                 C   s   |   D ]}| |_qd S r;   )rN  rX  rT  r<   r<   r?   _init_parent_of_parts  s   z#Deb822Element._init_parent_of_partsc                 C   r   )Nr   c                 s   r   r;   r   r   r<   r<   r?   rz     r   z0Deb822Element.convert_to_text.<locals>.<genexpr>r   r   rV   r<   r<   r?   r     r   zDeb822Element.convert_to_textc                 C   s   || j u r
d | _d S d S r;   )rX  rL  )rL   parentr<   r<   r?   clear_parent_if_parent  s   

z$Deb822Element.clear_parent_if_parent)rZ   r[   r\   r]   r^   rR   rN  rS  r   rV  r_   rX  r`   rZ  r   r]  r<   r<   r<   r?   r   q  s     


r   c                       s,   e Zd ZdZdZ fddZdd Z  ZS )Deb822ErrorElementa|  Element representing elements or tokens that are out of place

    Commonly, it will just be instances of Deb822ErrorToken, but it can be other
    things.  As an example if a parser discovers out of order elements/tokens,
    it can bundle them in a Deb822ErrorElement to signal that the sequence of
    elements/tokens are invalid (even if the tokens themselves are valid).
    _partsc                       t    || _|   d S r;   )r   rR   r`  rZ  )rL   r  r   r<   r?   rR        
zDeb822ErrorElement.__init__c                 c       | j E d H  d S r;   r_  rV   r<   r<   r?   rN       zDeb822ErrorElement.iter_parts)rZ   r[   r\   r]   r^   rR   rN  r  r<   r<   r   r?   r^    s
    r^  c                       sp   e Zd ZdZdZ fddZedd Zedd Zed	d
 Z	dd Z
dd Zdd Zdd Zdd Z  ZS )Deb822ValueLineElementz!Consists of one "line" of a value)_comment_element_continuation_line_token_leading_whitespace_token_value_tokens_trailing_whitespace_token_newline_tokenc                    sR   t    |d ur|d u rtd|| _|| _|| _|| _|| _|| _| 	  d S )Nz)Only continuation lines can have comments)
r   rR   r   rf  rg  rh  ri  rj  rk  rZ  )rL   comment_elementcontinuation_line_tokenleading_whitespace_tokenr|   trailing_whitespace_tokennewline_tokenr   r<   r?   rR     s   
zDeb822ValueLineElement.__init__c                 C      | j S r;   rf  rV   r<   r<   r?   rl       z&Deb822ValueLineElement.comment_elementc                 C   rq  r;   )rg  rV   r<   r<   r?   rm    rs  z.Deb822ValueLineElement.continuation_line_tokenc                 C   rq  r;   )rk  rV   r<   r<   r?   rp    rs  z$Deb822ValueLineElement.newline_tokenc                 C   s"   | j d u rt | _ | | j _d S d S r;   )rk  r   rX  rV   r<   r<   r?   add_newline_if_missing  s   
z-Deb822ValueLineElement.add_newline_if_missingc                 c   s2    | j r| j V  | jE d H  | jr| jV  d S d S r;   )rh  ri  rj  rV   r<   r<   r?   _iter_content_parts  s   z*Deb822ValueLineElement._iter_content_partsc                 c   s4    |   D ]}t|tr| E d H  q|V  qd S r;   )ru  rn   r   r   rT  r<   r<   r?   _iter_content_tokens  s   
z+Deb822ValueLineElement._iter_content_tokensc                 C   sN   t | jdkr| js| jst| jd tr| jd jS ddd |  D S )Nr<  r   r   c                 s   r   r;   r   r   r<   r<   r?   rz     r   zADeb822ValueLineElement.convert_content_to_text.<locals>.<genexpr>)	r#  ri  rh  rj  rn   r   r   r   rv  rV   r<   r<   r?   convert_content_to_text
  s   z.Deb822ValueLineElement.convert_content_to_textc                 c   sB    | j r| j V  | jr| jV  |  E d H  | jr| jV  d S d S r;   )rf  rg  ru  rk  rV   r<   r<   r?   rN    s   z!Deb822ValueLineElement.iter_parts)rZ   r[   r\   r]   r^   rR   r_   rl  rm  rp  rt  ru  rv  rw  rN  r  r<   r<   r   r?   re    s    


re  c                       s<   e Zd ZdZ fddZedd Zdd Zdd	 Z  Z	S )
Deb822ValueElement_value_entry_elementsc                    ra  r;   )r   rR   rz  rZ  )rL   value_entry_elementsr   r<   r?   rR   %  rb  zDeb822ValueElement.__init__c                 C   rq  )zRead-only list of value entriesry  rV   r<   r<   r?   value_lines+  s   zDeb822ValueElement.value_linesc                 c   rc  r;   ry  rV   r<   r<   r?   rN  1  rd  zDeb822ValueElement.iter_partsc                 C   s   | j r| j d   d S d S )Nr=  )rz  rt  rV   r<   r<   r?   add_final_newline_if_missing5  s   z/Deb822ValueElement.add_final_newline_if_missing)
rZ   r[   r\   r^   rR   r_   r|  rN  r}  r  r<   r<   r   r?   rx  "  s    
rx  c                       s<   e Zd ZdZ fddZ fddZdd Zdd	 Z  ZS )
r7  )_text_cached_text_no_comments_cachedrc   c                    s|   t    || _|   t|d trt|d ts"t| jjd t	|dkr6|d }|j
| _|j
| _d S d | _d | _d S )Nr   r=  z) MUST start and end on a Deb822ValueTokenr<  )r   rR   rc   rZ  rn   r   r   r   rZ   r#  r   r~  r  )rL   tokensr   r   r<   r?   rR   ?  s   

z!Deb822ParsedValueElement.__init__c                    s   | j d u rt  | _ | j S r;   )r  r   r   rV   r   r<   r?   r   O  s   
z(Deb822ParsedValueElement.convert_to_textc                 C   s*   | j d u rddd |  D | _ | j S )Nr   c                 s       | ]	}|j s|jV  qd S r;   r   r   r   r<   r<   r?   rz   X  s
    zLDeb822ParsedValueElement.convert_to_text_without_comments.<locals>.<genexpr>)r  r   r   rV   r<   r<   r?    convert_to_text_without_commentsU  s
   


z9Deb822ParsedValueElement.convert_to_text_without_commentsc                 c   rc  r;   r   rV   r<   r<   r?   rN  ]  rd  z#Deb822ParsedValueElement.iter_parts)	rZ   r[   r\   r^   rR   r   r  rN  r  r<   r<   r   r?   r7  ;  s    r7  c                       s8   e Zd ZdZ fddZdd Zdd Zdd	 Z  ZS )
Deb822CommentElement_comment_tokensc                    s(   t    || _|std|   d S )Nz5Comment elements must have at least one comment token)r   rR   r  r   rZ  )rL   comment_tokensr   r<   r?   rR   e  s
   
zDeb822CommentElement.__init__c                 C   rW  r;   )r#  r  rV   r<   r<   r?   __len__m  r   zDeb822CommentElement.__len__c                 C   s
   | j | S r;   r  rL   itemr<   r<   r?   __getitem__q  r   z Deb822CommentElement.__getitem__c                 c   rc  r;   r  rV   r<   r<   r?   rN  u  rd  zDeb822CommentElement.iter_parts)	rZ   r[   r\   r^   rR   r  r  rN  r  r<   r<   r   r?   r  b  s    r  c                       s   e Zd ZdZ fddZedd Zedd Zedd	 Zej	d
d	 Z	dddZ
edd Zej	dd Zdd Z  ZS )r:   rf  _field_token_separator_token_value_elementc                    s.   t    || _|| _|| _|| _|   d S r;   )r   rR   rf  r  r  r  rZ  )rL   rl  field_tokenr   r   r   r<   r?   rR   }  s   
z"Deb822KeyValuePairElement.__init__c                 C      | j jS r;   )r  r   rV   r<   r<   r?   r     s   z$Deb822KeyValuePairElement.field_namec                 C   rq  r;   )r  rV   r<   r<   r?   r    rs  z%Deb822KeyValuePairElement.field_tokenc                 C   rq  r;   )r  rV   r<   r<   r?   r     rs  z'Deb822KeyValuePairElement.value_elementc                 C   s   | j |  || _ | |_d S r;   )r  r]  rX  rW   r<   r<   r?   r     s   
Tc                 C   s   |j | |dS r%  )r  )rL   interpreterr  r<   r<   r?   interpret_as  s   z&Deb822KeyValuePairElement.interpret_asc                 C   rq  r;   rr  rV   r<   r<   r?   rl    rs  z)Deb822KeyValuePairElement.comment_elementc                 C   sJ   |d ur|d j dstd| jr| j|  |d ur | |_|| _d S )Nr=  r   z&Field comments must end with a newline)r   r   r   rf  r]  rX  )rL   rU   r<   r<   r?   rl    s   
c                 c   s,    | j r| j V  | jV  | jV  | jV  d S r;   r  rV   r<   r<   r?   rN    s   z$Deb822KeyValuePairElement.iter_partsr
  )rZ   r[   r\   r^   rR   r_   r   r  r   r`   r  rl  rN  r  r<   r<   r   r?   r:   z  s$    






c                 C   sT   | dkrdS d| d d v rt d| ds|  d } | ds(d|   } | S )Nr   z#
r   r=  z-Comment lines must not have embedded newlines#z# )r   r   rstrip
startswithlstrip)cr<   r<   r?   r     s   

r   Fc                 C   sv   d }d }t | tr%| \}}|r |dkrd}t|j||dd }t|}nd }t | tr2| }|j}nt| }|||fS )Nr   zECannot resolve key "{key}" with index {index}. The key is not indexedr   index)rn   tupleKeyErrorr.  r   r   r   )r  raise_if_indexedr  
name_tokenr   r1  r<   r<   r?   _unpack_key  s    



r  c                 c   sF    |sdd | D E d H  d S | D ]}d dd | D V  qd S )Nc                 s   s    | ]}|  V  qd S r;   r   rx   r<   r<   r?   rz         z0_convert_value_lines_to_lines.<locals>.<genexpr>r   c                 s   r  r;   r  r   r<   r<   r?   rz     s    r[  )r|  strip_commentselementr<   r<   r?   _convert_value_lines_to_lines  s   r  c                   @   sT   e Zd Zedd Zedd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd ZdS )AutoResolvingMixinc                 C      dS r   r<   rV   r<   r<   r?   _auto_resolve_ambiguous_fields     z1AutoResolvingMixin._auto_resolve_ambiguous_fieldsc                 C   r  r;   r  rV   r<   r<   r?   
_paragraph  r  zAutoResolvingMixin._paragraphc                 C   r  r;   )r  kvpair_countrV   r<   r<   r?   r    s   zAutoResolvingMixin.__len__c                 C   s   | j |S r;   )r  contains_kvpair_elementr  r<   r<   r?   __contains__
     zAutoResolvingMixin.__contains__c                 C   s   t | j S r;   )r   r  	iter_keysrV   r<   r<   r?   r}        zAutoResolvingMixin.__iter__c                 C   sF   | j rt|tr| j|df}n| j|}|d usJ | ||S r   )r  rn   rE  r  r   _interpret_value)rL   r  r>   r<   r<   r?   r    s
   zAutoResolvingMixin.__getitem__c                 C   s   | j | d S r;   )r  remove_kvpair_elementr  r<   r<   r?   __delitem__  s   zAutoResolvingMixin.__delitem__c                 C   r  r;   r  rL   r   rU   r<   r<   r?   r    rO  z#AutoResolvingMixin._interpret_valueN)rZ   r[   r\   r_   r  r  r  r  r}   r  r  r  r<   r<   r<   r?   r    s    

	r  c                   @   sT   e Zd Zedd Zedd Zedd Zedd Zd	d
 Zdd Z	dd Z
dS ) Deb822ParagraphToStrWrapperMixinc                 C   r  r   r<   rV   r<   r<   r?   !_auto_map_initial_line_whitespace+  r  zBDeb822ParagraphToStrWrapperMixin._auto_map_initial_line_whitespacec                 C   r  r   r<   rV   r<   r<   r?   _discard_comments_on_read0  r  z:Deb822ParagraphToStrWrapperMixin._discard_comments_on_readc                 C   r  r   r<   rV   r<   r<   r?   +_auto_map_final_newline_in_multiline_values5  r  zLDeb822ParagraphToStrWrapperMixin._auto_map_final_newline_in_multiline_valuesc                 C   r  r   r<   rV   r<   r<   r?   )_preserve_field_comments_on_field_updates:  r  zJDeb822ParagraphToStrWrapperMixin._preserve_field_comments_on_field_updatesc                    s   |j }|j}t|dkr|d }| }| jr| }|S | js#| jr=t|| j}| j d fddt	|ddD }n| }| j
rP|d dkrP|d d }|S )	Nr<  r   r   c                 3   s0    | ]\}} r|d kr|  d n|V  qdS )r<  r   N)strip)ry   rH  lineauto_map_spacer<   r?   rz   U  s    $zIDeb822ParagraphToStrWrapperMixin._convert_value_to_str.<locals>.<genexpr>startr=  r   )r   r|  r#  r   r  r  r  r  r   	enumerater  )rL   rp   r   value_entriesvalue_entryr=   	converteras_textr<   r  r?   _convert_value_to_str?  s(   

z6Deb822ParagraphToStrWrapperMixin._convert_value_to_strc           
      C   s  | j }d }|r&| jr&d }|}t|tr|df}| jj|dd}|d ur&|j}| jrgz|d}W n t	y;   d}Y nw |dksF|t
|krT| jj|| ||d d S |dd\}}	dd	| d|	f}|dsw| jsst	d
|d7 }| jj||||d d S )Nr   Tuse_getr   r=  preserve_original_field_commentfield_commentr<  r   r   zeValues must end with a newline (or be single line values and use the auto whitespace mapping feature))r  r  rn   rE  r  r   rl  r  r  r   r#  set_field_to_simple_valuer  splitr   r   r  set_field_from_raw_string)
rL   r  rU   keep_commentscomment
key_lookuporig_kvpairidx
first_linerestr<   r<   r?   __setitem__`  sH   



z,Deb822ParagraphToStrWrapperMixin.__setitem__c                 C   s   t d| |S )Nr1   )r-   r  r  r<   r<   r?   r    s   z1Deb822ParagraphToStrWrapperMixin._interpret_valueN)rZ   r[   r\   r_   r  r  r  r  r  r  r  r<   r<   r<   r?   r  '  s    



!-r  c                   @   s@   e Zd ZdddddZedd Zedd	 Zed
d ZdS )AbstractDeb822ParagraphWrapperFTauto_resolve_ambiguous_fieldsr  c                C   s   || _ || _|| _d S r;   )*_AbstractDeb822ParagraphWrapper__paragraph>_AbstractDeb822ParagraphWrapper__auto_resolve_ambiguous_fields9_AbstractDeb822ParagraphWrapper__discard_comments_on_read)rL   r   r  r  r<   r<   r?   rR     s   
z'AbstractDeb822ParagraphWrapper.__init__c                 C   rq  r;   )r  rV   r<   r<   r?   r    rs  z)AbstractDeb822ParagraphWrapper._paragraphc                 C   rq  r;   )r  rV   r<   r<   r?   r    rs  z8AbstractDeb822ParagraphWrapper._discard_comments_on_readc                 C   rq  r;   )r  rV   r<   r<   r?   r    rs  z=AbstractDeb822ParagraphWrapper._auto_resolve_ambiguous_fieldsN)rZ   r[   r\   rR   r_   r  r  r  r<   r<   r<   r?   r    s    

r  c                       s0   e Zd Zddd	d
 fddZdd	 Z  ZS )"Deb822InterpretingParagraphWrapperFTr  returnNc                   s   t  j|||d || _d S Nr  )r   rR   _interpretation)rL   r   interpretationr  r  r   r<   r?   rR     s
   
z+Deb822InterpretingParagraphWrapper.__init__c                 C   s   | j |S r;   )r  r  r  r<   r<   r?   r    r  z3Deb822InterpretingParagraphWrapper._interpret_value)r  N)rZ   r[   r\   rR   r  r  r<   r<   r   r?   r    s    r  c                       sN   e Zd Zdddddd fdd
Zedd Zedd	 Zed
d Z  ZS )Deb822DictishParagraphWrapperTFr   auto_map_initial_line_whitespacer  (preserve_field_comments_on_field_updates*auto_map_final_newline_in_multiline_valuesc                   s(   t  j|||d || _|| _|| _d S r  )r   rR   @_Deb822DictishParagraphWrapper__auto_map_initial_line_whitespaceH_Deb822DictishParagraphWrapper__preserve_field_comments_on_field_updatesJ_Deb822DictishParagraphWrapper__auto_map_final_newline_in_multiline_values)rL   r   r  r  r  r  r  r   r<   r?   rR     s   
z&Deb822DictishParagraphWrapper.__init__c                 C   rq  r;   )r  rV   r<   r<   r?   r    rs  z?Deb822DictishParagraphWrapper._auto_map_initial_line_whitespacec                 C   rq  r;   )r  rV   r<   r<   r?   r    rs  zGDeb822DictishParagraphWrapper._preserve_field_comments_on_field_updatesc                 C   rq  r;   )r  rV   r<   r<   r?   r    rs  zIDeb822DictishParagraphWrapper._auto_map_final_newline_in_multiline_values)	rZ   r[   r\   rR   r_   r  r  r  r  r<   r<   r   r?   r    s    

r  c                   @   s  e Zd Zedd Zedd Zedd Zedd Zd	d
ddZ	d	d	d	d	d	dddZ
edd Zdd Zdd Zdd Zdd Zedd Zdd Zdd Z	 d3d!d"Zd#d$ Zd%d& Z	'd4d(d)Zd'd'd*d+d,Zd'd'd*d-d.Zed/d0 Zed1d0 Z	'd4d2d0Zd'S )5Deb822ParagraphElementc                 C   s   t g t S r;   )r   r   clsr<   r<   r?   new_empty_paragraph  s   z*Deb822ParagraphElement.new_empty_paragraphc                 C   s&   |   }| D ]\}}|||< q|S r;   )r  items)r  mappingr   kr>   r<   r<   r?   	from_dict  s   
z Deb822ParagraphElement.from_dictc                 C   s@   |st dtdd |D }t|t|krt||S t|S )Nz9A paragraph must consist of at least one field/value pairc                 s   r   r;   r   ry   kvr<   r<   r?   rz     r   z6Deb822ParagraphElement.from_kvpairs.<locals>.<genexpr>)r   r   r#  r   %Deb822DuplicateFieldsParagraphElement)r  kvpair_elementskvpair_orderr<   r<   r?   from_kvpairs  s   
z#Deb822ParagraphElement.from_kvpairsc                 C   r  )z0Tell whether this paragraph has duplicate fieldsFr<   rV   r<   r<   r?   has_duplicate_fields  s   z+Deb822ParagraphElement.has_duplicate_fieldsTr  c                C   s   t | ||dS )a  Provide a Dict-like view of the paragraph

        This method returns a dict-like object representing this paragraph and
        is useful for accessing fields in a given interpretation. It is possible
        to use multiple versions of this dict-like view with different interpretations
        on the same paragraph at the same time (for different fields).

            >>> example_deb822_paragraph = '''
            ... Package: foo
            ... # Field comment (because it becomes just before a field)
            ... Architecture: amd64
            ... # Inline comment (associated with the next line)
            ...               i386
            ... # We also support arm
            ...               arm64
            ...               armel
            ... '''
            >>> dfile = parse_deb822_file(example_deb822_paragraph.splitlines(keepends=True))
            >>> paragraph = next(iter(dfile))
            >>> list_view = paragraph.as_interpreted_dict_view(LIST_SPACE_SEPARATED_INTERPRETATION)
            >>> # With the defaults, you only deal with the semantic values
            >>> # - no leading or trailing whitespace on the first part of the value
            >>> list(list_view["Package"])
            ['foo']
            >>> with list_view["Architecture"] as arch_list:
            ...     orig_arch_list = list(arch_list)
            ...     arch_list.replace('i386', 'kfreebsd-amd64')
            >>> orig_arch_list
            ['amd64', 'i386', 'arm64', 'armel']
            >>> list(list_view["Architecture"])
            ['amd64', 'kfreebsd-amd64', 'arm64', 'armel']
            >>> print(paragraph.dump(), end='')
            Package: foo
            # Field comment (because it becomes just before a field)
            Architecture: amd64
            # Inline comment (associated with the next line)
                          kfreebsd-amd64
            # We also support arm
                          arm64
                          armel
            >>> # Format preserved and architecture replaced
            >>> with list_view["Architecture"] as arch_list:
            ...     # Prettify the result as sorting will cause awkward whitespace
            ...     arch_list.reformat_when_finished()
            ...     arch_list.sort()
            >>> print(paragraph.dump(), end='')
            Package: foo
            # Field comment (because it becomes just before a field)
            Architecture: amd64
            # We also support arm
                          arm64
                          armel
            # Inline comment (associated with the next line)
                          kfreebsd-amd64
            >>> list(list_view["Architecture"])
            ['amd64', 'arm64', 'armel', 'kfreebsd-amd64']
            >>> # Format preserved and architecture values sorted

        :param interpretation: Decides how the field values are interpreted.  As an example,
          use LIST_SPACE_SEPARATED_INTERPRETATION for fields such as Architecture in the
          debian/control file.
        :param auto_resolve_ambiguous_fields: This parameter is only relevant for paragraphs
          that contain the same field multiple times (these are generally invalid).  If the
          caller requests an ambiguous field from an invalid paragraph via a plain field name,
          the return dict-like object will refuse to resolve the field (not knowing which
          version to pick).  This parameter (if set to True) instead changes the error into
          assuming the caller wants the *first* variant.
        r  )r  )rL   r  r  r<   r<   r?   as_interpreted_dict_view  s
   Jz/Deb822ParagraphElement.as_interpreted_dict_viewr  c                C   s   t | |||||dS )aw  Provide a Dict[str, str]-like view of this paragraph with non-standard parameters

        This method returns a dict-like object representing this paragraph that is
        optionally configured differently from the default view.

            >>> example_deb822_paragraph = '''
            ... Package: foo
            ... # Field comment (because it becomes just before a field)
            ... Depends: libfoo,
            ... # Inline comment (associated with the next line)
            ...          libbar,
            ... '''
            >>> dfile = parse_deb822_file(example_deb822_paragraph.splitlines(keepends=True))
            >>> paragraph = next(iter(dfile))
            >>> # With the defaults, you only deal with the semantic values
            >>> # - no leading or trailing whitespace on the first part of the value
            >>> paragraph["Package"]
            'foo'
            >>> # - no inline comments in multiline values (but whitespace will be present
            >>> #   subsequent lines.)
            >>> print(paragraph["Depends"])
            libfoo,
                     libbar,
            >>> paragraph['Foo'] = 'bar'
            >>> paragraph.get('Foo')
            'bar'
            >>> paragraph.get('Unknown-Field') is None
            True
            >>> # But you get asymmetric behaviour with set vs. get
            >>> paragraph['Foo'] = ' bar\n'
            >>> paragraph['Foo']
            'bar'
            >>> paragraph['Bar'] = '     bar\n#Comment\n another value\n'
            >>> # Note that the whitespace on the first line has been normalized.
            >>> print("Bar: " + paragraph['Bar'])
            Bar: bar
             another value
            >>> # The comment is present (in case you where wondering)
            >>> print(paragraph.get_kvpair_element('Bar').convert_to_text(), end='')
            Bar: bar
            #Comment
             another value
            >>> # On the other hand, you can choose to see the values as they are
            >>> # - We will just reset the paragraph as a "nothing up my sleeve"
            >>> dfile = parse_deb822_file(example_deb822_paragraph.splitlines(keepends=True))
            >>> paragraph = next(iter(dfile))
            >>> nonstd_dictview = paragraph.configured_view(
            ...     discard_comments_on_read=False,
            ...     auto_map_initial_line_whitespace=False,
            ...     # For paragraphs with duplicate fields, you can choose to get an error
            ...     # rather than the dict picking the first value available.
            ...     auto_resolve_ambiguous_fields=False,
            ...     auto_map_final_newline_in_multiline_values=False,
            ... )
            >>> # Because we have reset the state, Foo and Bar are no longer there.
            >>> 'Bar' not in paragraph and 'Foo' not in paragraph
            True
            >>> # We can now see the comments (discard_comments_on_read=False)
            >>> # (The leading whitespace in front of "libfoo" is due to
            >>> #  auto_map_initial_line_whitespace=False)
            >>> print(nonstd_dictview["Depends"], end='')
             libfoo,
            # Inline comment (associated with the next line)
                     libbar,
            >>> # And all the optional whitespace on the first value line
            >>> # (auto_map_initial_line_whitespace=False)
            >>> nonstd_dictview["Package"] == ' foo\n'
            True
            >>> # ... which will give you symmetric behaviour with set vs. get
            >>> nonstd_dictview['Foo'] = '  bar \n'
            >>> nonstd_dictview['Foo']
            '  bar \n'
            >>> nonstd_dictview['Bar'] = '  bar \n#Comment\n another value\n'
            >>> nonstd_dictview['Bar']
            '  bar \n#Comment\n another value\n'
            >>> # But then you get no help either.
            >>> try:
            ...     nonstd_dictview["Baz"] = "foo"
            ... except ValueError:
            ...     print("Rejected")
            Rejected
            >>> # With auto_map_initial_line_whitespace=False, you have to include minimum a newline
            >>> nonstd_dictview["Baz"] = "foo\n"
            >>> # The absence of leading whitespace gives you the terse variant at the expensive
            >>> # readability
            >>> paragraph.get_kvpair_element('Baz').convert_to_text()
            'Baz:foo\n'
            >>> # But because they are views, changes performed via one view is visible in the other
            >>> paragraph['Foo']
            'bar'
            >>> # The views show the values according to their own rules. Therefore, there is an
            >>> # asymmetric between paragraph['Foo'] and nonstd_dictview['Foo']
            >>> # Nevertheless, you can read or write the fields via either - enabling you to use
            >>> # the view that best suit your use-case for the given field.
            >>> 'Baz' in paragraph and nonstd_dictview.get('Baz') is not None
            True
            >>> # Deletion via the view also works
            >>> del nonstd_dictview['Baz']
            >>> 'Baz' not in paragraph and nonstd_dictview.get('Baz') is None
            True


        :param discard_comments_on_read: When getting a field value from the dict,
          this parameter decides how in-line comments are handled.  When setting
          the value, inline comments are still allowed and will be retained.
          However, keep in mind that this option makes getter and setter assymetric
          as a "get" following a "set" with inline comments will omit the comments
          even if they are there (see the code example).
        :param auto_map_initial_line_whitespace: Special-case the first value line
          by trimming unnecessary whitespace leaving only the value. For single-line
          values, all space including newline is pruned. For multi-line values, the
          newline is preserved / needed to distinguish the first line from the
          following lines.  When setting a value, this option normalizes the
          whitespace of the initial line of the value field.
          When this option is set to True makes the dictionary behave more like the
          original Deb822 module.
        :param preserve_field_comments_on_field_updates: Whether to preserve the field
          comments when mutating the field.
        :param auto_resolve_ambiguous_fields: This parameter is only relevant for paragraphs
          that contain the same field multiple times (these are generally invalid).  If the
          caller requests an ambiguous field from an invalid paragraph via a plain field name,
          the return dict-like object will refuse to resolve the field (not knowing which
          version to pick).  This parameter (if set to True) instead changes the error into
          assuming the caller wants the *first* variant.
        :param auto_map_final_newline_in_multiline_values: This parameter controls whether
          a multiline field with have / need a trailing newline. If True, the trailing
          newline is hidden on get and automatically added in set (if missing).
          When this option is set to True makes the dictionary behave more like the
          original Deb822 module.
        r  )r  )rL   r  r  r  r  r  r<   r<   r?   configured_view^  s    z&Deb822ParagraphElement.configured_viewc                 C   s   | S r;   r<   rV   r<   r<   r?   r    r  z!Deb822ParagraphElement._paragraphc                 C   r  )9Re-order the given field so it is "last" in the paragraphr  rL   fieldr<   r<   r?   
order_last  r  z!Deb822ParagraphElement.order_lastc                 C   r  ):Re-order the given field so it is "first" in the paragraphr  r  r<   r<   r?   order_first  r  z"Deb822ParagraphElement.order_firstc                 C   r  )Re-order the given field so appears directly after the reference field in the paragraph

        The reference field must be present.r  rL   r  reference_fieldr<   r<   r?   order_before  r  z#Deb822ParagraphElement.order_beforec                 C   r  )Re-order the given field so appears directly before the reference field in the paragraph

        The reference field must be present.
        r  r  r<   r<   r?   order_after  r  z"Deb822ParagraphElement.order_afterc                 C   r  r;   r  rV   r<   r<   r?   r    r  z#Deb822ParagraphElement.kvpair_countc                 C   r  r;   r  rV   r<   r<   r?   r    rO  z Deb822ParagraphElement.iter_keysc                 C   r  r;   r  r  r<   r<   r?   r    rO  z.Deb822ParagraphElement.contains_kvpair_elementFc                 C   r  r;   r  )rL   r  r  r<   r<   r?   r     r  z)Deb822ParagraphElement.get_kvpair_elementc                 C   r  r;   r  r  r<   r<   r?   set_kvpair_element$  rO  z)Deb822ParagraphElement.set_kvpair_elementc                 C   r  r;   r  rL   r   r<   r<   r?   r  (  rO  z,Deb822ParagraphElement.remove_kvpair_elementNc                 C   r  )  Re-order all fields

        :param key: Provide a key function (same semantics as for sorted).  Keep in mind that
          the module preserve the cases for field names - in generally, callers are recommended
          to use "lower()" to normalize the case.
        r  r  r<   r<   r?   sort_fields,  s   
z"Deb822ParagraphElement.sort_fieldsr  c                C   s6   d|v rt dd|  d }| j||||d dS )a	  Sets a field in this paragraph to a simple "word" or "phrase"

        In many cases, it is better for callers to just use the paragraph as
        if it was a dictionary.  However, this method does enable to you choose
        the field comment (if any), which can be a reason for using it.

        This is suitable for "simple" fields like "Package".  Example:

            >>> example_deb822_paragraph = '''
            ... Package: foo
            ... '''
            >>> dfile = parse_deb822_file(example_deb822_paragraph.splitlines(keepends=True))
            >>> p = next(iter(dfile))
            >>> p.set_field_to_simple_value("Package", "mscgen")
            >>> p.set_field_to_simple_value("Architecture", "linux-any kfreebsd-any",
            ...                             field_comment=['Only ported to linux and kfreebsd'])
            >>> p.set_field_to_simple_value("Priority", "optional")
            >>> print(p.dump(), end='')
            Package: mscgen
            # Only ported to linux and kfreebsd
            Architecture: linux-any kfreebsd-any
            Priority: optional
            >>> # Values are formatted nicely by default, but it does not work with
            >>> # multi-line values
            >>> p.set_field_to_simple_value("Foo", "bar\nbin\n")
            Traceback (most recent call last):
                ...
            ValueError: Cannot use set_field_to_simple_value for values with newlines

        :param item: Name of the field to set.  If the paragraph already
          contains the field, then it will be replaced.  If the field exists,
          then it will preserve its order in the paragraph.  Otherwise, it is
          added to the end of the paragraph.
          Note this can be a "paragraph key", which enables you to control
          *which* instance of a field is being replaced (in case of duplicate
          fields).
        :param simple_value: The text to use as the value.  The value must not
          contain newlines.  Leading and trailing will be stripped but space
          within the value is preserved.  The value cannot contain comments
          (i.e. if the "#" token appears in the value, then it is considered
          a value rather than "start of a comment)
        :param preserve_original_field_comment: See the description for the
          parameter with the same name in the set_field_from_raw_string method.
        :param field_comment: See the description for the parameter with the same
          name in the set_field_from_raw_string method.
        r   z=Cannot use set_field_to_simple_value for values with newlinesr   r  N)r   r  r  )rL   r  simple_valuer  r  	raw_valuer<   r<   r?   r  8  s   6
z0Deb822ParagraphElement.set_field_to_simple_valuec                C   s  g }|dur|durt dn|dur&t|ts$|dd |D  d}d}t|\}}}|}z	| j|dd}	W n tyM   |rB | j|dfdd}	Y nw |du rTd}|	rY|	j}d	||f}
|
j	dd
}t
|ddD ](\}}|ds}t dj|d|dkr|d dvrd}t |j||d dqlt|dkr|d drt d|| tt|}| }|rt d| tt|}t|tsJ ||}|dusJ |r|	r|	j|_d|	_n|dur||_| || dS )ax  Sets a field in this paragraph to a given text value

        In many cases, it is better for callers to just use the paragraph as
        if it was a dictionary.  However, this method does enable to you choose
        the field comment (if any) and lets to have a higher degree of control
        over whitespace (on the first line), which can be a reason for using it.

        Example usage:

            >>> example_deb822_paragraph = '''
            ... Package: foo
            ... '''
            >>> dfile = parse_deb822_file(example_deb822_paragraph.splitlines(keepends=True))
            >>> p = next(iter(dfile))
            >>> raw_value = '''
            ... Build-Depends: debhelper-compat (= 12),
            ...                some-other-bd,
            ... # Comment
            ...                another-bd,
            ... '''.lstrip()  # Remove leading newline, but *not* the trailing newline
            >>> fname, new_value = raw_value.split(':', 1)
            >>> p.set_field_from_raw_string(fname, new_value)
            >>> print(p.dump(), end='')
            Package: foo
            Build-Depends: debhelper-compat (= 12),
                           some-other-bd,
            # Comment
                           another-bd,
            >>> # Format preserved

        :param item: Name of the field to set.  If the paragraph already
          contains the field, then it will be replaced.  Otherwise, it is
          added to the end of the paragraph.
          Note this can be a "paragraph key", which enables you to control
          *which* instance of a field is being replaced (in case of duplicate
          fields).
        :param raw_string_value: The text to use as the value.  The text must
          be valid deb822 syntax and is used *exactly* as it is given.
          Accordingly, multi-line values must include mandatory leading space
          on continuation lines, newlines after the value, etc. On the
          flip-side, any optional space or comments will be included.

          Note that the first line will *never* be read as a comment (if the
          first line of the value starts with a "#" then it will result
          in "Field-Name:#..." which is parsed as a value starting with "#"
          rather than a comment).
        :param preserve_original_field_comment: If True, then if there is an
          existing field and that has a comment, then the comment will remain
          after this operation.  This is the default is the `field_comment`
          parameter is omitted.
          Note that if the parameter is True and the item is ambiguous, this
          will raise an AmbiguousDeb822FieldKeyError.  When the parameter is
          omitted, the ambiguity is resolved automatically and if the resolved
          field has a comment then that will be preserved (assuming
          field_comment is None).
        :param field_comment: If not None, add or replace the comment for
          the field.  Each string in the in the list will become one comment
          line (inserted directly before the field name). Will appear in the
          same order as they do in the list.

          If you want complete control over the formatting of the comments,
          then ensure that each line start with "#" and end with "\n" before
          the call.  Otherwise, leading/trailing whitespace is normalized
          and the missing "#"/"\n" character is inserted.
        NzNThe "preserve_original_field_comment" conflicts with "field_comment" parameterc                 s       | ]}t |V  qd S r;   )r   r   r<   r<   r?   rz     r  zCDeb822ParagraphElement.set_field_from_raw_string.<locals>.<genexpr>FTr  r   r   r   r<  r  r   z2Line {i} in new value was missing trailing newline)rH  )r   	r  zLine {i} in new value was invalid.  It must either start with " " space (continuation line) or "#" (comment line). The line started with "{line}")rH  r  r=  r  z2The last line in a value field cannot be a commentr   )r   rn   r  r   r  r   r   r   r   r   r  r   r.  r#  r  r   r   r   r~   r   rl  r  )rL   r  raw_string_valuer  r  r   r   r8  cased_field_nameoriginalraw	raw_linesrH  r  r1  r   r   r   rU   r<   r<   r?   r  |  sf   J



z0Deb822ParagraphElement.set_field_from_raw_stringc                 C   rB   r;   r<   rL   fdr<   r<   r?   dump  r  zDeb822ParagraphElement.dumpc                 C   rB   r;   r<   rV   r<   r<   r?   r    r  c                 C   D   |d u rd dd |  D S |  D ]}||jd qd S )Nr   c                 s   r   r;   r   r   r<   r<   r?   rz     r   z.Deb822ParagraphElement.dump.<locals>.<genexpr>utf-8r   r   writer   encoderL   r  r   r<   r<   r?   r    
   r  r;   )rZ   r[   r\   classmethodr  r  r  r_   r  r  r  r  r  r  r  r  r  r  r  r   r  r  r	  r  r  r.   r  r<   r<   r<   r?   r    s^    



R 



H 


r  c                       s   e Zd ZdZ fddZe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dZdd ZdddZdd Z  ZS ) r   zParagraph implementation optimized for valid deb822 files

    When there are no duplicated fields, we can use simpler and faster
    datastructures for common operations.
    c                    s,   t    dd |D | _|| _|   d S )Nc                 S   s   i | ]}|j |qS r<   r  r  r<   r<   r?   
<dictcomp>)  s    zDDeb822NoDuplicateFieldsParagraphElement.__init__.<locals>.<dictcomp>)r   rR   _kvpair_elements_kvpair_orderrZ  )rL   r  r  r   r<   r?   rR   #  s   
z0Deb822NoDuplicateFieldsParagraphElement.__init__c                 C   rW  r;   )r#  r  rV   r<   r<   r?   r  -  r;  z4Deb822NoDuplicateFieldsParagraphElement.kvpair_countc                 C   "   t |dd\}}}| j| dS )r  Tr  N)r  r   r  rL   r  unpacked_fieldr8  r<   r<   r?   r  2     z2Deb822NoDuplicateFieldsParagraphElement.order_lastc                 C   r!  )r  Tr"  N)r  r   r  r#  r<   r<   r?   r  8  r%  z3Deb822NoDuplicateFieldsParagraphElement.order_firstc                 C   6   t |dd\}}}t |dd\}}}| j|| dS )r   Tr"  N)r  r   r  rL   r  r  r$  r8  unpacked_ref_fieldr<   r<   r?   r  >  s   z4Deb822NoDuplicateFieldsParagraphElement.order_beforec                 C   r&  )r  Tr"  N)r  r   r  r'  r<   r<   r?   r  G  s   z3Deb822NoDuplicateFieldsParagraphElement.order_afterc                 c       dd | j D E d H  d S )Nc                 s   r  r;   )rE  )ry   r  r<   r<   r?   rz   S  r  zDDeb822NoDuplicateFieldsParagraphElement.iter_keys.<locals>.<genexpr>r   rV   r<   r<   r?   r  Q     z1Deb822NoDuplicateFieldsParagraphElement.iter_keysc                 C   s*   t |dd\}}}| j|= | j| d S NTr"  )r  r  r   rY   )rL   r   r8  r<   r<   r?   r  U  s   z=Deb822NoDuplicateFieldsParagraphElement.remove_kvpair_elementc                 C   s:   t |tttfs
dS td|}t|dd\}}}|| jv S )NFr5   Tr"  )rn   rE  r  r   r-   r  r  )rL   r  r   r8  r<   r<   r?   r  [  s
   

z?Deb822NoDuplicateFieldsParagraphElement.contains_kvpair_elementFc                 C   s,   t |dd\}}}|r| j|S | j| S r,  )r  r  get)rL   r  r  r8  r<   r<   r?   r   c  s   
z:Deb822NoDuplicateFieldsParagraphElement.get_kvpair_elementc                 C   s   t |dd\}}}t|tr||jurtd|j}n||jkr$td|j}| j|}|| j|< | j	| |d ur?d |_
| |_
d S )NTr"  zOKey is a Deb822FieldNameToken, but not *the* Deb822FieldNameToken for the valuegCannot insert value under a different field value than field name from its Deb822FieldNameToken implies)r  rn   r   r  r   r   r  r-  r   r   rX  )rL   r   rU   r8  original_valuer<   r<   r?   r  m  s   




z:Deb822NoDuplicateFieldsParagraphElement.set_kvpair_elementNc                 C   sN   t | jD ]}| jtd| }|j   |du rt}tt| j|d| _dS )r  r   Nr   )	r   r   r  r-   r   r}  r   r   sorted)rL   r   last_field_namelast_kvpairr<   r<   r?   r	    s   
z3Deb822NoDuplicateFieldsParagraphElement.sort_fieldsc                 #   s&     fddt d jD E d H  d S )Nc                 3   s    | ]} j | V  qd S r;   )r  r   rV   r<   r?   rz     s    zEDeb822NoDuplicateFieldsParagraphElement.iter_parts.<locals>.<genexpr>zIterable[_strI])r-   r   rV   r<   rV   r?   rN    s   

z2Deb822NoDuplicateFieldsParagraphElement.iter_partsr  r;   )rZ   r[   r\   r]   rR   r_   r  r  r  r  r  r  r  r  r   r  r	  rN  r  r<   r<   r   r?   r     s"    

	




r   c                       s   e Zd Z fddZe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edd Zdd Z	d)ddZ	d)ddZedd Zd d! Zd"d# Zd$d% Zd*d'd(Z  ZS )+r  c                    s.   t    t | _i | _| | |   d S r;   )r   rR   r	   r   r  _init_kvpair_fieldsrZ  )rL   r  r   r<   r?   rR     s   

z.Deb822DuplicateFieldsParagraphElement.__init__c                 C   s   t | jt | jkS r;   )r#  r   r  rV   r<   r<   r?   r    s   z:Deb822DuplicateFieldsParagraphElement.has_duplicate_fieldsc                 C   s\   | j rJ | jr
J |D ]}|j}| j |}|| jvr#|g| j|< q| j| | qd S r;   )r   r  r   r   )rL   kvpairsr  r   rM   r<   r<   r?   r3    s   


z9Deb822DuplicateFieldsParagraphElement._init_kvpair_fieldsc                 C   sf   t |\}}}| j| }g }|d us|d ur-| ||||}|d us$J || ||fS |}||fS r;   )r  r  _resolve_to_single_noder   )rL   r  r   r  r  nodesnodes_being_relocatedsingle_noder<   r<   r?   _nodes_being_relocated  s   

z<Deb822DuplicateFieldsParagraphElement._nodes_being_relocatedc                 C   s   |  |\}}t|dkst|t|ksJ | j}|D ]}|j|u r$q|| |jdus0J |||j qt|dkrV|d |d urX|d }|| || dS dS dS )r  r<  Nr   r=  )r9  r#  r   r   remove_nodeinsert_node_afterrY   r   rL   r  r6  r7  r  rM   r8  r<   r<   r?   r    s    


z0Deb822DuplicateFieldsParagraphElement.order_lastc                 C   s   |  |\}}t|dkst|t|ksJ | j}|D ]}|j|u r$q|| |jdus0J |||j qt|dkrW|d |d urY|d }|| |d| dS dS dS )r  r<  Nr   )r9  r#  r   r   r:  insert_node_beforerY   insertr<  r<   r<   r?   r    s    


z1Deb822DuplicateFieldsParagraphElement.order_firstc                 C   s   |  |\}}t|dkst|t|ksJ |  |\}}|d }||v r*td| j}|D ]}	||	 ||	| q/t|dkrVt|dkrX|d jj}
| |
 dS dS dS )r   r<  r   *Cannot re-order a field relative to itselfN)	r9  r#  r   r   r:  r=  rU   r   !_regenerate_relative_kvapir_orderrL   r  r  r6  r7  r8  reference_nodesreference_noder  rM   r   r<   r<   r?   r    s    
z2Deb822DuplicateFieldsParagraphElement.order_beforec                 C   s   |  |\}}t|dkst|t|ksJ |  |\}}|d }||v r*td| j}t|D ]}	||	 ||	| q1t|dkrXt|dkrZ|d jj}
| 	|
 dS dS dS )r  r<  r=  r?  r   N)
r9  r#  r   r   r   r:  r;  rU   r   r@  rA  r<   r<   r?   r  	  s    
z1Deb822DuplicateFieldsParagraphElement.order_afterc                 C   s8   g }| j  D ]}|jj|kr|| q|| j|< d S r;   )r   r   rU   r   r   r  )rL   r   r6  rM   r<   r<   r?   r@  !	  s   
zGDeb822DuplicateFieldsParagraphElement._regenerate_relative_kvapir_orderc                 c   rc  r;   r*  rV   r<   r<   r?   rN  )	  rd  z0Deb822DuplicateFieldsParagraphElement.iter_partsc                 C   rW  r;   )r#  r   rV   r<   r<   r?   r  -	  r;  z2Deb822DuplicateFieldsParagraphElement.kvpair_countc                 c   r)  )Nc                 s   r   r;   r  r  r<   r<   r?   rz   4	  r   zBDeb822DuplicateFieldsParagraphElement.iter_keys.<locals>.<genexpr>r*  rV   r<   r<   r?   r  2	  r+  z/Deb822DuplicateFieldsParagraphElement.iter_keysFc                 C   s   |d u r.t |dkr,|d ur| ||}|d ur|S d}t|j|t |t |d dd}z|| W S  tyJ   |r?Y d S d}t|j||dw )Nr<  zAmbiguous key {key} - the field appears {res_len} times. Use ({key}, index) to denote which instance of the field you want.  (Index can be 0..{res_len_1} or e.g. -1 to denote the last field))r   res_len	res_len_1r   z>Field "{key}" was present but the index "{index}" was invalid.r  )r#  _find_node_via_name_tokenr   r.  
IndexErrorr  )rL   r6  r   r  r  r  rM   r1  r<   r<   r?   r5  6	  s(   

z=Deb822DuplicateFieldsParagraphElement._resolve_to_single_nodec                 C   s\   t |\}}}|r| j|}|d u rd S n| j| }| j|||||d}|d ur,|jS d S )Nr  )r  r  r-  r5  rU   )rL   r  r  r   r  r  r6  rM   r<   r<   r?   r   S	  s   
z8Deb822DuplicateFieldsParagraphElement.get_kvpair_elementc                 C   s"   |D ]}| |j ju r|  S qd S r;   )rU   r  )r  elementsrM   r<   r<   r?   rF  d	  s
   
z?Deb822DuplicateFieldsParagraphElement._find_node_via_name_tokenc                 C   s0   t |tttfs
dS td|}| j|ddd uS )NFr5   Tr  )rn   rE  r  r   r-   r   r  r<   r<   r?   r  s	  s   
z=Deb822DuplicateFieldsParagraphElement.contains_kvpair_elementc                 C   s  t |\}}}|r8||jur4| j|j}d }|d ur!| ||}|d u r)td|d us/J ||}|j}n||jkrAtd|j}| j|}|d u sP|s|d urc|dkrcd}t|j	||d| j
|}|| jvrv|g| j|< d S | j| | d S d}	|d u rd}	|d }t|dkr|g| j|< n|| }d |j_| |_||_|	rt|dkr|dd  D ]}
d |
j_| j
|
 qd S d S d S )	NzKey is a Deb822FieldNameToken, but not *the* Deb822FieldNameToken for the value nor the Deb822FieldNameToken for an existing field in the paragraphr.  r   zCannot replace field ({key}, {index}) as the field does not exist in the first place.  Please index-less key or ({key}, 0) if you want to add the field.r  FTr<  )r  r  r  r-  r   rF  r   r  r  r.  r   r   r#  rU   rX  r:  )rL   r   rU   r  r  original_nodesoriginal_noder1  rM   replace_allr   r<   r<   r?   r  z	  sV   



z8Deb822DuplicateFieldsParagraphElement.set_kvpair_elementc                 C   s   t |\}}}| j| }|d u r)|d u r)|D ]}d |j_| j| q| j|= d S |d urD| ||}|d u rAd}t|j|d|}n|d usJJ z|| }W n tyb   d}t|j||dw t	|dkrn| j|= n|
| d |j_| j| d S )Nz>The field "{key}" is present but key used to access it is not.r   z@The field "{key}" is present, but the index "{idx}" was invalid.)r   r  r<  )r  r  rU   rX  r   r:  rF  r  r.  r#  rY   )rL   r   r  r  
field_listrM   rJ  r1  r<   r<   r?   r  	  s4   


z;Deb822DuplicateFieldsParagraphElement.remove_kvpair_elementNc                    s`   |du rt }|  fdd}t| jD ]}|j   t| j|d}t | _i | _| | dS )a  Re-order all fields

        :param key: Provide a key function (same semantics as for sorted).   Keep in mind that
          the module preserve the cases for field names - in generally, callers are recommended
          to use "lower()" to normalize the case.
        Nc                    s
    | j S r;   r  )r  key_implr<   r?   _actual_key	  r   zFDeb822DuplicateFieldsParagraphElement.sort_fields.<locals>._actual_keyr   )	r   r   r   r   r}  r0  r	   r  r3  )rL   r   rO  r2  sorted_kvpair_listr<   rM  r?   r	  	  s   	
z1Deb822DuplicateFieldsParagraphElement.sort_fieldsr  r;   )rZ   r[   r\   rR   r_   r  r3  r9  r  r  r  r  r@  rN  r  r  r5  r   staticmethodrF  r  r  r  r	  r  r<   r<   r   r?   r    s2    	

	


@#r  c                       s   e Zd ZdZ fddZedd Zedd Zdd	 Z	d
d Z
dd Zdd Zdd Zdd Zedd Zedd Z	dddZ  ZS )Deb822FileElementz!Represents the entire deb822 filec                    ra  r;   )r   rR   _token_and_elementsrZ  )rL   token_and_elementsr   r<   r?   rR   	  rb  zDeb822FileElement.__init__c                 C   s
   | t  S )zCreates a new Deb822FileElement with no contents

        Note that a deb822 file must be non-empty to be considered valid
        )r	   r  r<   r<   r?   new_empty_file
  s   
z Deb822FileElement.new_empty_filec                 C   s6   d}| D ]}d}|r|j r dS q|sdS |  du S )a  Returns true if the file is valid

        Invalid elements include error elements (Deb822ErrorElement) but also
        issues such as paragraphs with duplicate fields or "empty" files
        (a valid deb822 file contains at least one paragraph).
        FTN)r  r   )rL   had_paragraphr   r<   r<   r?   is_valid_file
  s   	
zDeb822FileElement.is_valid_filec                 C   s   t t| jtddS )z:Returns the first Deb822ErrorElement (or None) in the filerU  N)r~   r   rV  r^  rV   r<   r<   r?   r   !
     z*Deb822FileElement.find_first_error_elementc                 C   s   t | tS r;   )r   rS  r  rV   r<   r<   r?   r}   &
  r  zDeb822FileElement.__iter__c                 c   rc  r;   )rS  rV   r<   r<   r?   rN  *
  rd  zDeb822FileElement.iter_partsc           	      C   s   d}d}|dkr| j s| | dS | j j}t| j }n d}| j  D ]}|j}t|tr1|d7 }||d kr;|} nq#|du rG| | dS |rW| t	d}| j 
||}| j 
| || dS )a  Inserts a paragraph into the file at the given "index" of paragraphs

        Note that if the index is between two paragraphs containing a "free
        floating" comment (e.g. paragrah/start-of-file, empty line, comment,
        empty line, paragraph) then it is unspecified which "side" of the
        comment the new paragraph will appear and this may change between
        versions of python-debian.


        >>> original = '''
        ... Package: libfoo-dev
        ... Depends: libfoo1 (= ${binary:Version}), ${shlib:Depends}, ${misc:Depends}
        ... '''.lstrip()
        >>> deb822_file = parse_deb822_file(original.splitlines(keepends=True))
        >>> para1 = Deb822ParagraphElement.new_empty_paragraph()
        >>> para1["Source"] = "foo"
        >>> para1["Build-Depends"] = "debhelper-compat (= 13)"
        >>> para2 = Deb822ParagraphElement.new_empty_paragraph()
        >>> para2["Package"] = "libfoo1"
        >>> para2["Depends"] = "${shlib:Depends}, ${misc:Depends}"
        >>> deb822_file.insert(0, para1)
        >>> deb822_file.insert(1, para2)
        >>> expected = '''
        ... Source: foo
        ... Build-Depends: debhelper-compat (= 13)
        ...
        ... Package: libfoo1
        ... Depends: ${shlib:Depends}, ${misc:Depends}
        ...
        ... Package: libfoo-dev
        ... Depends: libfoo1 (= ${binary:Version}), ${shlib:Depends}, ${misc:Depends}
        ... '''.lstrip()
        >>> deb822_file.dump() == expected
        True
        NTr   r<  r   )rS  r   r   boolr   rU   rn   r  _set_parentr   insert_before)	rL   r  paraanchor_nodeneeds_newlinerH  rM   entrynl_tokenr<   r<   r?   r>  .
  s.   &

zDeb822FileElement.insertc                 C   sl   | j j}|jdur|j| u rtdtd|r(t|ts(| j | td | j | | | |_dS )a  Appends a paragraph to the file

        >>> deb822_file = Deb822FileElement.new_empty_file()
        >>> para1 = Deb822ParagraphElement.new_empty_paragraph()
        >>> para1["Source"] = "foo"
        >>> para1["Build-Depends"] = "debhelper-compat (= 13)"
        >>> para2 = Deb822ParagraphElement.new_empty_paragraph()
        >>> para2["Package"] = "foo"
        >>> para2["Depends"] = "${shlib:Depends}, ${misc:Depends}"
        >>> deb822_file.append(para1)
        >>> deb822_file.append(para2)
        >>> expected = '''
        ... Source: foo
        ... Build-Depends: debhelper-compat (= 13)
        ...
        ... Package: foo
        ... Depends: ${shlib:Depends}, ${misc:Depends}
        ... '''.lstrip()
        >>> deb822_file.dump() == expected
        True
        Nz(Paragraph is already a part of this filez/Paragraph is already part of another Deb822Filer   )rS  rm   rX  r   rn   r   r   rZ  )rL   r   tail_elementr<   r<   r?   r   s
  s   


zDeb822FileElement.appendc                 C   s
   | |_ |S r;   )rX  )rL   r=   r<   r<   r?   rZ  
  s   zDeb822FileElement._set_parentc                 C   rB   r;   r<   r  r<   r<   r?   r  
  r  zDeb822FileElement.dumpc                 C   rB   r;   r<   rV   r<   r<   r?   r  
  r  Nc                 C   r  )Nr   c                 s   r   r;   r   r   r<   r<   r?   rz   
  r   z)Deb822FileElement.dump.<locals>.<genexpr>r  r  r  r<   r<   r?   r  
  r  r;   )rZ   r[   r\   r]   rR   r  rU  r_   rW  r   r}   rN  r>  r   rZ  r.   r  r  r<   r<   r   r?   rR  	  s&    

E&

rR  )constructorc                 C   s   | rt jS t jS r;   )r7  r  r   )discard_commentsr<   r<   r?   _parsed_value_render_factory
  s   
rd  c                   C   s   t dS r   )r   r<   r<   r<   r?   r@   
  s    c                 C   s   t | t p
| jdkS r   )rn   r   r   r'  r<   r<   r?   _non_end_of_line_token
  rX  re  c                 c   sJ   t | }d}d}d}|D ]}d}t|tr4| }t|tr3d}|}d}tdt|d}|dus3J nt|tr@|}d}d}nt|trGd}|durN|V  |rt|	t
}tdt|d}|du sj|jdksjJ d}	d}
|rt|d tr~td| }
|rt|d trtd|d }	|d	d }t|||	||
|V  d}d}qdS )
zYParser helper - consumes tokens part of a Deb822ValueEntryElement and turns them into oneNFTr   r   r   r=  r   r<  )r   rn   r  peekr   r-   r~   r   list	takewhilere  r   r   ro   re  )token_streambuffered_streamrl  rm  r   start_of_value_entry
next_tokentokens_in_value	eol_tokenleading_whitespacetrailing_whitespacer<   r<   r?   _build_value_line
  sf   



rq  c              	   c   sV   t | }|D ]}d}d }t|tr0|}| }t|t}|r/zt|}W n ty.   tw nt|tr7d}|r|}|d}t	|dk r[|rK|V  |g}|
| t|V   d S |\}	}
t|	trt|
trt|d  t|d  t|td||	|
V  q|g}|
|t | }|rt|trt|d  || t|V  q|V  qd S )NFT   r   )r   rn   r  rf  r   r~   StopIterationAssertionErrorr?  r#  r   r^  r   rx  r:   r-   rh  re  r   r   )ri  rj  token_or_elementstart_of_fieldrl  rl  r   next_tokenserror_elements	separatorr   error_tokensnlr<   r<   r?   _build_field_with_valueD  s`   










r|  )accept_files_with_error_tokens#accept_files_with_duplicated_fieldsc                C   s  t | }t|}t|}t|}t|}t|}t|}tt|}|s<|	 }|dur<|
 dd}tdj|d|st|D ]?\}}t|trt }	d}
| D ]}t|\}}}t|tsdJ ||	v rl|}
 n|	| qT|
durd}t|j|
|dqB|S )am  

    :param sequence: An iterable over lines of str or bytes (an open file for
      reading will do).  The lines must include the trailing line ending ("\n").
    :param accept_files_with_error_tokens: If True, files with critical syntax
      or parse errors will be returned as "successfully" parsed. Usually,
      working on files with these kind of errors are not desirable as it is
      hard to make sense of such files (and they might in fact not be a deb822
      file at all).  When set to False (the default) a ValueError is raised if
      there is a critical syntax or parse error.
      Note that duplicated fields in a paragraph is not considered a critical
      parse error by this parser as the implementation can gracefully cope
      with these. Use accept_files_with_duplicated_fields to determine if
      such files should be accepted.
    :param accept_files_with_duplicated_fields: If True, then
      files containing paragraphs with duplicated fields will be returned as
      "successfully" parsed even though they are invalid according to the
      specification.  The paragraphs will prefer the first appearance of the
      field unless caller explicitly requests otherwise (e.g., via
      Deb822ParagraphElement.configured_view).  If False, then this method
      will raise a ValueError if any duplicated fields are seen inside any
      paragraph.
    Nr   z\nz4Syntax or Parse error on the line: "{error_as_text}")error_as_textz6Duplicate field "{dup_field}" in paragraph number {no})	dup_fieldno)r   %_combine_comment_tokens_into_elementsrq  (_combine_vl_elements_into_value_elementsr|  %_combine_kvp_elements_into_paragraphs#_combine_error_tokens_into_elementsrR  r	   r   r   r   r   r.  r  rn   r  setkeysr  rE  add)sequencer}  r~  r  r   error_elementr  r  r   field_namesr  r  r   r8  r1  r<   r<   r?   r     s@   "
r   __main__r  )ycollections.abccollections
contextlibr,  rJ   abcr   typesr   r   debian._deb822_repro.formatterr   r   r   debian._utilr   r	   r
   r   r   r   debian._deb822_repro.typesr   debian._deb822_repro.tokensr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   debian._deb822_repro._utilr   r   r    typingr!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   StreamingValueParserrE  StrToValueParserKVPNodeImportErrorrD   AbstractContextManagerra   r  r  rh   r4  r9  r:  rC  rK  r   r^  re  rx  r7  r  r:   r   r  r  r  MutableMappingr  r  r  r  r  r   r  rR  r  r  r  r  r  rd  #LIST_SPACE_SEPARATED_INTERPRETATION#LIST_COMMA_SEPARATED_INTERPRETATIONLIST_UPLOADERS_INTERPRETATIONre  rq  r|  r   rZ   doctesttestmodr<   r<   r<   r?   <module>   s    DH(P    
? $"0AZ'H
"-l&    6  b 
;	`?
K