a 'Dgm(@sddlZddlmZddlmZddlmZmZmZm Z ddl Z ddl m Z e eZGdddZdd Zeed d d Zeeeeed ddZGddde jZGddde jjZe de eeeeffdddZe ddddZdS)N) defaultdict)chain)AnyDictListTuple) performancec@s2eZdZeejejdddZddZddZdS) SchemaPathMarkspath start_markend_markcCs||_||_||_dSNr )selfr r r r6/usr/lib/python3.9/site-packages/cloudinit/safeyaml.py__init__szSchemaPathMarks.__init__cCsp|jj|jjks |jj|jjkr$dS|jj|jjkrH|jj|jjkrHdS|jj|jjkrl|jj|jjkrldSdS)z;Return whether other start/end marks are within self marks.FT)r liner columnrotherrrr __contains__s zSchemaPathMarks.__contains__cCs@|jj|jjko>|jj|jjko>|jj|jjko>|jj|jjkSr)r rrr rrrr__eq__/szSchemaPathMarks.__eq__N) __name__ __module__ __qualname__stryamlZMarkrrrrrrrr sr cCs0|dddD]}||vr||ks|SqdS)Nr)Z child_markmarksmarkrrr_find_closest_parent8s r!) line_markscCs\|D]R}t||}|r|jdd\}}|j|jksB|j|js|j||jd|_qdS)zP Update any SchemaPathMarks.path for items not under the proper parent. .N)r!r rsplit startswithreplace)r"r parentZ path_prefixZ _path_idxrrr_reparent_schema_mark_children?s r))new_markrreturncCsbg}d}|D]8}||vr$||q ||vr:d}||||q |rTt|n |||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*r new_marksZreparent_pathsr rrr_add_mark_and_reparent_marksNs     r.csPeZdZdZfddZddZd fdd Zdfd d Zfd d ZZ 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 } cst|tt|_dSr)superrrlistschemamarks_by_line)rstream __class__rrrs z#_CustomSafeLoaderWithMarks.__init__cCs|jj|jvrb|j|jjd}|j|jjddD]}||vr6||vr6|}q6||vrb|jdSt|jddD]8\}}|dddD] }||vr|jdSqqtdS)Nrr$r#T)reverser)r rr2r sorteditems)rnodeZmost_specific_markZ path_markZ _line_num schema_marksr rrr_get_nested_path_prefixs(   z2_CustomSafeLoaderWithMarks._get_nested_path_prefixFc srtj||d}||}|jD]L\}}||j}|jj}t||j|j} |j|} t | | } | |j|<q |S)Ndeep) r0construct_mappingr<valuer rr r r2r.) rr:r>mappingnested_path_prefixZkey_nodeZ value_node node_key_pathline_numr*r;r-r4rrr?s     z,_CustomSafeLoaderWithMarks.construct_mappingc s tj|dd}||}t|jD]\}}|jj}||}t||j|j} ||j vrh| g|j |<q$||jjkr|j |} t | | } | |j |<q$t ||jjD]b} | |j vr|j | } t | | } | |kr| dj |kr| dt|| dj| dj| |j | <qq$|S)NTr=rr)r0construct_sequencer< enumerater@r rr r r2r.ranger insert) rr:r>ZsequencerBindexZ sequence_itemrDrCr*r;r-Z inner_liner4rrrEsL          z-_CustomSafeLoaderWithMarks.construct_sequencecs8t}t|tr4tddt|jD|d<|S)NcSsg|]}|j|jjdfqS)r$)r r r).0vrrr sz>_CustomSafeLoaderWithMarks.get_single_data.. schemamarks)r0get_single_data isinstancedictrr2valuesrdatar4rrrNs   z*_CustomSafeLoaderWithMarks.get_single_data)F)F) rrr__doc__rr<r?rErN __classcell__rrr4rr/qs  *r/c@seZdZdZddZdS)NoAliasSafeDumperz>A class which avoids constructing anchors/aliases on yaml dumpcCsdS)NTrrRrrrignore_aliasessz NoAliasSafeDumper.ignore_aliasesN)rrrrTrWrrrrrVsrVz Loading yaml)r+cCs0tj|td}t|tsi}n |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 )LoaderrM)rloadr/rOrPpop)ZblobresultrMrrrload_with_markss   r\z Dumping yamlTFc Cs$tj|dd||d|rtntjjdS)z%Return data in nicely formatted yaml. F)Z line_breakindentexplicit_start explicit_endZdefault_flow_styleZDumper)rdumprVdumper SafeDumper)objr`raZnoaliasrrrdumps srf)TTF)Zlogging collectionsr itertoolsrtypingrrrrrZ cloudinitrZ getLoggerrZLOGr r!r)r.Z SafeLoaderr/rcrdrVZtimedrintr\rfrrrrs$    " #}