f#)ddlmZddlmZddlmZmZmZmZddl Z e jZ GddZ Gdde jZ d Zd ee fd Zd e d ee dee fdZGdde jZe j%de j&Gdde j(j*ZdeeeeefffdZdZddZy)) defaultdict)chain)AnyDictListTupleNcTeZdZdedej dej fdZdZdZy)SchemaPathMarkspath start_markend_markc.||_||_||_yN)r r r )selfr r r s 4/usr/lib/python3/dist-packages/cloudinit/safeyaml.py__init__zSchemaPathMarks.__init__s $  c&|jj|jjks-|jj|jjkDry|jj|jjk(r.|jj|jjkry|jj|jjk(r.|jj|jjkDryy)z;Return whether other start/end marks are within self marks.FT)r liner columnrothers r __contains__zSchemaPathMarks.__contains__s    ! !DOO$8$8 8~~""T]]%7%77    ! !T__%9%9 9  ''$//*@*@@ NN  4==#5#5 5%% (<(<<rcv|jj|jjk(xr|jj|jjk(xr\|jj|jjk(xr-|jj|jjk(Sr)r rrr rs r__eq__zSchemaPathMarks.__eq__+s OO E$4$4$9$9 9 >&&%*:*:*A*AA > ""enn&9&99 > $$(=(==  rN) __name__ __module__ __qualname__stryamlMarkrrrrrr r s.!S!dii!499! & rr ceZdZfdZxZS)_CustomSafeLoaderc"t||Sr)superconstruct_scalar)rnode __class__s rconstruct_python_unicodez*_CustomSafeLoader.construct_python_unicode5sw'--r)rrrr* __classcell__r)s@rr$r$4s ..rr$c:|dddD]}||vs||k(r|cSy)Nr") child_markmarksmarks r_find_closest_parentr29s2dd   jD&8K r line_markscD|D]}t||}|s|jjdd\}}|j|jk(s&|jj|jrp|jj ||jd|_y)zP Update any SchemaPathMarks.path for items not under the proper parent. .N)r2r rsplit startswithreplace)r3r1parent path_prefix _path_idxs r_reparent_schema_mark_childrenr=@sK%dJ7 %)YY%5%5c1%= "KyyFKK'tyy/C/C 0!II--k6;;J Krnew_markr0returncg}d}|D]@}||vr|j|||vrd}|j||j|B|r t||S|j||S)a=Insert new_mark into marks, ordering ancestors first. Reparent existing SchemaPathMarks.path when new_mark is a parent of an existing mark item. Because schema processing is depth first, leaf/child mappings and sequences may be processed for SchemaPathMarks before their parents. This leads to SchemaPathMarks.path of 'grandchildren' being incorrectly parented by the root dictionary instead of an intermediary parents below root. Walk through the list of existing marks and reparent marks that are contained within the new_mark. FT)appendr=)r>r0 new_marksreparent_pathsr1s r_add_mark_and_reparent_marksrDOs"IN x    T "  9 $!N   X &&y1  " rcHeZdZdZfdZdZfdZdfd ZfdZxZ S)_CustomSafeLoaderWithMarksaA loader which provides line and column start and end marks for YAML. If the YAML loaded represents a dictionary, get_single_data will inject a top-level "schemamarks" key in that dictionary which can be used at call-sites to process YAML paths schemamark metadata when annotating YAML files for errors. The schemamarks key is dictionary where each key is a dot-delimited path into the YAML object. Each dot represents an element that is nested under a parent and list items are represented with the format `.`. The values in schemamarks will be the line number in the original content where YAML element begins to aid in annotation when encountering schema errors. The example YAML shows expected schemamarks for both dicts and lists: one: val1 two: subtwo: val2 three: [val3, val4] schemamarks == { "one": 1, "two": 2, "two.subtwo": 3, "three": 4, "three.0": 4, "three.1": 4 } cLt||tt|_yr)r&rrlistschemamarks_by_line)rstreamr)s rrz#_CustomSafeLoaderWithMarks.__init__s"      rc|jj|jvrp|j|jjd}|j|jjddD]}||vs||vs |}||vr|jdzSt |jj dD])\}}|dddD]}||vs|jdccS+y)Nrr6r5T)reverser.)r rrIr sorteditems)rr(most_specific_mark path_mark _line_num schema_marksr1s r_get_nested_path_prefixz2_CustomSafeLoaderWithMarks._get_nested_path_prefixs ??  4#;#; ;!%!9!9$$"" "55doo6J6JK 3 9$6H)H)2&  3 )))..44'-  $ $ * * ,d(  + #I|%TrT* +4<"ii[?* + + rcRt ||}|j|}|jD]v\}}||j}|jj }t ||j|j}|j|} t|| } | |j|<x|Sr) r&construct_mappingrTvaluer rr r rIrD) rr(mappingnested_path_prefixkey_node value_node node_key_pathline_numr>rSrBr)s rrVz,_CustomSafeLoaderWithMarks.construct_mappings'+D1!99$?$(JJ ; Hj128>>2BCM**//H&x22J4G4GH 33H=L4X|LI1:D $ $X . ;rc "t ||d}|j|}t|jD]R\}}|j j }||}t||j |j} ||jvr| g|j|<c||jj k(r+|j|} t| | } | |j|<t||jj D]} | |jvs|j| } t| | } | |k(rI| dj|k7r7| jdt|| dj | dj| |j| <U|S)NT)deeprr.)r&construct_sequencerT enumeraterWr rr r rIrDranger insert)rr(r_sequencerYindex sequence_itemr]r\r>rSrB inner_liner)s rr`z-_CustomSafeLoaderWithMarks.construct_sequences7-d->!99$?$-djj$9$ M E=$//44H125':M&}779O9OHt7776>Z((2}55:::#'#;#;H#EL < ,!I:CD,,X6&+ -"8"8"="='M &)A)AA+/+C+CJ+OL(D (,)I!+h 6$0O$8$8M$I ) 0 0$%$3(5(4Q(B(B(4R(8(A(A%&!"DMD44Z@)M!$ MJrct|}t|tr]tt |j j Dcgc]'}|j|jjdzf)c}|d<|Scc}w)Nr6 schemamarks) r&get_single_data isinstancedictrrIvaluesr r r)rdatavr)s rrjz*_CustomSafeLoaderWithMarks.get_single_datasyw&( dD !"&#D$<$<$C$C$EFVVQ\\..23#D   s,A?)F) rrr__doc__rrTrVr`rjr+r,s@rrFrFrs': * (T  rrFz tag:yaml.org,2002:python/unicodeceZdZdZdZy)NoAliasSafeDumperz>A class which avoids constructing anchors/aliases on yaml dumpcy)NTr")rrns rignore_aliasesz NoAliasSafeDumper.ignore_aliasessrN)rrrrprtr"rrrrrrs Hrrrctj|t}t|tsi}||fS|j d}||fS)aPerform YAML SafeLoad and track start and end marks during parse. JSON schema errors come with an encoded object path such as: .. YAML loader needs to preserve a mapping of schema path to line and column marks to annotate original content with JSON schema error marks for the command: cloud-init devel schema --annotate Loaderri)r loadrFrkrlpop)blobresultris rload_with_marksr|sLYYt$> ?F fd #  ; jj/ ; rc8tj|tS)Nrv)r rxr$)rzs rrxrxs 99T"3 44rc tj|dd||d|r tStjjS)z%Return data in nicely formatted yaml. F) line_breakindentexplicit_start explicit_enddefault_flow_styleDumper)r dumprrdumper SafeDumper)objrrnoaliass rdumpsrsF 99 %! %,! 37++2H2H r)TTF) collectionsr itertoolsrtypingrrrrr YAMLErrorr SafeLoaderr$r2r=rDrFadd_constructorr*rrrrrintr|rxrr"rrrs$)) NN   D..  KtO/D K  &*?&;  / Fzzz!!&..  ..U3S#X#67*5 r