Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_non_int_array_index_error(self, quiet_logger):
from collections import deque
yamldata = """---
- 1
"""
yaml = YAML()
data = yaml.load(yamldata)
path = YAMLPath("[0]")
processor = Processor(quiet_logger, data)
strp = str(path)
path._escaped = deque([
(PathSegmentTypes.INDEX, "0F"),
])
path._unescaped = deque([
(PathSegmentTypes.INDEX, "0F"),
])
with pytest.raises(YAMLPathException) as ex:
nodes = list(processor._get_nodes_by_index(data, path, 0))
assert -1 < str(ex.value).find("is not an integer array index")
def test_list_to_deque_parsing(parser):
parsed_list = [
(PathSegmentTypes.KEY, 'aliases'),
(PathSegmentTypes.ANCHOR, 'secretIdentity')
]
verify_queue = deque([
(PathSegmentTypes.KEY, 'aliases'),
(PathSegmentTypes.ANCHOR, 'secretIdentity')
])
assert verify_queue == parser._parse_path(parsed_list)
def test_escaped(self):
testpath = Path(r"abc.def\.ghi")
assert list(testpath.escaped) == [
(PathSegmentTypes.KEY, "abc"),
(PathSegmentTypes.KEY, "def.ghi"),
]
def test_get_elements_by_none_refs(yamlpath, yamldata):
tally = 0
for _ in yamlpath._get_elements_by_ref(None, (PathSegmentTypes.INDEX, ("bad_index[4F]", "4F", "4F"))):
tally += 1
for _ in yamlpath._get_elements_by_ref(yamldata, None):
tally += 1
assert tally == 0
Parameters:
1. yaml_path (deque) The pre-parsed YAML Path to follow
2. depth (int) Index of the YAML Path segment to evaluate
3. value (Any) The expected value for the final YAML Path entry
Returns: (Any) The most appropriate default value
Raises: N/A
"""
default_value = wrap_type(value)
segments = yaml_path.escaped
if not (segments and len(segments) > depth):
return default_value
typ = segments[depth][0]
if typ == PathSegmentTypes.INDEX:
default_value = CommentedSeq()
elif typ == PathSegmentTypes.KEY:
default_value = CommentedMap()
return default_value
):
# Ignore unescaped, non-demarcated whitespace
continue
elif seeking_regex_delim:
# This first non-space symbol is now the RegEx delimiter
seeking_regex_delim = False
capturing_regex = True
demarc_stack.append(char)
demarc_count += 1
continue
elif seeking_anchor_mark and char == "&":
# Found an expected (permissible) ANCHOR mark
seeking_anchor_mark = False
segment_type = PathSegmentTypes.ANCHOR
continue
elif seeking_collector_operator and char in ['+', '-']:
seeking_collector_operator = False
if char == '+':
collector_operator = CollectorOperators.ADDITION
elif char == '-':
collector_operator = CollectorOperators.SUBTRACTION
continue
elif char in ['"', "'"]:
# Found a string demarcation mark
if demarc_count > 0:
# Already appending to an ongoing demarcated value
if char == demarc_stack[-1]:
# Close a matching pair
):
# Ignore unescaped, non-demarcated whitespace
continue
elif seeking_regex_delim:
# This first non-space symbol is now the RegEx delimiter
seeking_regex_delim = False
capturing_regex = True
demarc_stack.append(c)
demarc_count += 1
continue
elif seeking_anchor_mark and c == "&":
# Found an expected (permissible) ANCHOR mark
seeking_anchor_mark = False
element_type = PathSegmentTypes.ANCHOR
continue
elif c in ['"', "'"]:
# Found a string demarcation mark
if demarc_count > 0:
# Already appending to an ongoing demarcated value
if c == demarc_stack[-1]:
# Close a matching pair
demarc_stack.pop()
demarc_count -= 1
# Record the element_id when all pairs have closed
# unless there is no element_id.
if demarc_count < 1:
if element_id:
# Unless the element has already been
self.log.debug(
"Parser::str_path: Building stringified <{}>{}..."
.format(type(yaml_path), yaml_path)
)
parsed_path = self.parse_path(yaml_path)
add_sep = False
ppath = ""
pathsep = kwargs.pop("pathsep", self._infer_pathsep(yaml_path))
# FSLASH pathsep requires a path starting with a /
if pathsep == '/':
ppath = "/"
for (ptype, element_vers) in parsed_path:
element_id = element_vers[2]
if ptype == PathSegmentTypes.KEY:
if add_sep:
ppath += pathsep
ppath += (
element_id
.replace(pathsep, "\\{}".format(pathsep))
.replace("&", r"\&")
.replace("[", r"\[")
.replace("]", r"\]")
)
elif ptype == PathSegmentTypes.INDEX:
ppath += "[{}]".format(element_id)
elif ptype == PathSegmentTypes.ANCHOR:
if add_sep:
ppath += "[&{}]".format(element_id)
else:
2. depth (int) Index of the YAML Path segment to evaluate
3. value (Any) The expected value for the final YAML Path entry
Returns: (Any) The most appropriate default value
Raises: N/A
"""
default_value = wrap_type(value)
segments = yaml_path.escaped
if not (segments and len(segments) > depth):
return default_value
typ = segments[depth][0]
if typ == PathSegmentTypes.INDEX:
default_value = CommentedSeq()
elif typ == PathSegmentTypes.KEY:
default_value = CommentedMap()
return default_value
if (
matched_nodes < 1
and segment_type is not PathSegmentTypes.SEARCH
):
# Add the missing element
self.logger.debug(
("Processor::_get_optional_nodes: Element <{}>{} is"
+ " unknown in the data! Applying default, <{}>{}."
).format(segment_type, except_segment, type(value), value)
)
if isinstance(data, list):
self.logger.debug(
"Processor::_get_optional_nodes: Dealing with a list"
)
if (
segment_type is PathSegmentTypes.ANCHOR
and isinstance(stripped_attrs, str)
):
next_node = build_next_node(
yaml_path, depth + 1, value
)
new_ele = append_list_element(
data, next_node, stripped_attrs
)
for node_coord in self._get_optional_nodes(
new_ele, yaml_path, value, depth + 1,
data, len(data) - 1
):
matched_nodes += 1
yield node_coord
elif (
segment_type in [