o |a @svddlZddlZddlZddlZddlmZddlmZddlm Z ddl m Z m Z m Z ddlmZmZmZmZmZmZddlmZddlmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&dd l'm(Z(m)Z)m*Z*z[dd l+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;dd lmZ>m?Z?m@Z@mAZAmBZBmCZCmDZDe2ee)ege>fZEe2eFge,e/d e>ffZGedZHWneIye;sddZ8ddZ9YnwGddde4e?ZJGddde4e>e=fejKdZLGddde4e<ZMGdddeMe<e4efZNddZOGdddeNeLe>e=fe>fZPdd ZQd!d"ZRd#d$ZSd%d&ZTGd'd(d(ZUGd)d*d*eUZVGd+d,d,eUZWGd-d.d.eUZXGd/d0d0eUZYGd1d2d2eUZZGd3ddeUZ[d4d5Z\ 6d[d7d8Z]d9d:Z^Gd;d<dd>e_eFejj`e@eFfeZaGd?d@d@e_e<eZbGdAdBdBebe<ZcGdCdDdDebeFeaZdGdEdFdFeUeaeZeGdGdHdHeeZfGdIdJdJeeZgGdKdLdLeUZhe(e#eVZie(eeZZje(eWeXZke(e[eeeejldMZmdNdOZnePe&eQeYedPdenZoePe%eSeYe e enZpePe%eTeYe e enZqdQdRZrdSdTZsdUdVZtd6d6dWdXdYZuevdZkr9ddlwZwewxdSdS)\N)ABC) TracebackType) ReferenceType)FormatterContentToken%one_value_per_line_trailing_separator format_field) resolve_ref LinkedListLinkedListNode OrderedSet_strIdefault_field_sort_key)AmbiguousDeb822FieldKeyError) Deb822TokenDeb822ValueToken'Deb822SemanticallySignificantWhiteSpaceDeb822SpaceSeparatorTokenDeb822CommentTokenDeb822WhitespaceTokenDeb822ValueContinuationTokenDeb822NewlineAfterValueTokenDeb822CommaTokenDeb822FieldNameTokenDeb822FieldSeparatorTokenDeb822ErrorTokentokenize_deb822_filecomma_split_tokenizerwhitespace_split_tokenizer)combine_into_replacementBufferingIteratorlen_check_iterator)IterableIteratorListUnionDictOptionalCallableAnyGenericTypeTupleIOcastoverloadMapping TYPE_CHECKING)T)STVETE ParagraphKeyTokenOrElement CommentishParagraphKeyBaseFormatterCallbackrDeb822KeyValuePairElementcCs|SN)tvr<r<>/usr/lib/python3/dist-packages/debian/_deb822_repro/parsing.py.r@cCdSr;r<)fr<r<r?r@/rAc@sFeZdZdZdZddZddZeddZej d dZd d Z d S) ValueReferenceafReference 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_notifiercCs(t||_||_||_||_||_dSr;)weakrefrefrErFrGrHrI)selfnoderender value_factoryremoval_handlermutation_notifierr<r<r?__init__Hs  zValueReference.__init__cCs.|jdur td|}|durtd|S)Nz(Cannot use ValueReference after remove()z-ValueReference is invalid (garbage collected))rE RuntimeError)rLrMr<r<r? _resolve_nodeUs zValueReference._resolve_nodecCs||jS)z Resolve the reference into a str)rFrTvaluerLr<r<r?rU`szValueReference.valuecCs*|||_|jdur|dSdS)aUpdate 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)rGrTrUrIrL new_valuer<r<r?rUfs  cCs|td|d|_dS)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. zLinkedListNode[TokenOrElement]N)rHr-rTrErVr<r<r?removevs zValueReference.removeN) __name__ __module__ __qualname____doc__ __slots__rRrTpropertyrUsetterrYr<r<r<r?rD2s    rDcseZdZddZddZddZfddZed d Zd d Z d dZ d>ddZ ddZ ddZ ddZddZddZddZddZd d!Zed"d#Zd$d%Zd&d'Zd(d)Zd*d+Z ,d?d-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d,d8d9d:Zd7d;drVr<r? z1Deb822ParsedTokenList.__iter__..) value_partsrVr<rVr?__iter__szDeb822ParsedTokenList.__iter__cCstt|dduSr;)nextiterrVr<r<r?__bool__szDeb822ParsedTokenList.__bool__cs&|dur |jr |t|||Sr;)rk _update_fieldsuper__exit__)rLexc_typeexc_valexc_tb __class__r<r?rszDeb822ParsedTokenList.__exit__c#rw)Nc3s |] }t|jr|VqdSr;)rnrdrxrVr<r?rzsz4Deb822ParsedTokenList.value_parts..rcrVr<rVr?r|sz!Deb822ParsedTokenList.value_partscC d|_dSNT)rkrVr<r<r? _mark_changed z#Deb822ParsedTokenList._mark_changedc#s$fddjDEdHdS)aIterate 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. c3s<|]}t|jjrttd|jjjjVqdS)zLinkedListNode[VE]N) rnrUrdrDr-rFrG _remove_noder)rynrVr<r?rzs  z>Deb822ParsedTokenList.iter_value_references..N)rc iter_nodesrVr<rVr?iter_value_referencess z+Deb822ParsedTokenList.iter_value_referencesTcCsR|}|jr d}d|_||j||r%|js'|jtddSdSdSNFT )rg is_whitespacerk,_append_continuation_line_token_if_necessaryrcappendr)rLspace_after_separatorseparator_tokenr<r<r?append_separators  z&Deb822ParsedTokenList.append_separatorcCsR|j}|jD]}t|j|r$||j|kr$|||_d|_dSqtd)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) rdrcrrnrUrFrGrk ValueError)rL orig_valuerXrrrMr<r<r?replaces zDeb822ParsedTokenList.replacecCsL|j}|jD]}t|j|r||j|kr|}nqtd||S)zRemove the first instance of a value Removal will invalidate ValueReferences to the value being removed. ValueReferences to other values will be unaffected. zlist.remove(x): x not in list)rdrcrrnrUrFrr)rLrUrrrMnode_to_remover<r<r?rYs zDeb822ParsedTokenList.removecCs0|j}d|_d}d}d}d}|jddD]}|j}t|tr$|jr$d}qt||r-|}nq|jddD]} | j} t| trD| jrDd}q4t| |rM| }nq4|dur]|dur]|j dS|durf|sfd} n |duro|sod} n|du} | r{|} |j } n|j } |} | dur| |j_ | dur| |j_ t| | dS)NTF skip_current)rdrk iter_previousrUrnr is_comment iter_nextrcclear next_node previous_node head_node tail_noder link_nodes)rLrrrfirst_value_on_lhsfirst_value_on_rhscomment_before_previous_valuecomment_before_next_value past_node past_token future_node future_tokendelete_lhs_of_nodefirst_remain_lhsfirst_remain_rhsr<r<r?rsT;     z"Deb822ParsedTokenList._remove_nodecCs||}||dSr;)rG append_value)rLrUvtr<r<r?rxs zDeb822ParsedTokenList.appendcCs|j}|r+d}|j}|j}t|D]}t||rd}nt||r#nq|r*|n|jtd|d|_ ||dSr) rcrerdreversedrnrrrrrk)rLrr|needs_separatorrsrrr=r<r<r?r}s&   z"Deb822ParsedTokenList.append_valuecCs|jj}|duo|dSN )rcrmconvert_to_textendswithrLrmr<r<r?_previous_is_newlinesz*Deb822ParsedTokenList._previous_is_newlinecCs"|rtd|jtdS)Nz9Cannot add a newline after a token that ends on a newline)rrrcrrrVr<r<r?append_newlinesz$Deb822ParsedTokenList.append_newlinecCsB|jj}|dus|ds|tt|}|j|dSr)rcrmrrrr_format_commentr)rL comment_textrm comment_tokenr<r<r?append_comments  z$Deb822ParsedTokenList.append_commentcCs<|j}|durd}|jD] }t|tr|j}nq ||_|SNr)rlrcrnrtext)rLchartokenr<r<r?_continuation_line_chars  z-Deb822ParsedTokenList._continuation_line_charcCs<|jj}|dur|dr|jt|jdSdSdSr)rcrmrrrrrrr<r<r?rszBDeb822ParsedTokenList._append_continuation_line_token_if_necessarycCs|d|_dSr)_enable_reformattingrkrVr<r<r?reformat_when_finisheds z,Deb822ParsedTokenList.reformat_when_finishedcCr)NFrirVr<r<r?rrz*Deb822ParsedTokenList._enable_reformattingcCrrrrVr<r<r?no_reformatting_when_finishedrz3Deb822ParsedTokenList.no_reformatting_when_finishedFcCs||_d|_|r d|_dSdS)alUse 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)rjrirk)rL formatterforce_reformatr<r<r?value_formatters   z%Deb822ParsedTokenList.value_formatterccs2|jD]}t|tr|EdHq|VqdSr;)rcrn Deb822Element iter_tokens)rLter<r<r?_iter_content_as_tokenss   z-Deb822ParsedTokenList._iter_content_as_tokenscsH|}|j|j|jfdd}t|j|jjt |j |S)Nc3std}D]2}t|tr&|jrt|jVqt|r%|j}t|Vqt|s-J|}t|VqdS)N) rnrrrrrrr value_token)rrrs token_listrrr<r? _token_iters   zNDeb822ParsedTokenList._generate_reformatted_field_content.._token_iter) rgrdrercrrjrb field_namerrr)rLrrr<rr?#_generate_reformatted_field_contents z9Deb822ParsedTokenList._generate_reformatted_field_contentcCddd|DS)Nrcs|]}|jVqdSr;rryr=r<r<r?rzz@Deb822ParsedTokenList._generate_field_content..)joinrrVr<r<r?_generate_field_contentz-Deb822ParsedTokenList._generate_field_contentc Cs|j}|j}|j}|j}|D] }|js|jsnqtd|dus%Jt|t r1|jr1td| ds<| |j rK|}d||f}n|}|jdd}tt|} | } | rgtd|tt| } t| tstJ| |} | dusJ| j|_d|_dS) Nz>Field must have content (i.e. non-whitespace and non-comments)z Fields must not end on a commentr:Tkeepends$Syntax error in new field value for F)rbrrcrmrrrrrnrrrrrirrr splitlinesparse_deb822_filerfind_first_error_elementr~'Deb822NoDuplicateFieldsParagraphElementget_kvpair_element value_elementrk) rLrprrrmr= value_textr new_content deb822_file error_token paragraphnew_kvpair_elementr<r<r?rs:          z#Deb822ParsedTokenList._update_fieldNkeyreversec sfd}|j}|jfdd}g}|jD]=}|j}t|tr)|jr)|dur(|}qt||rRg} |durI|jddD]} | |urBn| | jq:| || fd}q|j ||dd|_ |j d} | j} |D]E\}} | rd} | rfdd | D} |n"| stfd d | Ds|jdd | r|n|j td |j| ||qkdS)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. Ncsr|dS|dSNrr)xrr<r?key_funcBs  z5Deb822ParsedTokenList.sort_elements..key_funcFrrTcsg|] }t|s|qSr<rnryrrsr<r? isz7Deb822ParsedTokenList.sort_elements..c3s|]}t|VqdSr;rrrr<r?rznr{z6Deb822ParsedTokenList.sort_elements..)rr)rdrercrrUrnrrrrsortrkrrgrranyrrextendr) rLrrcomment_start_noderrrpartsrMrUcomments keep_node first_valueseparator_is_spacer<)rrsr? sort_elements,sR         z#Deb822ParsedTokenList.sort_elementsrc s4dur|jfdd|d<|jdi|dS)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. Ncs |Sr;r<)rrrNr<r?r@s z,Deb822ParsedTokenList.sort..rr<)rFr)rLrkwargsr<rr?r|szDeb822ParsedTokenList.sortTF)!rZr[r\rRr}rrr_r|rrrrrYrrrrrrrrrrrrrrrrrr __classcell__r<r<rr?rasF#   t   + RrazDeb822ParsedTokenList[VE, ST]c@seZdZ dddZdS)InterpretationTcCtr;NotImplementedError)rLrpdiscard_comments_on_readr<r<r? interpretzInterpretation.interpretNr )rZr[r\rr<r<r<r?r sr csLeZdZfddZ dddZddZdd Zd d Z dd d ZZ S)!GenericContentBasedInterpretationcst||_||_dSr;)rrR _tokenizer _value_parser)rL tokenizer value_parserrr<r?rRs  z*GenericContentBasedInterpretation.__init__TcCrr;rrLrprrr<r<r?_high_level_interpretationzz vtype_namet1zSBad tokenizer - the token did not cover the input text exactly ({t1_len} != {v_len})t1_lenv_len) rrr~textwrapdedentformatrnrrrrZr#r)r> token_iterr)t2msgparserrrr<r?rGs.    z0_parser_to_value_factory.._value_factoryr<)r3rrrGr<r2r?rhsrhcs(eZdZfddZ dddZZS)ListInterpretationcs*t||||_||_||_||_dSr;)rrRrdrerg_render_factory)rLrrrrrsrurender_factoryrr<r?rRs   zListInterpretation.__init__Tc Cs"t|||j|j|j|j||Sr;)rardrerrgr5rr<r<r?rsz-ListInterpretation._high_level_interpretationr )rZr[r\rRrr r<r<rr?r4s r4cCs t|gSr;)Deb822ParsedValueElement)r_r<r<r?_parse_whitespace_list_value$rr9cCs t|tSr;)rnrr'r<r<r?_is_comma_token) r:cCs|t}|g}|dur|||dn|||r4t|dts4||r4t|dtr'|t |dt |S)N) peek_findr:r peek_many peek_bufferrnrro consume_manyr#r7)rr comma_offsetr|r<r<r?_parse_comma_list_value/s rCc Csf|g}d}|dur|t}|dur|dg}|||dd}t|d}|dkrf||}t|tr^|jdr]|| |t|dtrS|djds[Jdt |d}n|d8}|dks-|rm t|S|| |t|dt s~Jn,| }t|}|||rt|dts| |d8}|rt|dtr| ||dus t|S)Nr=r<Fr>zGot: T)r>r:rr?r#rnrrrrAstrrr@ror7) rrr|rBpeeked_elementscomma_was_separatoriremaining_partconsume_elementsr<r<r?_parse_uploaders_list_valueAsL         &rKc@steZdZdZdZddZddZddZd d Zd d d dZ e ddZ e j ddZ ddZ ddZddZd S)rz1Composite elements (consists of 1 or more tokens))_parent_element __weakref__cCs d|_dSr;)rLrVr<r<r?rRvrzDeb822Element.__init__cCrr;rrVr<r<r? iter_partszzDeb822Element.iter_partsccs$|D] }t||r|VqdSr;)rNrnrLonly_element_or_token_typepartr<r<r?iter_parts_of_type~s   z Deb822Element.iter_parts_of_typeccsB|D]}|jdusJt|tr|EdHq|VqdSr;)rNrLrnrrrLrRr<r<r?rs  zDeb822Element.iter_tokensNrQccsN|D]}|dust||rtd|Vt|tr$|j|dEdHqdS)Nr4rU)rNrnr-r iter_recurserPr<r<r?rVs   zDeb822Element.iter_recursecC t|jSr;)rrLrVr<r<r?parent_elementr;zDeb822Element.parent_elementcCs"|dur t||_dSd|_dSr;)rJrKrL)rL new_parentr<r<r?rXs"cCs|D]}||_qdSr;)rNrXrTr<r<r?_init_parent_of_partss z#Deb822Element._init_parent_of_partscCr)Nrcsrr;rrr<r<r?rzrz0Deb822Element.convert_to_text..rrrVr<r<r?rrzDeb822Element.convert_to_textcCs||jur d|_dSdSr;)rXrL)rLparentr<r<r?clear_parent_if_parents  z$Deb822Element.clear_parent_if_parent)rZr[r\r]r^rRrNrSrrVr_rXr`rZrr]r<r<r<r?rqs     rcs,eZdZdZdZfddZddZZS)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). _partsct||_|dSr;)rrRr`rZ)rLrrr<r?rR  zDeb822ErrorElement.__init__cc|jEdHdSr;r_rVr<r<r?rNzDeb822ErrorElement.iter_parts)rZr[r\r]r^rRrNr r<r<rr?r^s  r^cspeZdZdZdZfddZeddZeddZed d Z d d Z d dZ ddZ ddZ ddZZS)Deb822ValueLineElementz!Consists of one "line" of a value)_comment_element_continuation_line_token_leading_whitespace_token _value_tokens_trailing_whitespace_token_newline_tokencsRt|dur|durtd||_||_||_||_||_||_| dS)Nz)Only continuation lines can have comments) rrRrrfrgrhrirjrkrZ)rLcomment_elementcontinuation_line_tokenleading_whitespace_tokenr|trailing_whitespace_token newline_tokenrr<r?rRs  zDeb822ValueLineElement.__init__cC|jSr;rfrVr<r<r?rlz&Deb822ValueLineElement.comment_elementcCrqr;)rgrVr<r<r?rmrsz.Deb822ValueLineElement.continuation_line_tokencCrqr;)rkrVr<r<r?rprsz$Deb822ValueLineElement.newline_tokencCs"|jdurt|_||j_dSdSr;)rkrrXrVr<r<r?add_newline_if_missings  z-Deb822ValueLineElement.add_newline_if_missingccs2|jr|jV|jEdH|jr|jVdSdSr;)rhrirjrVr<r<r?_iter_content_partss  z*Deb822ValueLineElement._iter_content_partsccs4|D]}t|tr|EdHq|VqdSr;)rurnrrrTr<r<r?_iter_content_tokenss   z+Deb822ValueLineElement._iter_content_tokenscCsNt|jdkr|js|jst|jdtr|jdjSddd|DS)Nr<rrcsrr;rrr<r<r?rzrzADeb822ValueLineElement.convert_content_to_text..) r#rirhrjrnrrrrvrVr<r<r?convert_content_to_text s z.Deb822ValueLineElement.convert_content_to_textccsB|jr|jV|jr|jV|EdH|jr|jVdSdSr;)rfrgrurkrVr<r<r?rNs z!Deb822ValueLineElement.iter_parts)rZr[r\r]r^rRr_rlrmrprtrurvrwrNr r<r<rr?res     recs<eZdZdZfddZeddZddZdd ZZ S) Deb822ValueElement_value_entry_elementscrar;)rrRrzrZ)rLvalue_entry_elementsrr<r?rR%rbzDeb822ValueElement.__init__cCrq)zRead-only list of value entriesryrVr<r<r? value_lines+szDeb822ValueElement.value_linesccrcr;ryrVr<r<r?rN1rdzDeb822ValueElement.iter_partscCs|jr |jddSdS)Nr=)rzrtrVr<r<r?add_final_newline_if_missing5sz/Deb822ValueElement.add_final_newline_if_missing) rZr[r\r^rRr_r|rNr}r r<r<rr?rx"s  rxcs<eZdZdZfddZfddZddZdd ZZS) r7) _text_cached_text_no_comments_cachedrccs|t||_|t|dtrt|dts"t|jjdt |dkr6|d}|j |_ |j |_ dSd|_ d|_ dS)Nrr=z) MUST start and end on a Deb822ValueTokenr<) rrRrcrZrnrrrrZr#rr~r)rLtokensrrr<r?rR?s     z!Deb822ParsedValueElement.__init__cs|jdur t|_|jSr;)rrrrVrr<r?rOs  z(Deb822ParsedValueElement.convert_to_textcCs*|jdurddd|D|_|jS)Nrcs|] }|js|jVqdSr;rrrr<r<r?rzXs zLDeb822ParsedValueElement.convert_to_text_without_comments..)rrrrVr<r<r? convert_to_text_without_commentsUs   z9Deb822ParsedValueElement.convert_to_text_without_commentsccrcr;rrVr<r<r?rN]rdz#Deb822ParsedValueElement.iter_parts) rZr[r\r^rRrrrNr r<r<rr?r7;s   r7cs8eZdZdZfddZddZddZdd ZZS) Deb822CommentElement_comment_tokenscs(t||_|std|dS)Nz5Comment elements must have at least one comment token)rrRrrrZ)rLcomment_tokensrr<r?rRes  zDeb822CommentElement.__init__cCrWr;)r#rrVr<r<r?__len__mrzDeb822CommentElement.__len__cCs |j|Sr;rrLitemr<r<r? __getitem__qrz Deb822CommentElement.__getitem__ccrcr;rrVr<r<r?rNurdzDeb822CommentElement.iter_parts) rZr[r\r^rRrrrNr r<r<rr?rbs  rcseZdZdZfddZeddZeddZedd Zej d d Z dd d Z eddZ e j ddZ ddZ Z S)r:rf _field_token_separator_token_value_elementcs.t||_||_||_||_|dSr;)rrRrfrrrrZ)rLrl field_tokenrrrr<r?rR}s  z"Deb822KeyValuePairElement.__init__cC|jjSr;)rrrVr<r<r?rsz$Deb822KeyValuePairElement.field_namecCrqr;)rrVr<r<r?rrsz%Deb822KeyValuePairElement.field_tokencCrqr;)rrVr<r<r?rrsz'Deb822KeyValuePairElement.value_elementcCs|j|||_||_dSr;)rr]rXrWr<r<r?rs  TcCs|j||dSr%)r)rL interpreterrr<r<r? interpret_assz&Deb822KeyValuePairElement.interpret_ascCrqr;rrrVr<r<r?rlrsz)Deb822KeyValuePairElement.comment_elementcCsJ|dur|djdstd|jr|j||dur ||_||_dS)Nr=rz&Field comments must end with a newline)rrrrfr]rX)rLrUr<r<r?rls  ccs,|jr|jV|jV|jV|jVdSr;rrVr<r<r?rNs  z$Deb822KeyValuePairElement.iter_partsr )rZr[r\r^rRr_rrrr`rrlrNr r<r<rr?r:zs$         cCsT|dkrdSd|ddvrtd|ds|d}|ds(d|}|S)Nrz# rr=z-Comment lines must not have embedded newlines#z# )rrrstrip startswithlstrip)cr<r<r?rs    rFcCsvd}d}t|tr%|\}}|r |dkrd}t|j||dd}t|}nd}t|tr2|}|j}nt|}|||fS)NrzECannot resolve key "{key}" with index {index}. The key is not indexedrindex)rntupleKeyErrorr.r rr)rraise_if_indexedr name_tokenrr1r<r<r? _unpack_keys     rccsF|sdd|DEdHdS|D]}ddd|DVqdS)Ncss|]}|VqdSr;rrxr<r<r?rzz0_convert_value_lines_to_lines..rcsrr;rrr<r<r?rzsr[)r|strip_commentselementr<r<r?_convert_value_lines_to_liness rc@sTeZdZeddZeddZddZddZd d Zd d Z d dZ ddZ dS)AutoResolvingMixincCdSrr<rVr<r<r?_auto_resolve_ambiguous_fieldsz1AutoResolvingMixin._auto_resolve_ambiguous_fieldscCrr;rrVr<r<r? _paragraphrzAutoResolvingMixin._paragraphcCrr;)r kvpair_countrVr<r<r?rszAutoResolvingMixin.__len__cCs |j|Sr;)rcontains_kvpair_elementrr<r<r? __contains__  zAutoResolvingMixin.__contains__cCst|jSr;)rr iter_keysrVr<r<r?r}zAutoResolvingMixin.__iter__cCsF|jrt|tr|j|df}n|j|}|dusJ|||Sr)rrnrErr_interpret_value)rLrr>r<r<r?rs    zAutoResolvingMixin.__getitem__cCs|j|dSr;)rremove_kvpair_elementrr<r<r? __delitem__szAutoResolvingMixin.__delitem__cCrr;rrLrrUr<r<r?rrOz#AutoResolvingMixin._interpret_valueN) rZr[r\r_rrrrr}rrrr<r<r<r?rs   rc@sTeZdZeddZeddZeddZeddZd d Zd d Z d dZ dS) Deb822ParagraphToStrWrapperMixincCrrr<rVr<r<r?!_auto_map_initial_line_whitespace+rzBDeb822ParagraphToStrWrapperMixin._auto_map_initial_line_whitespacecCrrr<rVr<r<r?_discard_comments_on_read0rz:Deb822ParagraphToStrWrapperMixin._discard_comments_on_readcCrrr<rVr<r<r?+_auto_map_final_newline_in_multiline_values5rzLDeb822ParagraphToStrWrapperMixin._auto_map_final_newline_in_multiline_valuescCrrr<rVr<r<r?)_preserve_field_comments_on_field_updates:rzJDeb822ParagraphToStrWrapperMixin._preserve_field_comments_on_field_updatescs|j}|j}t|dkr|d}|}|jr|}|S|js#|jr=t||j}|jdfddt |ddD}n|}|j rP|ddkrP|dd}|S) Nr<rrc3s0|]\}}r|dkr|dn|VqdS)r<rN)strip)ryrHlineauto_map_spacer<r?rzUs$zIDeb822ParagraphToStrWrapperMixin._convert_value_to_str..startr=r) rr|r#rrrrrr enumerater)rLrpr value_entries value_entryr= converteras_textr<rr?_convert_value_to_str?s(     z6Deb822ParagraphToStrWrapperMixin._convert_value_to_strc Cs|j}d}|r&|jr&d}|}t|tr|df}|jj|dd}|dur&|j}|jrgz|d}Wn t y;d}Ynw|dksF|t |krT|jj || ||ddS| dd\}} dd | d| f}|dsw|jsst d |d7}|jj||||ddS) NrTuse_getrr=preserve_original_field_comment field_commentr<rrzeValues must end with a newline (or be single line values and use the auto whitespace mapping feature))rrrnrErrrlrrrr#set_field_to_simple_valuersplitrrrset_field_from_raw_string) rLrrU keep_commentscomment key_lookup orig_kvpairidx first_linerestr<r<r? __setitem__`sH     z,Deb822ParagraphToStrWrapperMixin.__setitem__cCstd||S)Nr1)r-rrr<r<r?rsz1Deb822ParagraphToStrWrapperMixin._interpret_valueN) rZr[r\r_rrrrrrrr<r<r<r?r's    ! -rc@s@eZdZdddddZeddZedd Zed d Zd S) AbstractDeb822ParagraphWrapperFTauto_resolve_ambiguous_fieldsrcCs||_||_||_dSr;)*_AbstractDeb822ParagraphWrapper__paragraph>_AbstractDeb822ParagraphWrapper__auto_resolve_ambiguous_fields9_AbstractDeb822ParagraphWrapper__discard_comments_on_read)rLrrrr<r<r?rRs z'AbstractDeb822ParagraphWrapper.__init__cCrqr;)rrVr<r<r?rrsz)AbstractDeb822ParagraphWrapper._paragraphcCrqr;)rrVr<r<r?rrsz8AbstractDeb822ParagraphWrapper._discard_comments_on_readcCrqr;)rrVr<r<r?rrsz=AbstractDeb822ParagraphWrapper._auto_resolve_ambiguous_fieldsN)rZr[r\rRr_rrrr<r<r<r?rs   rcs0eZdZddd d fddZdd ZZS) "Deb822InterpretingParagraphWrapperFTrreturnNcstj|||d||_dSNr)rrR_interpretation)rLrinterpretationrrrr<r?rRs  z+Deb822InterpretingParagraphWrapper.__init__cCs |j|Sr;)rrrr<r<r?rrz3Deb822InterpretingParagraphWrapper._interpret_value)rN)rZr[r\rRrr r<r<rr?rs rcsNeZdZddddddfdd ZeddZedd Zed d ZZS) Deb822DictishParagraphWrapperTFr auto_map_initial_line_whitespacer(preserve_field_comments_on_field_updates*auto_map_final_newline_in_multiline_valuescs(tj|||d||_||_||_dSr)rrR@_Deb822DictishParagraphWrapper__auto_map_initial_line_whitespaceH_Deb822DictishParagraphWrapper__preserve_field_comments_on_field_updatesJ_Deb822DictishParagraphWrapper__auto_map_final_newline_in_multiline_values)rLrrrrrrrr<r?rRs z&Deb822DictishParagraphWrapper.__init__cCrqr;)rrVr<r<r?rrsz?Deb822DictishParagraphWrapper._auto_map_initial_line_whitespacecCrqr;)rrVr<r<r?rrszGDeb822DictishParagraphWrapper._preserve_field_comments_on_field_updatescCrqr;)rrVr<r<r?rrszIDeb822DictishParagraphWrapper._auto_map_final_newline_in_multiline_values) rZr[r\rRr_rrrr r<r<rr?rs  rc@seZdZeddZeddZeddZeddZd d d d Z d d d d d d ddZ eddZ ddZ ddZ ddZddZeddZddZddZ d3d!d"Zd#d$Zd%d&Z 'd4d(d)Zd'd'd*d+d,Zd'd'd*d-d.Zed/d0Zed1d0Z 'd4d2d0Zd'S)5Deb822ParagraphElementcCs tgtSr;)rr clsr<r<r?new_empty_paragraphs z*Deb822ParagraphElement.new_empty_paragraphcCs&|}|D]\}}|||<q|Sr;)ritems)rmappingrkr>r<r<r? from_dicts z Deb822ParagraphElement.from_dictcCs@|stdtdd|D}t|t|krt||St|S)Nz9A paragraph must consist of at least one field/value paircsrr;rrykvr<r<r?rzrz6Deb822ParagraphElement.from_kvpairs..)rr r#r%Deb822DuplicateFieldsParagraphElement)rkvpair_elements kvpair_orderr<r<r? from_kvpairss  z#Deb822ParagraphElement.from_kvpairscCr)z0Tell whether this paragraph has duplicate fieldsFr<rVr<r<r?has_duplicate_fieldssz+Deb822ParagraphElement.has_duplicate_fieldsTrcCst|||dS)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)rLrrr<r<r?as_interpreted_dict_views Jz/Deb822ParagraphElement.as_interpreted_dict_viewrcCst||||||dS)awProvide 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)rLrrrrrr<r<r?configured_view^s z&Deb822ParagraphElement.configured_viewcCs|Sr;r<rVr<r<r?rrz!Deb822ParagraphElement._paragraphcCr)9Re-order the given field so it is "last" in the paragraphrrLfieldr<r<r? order_lastrz!Deb822ParagraphElement.order_lastcCr):Re-order the given field so it is "first" in the paragraphrrr<r<r? order_firstrz"Deb822ParagraphElement.order_firstcCr)Re-order the given field so appears directly after the reference field in the paragraph The reference field must be present.rrLrreference_fieldr<r<r? order_beforerz#Deb822ParagraphElement.order_beforecCr)Re-order the given field so appears directly before the reference field in the paragraph The reference field must be present. rrr<r<r? order_afterrz"Deb822ParagraphElement.order_aftercCrr;rrVr<r<r?rrz#Deb822ParagraphElement.kvpair_countcCrr;rrVr<r<r?rrOz Deb822ParagraphElement.iter_keyscCrr;rrr<r<r?rrOz.Deb822ParagraphElement.contains_kvpair_elementFcCrr;r)rLrrr<r<r?rrz)Deb822ParagraphElement.get_kvpair_elementcCrr;rrr<r<r?set_kvpair_element$rOz)Deb822ParagraphElement.set_kvpair_elementcCrr;rrLrr<r<r?r(rOz,Deb822ParagraphElement.remove_kvpair_elementNcCr) 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. rrr<r<r? sort_fields,s z"Deb822ParagraphElement.sort_fieldsrcCs6d|vrtdd|d}|j||||ddS)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. rz=Cannot use set_field_to_simple_value for values with newlinesrrN)rrr)rLr simple_valuerr raw_valuer<r<r?r8s6 z0Deb822ParagraphElement.set_field_to_simple_valuecCsg}|dur|durtdn|dur&t|ts$|dd|Dd}d}t|\}}}|}z |j|dd} WntyM|rB|j|dfdd} Ynw|durTd}| rY| j}d ||f} | j dd } t | d d D](\} } | d s}tdj | d| d kr| ddvrd}t|j | | ddqlt | d kr| ddrtd|| tt|}|}|rtd|tt|}t|tsJ||}|dusJ|r| r| j|_d| _n|dur||_|||dS)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" parametercs|]}t|VqdSr;)rrr<r<r?rzrzCDeb822ParagraphElement.set_field_from_raw_string..FTrrrrr<rrz2Line {i} in new value was missing trailing newline)rH)r rzLine {i} in new value was invalid. It must either start with " " space (continuation line) or "#" (comment line). The line started with "{line}")rHrr=rz2The last line in a value field cannot be a commentr)rrnrrrrrrrrrrr.r#rrrrr~rrlr)rLrraw_string_valuerrrrr8cased_field_nameoriginalraw raw_linesrHrr1rrrrUr<r<r?r|sfJ          z0Deb822ParagraphElement.set_field_from_raw_stringcCrBr;r<rLfdr<r<r?dumprzDeb822ParagraphElement.dumpcCrBr;r<rVr<r<r?r rcCD|durddd|DS|D] }||jdqdS)Nrcsrr;rrr<r<r?rzrz.Deb822ParagraphElement.dump..utf-8rrwriterencoderLrrr<r<r?r  r r;)rZr[r\ classmethodrrrr_rrrrrrrrrrrrrrr rrr.rr<r<r<r?rs^     R      H   rcseZdZdZfddZeddZddZdd Zd d Z d d Z ddZ ddZ ddZ dddZddZdddZddZZS) rzParagraph implementation optimized for valid deb822 files When there are no duplicated fields, we can use simpler and faster datastructures for common operations. cs,tdd|D|_||_|dS)NcSsi|]}|j|qSr<rrr<r<r? )szDDeb822NoDuplicateFieldsParagraphElement.__init__..)rrR_kvpair_elements _kvpair_orderrZ)rLrrrr<r?rR#s  z0Deb822NoDuplicateFieldsParagraphElement.__init__cCrWr;)r#rrVr<r<r?r-r;z4Deb822NoDuplicateFieldsParagraphElement.kvpair_countcC"t|dd\}}}|j|dS)rTrN)rr rrLrunpacked_fieldr8r<r<r?r2z2Deb822NoDuplicateFieldsParagraphElement.order_lastcCr!)rTr"N)rr rr#r<r<r?r8r%z3Deb822NoDuplicateFieldsParagraphElement.order_firstcC6t|dd\}}}t|dd\}}}|j||dS)rTr"N)rr rrLrrr$r8unpacked_ref_fieldr<r<r?r>sz4Deb822NoDuplicateFieldsParagraphElement.order_beforecCr&)rTr"N)rr rr'r<r<r?rGsz3Deb822NoDuplicateFieldsParagraphElement.order_afterccdd|jDEdHdS)Ncsr r;)rE)ryrr<r<r?rzSrzDDeb822NoDuplicateFieldsParagraphElement.iter_keys..r rVr<r<r?rQz1Deb822NoDuplicateFieldsParagraphElement.iter_keyscCs*t|dd\}}}|j|=|j|dSNTr")rrr rY)rLrr8r<r<r?rUsz=Deb822NoDuplicateFieldsParagraphElement.remove_kvpair_elementcCs:t|tttfs dStd|}t|dd\}}}||jvS)NFr5Tr")rnrErrr-rr)rLrrr8r<r<r?r[s   z?Deb822NoDuplicateFieldsParagraphElement.contains_kvpair_elementFcCs,t|dd\}}}|r|j|S|j|Sr,)rrget)rLrrr8r<r<r?rcs  z:Deb822NoDuplicateFieldsParagraphElement.get_kvpair_elementcCst|dd\}}}t|tr||jurtd|j}n ||jkr$td|j}|j|}||j|<|j ||dur?d|_ ||_ dS)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) rrnrrrrrr-r rrX)rLrrUr8original_valuer<r<r?rms       z:Deb822NoDuplicateFieldsParagraphElement.set_kvpair_elementNcCsNt|jD]}|jtd|}|j|durt}tt|j|d|_dS)rr Nr) rr rr-rr}r r sorted)rLrlast_field_name last_kvpairr<r<r?r s z3Deb822NoDuplicateFieldsParagraphElement.sort_fieldsc#s&fddtdjDEdHdS)Nc3s|]}j|VqdSr;)rrrVr<r?rzszEDeb822NoDuplicateFieldsParagraphElement.iter_parts..zIterable[_strI])r-r rVr<rVr?rNs  z2Deb822NoDuplicateFieldsParagraphElement.iter_partsr r;)rZr[r\r]rRr_rrrrrrrrrrr rNr r<r<rr?rs"      rcseZdZfddZeddZddZddZd d Zd d Z d dZ ddZ ddZ ddZ eddZddZ d)ddZ d)ddZeddZd d!Zd"d#Zd$d%Zd*d'd(ZZS)+rcs.tt|_i|_|||dSr;)rrRr r r_init_kvpair_fieldsrZ)rLrrr<r?rRs   z.Deb822DuplicateFieldsParagraphElement.__init__cCst|jt|jkSr;)r#r rrVr<r<r?rsz:Deb822DuplicateFieldsParagraphElement.has_duplicate_fieldscCs\|jrJ|jr J|D]}|j}|j|}||jvr#|g|j|<q |j||q dSr;)r rrr)rLkvpairsrrrMr<r<r?r3s    z9Deb822DuplicateFieldsParagraphElement._init_kvpair_fieldscCsft|\}}}|j|}g}|dus|dur-|||||}|dus$J||||fS|}||fSr;)rr_resolve_to_single_noder)rLrrrrnodesnodes_being_relocated single_noder<r<r?_nodes_being_relocateds   z.r*rVr<r<r?r2 r+z/Deb822DuplicateFieldsParagraphElement.iter_keysFcCs|dur.t|dkr,|dur|||}|dur|Sd}t|j|t|t|ddd}z||WStyJ|r?YdSd}t|j||dw)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))rres_len res_len_1rz>Field "{key}" was present but the index "{index}" was invalid.r)r#_find_node_via_name_tokenrr. IndexErrorr)rLr6rrrrrMr1r<r<r?r56 s(     z=Deb822DuplicateFieldsParagraphElement._resolve_to_single_nodecCs\t|\}}}|r|j|}|durdSn|j|}|j|||||d}|dur,|jSdS)Nr)rrr-r5rU)rLrrrrrr6rMr<r<r?rS s  z8Deb822DuplicateFieldsParagraphElement.get_kvpair_elementcCs"|D] }||jjur|SqdSr;)rUr)relementsrMr<r<r?rFd s  z?Deb822DuplicateFieldsParagraphElement._find_node_via_name_tokencCs0t|tttfs dStd|}|j|ddduS)NFr5Tr)rnrErrr-rrr<r<r?rs s z=Deb822DuplicateFieldsParagraphElement.contains_kvpair_elementc Cst|\}}}|r8||jur4|j|j}d}|dur!|||}|dur)td|dus/J||}|j}n ||jkrAtd|j}|j|}|dusP|s|durc|dkrcd}t|j ||d|j |}||jvrv|g|j|<dS|j| |dSd} |durd} |d}t |dkr|g|j|<n||}d|j _||_||_ | rt |dkr|ddD]} d| j _|j | qdSdSdS) NzKey is a Deb822FieldNameToken, but not *the* Deb822FieldNameToken for the value nor the Deb822FieldNameToken for an existing field in the paragraphr.rzCannot 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.rFTr<)rrrr-rrFrrrr.r rr#rUrXr:) rLrrUrroriginal_nodes original_noder1rM replace_allrr<r<r?rz sV            z8Deb822DuplicateFieldsParagraphElement.set_kvpair_elementcCst|\}}}|j|}|dur)|dur)|D] }d|j_|j|q|j|=dS|durD|||}|durAd}t|j|d|}n|dusJJz||}Wntybd}t|j||dwt |dkrn|j|=n| |d|j_|j|dS)Nz>The field "{key}" is present but key used to access it is not.rz@The field "{key}" is present, but the index "{idx}" was invalid.)rrr<) rrrUrXr r:rFrr.r#rY)rLrrr field_listrMrJr1r<r<r?r s4        z;Deb822DuplicateFieldsParagraphElement.remove_kvpair_elementNcs`|durt}|fdd}t|jD]}|jt|j|d}t|_i|_||dS)aRe-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. Ncs |jSr;r)rkey_implr<r? _actual_key rzFDeb822DuplicateFieldsParagraphElement.sort_fields.._actual_keyr) r rr rr}r0r rr3)rLrrOr2sorted_kvpair_listr<rMr?r  s   z1Deb822DuplicateFieldsParagraphElement.sort_fieldsr r;)rZr[r\rRr_rr3r9rrrrr@rNrrr5r staticmethodrFrrrr r r<r<rr?rs2       @#rcseZdZdZfddZeddZeddZdd Z d d Z d d Z ddZ ddZ ddZeddZeddZ dddZZS)Deb822FileElementz!Represents the entire deb822 filecrar;)rrR_token_and_elementsrZ)rLtoken_and_elementsrr<r?rR rbzDeb822FileElement.__init__cCs |tS)zCreates a new Deb822FileElement with no contents Note that a deb822 file must be non-empty to be considered valid )r rr<r<r?new_empty_file s z Deb822FileElement.new_empty_filecCs6d}|D] }d}|r |jrdSq|sdS|duS)aReturns 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)rr)rL had_paragraphrr<r<r? is_valid_file s   zDeb822FileElement.is_valid_filecCstt|jtddS)z:Returns the first Deb822ErrorElement (or None) in the filerUN)r~rrVr^rVr<r<r?r! z*Deb822FileElement.find_first_error_elementcCst|tSr;)rrSrrVr<r<r?r}& rzDeb822FileElement.__iter__ccrcr;)rSrVr<r<r?rN* rdzDeb822FileElement.iter_partsc Csd}d}|dkr|js||dS|jj}t|j}n d}|jD]}|j}t|tr1|d7}||dkr;|}nq#|durG||dS|rW|t d}|j ||}|j |||dS)aInserts 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 NTrr<r) rSrrboolrrUrnr _set_parentr insert_before) rLrpara anchor_node needs_newlinerHrMentrynl_tokenr<r<r?r>. s.&    zDeb822FileElement.insertcCsl|jj}|jdur|j|urtdtd|r(t|ts(|j|td|j||||_dS)aAppends 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)rSrmrXrrnrrrZ)rLr tail_elementr<r<r?rs s   zDeb822FileElement.appendcCs ||_|Sr;)rX)rLr=r<r<r?rZ szDeb822FileElement._set_parentcCrBr;r<rr<r<r?r rzDeb822FileElement.dumpcCrBr;r<rVr<r<r?r rNcCr)Nrcsrr;rrr<r<r?rz rz)Deb822FileElement.dump..rrrr<r<r?r rr;)rZr[r\r]rRrrUr_rWrr}rNr>rrZr.rr r<r<rr?rR s&   E&  rR) constructorcCs|rtjStjSr;)r7rr)discard_commentsr<r<r?_parsed_value_render_factory s rdcCstdSr)rr<r<r<r?r@ scCst|t p |jdkSr)rnrrr'r<r<r?_non_end_of_line_token rXrec csJt|}d}d}d}|D]}d}t|tr4|}t|tr3d}|}d}tdt|d}|dus3Jnt|tr@|}d}d}nt|trGd}|durN|V|rt| t }tdt|d}|dusj|j dksjJd} d} |rt|dt r~td| } |rt|dt rtd|d} |d d}t||| || |Vd}d}q dS) zYParser helper - consumes tokens part of a Deb822ValueEntryElement and turns them into oneNFTrrrr=rr<)rrnrpeekrr-r~rlist takewhilererrrore) token_streambuffered_streamrlrmrstart_of_value_entry next_tokentokens_in_value eol_tokenleading_whitespacetrailing_whitespacer<r<r?_build_value_line sf      rqc csVt|}|D]}d}d}t|tr0|}|}t|t}|r/zt|}Wnty.twnt|tr7d}|r|}|d}t |dkr[|rK|V|g}| |t |VdS|\} } t| t rt| t rt|dt|dt|td|| | Vq|g} | |t|} | rt| trt|d| | t | Vq|VqdS)NFTr)rrnrrfrr~ StopIterationAssertionErrorr?r#rr^rrxr:r-rhrerr) rirjtoken_or_elementstart_of_fieldrlrlr next_tokenserror_elements separatorr error_tokensnlr<r<r?_build_field_with_valueD s`               r|)accept_files_with_error_tokens#accept_files_with_duplicated_fieldscCst|}t|}t|}t|}t|}t|}t|}tt|}|s<| }|dur<| dd}t dj |d|st|D]?\}}t|trt} d} |D]} t| \} } } t| tsdJ| | vrl| } n| | qT| durd}t |j | |dqB|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. Nrz\nz4Syntax or Parse error on the line: "{error_as_text}") error_as_textz6Duplicate field "{dup_field}" in paragraph number {no}) dup_fieldno)r%_combine_comment_tokens_into_elementsrq(_combine_vl_elements_into_value_elementsr|%_combine_kvp_elements_into_paragraphs#_combine_error_tokens_into_elementsrRr rrrrr.rrnrsetkeysrrEadd)sequencer}r~rr error_elementrrr field_namesrrrr8r1r<r<r?r s@"    r__main__r )ycollections.abc collections contextlibr,rJabcrtypesrrdebian._deb822_repro.formatterrrr debian._utilrr r r r r debian._deb822_repro.typesrdebian._deb822_repro.tokensrrrrrrrrrrrrrrrdebian._deb822_repro._utilrrr typingr!r"r#r$r%r&r'r(r)r*r+r,r-r.r/r0r1r2r3r4r5r6r7r8r9StreamingValueParserrEStrToValueParserKVPNode ImportErrorrDAbstractContextManagerrar rrhr4r9r:rCrKrr^rerxr7rr:rrrrMutableMappingrrrrrrrrRrrrrrrd#LIST_SPACE_SEPARATED_INTERPRETATION#LIST_COMMA_SEPARATED_INTERPRETATIONLIST_UPLOADERS_INTERPRETATIONrerqr|rrZdoctesttestmodr<r<r<r?s     DH (  P ? $"0AZ'H "-l&6b ; `? K