o x[hm(@sddlZddlmZddlmZddlmZmZmZm Z ddl Z ddl m Z e eZGdddZdd Zd eefd d Zd edeedeefddZGddde jZGddde jjZe dde eeeefffddZe ddddZdS)N) defaultdict)chain)AnyDictListTuple) performancec@s6eZdZdedejdejfddZddZdd Zd S) SchemaPathMarkspath start_markend_markcCs||_||_||_dSN)r r r )selfr r r r4/usr/lib/python3/dist-packages/cloudinit/safeyaml.py__init__s zSchemaPathMarks.__init__cCsp|jj|jjks|jj|jjkrdS|jj|jjkr$|jj|jjkr$dS|jj|jjkr6|jj|jjkr6dSdS)z;Return whether other start/end marks are within self marks.FT)r liner columnrotherrrr __contains__szSchemaPathMarks.__contains__cCs@|jj|jjko|jj|jjko|jj|jjko|jj|jjkSr )r rrr rrrr__eq__/szSchemaPathMarks.__eq__N) __name__ __module__ __qualname__stryamlMarkrrrrrrrr s r cCs0|dddD]}||vr||ks|SqdS)Nr) child_markmarksmarkrrr_find_closest_parent8s r" line_markscCs\|D])}t||}|r+|jdd\}}|j|jks!|j|js+|j||jd|_qdS)zP Update any SchemaPathMarks.path for items not under the proper parent. .N)r"r rsplit startswithreplace)r#r!parent path_prefix _path_idxrrr_reparent_schema_mark_children?s r,new_markr returncCsdg}d}|D]}||vr||q||vrd}||||q|r+t||S|||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_marksreparent_pathsr!rrr_add_mark_and_reparent_marksNs    r2csPeZdZdZfddZddZd fdd Zd fd 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|jvr1|j|jjd}|j|jjddD] }||vr'||vr'|}q||vr1|jdSt|jddD]\}}|dddD]}||vrU|jdSqEq:dS)Nrr%r$T)reverser)r rr6r sorteditems)rnodemost_specific_mark path_mark _line_num schema_marksr!rrr_get_nested_path_prefixs.   z2_CustomSafeLoaderWithMarks._get_nested_path_prefixFc srtj||d}||}|jD]&\}}||j}|jj}t||j|j} |j|} t | | } | |j|<q|S)Ndeep) r4construct_mappingrCvaluer rr r r6r2) rr>rEmappingnested_path_prefixkey_node value_node node_key_pathline_numr-rBr0r8rrrFs     z,_CustomSafeLoaderWithMarks.construct_mappingc s tj|dd}||}t|jD]q\}}|jj}||}t||j|j} ||j vr4| g|j |<q||jjkrJ|j |} t | | } | |j |<qt ||jjD]1} | |j vr|j | } t | | } | |kr}| dj |kr}| dt|| dj| dj| |j | <qQq|S)NTrDrr)r4construct_sequencerC enumeraterGr rr r r6r2ranger insert) rr>rEsequencerIindex sequence_itemrMrLr-rBr0 inner_liner8rrrNsL          z-_CustomSafeLoaderWithMarks.construct_sequencecs8t}t|trtddt|jD|d<|S)NcSsg|] }|j|jjdfqS)r%)r r r).0vrrr sz>_CustomSafeLoaderWithMarks.get_single_data.. schemamarks)r4get_single_data isinstancedictrr6valuesrdatar8rrrZs   z*_CustomSafeLoaderWithMarks.get_single_data)F) rrr__doc__rrCrFrNrZ __classcell__rrr8rr3qs *r3c@seZdZdZddZdS)NoAliasSafeDumperz>A class which avoids constructing anchors/aliases on yaml dumpcCsdS)NTrr^rrrignore_aliasessz NoAliasSafeDumper.ignore_aliasesN)rrrr`rcrrrrrbs rbz Loading yamlcCs6tj|td}t|tsi}||fS|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 )LoaderrY)rloadr3r[r\pop)blobresultrYrrrload_with_markss   riz Dumping yamlTFc Cs(tj|dd||d|rtdStjjdS)z%Return data in nicely formatted yaml. F) line_breakindentexplicit_start explicit_enddefault_flow_styleDumper)rdumprbdumper SafeDumper)objrnronoaliasrrrdumps srw)TTF)logging collectionsr itertoolsrtypingrrrrr cloudinitr getLoggerrLOGr r"r,r2 SafeLoaderr3rsrtrbtimedrintrirwrrrrs.    " #}