3 d?Z d@dAZ!dBdCZ"dDdEZ#dFdGZ$dHdIZ%dJdKZ&dLdMZ'dNdOZ(dPdQZ)dRdSZ*dTdUZ+dVdWZ,dXdYZ-dZd[Z.d\d]Z/d^d_Z0d`daZ1dbdcZ2dddeZ3ddfdgZ4dhdiZ5ddjdkZ6dldmZ7dndoZ8ddqdrZ9dsdtdudvdwdxdydzd{d|d}d~ddddZ:dddZ;ddZdddZ?dS)r!z!!)rztag:yaml.org,2002:NcCs||_d|_g|_|j|_g|_d|_g|_d|_d|_ d|_ d|_ d|_ d|_ d|_d|_d|_d|_d|_||_||_d|_|rd|kodknr||_d|_|r||jdkr||_d|_|d kr||_d|_d|_d|_d|_d|_dS) NFTr P   )rrr)streamencodingstatesexpect_stream_startstateeventseventindentsindent flow_level root_contextsequence_contextmapping_contextsimple_key_contextlinecolumn whitespace indention open_ended canonical allow_unicode best_indent best_widthbest_line_break tag_prefixesprepared_anchor prepared_taganalysisstyle)rrr1r&widthr2Z line_breakr r r r&sDzEmitter.__init__cCsg|_d|_dS)N)r r")rr r r disposejszEmitter.disposecCs:|jj|x(|js4|jjd|_|jd|_qWdS)Nr)r#appendneed_more_eventspopr$r")rr$r r r emitos   z Emitter.emitcCsX|js dS|jd}t|tr(|jdSt|tr<|jdSt|trP|jdSdSdS)NTrrrF)r# isinstanceDocumentStartEvent need_eventsSequenceStartEventMappingStartEvent)rr$r r r r>xs       zEmitter.need_more_eventscCsxd}x`|jddD]N}t|ttfr0|d7}n&t|ttfrH|d8}nt|trVd}|dkrdSqWt|j|dkS)NrrF)r#rBrCCollectionStartEventDocumentEndEventZCollectionEndEventStreamEndEventlen)rcountlevelr$r r r rDs   zEmitter.need_eventsFcCsF|jj|j|jdkr.|r&|j|_qBd|_n|sB|j|j7_dS)Nr)r%r=r&r3)rflow indentlessr r r increase_indents  zEmitter.increase_indentcCsPt|jtr>|jjr,t|jd r,|jj|_|j|j|_nt d|jdS)Nrz%expected StreamStartEvent, but got %s) rBr$ZStreamStartEventrhasattrrwrite_stream_startexpect_first_document_startr"r)rr r r r!s   zEmitter.expect_stream_startcCstd|jdS)Nzexpected nothing, but got %s)rr$)rr r r expect_nothingszEmitter.expect_nothingcCs |jddS)NT)first)expect_document_start)rr r r rSsz#Emitter.expect_first_document_startc Csvt|jtr(|jjs|jjr8|jr8|jdd|j|jjrX|j|jj}|j ||j j |_ |jjrt |jjj}xB|D]:}|jj|}||j |<|j|}|j|}|j||qW|o|jj o|j o|jj o|jj o|j }|s|j|jdd|jr|j|j|_nJt|jtrd|jrR|jdd|j|j|j|_ntd|jdS)Nz...Tz---z'expected DocumentStartEvent, but got %s)rBr$rCversionZtagsr0write_indicator write_indentprepare_versionwrite_version_directiveDEFAULT_TAG_PREFIXEScopyr6sortedkeysprepare_tag_handleprepare_tag_prefixwrite_tag_directiveexplicitr1check_empty_documentexpect_document_rootr"rJwrite_stream_endrTr) rrU version_textZhandleshandleprefix handle_text prefix_textimplicitr r r rVsB             zEmitter.expect_document_startcCsTt|jtrB|j|jjr0|jdd|j|j|j|_nt d|jdS)Nz...Tz%expected DocumentEndEvent, but got %s) rBr$rIrYrcrX flush_streamrVr"r)rr r r expect_document_ends   zEmitter.expect_document_endcCs|jj|j|jdddS)NT)root)r r=rn expect_node)rr r r reszEmitter.expect_document_rootcCs||_||_||_||_t|jtr.|jnt|jtt fr|j d|j t|jtrf|j qt|jt r|js|js|jjs|jr|jq|jqt|jtr|js|js|jjs|jr|jq|jntd|jdS)N&zexpected NodeEvent, but got %s)r(r)r*r+rBr$ AliasEvent expect_alias ScalarEventrHprocess_anchor process_tag expect_scalarrEr'r1Z flow_stylecheck_empty_sequenceexpect_flow_sequenceexpect_block_sequencerFcheck_empty_mappingexpect_flow_mappingexpect_block_mappingr)rrosequencemapping simple_keyr r r rps,           zEmitter.expect_nodecCs.|jjdkrtd|jd|jj|_dS)Nz!anchor is not specified for aliasr)r$anchorrrur r?r")rr r r rss  zEmitter.expect_aliascCs0|jdd|j|jj|_|jj|_dS)NT)rN)rPprocess_scalarr%r?r&r r")rr r r rw s  zEmitter.expect_scalarcCs6|jdddd|jd7_|jdd|j|_dS)N[T)r.r)rN)rXr'rPexpect_first_flow_sequence_itemr")rr r r rys zEmitter.expect_flow_sequencecCsxt|jtr@|jj|_|jd8_|jdd|jj|_ n4|j sR|j |j krZ|j |jj|j|jdddS)Nr]FT)r~)rBr$SequenceEndEventr%r?r&r'rXr r"r1r-r4rYr=expect_flow_sequence_itemrp)rr r r rs   z'Emitter.expect_first_flow_sequence_itemcCst|jtrZ|jj|_|jd8_|jr@|jdd|j |jdd|j j|_ n@|jdd|jsx|j |j kr|j |j j|j|jdddS)Nr,FrT)r~)rBr$rr%r?r&r'r1rXrYr r"r-r4r=rrp)rr r r r$s     z!Emitter.expect_flow_sequence_itemcCs6|jdddd|jd7_|jdd|j|_dS)N{T)r.r)rN)rXr'rPexpect_first_flow_mapping_keyr")rr r r r|6s zEmitter.expect_flow_mappingcCst|jtr@|jj|_|jd8_|jdd|jj|_ nn|j sR|j |j krZ|j |j r|jr|jj|j|jdddn&|jdd|jj|j|jdddS)Nr}FT)rr?)r)rBr$MappingEndEventr%r?r&r'rXr r"r1r-r4rYcheck_simple_keyr= expect_flow_mapping_simple_valuerpexpect_flow_mapping_value)rr r r r<s    z%Emitter.expect_first_flow_mapping_keycCst|jtrZ|jj|_|jd8_|jr@|jdd|j |jdd|j j|_ nz|jdd|jsx|j |j kr|j |j r|jr|j j|j|jdddn&|jdd|j j|j|jdddS) NrrFrT)rrr)r)rBr$rr%r?r&r'r1rXrYr r"r-r4rr=rrpr)rr r r expect_flow_mapping_keyMs"      zEmitter.expect_flow_mapping_keycCs*|jdd|jj|j|jdddS)N:FT)r)rXr r=rrp)rr r r rbs z(Emitter.expect_flow_mapping_simple_valuecCsD|js|j|jkr|j|jdd|jj|j|jdddS)NrT)r) r1r-r4rYrXr r=rrp)rr r r rgs  z!Emitter.expect_flow_mapping_valuecCs(|jo |j }|jd|d|j|_dS)NF)rNrO)r*r/rP expect_first_block_sequence_itemr")rrOr r r rzpszEmitter.expect_block_sequencecCs |jddS)NT)rU)expect_block_sequence_item)rr r r rusz(Emitter.expect_first_block_sequence_itemcCsb| r,t|jtr,|jj|_|jj|_n2|j|j dddd|jj |j |j dddS)N-T)r/)r~) rBr$rr%r?r&r r"rYrXr=rrp)rrUr r r rxs z"Emitter.expect_block_sequence_itemcCs|jdd|j|_dS)NF)rN)rPexpect_first_block_mapping_keyr")rr r r r}s zEmitter.expect_block_mappingcCs |jddS)NT)rU)expect_block_mapping_key)rr r r rsz&Emitter.expect_first_block_mapping_keycCs| r,t|jtr,|jj|_|jj|_nX|j|j rZ|jj |j |j dddn*|j dddd|jj |j|j dddS)NT)rrr)r/)r)rBr$rr%r?r&r r"rYrr=!expect_block_mapping_simple_valuerprXexpect_block_mapping_value)rrUr r r rs z Emitter.expect_block_mapping_keycCs*|jdd|jj|j|jdddS)NrFT)r)rXr r=rrp)rr r r rs z)Emitter.expect_block_mapping_simple_valuecCs6|j|jdddd|jj|j|jdddS)NrT)r/)r)rYrXr r=rrp)rr r r rsz"Emitter.expect_block_mapping_valuecCs"t|jto |jo t|jdtS)Nr)rBr$rEr#r)rr r r rxszEmitter.check_empty_sequencecCs"t|jto |jo t|jdtS)Nr)rBr$rFr#r)rr r r r{szEmitter.check_empty_mappingcCsRt|jt s|j rdS|jd}t|toP|jdkoP|jdkoP|joP|jdkS)NFr) rBr$rCr#rtrtagrlvalue)rr$r r r rds  zEmitter.check_empty_documentcCs d}t|jtrD|jjdk rD|jdkr6|j|jj|_|t|j7}t|jttfr|jj dk r|j dkrz|j |jj |_ |t|j 7}t|jtr|j dkr|j |jj|_ |t|j j7}|dkot|jtpt|jtr|j j r|j j p|jp|jS)Nr)rBr$Z NodeEventrr7prepare_anchorrKrtrHrr8 prepare_tagr9analyze_scalarrr rrr rrxr{)rZlengthr r r rs$      zEmitter.check_simple_keycCsR|jjdkrd|_dS|jdkr0|j|jj|_|jrH|j||jdd|_dS)NT)r$rr7rrX)r indicatorr r r rus  zEmitter.process_anchorcCs|jj}t|jtr|jdkr(|j|_|j s8|dkrn|jdkrN|jjdsd|jdkrn|jjdrnd|_dS|jjdr|dkrd}d|_n"|j s|dkr|jjrd|_dS|dkrt d|jdkr|j ||_|jr|j |jdd|_dS)Nrrrrztag is not specifiedT) r$rrBrtr:choose_scalar_styler1rlr8rrrX)rrr r r rvs,     zEmitter.process_tagcCs|jdkr|j|jj|_|jjdks,|jr0dS|jj r|jjdr|joZ|jjpZ|jj r|j rl|jj s||j r|jj rdS|jjr|jjdkr|j r|j r|jj r|jjS|jj s|jjdkr|jjr|jo|jj rdSdS)N"rrz|>')r9rr$rr:r1rlr+r rr'rrrr)rr r r rs& zEmitter.choose_scalar_stylecCs|jdkr|j|jj|_|jdkr.|j|_|j }|jdkrR|j|jj|n`|jdkrn|j |jj|nD|jdkr|j |jjn*|jdkr|j |jjn|j |jj|d|_d|_dS)Nrr>|) r9rr$rr:rr+write_double_quotedr write_single_quoted write_folded write_literal write_plain)rsplitr r r rs        zEmitter.process_scalarcCs,|\}}|dkr td||fd||fS)Nrzunsupported YAML version: %d.%dz%d.%d)r)rrWmajorminorr r r rZszEmitter.prepare_versioncCs|s td|ddks$|ddkr0td|xt|ddD]d}d|koTdknpd|kold knpd |kod knp|d ks>td ||fq>W|S)Nztag handle must not be emptyrrrz*tag handle must start and end with '!': %r09AZazz-_z*invalid character %r in the tag handle: %rrGrG)r)rrhchr r r r`!s HzEmitter.prepare_tag_handlecCs|s tdg}d}}|ddkr(d}x|t|kr||}d|koPdknsd|kohdknsd |kod kns|d kr|d7}q*||kr|j||||d}}|jd }x|D]}|jd t|qWq*W||kr|j|||dj|S)Nztag prefix must not be emptyrrrrrrrrrz-;/?!:@&=+$,_.~*'()[]zutf-8z%%%02Xr)rrKr=encodeordjoin)rrichunksstartendrdatar r r ra-s( H     zEmitter.prepare_tag_prefixc Cs|s td|dkr|Sd}|}t|jj}xH|D]@}|j|r4|dksZt|t|kr4|j|}|t|d}q4Wg}d}}x|t|kr^||} d| kodknpd| kodknpd| kod knp| d kp| dko|dkr|d 7}q||kr$|j||||d }}| jd } x | D]} |jd t| q@WqW||kr||j|||dj |} |rd|| fSd| SdS)Nztag must not be emptyrrrrrrrrz-;/?:@&=+$,_.~*'()[]rzutf-8z%%%02Xrz%s%sz!<%s>) rr^r6r_ startswithrKr=rrr) rrrhsuffixprefixesrirrrrrZ suffix_textr r r rDs@   H        zEmitter.prepare_tagcCs~|s tdxl|D]d}d|ko(dknpdd|ko@dknpdd|koXdknpd|dkstd ||fqW|S) Nzanchor must not be emptyrrrrrrz-_z&invalid character %r in the anchor: %r)r)rrrr r r rhs HzEmitter.prepare_anchorc Cs|st|ddddddddSd}d}d}d}d}d}d}d} d} d} |jdsX|jdr`d}d}d} t|dkpz|ddk} d}d}d}x|t|krv||}|dkr|d krd}d}|d krd}| rd}|d kr| rd}d}n<|d krd}|d krd}| rd}|dkr | r d}d}|dkr.d}|dkpPd|koLdkns|dksd|kordknsd|kodknr|dkrd}|jsd}nd}|dkr|dkrd}|t|dkrd}|rd} d}d}nJ|dkr:|dkrd}|t|dkr&d} |r0d} d}d}nd}d}|d7}|dk} |dt|kpr||ddk} qWd}d}d}d}d}|s|s|s| rd}}|rd}| rd}}}| s|rd}}}}|rd}}|rd}|rd}t|d||||||dS)NTF)r r rrrrrrz---z...ru …

rz#,[]{}&*!|>'"%@`z?:rz,?[]{}r#u …

r ~… u퟿uu�u)r rrKr2)rr Zblock_indicatorsZflow_indicatorsZ line_breaksZspecial_characters leading_spaceZ leading_breakZtrailing_spaceZtrailing_breakZ break_spaceZ space_breakZpreceeded_by_whitespaceZfollowed_by_whitespaceZprevious_spaceZprevious_breakindexrZunicode_charactersrrrrrr r r rrs     &&&        zEmitter.analyze_scalarcCst|jdr|jjdS)Nflush)rQrr)rr r r rms zEmitter.flush_streamcCs*|jr&|jjdr&|jjdj|jdS)Nzutf-16u)rrrwriter)rr r r rRszEmitter.write_stream_startcCs |jdS)N)rm)rr r r rfszEmitter.write_stream_endcCsf|js | r|}nd|}||_|jo(||_|jt|7_d|_|jrV|j|j}|jj|dS)NrF) r.r/r-rKr0rrrr)rrZneed_whitespacer.r/rr r r rXs   zEmitter.write_indicatorcCs||jpd}|j s.|j|ks.|j|kr6|j r6|j|j|krxd|_d||j}||_|jrl|j|j}|jj|dS)NrTr) r&r/r-r.write_line_breakrrrr)rr&rr r r rY-s   zEmitter.write_indentcCsP|dkr|j}d|_d|_|jd7_d|_|jr@|j|j}|jj|dS)NTrr) r5r.r/r,r-rrrr)rrr r r r:s zEmitter.write_line_breakcCs2d|}|jr|j|j}|jj||jdS)Nz %%YAML %s)rrrrr)rrgrr r r r[Es   zEmitter.write_version_directivecCs6d||f}|jr|j|j}|jj||jdS)Nz %%TAG %s %s)rrrrr)rrjrkrr r r rbLs    zEmitter.write_tag_directiveTc Cs,|jddd}d}d}}x|t|krd}|t|krF||}|r|dksZ|dkr|d|kr|j|jkr|r|dkr|t|kr|jn<|||}|jt|7_|jr|j|j}|jj||}n|rH|dks|dkr||dkr|j x4|||D]$} | dkr*|j n |j | qW|j|}nj|dksf|d ksf|dkr||kr|||}|jt|7_|jr|j|j}|jj||}|dkrd }|jd 7_|jr|j|j}|jj||d}|dk r|dk}|dk}|d7}q W|jdddS) NrTFrrru …

ru …

z''r) rXrKr-r4rYrrrrr) rtextrspacesbreaksrrrrbrr r r rUsb                 zEmitter.write_single_quotedrrbtnvfrer\N_LP) r  rrrrru
u
cCs|jddd}}xt|t|krd}|t|kr>||}|dks|dksd|ko`dknp|jod|ko~dknpd |kod kn r||kr|||}|jt|7_|jr|j|j}|jj||}|dk r||jkrd |j|}n<|d kr&d t |}n$|dkr>dt |}n dt |}|jt|7_|jrp|j|j}|jj||d}d|kot|dknr|dks||kr|j|||j kr|r|||d }||kr|}|jt|7_|jr|j|j}|jj||j d|_ d|_ ||dkrd }|jt|7_|jrt|j|j}|jj||d7}qW|jdddS)NrTru "\…

rrru퟿uu�rÿz\x%02Xu￿z\u%04Xz\U%08XrF)rXrKr2r-rrrrESCAPE_REPLACEMENTSrr4rYr.r/)rrrrrrrr r r rsf            8      zEmitter.write_double_quotedcCs\d}|rX|ddkr"|t|j7}|d dkr8|d7}n t|dksP|d dkrX|d7}|S) Nrru …

ru …

rr+rG)strr3rK)rrhintsr r r determine_block_hintss   zEmitter.determine_block_hintsc Cs|j|}|jd|d|d ddkr0d|_|jd}d}d}d}}x|t|krd}|t|krv||}|r |dks|dkr| r|dk r|dkr||d kr|j|dk}x0|||D] } | d kr|jq|j| qW|dk r|j|}n|r|dkr|d|kr@|j|jkr@|jn>|||} |jt| 7_|jrr| j |j} |j j | |}nh|dks|d kr|||} |jt| 7_|jr| j |j} |j j | |dkr|j|}|dk r|dk}|dk}|d7}qPWdS) NrTrrFru …

rru …

rG) rrXr0rrKrYr-r4rrrr) rrrrrrrrrrrr r r rsb               zEmitter.write_foldedc Cs<|j|}|jd|d|dddkr0d|_|jd}d}}x|t|kr6d}|t|krl||}|r|dks|dkrx0|||D] }|dkr|jq|j|qW|dk r|j|}nR|dks|dkr|||}|jr|j|j}|jj ||dkr|j|}|dk r,|dk}|d7}qFWdS) NrTrrru …

rrG) rrXr0rrKrYrrrr) rrrrrrrrrr r r rs>        zEmitter.write_literalc Cs|jr d|_|sdS|jsNd}|jt|7_|jrB|j|j}|jj|d|_d|_ d}d}d}}x|t|kr d}|t|kr||}|r|dkr|d|kr|j|j kr|r|j d|_d|_ n>|||}|jt|7_|jr|j|j}|jj||}n|r|dkr||dkr@|j x4|||D]$} | dkrf|j n |j | qNW|j d|_d|_ |}nV|dks|dkr|||}|jt|7_|jr|j|j}|jj||}|dk r|dk}|dk}|d7}qnWdS) NTrFrru …

ru …

) r(r0r.r-rKrrrrr/r4rYr) rrrrrrrrrrr r r r7sl              zEmitter.write_plain)NNNNN)FF)F)FFFF)F)F)FF)N)T)T)T)@rrrr\rr<r@r>rDrPr!rTrSrVrnrerprsrwryrrr|rrrrrzrrr}rrrrrxr{rdrrurvrrrZr`rarrrrmrRrfrXrYrr[rbrrrrrrrr r r r rs C     $        $ "   7 6 6#N)__all__errorrr#rr rr r r r  s