How to use the yamlpath.enums.PathSearchMethods function in yamlpath

To help you get started, we’ve selected a few yamlpath examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wwkimball / yamlpath / tests / test_commands_yaml_paths.py View on Github external
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)
github wwkimball / yamlpath / tests / test_yamlpath.py View on Github external
def test_append_list_element_value_error(yamlpath):
  with pytest.raises(ValueError):
    yamlpath.append_list_element([], PathSearchMethods, "anchor")
github wwkimball / yamlpath / yamlpath / parser.py View on Github external
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
                    + "]"
github wwkimball / yamlpath / yamlpath / parser.py View on Github external
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
github wwkimball / yamlpath / yamlpath / commands / yaml_paths.py View on Github external
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:
github wwkimball / yamlpath / yamlpath / parser.py View on Github external
, 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
github wwkimball / yamlpath / yamlpath / parser.py View on Github external
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
github wwkimball / yamlpath / yamlpath / yamlpath.py View on Github external
"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
                            )
github wwkimball / yamlpath / yamlpath / parser.py View on Github external
, 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