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_create_searchterms_from_pathattributes(self):
st = SearchTerms(False, PathSearchMethods.EQUALS, ".", "key")
assert str(st) == str(create_searchterms_from_pathattributes(st))
with pytest.raises(AttributeError):
_ = create_searchterms_from_pathattributes("nothing-to-see-here")
(PathSearchMethods.EQUALS, "="),
(PathSearchMethods.STARTS_WITH, "^"),
(PathSearchMethods.GREATER_THAN, ">"),
(PathSearchMethods.LESS_THAN, "<"),
(PathSearchMethods.GREATER_THAN_OR_EQUAL, ">="),
(PathSearchMethods.LESS_THAN_OR_EQUAL, "<="),
(PathSearchMethods.REGEX, "=~"),
])
def test_str(self, input, output):
assert output == str(input)
from itertools import zip_longest
content = """---
- &value Test value
- value
- *value
"""
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 = []
assertions = ["/&value", "/[1]"]
results = []
for assertion, path in zip_longest(assertions, yield_children(
quiet_logger, yaml_data,
SearchTerms(False, PathSearchMethods.EQUALS, "*", "value"),
PathSeperators.FSLASH, "", seen_anchors, search_anchors=True,
include_aliases=False
)):
assert assertion == str(path)
, 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
)
else:
raise YAMLPathException(
raise YAMLPathException(
"Missing search operand before operator, {}"
.format(char)
, yaml_path
)
else:
raise YAMLPathException(
"Unsupported search operator combination at {}"
.format(char)
, yaml_path
)
continue # pragma: no cover
elif char == "~":
if search_method == PathSearchMethods.EQUALS:
search_method = PathSearchMethods.REGEX
seeking_regex_delim = True
else:
raise YAMLPathException(
("Unexpected use of {} operator. Please try =~ if"
+ " you mean to search with a Regular"
+ " Expression."
).format(char)
, yaml_path
)
continue # pragma: no cover
elif not segment_id:
# All tests beyond this point require an operand
raise YAMLPathException(
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
)
else:
raise YAMLPathException(
"Unsupported search operator combination at {}"
.format(c)
, yaml_path
)
raise YAMLPathException(
"Missing search operand before operator, {}"
.format(c)
, yaml_path
)
else:
raise YAMLPathException(
"Unsupported search operator combination at {}"
.format(c)
, yaml_path
)
continue # pragma: no cover
elif c == "~":
if search_method == PathSearchMethods.EQUALS:
search_method = PathSearchMethods.REGEX
seeking_regex_delim = True
else:
raise YAMLPathException(
("Unexpected use of {} operator. Please try =~ if"
+ " you mean to search with a Regular"
+ " Expression."
).format(c)
, yaml_path
)
continue # pragma: no cover
elif not element_id:
# All tests beyond this point require an operand
raise YAMLPathException(
def search_matches(method: PathSearchMethods, needle: str,
haystack: Any) -> bool:
"""Perform a search."""
matches: bool = False
if method is PathSearchMethods.EQUALS:
if isinstance(haystack, int):
try:
matches = haystack == int(needle)
except ValueError:
matches = False
elif isinstance(haystack, float):
try:
matches = haystack == float(needle)
except ValueError:
matches = False
else:
matches = haystack == needle
elif method is PathSearchMethods.STARTS_WITH:
matches = str(haystack).startswith(needle)
elif method is PathSearchMethods.ENDS_WITH:
matches = str(haystack).endswith(needle)
, 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
)
else:
raise YAMLPathException(