def subpaths()

in chz/blueprint/_argmap.py [0:0]


    def subpaths(self, path: str, strict: bool = False) -> list[str]:
        """Returns the suffix of arguments this contains that would match a subpath of path.

        The invariant is that for each element `suffix` in the returned list, `path + suffix`
        would match an argument in this map.

        Args:
            strict: Whether to avoid returning arguments that match path exactly.
        """
        assert not path.endswith(".")

        wildcard_literal = path.split(".")[-1]
        # assert wildcard_literal
        assert path.endswith(wildcard_literal)

        path_plus_dot = path + "."

        ret = []
        for layer in self._layers:
            if not strict and path in layer.qualified:
                ret.append("")

            if not path:
                ret.extend([k for k in layer.qualified_sorted if k])
            else:
                index = bisect.bisect_left(layer.qualified_sorted, path_plus_dot)
                for i in range(index, len(layer.qualified_sorted)):
                    key = layer.qualified_sorted[i]
                    if not key.startswith(path_plus_dot):
                        break
                    ret.append(key.removeprefix(path_plus_dot))
                    assert key == join_arg_path(path, ret[-1])

            for key in layer.wildcard:
                # If it's not a wildcard, the logic is straightforward. But doing the equivalent
                # for wildcards is tricky!
                i = key.rfind(wildcard_literal)
                if i == -1:
                    continue
                # The not strict case is not complicated, we just regex match
                if layer._to_regex[key].fullmatch(path):
                    if not strict:
                        ret.append("")
                        assert layer._to_regex[key].fullmatch(path + ret[-1])
                    continue
                # This needs a little thinking about.
                # Say path is "foo.bar" and key is "...bar...baz"
                # Then wildcard_literal is "bar" and we check if "...bar" matches "foo.bar"
                # Since it does, we append "...baz"
                if (
                    i + len(wildcard_literal) < len(key)
                    and key[i + len(wildcard_literal)] == "."
                    and wildcard_key_to_regex(key[: i + len(wildcard_literal)]).fullmatch(path)
                ):
                    assert i == 0 or key[i - 1] == "."
                    ret.append(key[i + len(wildcard_literal) + 1 :])
                    assert layer._to_regex[key].fullmatch(join_arg_path(path, ret[-1]))
        return ret