예제 #1
0
def test_find_last_match(
    input_list: List[Tuple[str, str]], reference: str, expected_idx: int
) -> None:
    csp = create_search_path(input_list)
    assert (
        csp.find_last_match(SearchPathQuery(reference[0], reference[1])) == expected_idx
    )
    def prepend(self,
                provider: str,
                path: str,
                anchor: Optional[SearchPathQuery] = None) -> None:
        """
        Prepends to the search path.
        Note, this currently only takes effect if called before the ConfigRepository is instantiated.

        :param provider: who is providing this search path, can be Hydra,
               the @hydra.main() function, or individual plugins or libraries.
        :param path: path element, can be a file system path or a package path (For example pkg://hydra.conf)
        :param anchor: if string, acts as provider. if SearchPath can be used to match against provider and / or path
        """
        if anchor is None:
            self.config_search_path.insert(0,
                                           SearchPathElement(provider, path))
        else:
            if isinstance(anchor, str):
                anchor = SearchPathQuery(anchor, None)

            idx = self.find_first_match(anchor)
            if idx != -1:
                if idx > 0:
                    self.config_search_path.insert(
                        idx, SearchPathElement(provider, path))
                else:
                    self.prepend(provider, path, None)
            else:
                self.prepend(provider, path, None)
예제 #3
0
def test_initialize_with_config_path(hydra_restore_singletons: Any) -> None:
    assert not GlobalHydra().is_initialized()
    initialize(config_path="../hydra/test_utils/configs")
    assert GlobalHydra().is_initialized()

    gh = GlobalHydra.instance()
    assert gh.hydra is not None
    config_search_path = gh.hydra.config_loader.get_search_path()
    assert isinstance(config_search_path, ConfigSearchPathImpl)
    idx = config_search_path.find_first_match(
        SearchPathQuery(provider="main", path=None))
    assert idx != -1
예제 #4
0
 def add_conf_dir() -> None:
     if args.config_dir is not None:
         abs_config_dir = os.path.abspath(args.config_dir)
         if not os.path.isdir(abs_config_dir):
             raise SearchPathException(
                 f"Additional config directory '{abs_config_dir}' not found"
             )
         search_path.prepend(
             provider="command-line",
             path=f"file://{abs_config_dir}",
             anchor=SearchPathQuery(provider="schema"),
         )
예제 #5
0
def test_initialize_with_config_dir() -> None:
    try:
        assert not GlobalHydra().is_initialized()
        initialize(config_dir="../hydra/test_utils/configs", strict=True)
        assert GlobalHydra().is_initialized()

        gh = GlobalHydra.instance()
        assert gh.hydra is not None
        config_search_path = gh.hydra.config_loader.get_search_path()
        assert isinstance(config_search_path, ConfigSearchPathImpl)
        idx = config_search_path.find_first_match(
            SearchPathQuery(provider="main", search_path=None))
        assert idx != -1
    finally:
        GlobalHydra().clear()
    def append(self,
               provider: str,
               path: str,
               anchor: Optional[SearchPathQuery] = None) -> None:
        if anchor is None:
            self.config_search_path.append(SearchPathElement(provider, path))
        else:
            if isinstance(anchor, str):
                anchor = SearchPathQuery(anchor, None)

            idx = self.find_last_match(anchor)
            if idx != -1:
                self.config_search_path.insert(
                    idx + 1, SearchPathElement(provider, path))
            else:
                self.append(provider, path, anchor=None)
예제 #7
0
    assert csp.find_first_match(sp) == expected_idx


@pytest.mark.parametrize(  # type: ignore
    "base_list, provider, path, anchor_provider, result_list",
    [
        # appending to an empty list
        ([], "foo", "/path", None, [("foo", "/path")]),
        # appending to a non empty list
        ([("f1", "/p1")], "f2", "/p2", None, [("f1", "/p1"), ("f2", "/p2")]),
        # appending after an anchor at key 0
        (
            [("f1", "A"), ("f2", "B")],
            "f3",
            "B",
            SearchPathQuery(None, "A"),
            [("f1", "A"), ("f3", "B"), ("f2", "B")],
        ),
        # appending after an anchor at the end of the list
        (
            [("f1", "A"), ("f2", "B")],
            "f3",
            "B",
            SearchPathQuery(None, "B"),
            [("f1", "A"), ("f2", "B"), ("f3", "B")],
        ),
        # appending after a non existent anchor
        (
            [],
            "new_provider",
            "/path",