a '¡Dgm(ã@sþddlZddlmZddlmZddlmZmZmZm Z ddl Z ddl m Z e  e¡ZGdd„dƒZdd „Zeed œd d „Zeeeeed œdd„ZGdd„de jƒZGdd„de jjƒZe  d¡e eeeeffdœdd„ƒZe  d¡ddd„ƒZdS)éN)Ú defaultdict)Úchain)ÚAnyÚDictÚListÚTuple)Ú performancec@s2eZdZeejejdœdd„Zdd„Zdd„ZdS) ÚSchemaPathMarks©ÚpathÚ start_markÚend_markcCs||_||_||_dS©Nr )Úselfr r r ©rú6/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 Úcolumn©rÚotherrrrÚ __contains__s ÿþÿþÿþzSchemaPathMarks.__contains__cCs@|jj|jjko>|jj|jjko>|jj|jjko>|jj|jjkSr)r rrr rrrrÚ__eq__/sÿþüzSchemaPathMarks.__eq__N) Ú__name__Ú __module__Ú __qualname__ÚstrÚyamlZMarkrrrrrrrr sr cCs0|ddd…D]}||vr||ks|SqdS)Néÿÿÿÿr)Z child_markÚmarksÚmarkrrrÚ_find_closest_parent8s r!)Ú line_markscCs\|D]R}t||ƒ}|r|j dd¡\}}|j|jksB|j |j¡s|j ||jd¡|_qdS)zP Update any SchemaPathMarks.path for items not under the proper parent. Ú.éN)r!r ÚrsplitÚ startswithÚreplace)r"r ÚparentZ path_prefixZ _path_idxrrrÚ_reparent_schema_mark_children?s ÿr))Únew_markrÚreturncCsbg}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.csPeZdZdZ‡fdd„Zdd„Zd ‡fdd„ Zd‡fd d „ Z‡fd d „Z‡Z S)Ú_CustomSafeLoaderWithMarksaïA 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)ÚsuperrrÚlistÚschemamarks_by_line)rÚstream©Ú __class__rrr�s ÿz#_CustomSafeLoaderWithMarks.__init__cCs²|jj|jvrb|j|jjd}|j|jjdd…D]}||vr6||vr6|}q6||vrb|jdSt|j ¡dd�D]8\}}|ddd…D] }||vrŠ|j›d�SqŠqtdS)Nrr$r#T)ÚreverserÚ)r rr2r ÚsortedÚitems)rÚnodeZmost_specific_markZ path_markZ _line_numÚ schema_marksr rrrÚ_get_nested_path_prefix•s(ÿþ ÿ  ÿz2_CustomSafeLoaderWithMarks._get_nested_path_prefixFc srtƒj||d�}| |¡}|jD]L\}}|›|j›�}|jj}t||j|jƒ} |j|} t | | ƒ} | |j|<q |S)N©Údeep) r0Úconstruct_mappingr<Úvaluer rr r r2r.) rr:r>ÚmappingÚnested_path_prefixZkey_nodeZ value_nodeÚ node_key_pathÚline_numr*r;r-r4rrr?ªs  ÿ   z,_CustomSafeLoaderWithMarks.construct_mappingc s tƒj|dd�}| |¡}t|jƒD]â\}}|jj}|›|›�}t||j|jƒ} ||j vrh| g|j |<q$||jjkr”|j |} t | | ƒ} | |j |<q$t ||jjƒD]b} | |j vr¢|j | } t | | ƒ} | |krú| dj |krú|   dt|| dj| djƒ¡| |j | <q¢q$|S)NTr=rr)r0Úconstruct_sequencer<Ú enumerater@r rr r r2r.Úranger Úinsert) rr:r>ZsequencerBÚindexZ sequence_itemrDrCr*r;r-Z inner_liner4rrrE¸sL   ÿ   ÿ ÿ  ÿÿ þýþz-_CustomSafeLoaderWithMarks.construct_sequencecs8tƒ ¡}t|tƒr4tdd„t|j ¡ŽDƒƒ|d<|S)NcSsg|]}|j|jjdf‘qS)r$)r r r)Ú.0ÚvrrrÚ æsÿz>_CustomSafeLoaderWithMarks.get_single_data..Ú schemamarks)r0Úget_single_dataÚ isinstanceÚdictrr2Úvalues©rÚdatar4rrrNâs   þÿz*_CustomSafeLoaderWithMarks.get_single_data)F)F) rrrÚ__doc__rr<r?rErNÚ __classcell__rrr4rr/qs  *r/c@seZdZdZdd„ZdS)ÚNoAliasSafeDumperz>A class which avoids constructing anchors/aliases on yaml dumpcCsdS)NTrrRrrrÚignore_aliasesñsz NoAliasSafeDumper.ignore_aliasesN)rrrrTrWrrrrrVîsrVz Loading yaml)r+cCs0tj|td�}t|tƒsi}n | d¡}||fS)a…Perform 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)rÚloadr/rOrPÚpop)ZblobÚresultrMrrrÚload_with_marksõs   r\z Dumping yamlTFc Cs$tj|dd||d|rtntjjd�S)z%Return data in nicely formatted yaml.Ú éF)Z line_breakÚindentÚexplicit_startÚ explicit_endZdefault_flow_styleZDumper)rÚdumprVÚdumperÚ SafeDumper)Úobjr`raZnoaliasrrrÚdumps sùrf)TTF)ZloggingÚ collectionsrÚ itertoolsrÚtypingrrrrrZ cloudinitrZ getLoggerrZLOGr r!r)r.Z SafeLoaderr/rcrdrVZtimedrÚintr\rfrrrrÚs$    "þ #}