x[hm(ddlZddlmZddlmZddlmZmZmZm Z ddl Z ddl m Z ejeZGddZdZd eefd Zd ed eed eefdZGdde j(ZGdde j,j.Ze j2dd e eeeefffdZe j2dddZy)N) defaultdict)chain)AnyDictListTuple) performancecTeZdZdedej dej fdZdZdZy)SchemaPathMarkspath start_markend_markc.||_||_||_yN)r r r)selfr r rs 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 linercolumnrothers 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 rrrrs 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 c:|dddD]}||vs||k(r|cSy)Nr#) child_markmarksmarks r_find_closest_parentr)8s2dd   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)r)r rsplit startswithreplace)r*r(parent path_prefix _path_idxs r_reparent_schema_mark_childrenr4?sK%dJ7 %)YY%5%5c1%= "KyyFKK'tyy/C/C 0!II--k6;;J Krnew_markr'returncg}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)appendr4)r5r' new_marksreparent_pathsr(s r_add_mark_and_reparent_marksr;Ns"IN x    T "  9 $!N   X &&y1  " rcJeZdZdZfdZdZdfd Zdfd 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)superrrlistschemamarks_by_line)rstream __class__s rrz#_CustomSafeLoaderWithMarks.__init__s      rc|jj|jvrp|j|jjd}|j|jjddD]}||vs||vs |}||vr|jdzSt |jj dD])\}}|dddD]}||vs|jdccS+y)Nrr-r,T)reverser%)r rrAr sorteditems)rnodemost_specific_mark path_mark _line_num schema_marksr(s 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[?* + + rcVt |||}|j|}|jD]v\}}||j}|jj }t ||j|j} |j|} t| | } | |j|<x|S)Ndeep) r?construct_mappingrNvaluer rr rrAr;) rrIrQmappingnested_path_prefixkey_node value_node node_key_pathline_numr5rMr9rCs rrRz,_CustomSafeLoaderWithMarks.construct_mappings'+Dt+<!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)NTrPrr%)r?construct_sequencerN enumeraterSr rr rrAr;ranger insert)rrIrQsequencerUindex sequence_itemrYrXr5rMr9 inner_linerCs 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)Nr- schemamarks) r?get_single_data isinstancedictrrAvaluesr r r)rdatavrCs rrez*_CustomSafeLoaderWithMarks.get_single_datasyw&( dD !"&#D$<$<$C$C$EFVVQ\\..23#D   s,A?)F) rrr__doc__rrNrRr[re __classcell__)rCs@rr=r=qs': * (T  rr=ceZdZdZdZy)NoAliasSafeDumperz>A class which avoids constructing anchors/aliases on yaml dumpcy)NTr#)rris rignore_aliasesz NoAliasSafeDumper.ignore_aliasessrN)rrrrkrpr#rrrnrns Hrrnz Loading yamlctj|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 )Loaderrd)r!loadr=rfrgpop)blobresultrds rload_with_marksrwsLYYt$> ?F fd #  ; jj/ ; rz Dumping yamlc tj|dd||d|r tStjjS)z%Return data in nicely formatted yaml. F) line_breakindentexplicit_start explicit_enddefault_flow_styleDumper)r!dumprndumper SafeDumper)objr}r~noaliass rdumpsr sF 99 %! %,! 37++2H2H r)TTF)logging collectionsr itertoolsrtypingrrrrr! cloudinitr getLoggerrLOGr r)r4r; SafeLoaderr=rrrntimedr intrwrr#rrrs#)) !g!   D KtO/D K  &*?&;  / Fzzz ..>"U3S#X#67#*>" # r