/
snap
/
core18
/
2979
/
usr
/
lib
/
python3
/
dist-packages
/
cloudinit
/
/snap/core18/2979/usr/lib/python3/dist-packages/cloudinit
mkdir
upload
Name
Size
Mode
Actions
analyze/
-
0755
rm
cmd/
-
0755
rm
config/
-
0755
rm
distros/
-
0755
rm
filters/
-
0755
rm
handlers/
-
0755
rm
mergers/
-
0755
rm
net/
-
0755
rm
reporting/
-
0755
rm
sources/
-
0755
rm
__pycache__/
-
0755
rm
apport.py
5881
0644
edit
dl
rm
atomic_helper.py
1417
0644
edit
dl
rm
cloud.py
3574
0644
edit
dl
rm
dhclient_hook.py
2549
0644
edit
dl
rm
dmi.py
6979
0644
edit
dl
rm
event.py
2097
0644
edit
dl
rm
features.py
3146
0644
edit
dl
rm
gpg.py
4424
0644
edit
dl
rm
helpers.py
16828
0644
edit
dl
rm
importer.py
1789
0644
edit
dl
rm
log.py
4432
0644
edit
dl
rm
netinfo.py
23115
0644
edit
dl
rm
patcher.py
1130
0644
edit
dl
rm
persistence.py
2605
0644
edit
dl
rm
registry.py
1045
0644
edit
dl
rm
safeyaml.py
7905
0644
edit
dl
rm
settings.py
2041
0644
edit
dl
rm
signal_handler.py
1826
0644
edit
dl
rm
simpletable.py
1976
0644
edit
dl
rm
ssh_util.py
21211
0644
edit
dl
rm
stages.py
35311
0644
edit
dl
rm
subp.py
13558
0644
edit
dl
rm
templater.py
5965
0644
edit
dl
rm
temp_utils.py
3268
0644
edit
dl
rm
type_utils.py
726
0644
edit
dl
rm
url_helper.py
28628
0644
edit
dl
rm
user_data.py
14841
0644
edit
dl
rm
util.py
92622
0644
edit
dl
rm
version.py
596
0644
edit
dl
rm
warnings.py
3932
0644
edit
dl
rm
__init__.py
0
0644
edit
dl
rm
Edit:
/snap/core18/2979/usr/lib/python3/dist-packages/cloudinit/safeyaml.py
(7905B)
# Copyright (C) 2012 Canonical Ltd. # # Author: Scott Moser <scott.moser@canonical.com> # # This file is part of cloud-init. See LICENSE file for license information. from collections import namedtuple from itertools import chain from typing import Any, Dict, List, Tuple import yaml YAMLError = yaml.YAMLError # SchemaPathMarks track the path to an element within a loaded YAML file. # The start_mark and end_mark contain the row and column indicators # which represent the coordinates where the schema element begins and ends. SchemaPathMarks = namedtuple( "SchemaPathMarks", ("path", "start_mark", "end_mark") ) class _CustomSafeLoader(yaml.SafeLoader): def construct_python_unicode(self, node): return super().construct_scalar(node) def _fix_nested_map_index(new_key_path, marks): new_marks = [] for mark in marks: if "." not in mark.path: new_marks.append(mark) continue path_prefix, _path_idx = mark.path.rsplit(".", 1) if new_key_path not in mark.path and path_prefix in mark.path: new_marks.append( SchemaPathMarks( # Replace only the first match of path_prefix mark.path.replace(path_prefix, new_key_path, 1), mark.start_mark, mark.end_mark, ) ) else: new_marks.append(mark) return new_marks class _CustomSafeLoaderWithMarks(yaml.SafeLoader): """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 `<parent>.<list-index>`. 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 } """ def __init__(self, stream): super().__init__(stream) self.schemamarks_by_line: Dict[int, List[SchemaPathMarks]] = {} def _get_nested_path_prefix(self, node): if node.start_mark.line in self.schemamarks_by_line: return f"{self.schemamarks_by_line[node.start_mark.line][0][0]}." for _line_num, schema_marks in sorted( self.schemamarks_by_line.items(), reverse=True ): for mark in schema_marks[::-1]: if ( # Is the node within the scope of the furthest mark node.start_mark.line >= mark.start_mark.line and node.start_mark.column >= mark.start_mark.column and node.end_mark.line <= mark.end_mark.line and node.end_mark.column <= mark.end_mark.column ): return f"{mark.path}." return "" def construct_mapping(self, node): mapping = super().construct_mapping(node) nested_path_prefix = self._get_nested_path_prefix(node) for key_node, value_node in node.value: node_key_path = f"{nested_path_prefix}{key_node.value}" line_num = key_node.start_mark.line mark = SchemaPathMarks( node_key_path, key_node.start_mark, value_node.end_mark ) if line_num not in self.schemamarks_by_line: self.schemamarks_by_line[line_num] = [mark] else: self.schemamarks_by_line[line_num].append(mark) return mapping def construct_sequence(self, node, deep=False): sequence = super().construct_sequence(node, deep=True) nested_path_prefix = self._get_nested_path_prefix(node) for index, sequence_item in enumerate(node.value): line_num = sequence_item.start_mark.line node_key_path = f"{nested_path_prefix}{index}" marks = SchemaPathMarks( node_key_path, sequence_item.start_mark, sequence_item.end_mark ) if line_num not in self.schemamarks_by_line: self.schemamarks_by_line[line_num] = [marks] else: if line_num == sequence_item.end_mark.line: self.schemamarks_by_line[line_num].append(marks) else: # Incorrect multi-line mapping or sequence object. for inner_line in range( line_num, sequence_item.end_mark.line ): if inner_line in self.schemamarks_by_line: schema_marks = self.schemamarks_by_line[inner_line] new_marks = _fix_nested_map_index( node_key_path, schema_marks ) if ( inner_line == line_num and schema_marks[0].path != node_key_path ): new_marks.insert( 0, SchemaPathMarks( node_key_path, schema_marks[0].start_mark, schema_marks[-1].end_mark, ), ) self.schemamarks_by_line[inner_line] = new_marks return sequence def get_single_data(self): data = super().get_single_data() if isinstance(data, dict): # valid cloud-config schema is a dict data["schemamarks"] = dict( [ (v.path, v.start_mark.line + 1) # 1-based human-readable for v in chain(*self.schemamarks_by_line.values()) ] ) return data _CustomSafeLoader.add_constructor( "tag:yaml.org,2002:python/unicode", _CustomSafeLoader.construct_python_unicode, ) class NoAliasSafeDumper(yaml.dumper.SafeDumper): """A class which avoids constructing anchors/aliases on yaml dump""" def ignore_aliases(self, data): return True def load_with_marks(blob) -> Tuple[Any, Dict[str, int]]: """Perform YAML SafeLoad and track start and end marks during parse. JSON schema errors come with an encoded object path such as: <key1>.<key2>.<list_item_index> 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 """ result = yaml.load(blob, Loader=_CustomSafeLoaderWithMarks) if not isinstance(result, dict): schemamarks = {} else: schemamarks = result.pop("schemamarks") return result, schemamarks def load(blob): return yaml.load(blob, Loader=_CustomSafeLoader) def dumps(obj, explicit_start=True, explicit_end=True, noalias=False): """Return data in nicely formatted yaml.""" return yaml.dump( obj, line_break="\n", indent=4, explicit_start=explicit_start, explicit_end=explicit_end, default_flow_style=False, Dumper=(NoAliasSafeDumper if noalias else yaml.dumper.SafeDumper), ) # vi: ts=4 expandtab
Save
cmd:
run