How to use the yamlpath.exceptions.YAMLPathException 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 / toast / test_parser.py View on Github external
def test_uphappy_str_path_translations(parser, yaml_path):
    with pytest.raises(YAMLPathException):
        parser.str_path(yaml_path)
github wwkimball / yamlpath / yamlpath / processor.py View on Github external
self.logger.debug(
            "Processor::_get_nodes_by_index:  Seeking INDEX node at {}."
            .format(str_stripped)
        )

        if ':' in str_stripped:
            # Array index or Hash key slice
            slice_parts: List[str] = str_stripped.split(':', 1)
            min_match: str = slice_parts[0]
            max_match: str = slice_parts[1]
            if isinstance(data, list):
                try:
                    intmin: int = int(min_match)
                    intmax: int = int(max_match)
                except ValueError:
                    raise YAMLPathException(
                        "{} is not an integer array slice"
                        .format(str_stripped),
                        str(yaml_path),
                        str(unstripped_attrs)
                    )

                if intmin == intmax and len(data) > intmin:
                    yield NodeCoords([data[intmin]], data, intmin)
                else:
                    yield NodeCoords(data[intmin:intmax], data, intmin)

            elif isinstance(data, dict):
                for key, val in data.items():
                    if min_match <= key <= max_match:
                        yield NodeCoords(val, data, key)
        else:
github wwkimball / yamlpath / yamlpath / processor.py View on Github external
data, len(data) - 1
                        ):
                            matched_nodes += 1
                            yield node_coord
                    elif (
                            segment_type in [
                                PathSegmentTypes.INDEX,
                                PathSegmentTypes.KEY]
                    ):
                        if isinstance(stripped_attrs, int):
                            newidx = stripped_attrs
                        else:
                            try:
                                newidx = int(str(stripped_attrs))
                            except ValueError:
                                raise YAMLPathException(
                                    ("Cannot add non-integer {} subreference"
                                     + " to lists")
                                    .format(str(segment_type)),
                                    str(yaml_path),
                                    except_segment
                                )
                        for _ in range(len(data) - 1, newidx):
                            next_node = build_next_node(
                                yaml_path, depth + 1, value
                            )
                            append_list_element(data, next_node)
                        for node_coord in self._get_optional_nodes(
                                data[newidx], yaml_path, value,
                                depth + 1, data, newidx
                        ):
                            matched_nodes += 1
github wwkimball / yamlpath / yamlpath / yamlpath.py View on Github external
# type, assume it is a KEY.
                    if segment_type is None:
                        segment_type = PathSegmentTypes.KEY
                    path_segments.append((segment_type, segment_id))
                    segment_id = ""

                segment_type = None
                continue

            segment_id += char
            seeking_anchor_mark = False
            seeking_collector_operator = False

        # Check for unmatched subpath demarcations
        if collector_level > 0:
            raise YAMLPathException(
                "YAML Path contains an unmatched () collector pair",
                yaml_path
            )

        # Check for unterminated RegExes
        if capturing_regex:
            raise YAMLPathException(
                "YAML Path contains an unterminated Regular Expression",
                yaml_path
            )

        # Check for mismatched demarcations
        if demarc_count > 0:
            raise YAMLPathException(
                "YAML Path contains at least one unmatched demarcation mark",
                yaml_path
github wwkimball / yamlpath / yamlpath / parser.py View on Github external
Path seperator

        Returns:  N/A

        Raises:  N/A
        """
        self.log = logger

        pathsep = kwargs.pop("pathsep", "auto")
        if isinstance(pathsep, PathSeperators):
            self.pathsep = pathsep
        else:
            try:
                self.pathsep = PathSeperators.from_str(pathsep)
            except NameError:
                raise YAMLPathException(
                    "Unknown YAML Path seperator, {}.".format(pathsep)
                    , pathsep
                )
github wwkimball / yamlpath / yamlpath / parser.py View on Github external
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
                        )

                    continue  # pragma: no cover

                elif c == "~":
                    if search_method == PathSearchMethods.EQUALS:
                        search_method = PathSearchMethods.REGEX
github wwkimball / yamlpath / yamlpath / yamlpath.py View on Github external
continue

            segment_id += char
            seeking_anchor_mark = False
            seeking_collector_operator = False

        # Check for unmatched subpath demarcations
        if collector_level > 0:
            raise YAMLPathException(
                "YAML Path contains an unmatched () collector pair",
                yaml_path
            )

        # Check for unterminated RegExes
        if capturing_regex:
            raise YAMLPathException(
                "YAML Path contains an unterminated Regular Expression",
                yaml_path
            )

        # Check for mismatched demarcations
        if demarc_count > 0:
            raise YAMLPathException(
                "YAML Path contains at least one unmatched demarcation mark",
                yaml_path
            )

        # Store the final element_id, which must have been a KEY
        if segment_id:
            # Unless its type has already been identified as a special
            # type, assume it is a KEY.
            if segment_type is None:
github wwkimball / yamlpath / yamlpath / parser.py View on Github external
# Unless its type has already been identified as a special
                    # type, assume it is a KEY.
                    if element_type is None:
                        element_type = PathSegmentTypes.KEY
                    path_elements.append((element_type, element_id))
                    element_id = ""

                element_type = None
                continue

            element_id += c
            seeking_anchor_mark = False

        # Check for unterminated RegExes
        if capturing_regex:
            raise YAMLPathException(
                "YAML Path contains an unterminated Regular Expression.",
                yaml_path
            )

        # Check for mismatched demarcations
        if demarc_count > 0:
            raise YAMLPathException(
                "YAML Path contains at least one unmatched demarcation mark",
                yaml_path
            )

        # Store the final element_id, which must have been a KEY
        if element_id:
            # Unless its type has already been identified as a special
            # type, assume it is a KEY.
            if element_type is None:
github wwkimball / yamlpath / yamlpath / parser.py View on Github external
already a list it is blindly converted into a deque and returned.  When
        yaml_path is already a deque, it is blindly returned as-is.

        Raises:
          YAMLPathException when yaml_path is invalid
        """
        self.log.debug("Parser::parse_path:  Parsing {}...".format(yaml_path))

        if yaml_path is None:
            return deque()
        elif isinstance(yaml_path, deque):
            return yaml_path
        elif isinstance(yaml_path, list):
            return deque(yaml_path)
        elif isinstance(yaml_path, dict):
            raise YAMLPathException(
                "YAML paths must be strings, queues, or lists",
                yaml_path
            )

        if yaml_path in Parser._combined_static_parsings:
            return Parser._combined_static_parsings[yaml_path].copy()

        stripped_path = self._parse_path(yaml_path, True)
        unstripped_path = self._parse_path(yaml_path, False)
        combined_path = deque()

        for sref, uref in zip(stripped_path, unstripped_path):
            styp = sref[0]
            sele = sref[1]
            uele = uref[1]
            combined_path.append(