Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
aliases:
- &aValue val2
hash:
key1: val1
key2: *aValue
key3: val3
"""
processor = get_yaml_editor()
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
yaml_data = get_yaml_data(processor, quiet_logger, yaml_file)
seen_anchors = []
results = []
for assertion, path in zip_longest(assertions, yield_children(
quiet_logger, yaml_data,
SearchTerms(False, PathSearchMethods.EQUALS, "*", "anchor"),
PathSeperators.FSLASH, "", seen_anchors, search_anchors=True,
include_value_aliases=include_aliases
)):
assert assertion == str(path)
def test_append_list_element_value_error(yamlpath):
with pytest.raises(ValueError):
yamlpath.append_list_element([], PathSearchMethods, "anchor")
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:
ppath += "&{}".format(element_id)
elif ptype == PathSegmentTypes.SEARCH:
invert, method, attr, term = element_id
if method == PathSearchMethods.REGEX:
safe_term = "/{}/".format(term.replace("/", r"\/"))
else:
# Replace unescaped spaces with escaped spaces
safe_term = r"\ ".join(
list(map(
lambda ele: ele.replace(" ", r"\ ")
, term.split(r"\ ")
))
)
ppath += (
"["
+ str(attr)
+ ("!" if invert else "")
+ PathSearchMethods.to_operator(method)
+ safe_term
+ "]"
raise YAMLPathException(
"Double search inversion is meaningless at {}"
.format(c)
, yaml_path
)
# Invert the search
search_inverted = True
continue
elif c == "=":
# Exact value match OR >=|<=
element_type = PathSegmentTypes.SEARCH
if search_method is PathSearchMethods.LESS_THAN:
search_method = PathSearchMethods.LESS_THAN_OR_EQUAL
elif search_method is PathSearchMethods.GREATER_THAN:
search_method = PathSearchMethods.GREATER_THAN_OR_EQUAL
elif search_method is PathSearchMethods.EQUALS:
# Allow ==
continue
elif search_method is None:
search_method = PathSearchMethods.EQUALS
if element_id:
search_attr = element_id
element_id = ""
else:
raise YAMLPathException(
"Missing search operand before operator, {}"
.format(c)
, yaml_path
def get_search_term(logger: ConsolePrinter,
expression: str) -> Optional[SearchTerms]:
"""
Attempt to cast a search expression into a SearchTerms instance.
Will log an error and return None on failure.
"""
# The leading character must be a known search operator
check_operator = expression[0] if expression else ""
if not (PathSearchMethods.is_operator(check_operator)
or check_operator == '!'):
logger.error(
("Invalid search expression, '{}'. The first symbol of"
+ " every search expression must be one of: {}")
.format(expression,
", ".join(PathSearchMethods.get_operators())))
return None
if not len(expression) > 1:
# Empty expressions do nothing
logger.error(
"An EXPRESSION with only a search operator has no effect, '{}'."
.format(expression))
return None
try:
, yaml_path
)
continue # pragma: no cover
elif not element_id:
# All tests beyond this point require an operand
raise YAMLPathException(
"Missing search operand before operator, {}".format(c)
, yaml_path
)
elif c == "^":
# Value starts with
element_type = PathSegmentTypes.SEARCH
search_method = PathSearchMethods.STARTS_WITH
if element_id:
search_attr = element_id
element_id = ""
continue
elif c == "$":
# Value ends with
element_type = PathSegmentTypes.SEARCH
search_method = PathSearchMethods.ENDS_WITH
if element_id:
search_attr = element_id
element_id = ""
continue
elif c == "%":
# Value contains
element_id = ""
continue
elif c == "$":
# Value ends with
element_type = PathSegmentTypes.SEARCH
search_method = PathSearchMethods.ENDS_WITH
if element_id:
search_attr = element_id
element_id = ""
continue
elif c == "%":
# Value contains
element_type = PathSegmentTypes.SEARCH
search_method = PathSearchMethods.CONTAINS
if element_id:
search_attr = element_id
element_id = ""
continue
elif c == ">":
# Value greater than
element_type = PathSegmentTypes.SEARCH
search_method = PathSearchMethods.GREATER_THAN
if element_id:
search_attr = element_id
element_id = ""
continue
elif c == "<":
# Value less than
"Double search inversion is meaningless at {}"
.format(char)
, yaml_path
)
# Invert the search
search_inverted = True
continue
elif char == "=":
# Exact value match OR >=|<=
segment_type = PathSegmentTypes.SEARCH
if search_method is PathSearchMethods.LESS_THAN:
search_method = PathSearchMethods.LESS_THAN_OR_EQUAL
elif search_method is PathSearchMethods.GREATER_THAN:
search_method = PathSearchMethods.GREATER_THAN_OR_EQUAL
elif search_method is PathSearchMethods.EQUALS:
# Allow ==
continue
elif search_method is None:
search_method = PathSearchMethods.EQUALS
if segment_id:
search_attr = segment_id
segment_id = ""
else:
raise YAMLPathException(
"Missing search operand before operator, {}"
.format(char)
, yaml_path
)
, yaml_path
)
elif c == "^":
# Value starts with
element_type = PathSegmentTypes.SEARCH
search_method = PathSearchMethods.STARTS_WITH
if element_id:
search_attr = element_id
element_id = ""
continue
elif c == "$":
# Value ends with
element_type = PathSegmentTypes.SEARCH
search_method = PathSearchMethods.ENDS_WITH
if element_id:
search_attr = element_id
element_id = ""
continue
elif c == "%":
# Value contains
element_type = PathSegmentTypes.SEARCH
search_method = PathSearchMethods.CONTAINS
if element_id:
search_attr = element_id
element_id = ""
continue
elif c == ">":
# Value greater than